Skip to content

Commit

Permalink
Add --incompatible_java_info_merge_runtime_module_flags
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyg-stripe committed Nov 3, 2023
1 parent 3fc9375 commit 521dbb0
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,17 @@ public final class BuildLanguageOptions extends OptionsBase {
+ "host_javabase in compile will all be removed.")
public boolean incompatibleJavaCommonParameters;

@Option(
name = "incompatible_java_info_merge_runtime_module_flags",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
effectTags = {OptionEffectTag.UNKNOWN},
metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
help =
"If set to true, the JavaInfo constructor will merge add_exports and "
+ "add_opens of runtime_deps in addition to deps and exports.")
public boolean incompatibleJavaInfoMergeRuntimeModuleFlags;

@Option(
name = "max_computation_steps",
defaultValue = "0",
Expand Down Expand Up @@ -743,6 +754,7 @@ public StarlarkSemantics toStarlarkSemantics() {
INCOMPATIBLE_FIX_PACKAGE_GROUP_REPOROOT_SYNTAX,
incompatibleFixPackageGroupReporootSyntax)
.setBool(INCOMPATIBLE_JAVA_COMMON_PARAMETERS, incompatibleJavaCommonParameters)
.setBool(INCOMPATIBLE_JAVA_INFO_MERGE_RUNTIME_MODULE_FLAGS, incompatibleJavaInfoMergeRuntimeModuleFlags)
.setBool(INCOMPATIBLE_NO_ATTR_LICENSE, incompatibleNoAttrLicense)
.setBool(INCOMPATIBLE_NO_IMPLICIT_FILE_EXPORT, incompatibleNoImplicitFileExport)
.setBool(INCOMPATIBLE_NO_PACKAGE_DISTRIBS, incompatibleNoPackageDistribs)
Expand Down Expand Up @@ -849,6 +861,8 @@ public StarlarkSemantics toStarlarkSemantics() {
"+incompatible_do_not_split_linking_cmdline";
public static final String INCOMPATIBLE_JAVA_COMMON_PARAMETERS =
"+incompatible_java_common_parameters";
public static final String INCOMPATIBLE_JAVA_INFO_MERGE_RUNTIME_MODULE_FLAGS =
"-incompatible_java_info_merge_runtime_module_flags";
public static final String INCOMPATIBLE_NO_ATTR_LICENSE = "+incompatible_no_attr_license";
public static final String INCOMPATIBLE_NO_PACKAGE_DISTRIBS = "-incompatible_no_package_distribs";
public static final String INCOMPATIBLE_NO_IMPLICIT_FILE_EXPORT =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ public boolean isDepsetForJavaOutputSourceJarsEnabled(StarlarkThread thread)
.getBool(BuildLanguageOptions.INCOMPATIBLE_DEPSET_FOR_JAVA_OUTPUT_SOURCE_JARS);
}

@Override
public boolean isJavaInfoMergeRuntimeModuleFlagsEnabled(StarlarkThread thread)
throws EvalException {
checkPrivateAccess(thread);
return thread
.getSemantics()
.getBool(BuildLanguageOptions.INCOMPATIBLE_JAVA_INFO_MERGE_RUNTIME_MODULE_FLAGS);
}

@Override
public JavaInfo wrapJavaInfo(Info javaInfo, StarlarkThread thread)
throws EvalException, RuleErrorException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,12 @@ void checkJavaToolchainIsDeclaredOnRuleForStarlark(
useStarlarkThread = true)
boolean isDepsetForJavaOutputSourceJarsEnabled(StarlarkThread thread) throws EvalException;

@StarlarkMethod(
name = "_incompatible_java_info_merge_runtime_module_flags",
documented = false,
useStarlarkThread = true)
boolean isJavaInfoMergeRuntimeModuleFlagsEnabled(StarlarkThread thread) throws EvalException;

@StarlarkMethod(
name = "wrap_java_info",
parameters = {@Param(name = "java_info")},
Expand Down
15 changes: 13 additions & 2 deletions src/main/starlark/builtins_bzl/common/java/java_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,17 @@ def _javainfo_init(
direct = [output_jar],
transitive = [dep.transitive_runtime_jars for dep in concatenated_deps.exports_deps + runtime_deps],
)

# For backward compatibility, we use deps_exports for add_exports/add_opens for JavaInfo constructor
# rather than runtimedeps_exports_deps (used by java_info_for_compilation).
# However, runtimedeps_exports_deps makes more sense.
#
# TODO: When this flag is removed, move this logic into _javainfo_init_base
# and remove the special case from java_info_for_compilation.
module_flags_deps = concatenated_deps.deps_exports
if _java_common_internal._incompatible_java_info_merge_runtime_module_flags():
module_flags_deps = concatenated_deps.runtimedeps_exports_deps

result.update(
transitive_runtime_jars = transitive_runtime_jars,
transitive_source_jars = depset(
Expand All @@ -736,11 +747,11 @@ def _javainfo_init(
module_flags_info = _create_module_flags_info(
add_exports = depset(transitive = [
dep.module_flags_info.add_exports
for dep in concatenated_deps.deps_exports
for dep in module_flags_deps
]),
add_opens = depset(transitive = [
dep.module_flags_info.add_opens
for dep in concatenated_deps.deps_exports
for dep in module_flags_deps
]),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,18 +850,32 @@ public void buildHelperCreateJavaInfoWithManifestProto_javaRuleOutputJarsProvide

@Test
public void buildHelperCreateJavaInfoWithModuleFlags() throws Exception {
setBuildLanguageOptions("--noincompatible_java_info_merge_runtime_module_flags");
ruleBuilder().build();
scratch.file(
"foo/BUILD",
"load(':extension.bzl', 'my_rule')",
"java_library(",
" name = 'my_java_lib_direct',",
" srcs = ['java/A.java'],",
" add_exports = ['java.base/java.lang'],",
" add_opens = ['java.base/java.lang'],",
")",
"java_library(",
" name = 'my_java_lib_runtime',",
" srcs = ['java/A.java'],",
" add_opens = ['java.base/java.util'],",
")",
"java_library(",
" name = 'my_java_lib_exports',",
" srcs = ['java/A.java'],",
" add_opens = ['java.base/java.math'],",
")",
"my_rule(",
" name = 'my_starlark_rule',",
" dep = [':my_java_lib_direct'],",
" dep_runtime = [':my_java_lib_runtime'],",
" dep_exports = [':my_java_lib_exports'],",
" output_jar = 'my_starlark_rule_lib.jar',",
")");
assertNoEvents();
Expand All @@ -870,7 +884,56 @@ public void buildHelperCreateJavaInfoWithModuleFlags() throws Exception {
fetchJavaInfo().getProvider(JavaModuleFlagsProvider.class);

assertThat(ruleOutputs.toFlags())
.containsExactly("--add-opens=java.base/java.lang=ALL-UNNAMED");
.containsExactly(
"--add-exports=java.base/java.lang=ALL-UNNAMED",
// no java.base/java.util under --noincompatible_java_info_merge_runtime_module_flags
"--add-opens=java.base/java.lang=ALL-UNNAMED",
"--add-opens=java.base/java.math=ALL-UNNAMED")
.inOrder();
}

@Test
public void buildHelperCreateJavaInfoWithModuleFlagsIncompatibleMergeRuntime() throws Exception {
setBuildLanguageOptions("--incompatible_java_info_merge_runtime_module_flags");
ruleBuilder().build();
scratch.file(
"foo/BUILD",
"load(':extension.bzl', 'my_rule')",
"java_library(",
" name = 'my_java_lib_direct',",
" srcs = ['java/A.java'],",
" add_exports = ['java.base/java.lang'],",
" add_opens = ['java.base/java.lang'],",
")",
"java_library(",
" name = 'my_java_lib_runtime',",
" srcs = ['java/A.java'],",
" add_opens = ['java.base/java.util'],",
")",
"java_library(",
" name = 'my_java_lib_exports',",
" srcs = ['java/A.java'],",
" add_opens = ['java.base/java.math'],",
")",
"my_rule(",
" name = 'my_starlark_rule',",
" dep = [':my_java_lib_direct'],",
" dep_runtime = [':my_java_lib_runtime'],",
" dep_exports = [':my_java_lib_exports'],",
" output_jar = 'my_starlark_rule_lib.jar',",
")");
assertNoEvents();

JavaModuleFlagsProvider ruleOutputs =
fetchJavaInfo().getProvider(JavaModuleFlagsProvider.class);

assertThat(ruleOutputs.toFlags())
.containsExactly(
"--add-exports=java.base/java.lang=ALL-UNNAMED",
"--add-opens=java.base/java.util=ALL-UNNAMED",
"--add-opens=java.base/java.math=ALL-UNNAMED",
"--add-opens=java.base/java.lang=ALL-UNNAMED")
.inOrder();
}

@Test
Expand Down

0 comments on commit 521dbb0

Please sign in to comment.