Skip to content

Commit

Permalink
Experimentally export java_library_rule
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 515118611
Change-Id: I252dcf4206ff33c674dccaae53184b09d0de6fee
  • Loading branch information
comius authored and copybara-github committed Mar 8, 2023
1 parent 8327da8 commit 1b1922a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.
package com.google.devtools.build.lib.bazel.rules;

import static com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions.EXPERIMENTAL_JAVA_LIBRARY_EXPORT;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider.RuleSet;
Expand Down Expand Up @@ -45,6 +47,8 @@
import com.google.devtools.build.lib.starlarkbuildapi.java.JavaBootstrap;
import com.google.devtools.build.lib.util.ResourceFileLoader;
import java.io.IOException;
import net.starlark.java.eval.FlagGuardedValue;
import net.starlark.java.eval.Starlark;

/** Rules for Java support in Bazel. */
public class JavaRules implements RuleSet {
Expand Down Expand Up @@ -88,6 +92,11 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
JavaPluginInfo.PROVIDER,
ProguardSpecProvider.PROVIDER));

builder.addStarlarkAccessibleTopLevels(
"experimental_java_library_export_do_not_use",
FlagGuardedValue.onlyWhenExperimentalFlagIsTrue(
EXPERIMENTAL_JAVA_LIBRARY_EXPORT, Starlark.NONE));

try {
builder.addWorkspaceFileSuffix(
ResourceFileLoader.loadResource(BazelJavaRuleClasses.class, "jdk.WORKSPACE"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
* </ul>
*/
public final class BuildLanguageOptions extends OptionsBase {
@Option(
name = "experimental_java_library_export",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
help = "If enabled, experimental_java_library_export_do_not_use module is available.")
public boolean experimentalJavaLibraryExport;

@Option(
name = "incompatible_stop_exporting_language_modules",
Expand Down Expand Up @@ -654,6 +662,7 @@ public StarlarkSemantics toStarlarkSemantics() {
// This function connects command-line flags to their corresponding StarlarkSemantics keys.
StarlarkSemantics semantics =
StarlarkSemantics.builder()
.setBool(EXPERIMENTAL_JAVA_LIBRARY_EXPORT, experimentalJavaLibraryExport)
.setBool(
INCOMPATIBLE_STOP_EXPORTING_LANGUAGE_MODULES,
incompatibleStopExportingLanguageModules)
Expand Down Expand Up @@ -744,6 +753,7 @@ public StarlarkSemantics toStarlarkSemantics() {
// (In principle, a key not associated with a command-line flag may be declared anywhere.)

// booleans: the +/- prefix indicates the default value (true/false).
public static final String EXPERIMENTAL_JAVA_LIBRARY_EXPORT = "-experimental_java_library_export";
public static final String INCOMPATIBLE_STOP_EXPORTING_LANGUAGE_MODULES =
"-incompatible_stop_exporting_language_modules";
public static final String INCOMPATIBLE_REMOVE_RULE_NAME_PARAMETER =
Expand Down
12 changes: 10 additions & 2 deletions src/main/starlark/builtins_bzl/bazel/exports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

"""Exported builtins symbols that are specific to OSS Bazel."""

load("@_builtins//:common/java/java_library.bzl", "java_library")
load("@_builtins//:common/java/java_library.bzl", "JAVA_LIBRARY_ATTRS", "bazel_java_library_rule", "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")
Expand All @@ -24,7 +24,15 @@ load("@_builtins//:common/python/py_binary_macro.bzl", "py_binary")
load("@_builtins//:common/python/py_library_macro.bzl", "py_library")
load("@_builtins//:common/python/py_test_macro.bzl", "py_test")

exported_toplevels = {}
exported_toplevels = {
# This is an experimental export in Bazel. The interface will change in a way
# that breaks users. In the future, Build API team will provide an interface
# that is conceptually similar to this one and stable.
"experimental_java_library_export_do_not_use": struct(
bazel_java_library_rule = bazel_java_library_rule,
JAVA_LIBRARY_ATTRS = JAVA_LIBRARY_ATTRS,
),
}
exported_rules = {
"java_library": java_library,
"java_plugin": java_plugin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def bazel_java_library_rule(
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>.
Returns:
(list[provider]) A list containing DefaultInfo, JavaInfo,
(dict[str, provider]) A list containing DefaultInfo, JavaInfo,
InstrumentedFilesInfo, OutputGroupsInfo, ProguardSpecProvider providers.
"""
if not srcs and deps:
Expand Down
52 changes: 52 additions & 0 deletions src/test/shell/bazel/bazel_rules_java_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,56 @@ function test_rules_java_repository_builds_itself() {
|| fail "Build failed unexpectedly"
}


function test_experimental_java_library_export_do_not_use() {
mkdir -p java
cat >java/java_library.bzl <<EOF
def _impl(ctx):
return experimental_java_library_export_do_not_use.bazel_java_library_rule(
ctx,
ctx.files.srcs,
ctx.attr.deps,
ctx.attr.runtime_deps,
ctx.attr.plugins,
ctx.attr.exports,
ctx.attr.exported_plugins,
ctx.files.resources,
ctx.attr.javacopts,
ctx.attr.neverlink,
ctx.files.proguard_specs,
ctx.attr.add_exports,
ctx.attr.add_opens,
).values()
java_library = rule(
implementation = _impl,
attrs = experimental_java_library_export_do_not_use.JAVA_LIBRARY_ATTRS,
provides = [JavaInfo],
outputs = {
"classjar": "lib%{name}.jar",
"sourcejar": "lib%{name}-src.jar",
},
fragments = ["java", "cpp"],
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
)
EOF
cat >java/BUILD <<EOF
load(":java_library.bzl", "java_library")
package(default_visibility=['//visibility:public'])
java_library(name = 'hello_library',
srcs = ['HelloLibrary.java']);
EOF
cat >java/HelloLibrary.java <<EOF
package hello_library;
public class HelloLibrary {
public static void funcHelloLibrary() {
System.out.print("Hello, Library!;");
}
}
EOF

bazel build //java:hello_library &> $TEST_log && fail "build succeeded"
bazel build --experimental_java_library_export //java:hello_library &> $TEST_log || fail "build failed"
}

run_suite "rules_java tests"

0 comments on commit 1b1922a

Please sign in to comment.