forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IsRunfilesLibraryInfo provider and add support to java_library
`java_library` targets that directly depend on `@bazel_tools//tools/java/runfiles` have compile-time access to the constant `com.google.devtools.build.runfiles.RunfilesHelper.CURRENT_REPO_NAME`, which contains the canonical repository name of the repository that contains the target. The constant is inlined during compilation, ensuring that no new class is added to the classpath. If a `java_library` does not directly depend on the runfiles library, no additional action will be generated.
- Loading branch information
Showing
9 changed files
with
212 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/google/devtools/build/lib/analysis/IsRunfilesLibraryInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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. | ||
|
||
package com.google.devtools.build.lib.analysis; | ||
|
||
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; | ||
import com.google.devtools.build.lib.packages.BuiltinProvider; | ||
import com.google.devtools.build.lib.packages.NativeInfo; | ||
import com.google.devtools.build.lib.starlarkbuildapi.IsRunfilesLibraryInfoApi; | ||
|
||
/** | ||
* Provider that signals to direct dependents that this target is a runfiles library and that they | ||
* should ensure that their compiled code has access to its canonical repository name, e.g. | ||
* via a generated constant. | ||
*/ | ||
@Immutable | ||
public final class IsRunfilesLibraryInfo extends NativeInfo implements IsRunfilesLibraryInfoApi { | ||
|
||
public static final IsRunfilesLibraryInfoProvider PROVIDER = new IsRunfilesLibraryInfoProvider(); | ||
|
||
@Override | ||
public IsRunfilesLibraryInfoProvider getProvider() { | ||
return PROVIDER; | ||
} | ||
|
||
public static class IsRunfilesLibraryInfoProvider extends BuiltinProvider<IsRunfilesLibraryInfo> | ||
implements IsRunfilesLibraryInfoApi.RunEnvironmentInfoApiProvider { | ||
|
||
private IsRunfilesLibraryInfoProvider() { | ||
super("IsRunfilesLibraryInfo", IsRunfilesLibraryInfo.class); | ||
} | ||
|
||
@Override | ||
public IsRunfilesLibraryInfoApi constructor() { | ||
return new IsRunfilesLibraryInfo(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/com/google/devtools/build/lib/starlarkbuildapi/IsRunfilesLibraryInfoApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 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. | ||
|
||
package com.google.devtools.build.lib.starlarkbuildapi; | ||
|
||
import com.google.devtools.build.docgen.annot.DocCategory; | ||
import com.google.devtools.build.docgen.annot.StarlarkConstructor; | ||
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi; | ||
import com.google.devtools.build.lib.starlarkbuildapi.core.StructApi; | ||
import net.starlark.java.annot.StarlarkBuiltin; | ||
import net.starlark.java.annot.StarlarkMethod; | ||
|
||
/** | ||
* Provider that signals to direct dependents that this target is a runfiles library and that they | ||
* should ensure that their compiled code has access to its canonical repository name, e.g. | ||
* via a generated constant. | ||
*/ | ||
@StarlarkBuiltin( | ||
name = "IsRunfilesLibraryInfo", | ||
category = DocCategory.PROVIDER, | ||
doc = "Provider that signals to direct dependents that this target is a runfiles library and " | ||
+ "that they should ensure that their compiled code has access to its canonical repository " | ||
+ "name, e.g. via a generated constant.") | ||
public interface IsRunfilesLibraryInfoApi extends StructApi { | ||
|
||
@StarlarkBuiltin(name = "Provider", category = DocCategory.PROVIDER, documented = false, doc = "") | ||
interface RunEnvironmentInfoApiProvider extends ProviderApi { | ||
|
||
@StarlarkMethod( | ||
name = "IsRunfilesLibraryInfo", | ||
doc = "", | ||
documented = false, | ||
selfCall = true) | ||
@StarlarkConstructor | ||
IsRunfilesLibraryInfoApi constructor(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/starlark/builtins_bzl/common/java/runfiles_helper.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2021 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. | ||
|
||
load(":common/rule_util.bzl", "create_dep") | ||
load(":common/java/java_semantics.bzl", "semantics") | ||
|
||
java_common = _builtins.toplevel.java_common | ||
|
||
IsRunfilesLibraryInfo = _builtins.toplevel.IsRunfilesLibraryInfo | ||
|
||
_RUNFILES_HELPER_TEMPLATE = """package com.google.devtools.build.runfiles; | ||
public final class RunfilesHelper { | ||
public static final String CURRENT_REPO_NAME = "%s"; | ||
private RunfilesHelper() {} | ||
} | ||
""" | ||
|
||
def depends_on_runfiles_library(deps): | ||
for dep in deps: | ||
if IsRunfilesLibraryInfo in dep: | ||
return True | ||
return False | ||
|
||
def runfiles_helper_action(ctx): | ||
basename = "_runfiles_helper/%s_runfiles_helper" % ctx.attr.name | ||
|
||
java_file = ctx.actions.declare_file(basename + ".java") | ||
current_repo_name = ctx.label.workspace_name or ctx.workspace_name | ||
ctx.actions.write(java_file, _RUNFILES_HELPER_TEMPLATE % current_repo_name) | ||
|
||
jar_file = ctx.actions.declare_file(basename + ".jar") | ||
return java_common.compile( | ||
ctx, | ||
java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo], | ||
# The JLS (§13.1) guarantees that constants are inlined. Since the generated code only | ||
# contains constants, we can remove it from the runtime classpath. | ||
neverlink = True, | ||
output = jar_file, | ||
source_files = [java_file], | ||
) | ||
|
||
RUNFILES_HELPER_ACTION = create_dep( | ||
runfiles_helper_action, | ||
attrs = { | ||
"_java_toolchain": attr.label( | ||
default = semantics.JAVA_TOOLCHAIN_LABEL, | ||
providers = [java_common.JavaToolchainInfo], | ||
), | ||
}, | ||
) | ||
|
||
RUNFILES_HELPER_ACTION_IMPLICIT_ATTRS = { | ||
"_java_toolchain": attr.label( | ||
default = semantics.JAVA_TOOLCHAIN_LABEL, | ||
providers = [java_common.JavaToolchainInfo], | ||
), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
java_library( | ||
load(":java_runfiles_library.bzl", "java_runfiles_library") | ||
|
||
java_runfiles_library( | ||
name = "runfiles", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
java_library( | ||
name = "runfiles_impl", | ||
srcs = [ | ||
"Runfiles.java", | ||
"Util.java", | ||
], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def _java_runfiles_library_impl(ctx): | ||
return [ | ||
ctx.attr._runfiles_library[DefaultInfo], | ||
ctx.attr._runfiles_library[JavaInfo], | ||
IsRunfilesLibraryInfo(), | ||
] | ||
|
||
java_runfiles_library = rule( | ||
implementation = _java_runfiles_library_impl, | ||
attrs = { | ||
"_runfiles_library": attr.label(default = ":runfiles_impl"), | ||
}, | ||
) |