Skip to content

Commit

Permalink
Add android constraints inside java_lite_proto_library
Browse files Browse the repository at this point in the history
I’ve extended internal java semantics with add_constraints function (open source version doesn’t need it - empty function is enough).
In order to use internal add_constraint in tests (which depends on java_common.add_contraints -> experimental only), I've added experimental_google_legacy_api build option to all tests which depends on android constraint and java_lite_proto_library.

Resolved not allowed dependencies on .jar artifacts in Android binaries by adding JavaInfo inside 'provides' in java_lite_proto_library rule.

I've also added java_lite_proto_aspect as an artifact name for JavaLiteProtoLibrary inside GoogleJavaSematics.

PiperOrigin-RevId: 430192660
  • Loading branch information
Googler authored and Copybara-Service committed Feb 22, 2022
1 parent 2655ebe commit 8bd989e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi;
import com.google.devtools.build.lib.starlarkbuildapi.java.JavaCommonApi;
import com.google.devtools.build.lib.starlarkbuildapi.java.JavaToolchainStarlarkApiProviderApi;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Module;
import net.starlark.java.eval.Sequence;
Expand Down Expand Up @@ -270,9 +272,12 @@ public ProviderApi getMessageBundleInfo() {

@Override
public JavaInfo addConstraints(JavaInfo javaInfo, Sequence<?> constraints) throws EvalException {
// No implementation in Bazel. This method not callable in Starlark except through
// (discouraged) use of --experimental_google_legacy_api.
return null;
List<String> constraintStrings = Sequence.cast(constraints, String.class, "constraints");
ImmutableList<String> mergedConstraints =
Stream.concat(javaInfo.getJavaConstraints().stream(), constraintStrings.stream())
.distinct()
.collect(toImmutableList());
return JavaInfo.Builder.copyOf(javaInfo).setJavaConstraints(mergedConstraints).build();
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/starlark/builtins_bzl/common/java/java_semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def _get_coverage_runner(ctx):
# wrap the jar in JavaInfo so we can add it to deps for java_common.compile()
return JavaInfo(output_jar = runner_jar, compile_jar = runner_jar)

def _add_constraints(java_info, constraints):
return java_info

semantics = struct(
JAVA_TOOLCHAIN_LABEL = "@bazel_tools//tools/jdk:current_java_toolchain",
JAVA_PLUGINS_FLAG_ALIAS_LABEL = "@bazel_tools//tools/jdk:java_plugins_flag_alias",
Expand All @@ -57,4 +60,5 @@ semantics = struct(
LINT_PROGRESS_MESSAGE = "Running Android Lint for: %{label}",
check_proto_registry_collision = _check_proto_registry_collision,
get_coverage_runner = _get_coverage_runner,
add_constraints = _add_constraints,
)
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def _aspect_impl(target, ctx):
java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo],
enable_jspecify = False,
)
java_info = semantics.add_constraints(java_info, ["android"])
else:
# If there are no proto sources just pass along the compilation dependencies.
java_info = java_common.merge(deps)
Expand Down Expand Up @@ -148,4 +149,5 @@ java_lite_proto_library = rule(
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_java_lite"),
),
},
provides = [JavaInfo],
)
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public final void setUpMocks() throws Exception {

@Before
public final void setupStarlarkRule() throws Exception {
setBuildLanguageOptions("--experimental_builtins_injection_override=+java_lite_proto_library");
setBuildLanguageOptions(
"--experimental_builtins_injection_override=+java_lite_proto_library",
"--experimental_google_legacy_api");
}

private void mockToolchains() throws IOException {
Expand Down

0 comments on commit 8bd989e

Please sign in to comment.