Skip to content

Commit

Permalink
Support running tests on the target platform
Browse files Browse the repository at this point in the history
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 <[email protected]> and updated by
[email protected]

Progress on bazelbuild#10799.
  • Loading branch information
AustinSchuh committed Feb 2, 2023
1 parent 05bcb51 commit f8b61cb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.devtools.build.lib.packages.ExecGroup.DEFAULT_EXEC_GROUP_NAME;
import static com.google.devtools.build.lib.analysis.test.ExecutionInfo.DEFAULT_TEST_RUNNER_EXEC_GROUP;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
Expand Down Expand Up @@ -409,6 +410,34 @@ public ActionOwner getActionOwner() {
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 executionPlatform;
ImmutableMap<String, String> execProperties;

// If we have a toolchain, pull the target platform out of it.
if (toolchainContexts != null) {
executionPlatform = toolchainContexts.getTargetPlatform();
execProperties = executionPlatform.execProperties();
} else {
executionPlatform = null;
execProperties = getExecGroups().getExecProperties(DEFAULT_TEST_RUNNER_EXEC_GROUP);
}

ActionOwner actionOwner =
createActionOwner(
rule,
aspectDescriptors,
getConfiguration(),
execProperties,
executionPlatform);
return actionOwner;
}

@Override
@Nullable
public ActionOwner getActionOwner(String execGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ private TestParams createTestAction(int shards)
testRunfilesSupplier = SingleRunfilesSupplier.create(runfilesSupport);
}

ActionOwner actionOwner =
testConfiguration.useTargetPlatformForTests()
? ruleContext.getTestActionOwner()
: getOwner();
if (actionOwner == null) {
actionOwner = ruleContext.getActionOwner();
}

// Use 1-based indices for user friendliness.
for (int shard = 0; shard < shardRuns; shard++) {
String shardDir = shardRuns > 1 ? String.format("shard_%d_of_%d", shard + 1, shards) : null;
Expand Down Expand Up @@ -429,7 +437,7 @@ private TestParams createTestAction(int shards)
// TODO(b/234923262): Take exec_group into consideration when selecting sh tools
TestRunnerAction testRunnerAction =
new TestRunnerAction(
getOwner(),
actionOwner,
inputs,
testRunfilesSupplier,
testActionExecutable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ public static class TestOptions extends FragmentOptions {
help = "If true, undeclared test outputs will be archived in a zip file.")
public boolean zipUndeclaredTestOutputs;

@Option(
name = "use_target_platform_for_tests",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.EXECUTION_STRATEGY,
effectTags = {OptionEffectTag.EXECUTION},
help = "If true, then Bazel will run coverage postprocessing for test in a new spawn.")
public boolean useTargetPlatformForTests;

@Override
public FragmentOptions getExec() {
// Options here are either:
Expand Down Expand Up @@ -381,6 +389,10 @@ public boolean getZipUndeclaredTestOutputs() {
return options.zipUndeclaredTestOutputs;
}

public boolean useTargetPlatformForTests() {
return options.useTargetPlatformForTests;
}

/**
* Option converter that han handle two styles of value for "--runs_per_test":
*
Expand Down

0 comments on commit f8b61cb

Please sign in to comment.