Skip to content

Commit

Permalink
Add an is_android option to facilitate exec toolchain selection
Browse files Browse the repository at this point in the history
RELNOTES:None.
PiperOrigin-RevId: 562925627
Change-Id: I30a83d83d8d97947b446cb53ed6555c87b367b60
  • Loading branch information
scentini authored and copybara-github committed Sep 5, 2023
1 parent b29649f commit f064184
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,19 @@ public static class Options extends FragmentOptions {
+ " transition` with changed options to avoid potential action conflicts.")
public boolean androidPlatformsTransitionsUpdateAffected;

@Option(
name = "is_android",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
effectTags = OptionEffectTag.BAZEL_INTERNAL_CONFIGURATION,
help =
"This option exists for the purposes of enabling the toolchain resolution mechanism"
+ " to select a different `exec` toolchain when targeting Android. An example use"
+ " case is Rust: The Rust toolchain has a requirement that certain types of"
+ " libraries (proc-macro) that are built in `exec` mode *have* to be compiled with"
+ " the same toolchain as the libraries built in `target` mode.")
public boolean isAndroid;

@Override
public FragmentOptions getExec() {
Options exec = (Options) super.getExec();
Expand Down Expand Up @@ -1137,6 +1150,7 @@ public FragmentOptions getExec() {
exec.persistentBusyboxTools = persistentBusyboxTools;
exec.persistentMultiplexBusyboxTools = persistentMultiplexBusyboxTools;
exec.disableNativeAndroidRules = disableNativeAndroidRules;
exec.isAndroid = isAndroid;

// Unless the build was started from an Android device, exec means MAIN.
exec.configurationDistinguisher = ConfigurationDistinguisher.MAIN;
Expand Down Expand Up @@ -1193,6 +1207,7 @@ public FragmentOptions getExec() {
private final boolean hwasan;
private final boolean getJavaResourcesFromOptimizedJar;
private final boolean includeProguardLocationReferences;
private final boolean isAndroid;

public AndroidConfiguration(BuildOptions buildOptions) throws InvalidConfigurationException {
Options options = buildOptions.get(Options.class);
Expand Down Expand Up @@ -1254,6 +1269,7 @@ public AndroidConfiguration(BuildOptions buildOptions) throws InvalidConfigurati
this.hwasan = options.hwasan;
this.getJavaResourcesFromOptimizedJar = options.getJavaResourcesFromOptimizedJar;
this.includeProguardLocationReferences = options.includeProguardLocationReferences;
this.isAndroid = options.isAndroid;

if (incrementalDexingShardsAfterProguard < 0) {
throw new InvalidConfigurationException(
Expand Down Expand Up @@ -1534,6 +1550,10 @@ boolean outputLibraryMergedAssets() {
return outputLibraryMergedAssets;
}

boolean isAndroid() {
return isAndroid;
}

/** Returns the label provided with --legacy_main_dex_list_generator, if any. */
// TODO(b/147692286): Move R8's main dex list tool into tool repository.
@StarlarkConfigurationField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public BuildOptions patch(BuildOptionsView options, EventHandler eventHandler) {
// 2. Otherwise, leave --platforms alone (this will probably lead to build errors).
if (!androidOptions.androidPlatforms.isEmpty()) {
// If the current value of --platforms is not one of the values of --android_platforms, change
// it to be the first one. If the curent --platforms is part of --android_platforms, leave it
// it to be the first one. If the current --platforms is part of --android_platforms, leave it
// as-is.
// NOTE: This does not handle aliases at all, so if someone is using aliases with platform
// definitions this check will break.
Expand All @@ -90,6 +90,8 @@ public BuildOptions patch(BuildOptionsView options, EventHandler eventHandler) {
newOptions.get(CppOptions.class).enableCcToolchainResolution = true;
}

newOptions.get(AndroidConfiguration.Options.class).isAndroid = true;

if (androidOptions.androidPlatformsTransitionsUpdateAffected) {
ImmutableSet.Builder<String> affected = ImmutableSet.builder();
if (!options
Expand All @@ -102,6 +104,10 @@ public BuildOptions patch(BuildOptionsView options, EventHandler eventHandler) {
!= newOptions.get(CppOptions.class).enableCcToolchainResolution) {
affected.add("//command_line_option:incompatible_enable_cc_toolchain_resolution");
}
if (options.get(AndroidConfiguration.Options.class).isAndroid
!= newOptions.get(AndroidConfiguration.Options.class).isAndroid) {
affected.add("//command_line_option:is_android");
}
FunctionTransitionUtil.updateAffectedByStarlarkTransition(
newOptions.get(CoreOptions.class), affected.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public void dataBindingAnnotationProcessorFlags_v3_4() throws Exception {
(JavaCompileAction)
getGeneratingAction(getFirstArtifactEndingWith(allArtifacts, "app.jar"));
String dataBindingFilesDir =
targetConfig
getConfiguration(ctapp)
.getBinDirectory(RepositoryName.MAIN)
.getExecPath()
.getRelative("java/android/binary/databinding/app")
Expand Down Expand Up @@ -1214,7 +1214,7 @@ public void dataBinding_androidLocalTest_dataBindingEnabled_usesDataBindingFlags
getGeneratingAction(
getFirstArtifactEndingWith(allArtifacts, "databinding_enabled_test-class.jar"));
String dataBindingFilesDir =
targetConfig
getConfiguration(testTarget)
.getBinDirectory(RepositoryName.MAIN)
.getExecPath()
.getRelative("javatests/android/test/databinding/databinding_enabled_test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ public void testTestExecutableRunfiles() throws Exception {
.toList());
assertThat(runfiles.stream().map(Artifact::toString).collect(toImmutableList()))
.containsAtLeast(
getDeviceFixtureScript(getConfiguredTarget("//javatests/com/app:device_fixture"))
getDeviceFixtureScript(
getDirectPrerequisite(
androidInstrumentationTest.getConfiguredTarget(),
"//javatests/com/app:device_fixture"))
.toString(),
getInstrumentationApk(getConfiguredTarget("//javatests/com/app:instrumentation_app"))
.toString(),
Expand Down

0 comments on commit f064184

Please sign in to comment.