Support running tests on the target platform
This adds an --use_target_platform_for_tests option that changes tests to use the execution properties from the target platform rather than the host platform. I believe that the code is currently incorrect - if the host and target platforms differ, then tests are cross-compiled for the target platform, but use the execution properties for the host platform.
This matters for remote execution, where host and target platform may be different CPU architectures and operating systems (e.g., compiling on x64 Linux and running on ARM64 Mac). Currently, such tests will typically fail to run if they contain architecture or OS-specific code.
Based on work by Ulf Adams <ulf@engflow.com> and updated by tristan.maat@codethink.co.uk
Progress on #10799.
RELNOTES: Add --use_target_platform_for_tests which uses the target platform for executing tests instead of the execution platform.
Closes #17390.
PiperOrigin-RevId: 509734553
Change-Id: I889f2b1322564298ef4145562afc47623e2c2745
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
index 4009e63..7fbaadf 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
@@ -15,6 +15,7 @@
package com.google.devtools.build.lib.analysis;
import static com.google.common.collect.ImmutableList.toImmutableList;
+import static com.google.devtools.build.lib.analysis.test.ExecutionInfo.DEFAULT_TEST_RUNNER_EXEC_GROUP;
import static com.google.devtools.build.lib.packages.ExecGroup.DEFAULT_EXEC_GROUP_NAME;
import com.google.common.annotations.VisibleForTesting;
@@ -409,6 +410,38 @@
return getActionOwner(DEFAULT_EXEC_GROUP_NAME);
}
+ /**
+ * Returns a special action owner for test actions. Test actions should run on the target platform
+ * rather than the host platform. Note that the value is not cached (on the assumption that this
+ * method is only called once).
+ */
+ public ActionOwner getTestActionOwner() {
+ PlatformInfo testExecutionPlatform;
+ ImmutableMap<String, String> testExecProperties;
+
+ // If we have a toolchain, pull the target platform out of it.
+ if (toolchainContexts != null) {
+ // TODO(https://github.com/bazelbuild/bazel/issues/17466): This doesn't respect execution
+ // properties coming from the target's `exec_properties` attribute.
+ // src/test/java/com/google/devtools/build/lib/analysis/test/TestActionBuilderTest.java has a
+ // test to test for it when it gets figured out.
+ testExecutionPlatform = toolchainContexts.getTargetPlatform();
+ testExecProperties = testExecutionPlatform.execProperties();
+ } else {
+ testExecutionPlatform = null;
+ testExecProperties = getExecGroups().getExecProperties(DEFAULT_TEST_RUNNER_EXEC_GROUP);
+ }
+
+ ActionOwner actionOwner =
+ createActionOwner(
+ rule, aspectDescriptors, getConfiguration(), testExecProperties, testExecutionPlatform);
+
+ if (actionOwner == null) {
+ actionOwner = getActionOwner();
+ }
+ return actionOwner;
+ }
+
@Override
@Nullable
public ActionOwner getActionOwner(String execGroup) {