Skip to content

Commit

Permalink
Change SingleJar with Ijar
Browse files Browse the repository at this point in the history
Extended java_common.run_ijar with `output` param in order to be used from java_import.
Added test for this parameter.

PiperOrigin-RevId: 485560192
Change-Id: Iea5fb87f15ca90a6e509c9b5f1d5f4c40eb3ca7f
  • Loading branch information
kotlaja authored and copybara-github committed Nov 2, 2022
1 parent 4c0f9a7 commit cdce638
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,17 @@ private static List<String> tokenize(List<String> input) throws EvalException {
public Artifact buildIjar(
StarlarkActionFactory actions,
Artifact inputJar,
@Nullable Artifact outputJar,
@Nullable Label targetLabel,
JavaToolchainProvider javaToolchain)
throws EvalException {
String ijarBasename = FileSystemUtils.removeExtension(inputJar.getFilename()) + "-ijar.jar";
Artifact interfaceJar = actions.declareFile(ijarBasename, inputJar);
Artifact interfaceJar;
if (outputJar != null) {
interfaceJar = outputJar;
} else {
String ijarBasename = FileSystemUtils.removeExtension(inputJar.getFilename()) + "-ijar.jar";
interfaceJar = actions.declareFile(ijarBasename, inputJar);
}
FilesToRunProvider ijarTarget = javaToolchain.getIjar();
CustomCommandLine.Builder commandLine =
CustomCommandLine.builder().addExecPath(inputJar).addExecPath(interfaceJar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,21 @@ public JavaInfo createJavaCompileAction(
public Artifact runIjar(
StarlarkActionFactory actions,
Artifact jar,
Object output,
Object targetLabel,
JavaToolchainProvider javaToolchain)
JavaToolchainProvider javaToolchain,
StarlarkThread thread)
throws EvalException {
if (output != Starlark.NONE) {
checkPrivateAccess(thread);
}
return JavaInfoBuildHelper.getInstance()
.buildIjar(
actions, jar, targetLabel != Starlark.NONE ? (Label) targetLabel : null, javaToolchain);
actions,
jar,
output != Starlark.NONE ? (Artifact) output : null,
targetLabel != Starlark.NONE ? (Label) targetLabel : null,
javaToolchain);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ JavaInfoT createJavaCompileAction(
parameters = {
@Param(name = "actions", named = true, doc = "ctx.actions"),
@Param(name = "jar", positional = false, named = true, doc = "The jar to run ijar on."),
@Param(
name = "output",
positional = false,
named = true,
documented = false,
defaultValue = "None"),
@Param(
name = "target_label",
positional = false,
Expand All @@ -338,9 +344,15 @@ JavaInfoT createJavaCompileAction(
positional = false,
named = true,
doc = "A JavaToolchainInfo to used to find the ijar tool."),
})
},
useStarlarkThread = true)
FileApi runIjar(
StarlarkActionFactoryT actions, FileT jar, Object targetLabel, JavaToolchainT javaToolchain)
StarlarkActionFactoryT actions,
FileT jar,
Object output,
Object targetLabel,
JavaToolchainT javaToolchain,
StarlarkThread thread)
throws EvalException;

@StarlarkMethod(
Expand Down
10 changes: 5 additions & 5 deletions src/main/starlark/builtins_bzl/common/java/java_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ load(":common/java/java_common.bzl", "construct_defaultinfo")
load(":common/java/java_semantics.bzl", "semantics")
load(":common/java/proguard_validation.bzl", "VALIDATE_PROGUARD_SPECS_IMPLICIT_ATTRS", "validate_proguard_specs")
load(":common/rule_util.bzl", "merge_attrs")
load(":common/java/java_util.bzl", "create_single_jar")
load(":common/java/import_deps_check.bzl", "import_deps_check")

JavaInfo = _builtins.toplevel.JavaInfo
Expand Down Expand Up @@ -52,10 +51,11 @@ def _process_with_ijars_if_needed(jars, ctx):
interface_jar_directory = "_ijar/" + ctx.label.name + "/" + ijar_basename

interface_jar = ctx.actions.declare_file(interface_jar_directory)
create_single_jar(
ctx,
interface_jar,
depset([jar]),
java_common.run_ijar(
ctx.actions,
jar = jar,
output = interface_jar,
java_toolchain = semantics.find_java_toolchain(ctx),
)
file_dict[jar] = interface_jar

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3429,6 +3429,38 @@ public void disallowJavaImportEmptyJars_fails() throws Exception {
assertContainsEvent("Rule in 'foo' cannot use private API");
}

@Test
public void testRunIjarIsPrivateApi() throws Exception {
JavaToolchainTestUtil.writeBuildFileForJavaToolchain(scratch);
scratch.file(
"foo/custom_rule.bzl",
"def _impl(ctx):",
" java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo]",
" java_common.run_ijar(",
" ctx.actions,",
" jar = ctx.actions.declare_file('input.jar'),",
" output = ctx.actions.declare_file('output.jar'),",
" java_toolchain = java_toolchain,",
" )",
" return []",
"java_custom_library = rule(",
" implementation = _impl,",
" attrs = {",
" '_java_toolchain': attr.label(default = Label('//java/com/google/test:toolchain')),",
" },",
" fragments = ['java']",
")");
scratch.file(
"foo/BUILD",
"load(':custom_rule.bzl', 'java_custom_library')",
"java_custom_library(name = 'custom')");
reporter.removeHandler(failFastHandler);

getConfiguredTarget("//foo:custom");

assertContainsEvent("Rule in 'foo' cannot use private API");
}

@Test
public void testGetBuildInfoArtifacts() throws Exception {
scratch.file(
Expand Down

0 comments on commit cdce638

Please sign in to comment.