-
Notifications
You must be signed in to change notification settings - Fork 4.4k
WIP: Add java_current_repository rule #16281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.google.devtools.build.lib.bazel.rules.java; | ||
|
|
||
| import com.google.devtools.build.lib.analysis.config.StarlarkDefinedConfigTransition; | ||
| import com.google.devtools.build.lib.analysis.starlark.StarlarkAttributeTransitionProvider; | ||
| import com.google.devtools.build.lib.cmdline.Label; | ||
| import com.google.devtools.build.lib.cmdline.RepositoryMapping; | ||
| import com.google.devtools.build.lib.packages.Attribute; | ||
| import com.google.devtools.build.lib.packages.AttributeMap; | ||
| import com.google.devtools.build.lib.packages.StructImpl; | ||
| import net.starlark.java.eval.*; | ||
| import net.starlark.java.syntax.Location; | ||
|
|
||
| import static com.google.devtools.build.lib.packages.Attribute.attr; | ||
| import static com.google.devtools.build.lib.packages.BuildType.LABEL; | ||
| import static com.google.devtools.build.lib.packages.Type.STRING; | ||
|
|
||
| public class BazelJavaCurrentRepositorySupport { | ||
| private static final String CURRENT_REPOSITORY_SETTING = "@bazel_tools//tools/java/runfiles:current_repository"; | ||
| private static final Label RUNFILES_CONSTANTS_RULE = Label.parseCanonicalUnchecked("@@bazel_tools//tools/java/runfiles:java_current_repository"); | ||
|
|
||
| static final Attribute CURRENT_REPOSITORY_ATTRIBUTE = attr("_current_repository", STRING) | ||
| .value(new Attribute.ComputedDefault() { | ||
| @Override | ||
| public String getDefault(AttributeMap rule) { | ||
| return rule.getLabel().getRepository().getName(); | ||
| } | ||
| } | ||
| ) | ||
| .build(); | ||
|
|
||
| static final Attribute RUNFILES_CONSTANTS_ATTRIBUTE = attr("$runfiles_constants", LABEL) | ||
| .cfg(new StarlarkAttributeTransitionProvider(makeRunfilesConstantsTransition())) | ||
| .value(RUNFILES_CONSTANTS_RULE) | ||
| .build(); | ||
|
|
||
| private static StarlarkDefinedConfigTransition makeRunfilesConstantsTransition() { | ||
| try { | ||
| return StarlarkDefinedConfigTransition.newRegularTransition( | ||
| new StarlarkCallable() { | ||
| @Override | ||
| public Object fastcall(StarlarkThread thread, Object[] positional, Object[] named) throws EvalException { | ||
| String currentRepository = (String) ((StructImpl) positional[1]).getValue("_current_repository"); | ||
| return Dict.builder().put(CURRENT_REPOSITORY_SETTING, currentRepository).buildImmutable(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "_current_repository_transition_impl"; | ||
| } | ||
| }, | ||
| StarlarkList.empty(), | ||
| StarlarkList.immutableOf(CURRENT_REPOSITORY_SETTING), | ||
| StarlarkSemantics.DEFAULT, | ||
| Label.parseCanonicalUnchecked("@@_builtins//:common/java/java_common.bzl"), | ||
| Location.BUILTIN, | ||
| RepositoryMapping.ALWAYS_FALLBACK); | ||
| } catch (EvalException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,6 +206,8 @@ public static Object fromJava(Object x, @Nullable Mutability mutability) { | |
| return StarlarkList.copyOf(mutability, (List<?>) x); | ||
| } else if (x instanceof Map) { | ||
| return Dict.copyOf(mutability, (Map<?, ?>) x); | ||
| } else if (x instanceof Set && ((Set<?>) x).size() == 1) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? We are usually more thorough on reviews of Starlark changes. If you need it, perhaps it's better to submit it in a separate PR. |
||
| return StarlarkList.copyOf(mutability, (Set<?>) x); | ||
| } | ||
| throw new InvalidStarlarkValueException(x.getClass()); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,17 @@ load(":common/java/java_semantics.bzl", "semantics") | |
| JavaInfo = _builtins.toplevel.JavaInfo | ||
| JavaPluginInfo = _builtins.toplevel.JavaPluginInfo | ||
| CcInfo = _builtins.toplevel.CcInfo | ||
| transition = _builtins.toplevel.transition | ||
| repository_name = _builtins.native.repository_name | ||
|
|
||
| def _current_repository_transition_impl(settings, attr): | ||
| return {"@bazel_tools//tools/java/runfiles:current_repository": attr.current_repository} | ||
|
|
||
| _current_repository_transition = transition( | ||
| implementation = _current_repository_transition_impl, | ||
| inputs = [], | ||
| outputs = ["@bazel_tools//tools/java/runfiles:current_repository"], | ||
| ) | ||
|
|
||
| def bazel_java_library_rule( | ||
| ctx, | ||
|
|
@@ -37,7 +48,8 @@ def bazel_java_library_rule( | |
| neverlink = False, | ||
| proguard_specs = [], | ||
| add_exports = [], | ||
| add_opens = []): | ||
| add_opens = [], | ||
| runfiles_constants = None): | ||
| """Implements java_library. | ||
|
|
||
| Use this call when you need to produce a fully fledged java_library from | ||
|
|
@@ -58,6 +70,7 @@ def bazel_java_library_rule( | |
| proguard_specs: (list[File]) Files to be used as Proguard specification. | ||
| add_exports: (list[str]) Allow this library to access the given <module>/<package>. | ||
| add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>. | ||
| runfiles_constants: (Target) Target providing RunfilesConstants.CURRENT_REPOSITORY. | ||
| Returns: | ||
| (list[provider]) A list containing DefaultInfo, JavaInfo, | ||
| InstrumentedFilesInfo, OutputGroupsInfo, ProguardSpecProvider providers. | ||
|
|
@@ -68,7 +81,7 @@ def bazel_java_library_rule( | |
| target, base_info = basic_java_library( | ||
| ctx, | ||
| srcs, | ||
| deps, | ||
| deps + [runfiles_constants], | ||
| runtime_deps, | ||
| plugins, | ||
| exports, | ||
|
|
@@ -110,6 +123,8 @@ def _proxy(ctx): | |
| ctx.files.proguard_specs, | ||
| ctx.attr.add_exports, | ||
| ctx.attr.add_opens, | ||
| # This attribute is subject to an outgoing transition. | ||
| ctx.attr._runfiles_constants[0], | ||
| ).values() | ||
|
|
||
| JAVA_LIBRARY_IMPLICIT_ATTRS = BASIC_JAVA_LIBRARY_WITH_PROGUARD_IMPLICIT_ATTRS | ||
|
|
@@ -164,11 +179,21 @@ JAVA_LIBRARY_ATTRS = merge_attrs( | |
| "add_exports": attr.string_list(), | ||
| "add_opens": attr.string_list(), | ||
| "licenses": attr.license() if hasattr(attr, "license") else attr.string_list(), | ||
| # Consumed by _current_repository_transition. | ||
| "current_repository": attr.string(), | ||
| "_runfiles_constants": attr.label( | ||
| default = "@bazel_tools//tools/java/runfiles:java_current_repository", | ||
| providers = [JavaInfo], | ||
| cfg = _current_repository_transition, | ||
| ), | ||
| "_java_toolchain_type": attr.label(default = semantics.JAVA_TOOLCHAIN_TYPE), | ||
| "_allowlist_function_transition": attr.label( | ||
| default = "@bazel_tools//tools/allowlists/function_transition_allowlist", | ||
| ), | ||
|
Comment on lines
+182
to
+192
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would also be pulled into Google java_library. Monorepo probably doesn't need it and there might be an observable memory regression involved (1 new public attribute will use more memory for each target). Keep |
||
| }, | ||
| ) | ||
|
|
||
| java_library = rule( | ||
| _java_library = rule( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't change the name here, tooling uses it. Solution is to put |
||
| _proxy, | ||
| attrs = JAVA_LIBRARY_ATTRS, | ||
| provides = [JavaInfo], | ||
|
|
@@ -180,3 +205,10 @@ java_library = rule( | |
| compile_one_filetype = [".java"], | ||
| toolchains = [semantics.JAVA_TOOLCHAIN], | ||
| ) | ||
|
|
||
| def java_library(**kwargs): | ||
| _java_library( | ||
| # Skip over the leading '@'. | ||
| current_repository = repository_name()[1:], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can definitely change that, but don't we get the regression either way? |
||
| **kwargs | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this and other
StarlarkValueneeded?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This as well as the conversion of singleton sets are necessary to allow attaching a Starlark transition to
java_binary. That triggers logic that converts all attributes to Starlark values and these legacy types as well as the Java standard library class for singleton sets failed during conversion. Do you see a better way to handle this?