From f06418470988721c8c3efe38723f910989180ad4 Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 5 Sep 2023 16:28:53 -0700 Subject: [PATCH] Add an is_android option to facilitate exec toolchain selection RELNOTES:None. PiperOrigin-RevId: 562925627 Change-Id: I30a83d83d8d97947b446cb53ed6555c87b367b60 --- .../rules/android/AndroidConfiguration.java | 20 +++++++++++++++++++ .../android/AndroidPlatformsTransition.java | 8 +++++++- .../android/AndroidDataBindingV2Test.java | 4 ++-- .../AndroidInstrumentationTestTest.java | 5 ++++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidConfiguration.java index 92b644252f9e6d..c0df5c32ba1088 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidConfiguration.java @@ -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(); @@ -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; @@ -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); @@ -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( @@ -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( diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidPlatformsTransition.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidPlatformsTransition.java index 8eaa07760fea39..da3c1711483d73 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidPlatformsTransition.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidPlatformsTransition.java @@ -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. @@ -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 affected = ImmutableSet.builder(); if (!options @@ -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()); } diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingV2Test.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingV2Test.java index bcb616cb199678..6fd60d48d153d5 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingV2Test.java +++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingV2Test.java @@ -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") @@ -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") diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTestTest.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTestTest.java index aa62bf9ee47508..768aafc500037f 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTestTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTestTest.java @@ -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(),