Skip to content

Commit

Permalink
Starlarkify java_import
Browse files Browse the repository at this point in the history
Rewrite native java_import into Starlark.

PiperOrigin-RevId: 483678662
Change-Id: Ie3f914780fcb508338186455b5a8756f24374239
  • Loading branch information
kotlaja authored and copybara-github committed Oct 25, 2022
1 parent 69ddc0a commit 26ec43d
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ public boolean getUseIjars() {
return useIjars;
}

/**
* Returns true iff Java compilation should use ijars. Checks if the functions is been called from
* builtins.
*/
@Override
public boolean getUseIjarsInStarlark(StarlarkThread thread) throws EvalException {
checkPrivateAccess(thread);
return useIjars;
}

/** Returns true iff Java header compilation is enabled. */
public boolean useHeaderCompilation() {
return useHeaderCompilation;
Expand Down Expand Up @@ -278,7 +288,7 @@ public String getFixDepsTool() {
return fixDepsTool;
}

/** @return proper label only if --java_launcher= is specified, otherwise null. */
/** Returns proper label only if --java_launcher= is specified, otherwise null. */
@StarlarkConfigurationField(
name = "launcher",
doc = "Returns the label provided with --java_launcher, if any.",
Expand Down Expand Up @@ -397,6 +407,14 @@ public boolean getDisallowJavaImportEmptyJarsInStarlark(StarlarkThread thread)
return disallowJavaImportEmptyJars;
}

/** Returns true if java_import exports are not allowed. */
@Override
public boolean getDisallowJavaImportExportsInStarlark(StarlarkThread thread)
throws EvalException {
checkPrivateAccess(thread);
return disallowJavaImportExports;
}

@Override
public String starlarkOneVersionEnforcementLevel() {
return oneVersionEnforcementLevel().name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public Label getToolchainLabel() {
return label;
}

/** @return the target Java bootclasspath */
/** Returns the target Java bootclasspath. */
public BootClassPathInfo getBootclasspath() {
return bootclasspath;
}
Expand Down Expand Up @@ -325,7 +325,6 @@ public ImmutableSet<String> getReducedClasspathIncompatibleProcessors() {
return reducedClasspathIncompatibleProcessors;
}


/**
* Returns {@code true} if header compilation should be forcibly disabled, overriding
* --java_header_compilation.
Expand Down Expand Up @@ -374,6 +373,12 @@ public Artifact depsChecker() {
return depsChecker;
}

@Override
public Artifact getDepsCheckerForStarlark(StarlarkThread thread) throws EvalException {
checkPrivateAccess(thread);
return depsChecker();
}

@Nullable
public Artifact getResourceJarBuilder() {
return resourceJarBuilder;
Expand All @@ -397,12 +402,12 @@ ImmutableListMultimap<String, String> getCompatibleJavacOptions() {
return compatibleJavacOptions;
}

/** @return the map of target environment-specific javacopts. */
/** Returns the map of target environment-specific javacopts. */
public ImmutableList<String> getCompatibleJavacOptions(String key) {
return getCompatibleJavacOptions().get(key);
}

/** @return the list of default options for the java compiler */
/** Returns the list of default options for the java compiler. */
public ImmutableList<String> getJavacOptions(RuleContext ruleContext) {
ImmutableList.Builder<String> result = ImmutableList.<String>builder().addAll(javacOptions);
if (ruleContext != null) {
Expand All @@ -421,7 +426,7 @@ public NestedSet<String> getJvmOptions() {
return jvmOptions;
}

/** @return whether JavaBuilders supports running as a persistent worker or not */
/** Returns whether JavaBuilders supports running as a persistent worker or not. */
public boolean getJavacSupportsWorkers() {
return javacSupportsWorkers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,16 @@ public interface JavaConfigurationApi extends StarlarkValue {
doc = "Returns true if empty java_import jars are not allowed.",
useStarlarkThread = true)
boolean getDisallowJavaImportEmptyJarsInStarlark(StarlarkThread thread) throws EvalException;

@StarlarkMethod(
name = "use_ijars",
doc = "Returns true iff Java compilation should use ijars.",
useStarlarkThread = true)
boolean getUseIjarsInStarlark(StarlarkThread thread) throws EvalException;

@StarlarkMethod(
name = "disallow_java_import_exports",
doc = "Returns true if java_import exports are not allowed.",
useStarlarkThread = true)
boolean getDisallowJavaImportExportsInStarlark(StarlarkThread thread) throws EvalException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,12 @@ public interface JavaToolchainStarlarkApiProviderApi extends StructApi {
@Nullable
ImmutableList<String> getCompatibleJavacOptionsForStarlark(String key, StarlarkThread thread)
throws EvalException;

@Nullable
@StarlarkMethod(
name = "deps_checker",
documented = false,
useStarlarkThread = true,
allowReturnNones = true)
FileApi getDepsCheckerForStarlark(StarlarkThread thread) throws EvalException;
}
2 changes: 2 additions & 0 deletions src/main/starlark/builtins_bzl/bazel/exports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

load("@_builtins//:common/java/java_library.bzl", "java_library")
load("@_builtins//:common/java/java_plugin.bzl", "java_plugin")
load("@_builtins//:common/java/java_import.bzl", "java_import")
load("@_builtins//:common/java/proto/java_proto_library.bzl", "java_proto_library")
load("@_builtins//:common/cc/cc_proto_library.bzl", "cc_proto_library")

exported_toplevels = {}
exported_rules = {
"java_library": java_library,
"java_plugin": java_plugin,
"-java_import": java_import,
"java_proto_library": java_proto_library,
"+cc_proto_library": cc_proto_library,
}
Expand Down
80 changes: 80 additions & 0 deletions src/main/starlark/builtins_bzl/common/java/import_deps_check.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Creates the import deps checker for java rules"""

load(":common/java/java_semantics.bzl", "semantics")

java_common = _builtins.toplevel.java_common

def import_deps_check(
ctx,
jars_to_check,
declared_deps,
transitive_deps,
rule_class):
"""
Creates actions that checks import deps for java rules.
Args:
ctx: (RuleContext) Used to register the actions.
jars_to_check: (list[File]) A list of jars files to check.
declared_deps: (list[File]) A list of direct dependencies.
transitive_deps: (list[File]) A list of transitive dependencies.
rule_class: (String) Rule class.
Returns:
(File) Output file of the created action.
"""
java_toolchain = semantics.find_java_toolchain(ctx)
deps_checker = java_toolchain.deps_checker()
if deps_checker == None:
return None

jdeps_output = ctx.actions.declare_file("_%s/%s/jdeps.proto" % (rule_class, ctx.label.name))

args = ctx.actions.args()
args.add("-jar", deps_checker)
args.add_all(jars_to_check, before_each = "--input")
args.add_all(declared_deps, before_each = "--directdep")
args.add_all(
depset(order = "preorder", transitive = [declared_deps, transitive_deps]),
before_each = "--classpath_entry",
)
args.add_all(java_toolchain.bootclasspath, before_each = "--bootclasspath_entry")
args.add("--checking_mode=error")
args.add("--jdeps_output", jdeps_output)
args.add("--rule_label", ctx.label)

inputs = depset(
jars_to_check,
transitive = [
declared_deps,
transitive_deps,
java_toolchain.bootclasspath,
],
)
tools = [deps_checker, java_toolchain.java_runtime.files]

ctx.actions.run(
mnemonic = "ImportDepsChecker",
progress_message = "Checking the completeness of the deps for %s" % jars_to_check,
executable = java_toolchain.java_runtime.java_executable_exec_path,
arguments = [args],
inputs = inputs,
outputs = [jdeps_output],
tools = tools,
)

return jdeps_output
Loading

0 comments on commit 26ec43d

Please sign in to comment.