Skip to content

Commit

Permalink
Use the execution platform to determine which genrule.cmd* to run.
Browse files Browse the repository at this point in the history
Currently the host platform is used, which fails when using a Windows remote
executor from a non-Windows host.

Also change GenRuleWindowsConfiguredTargetTest to run on every host platform.

Fixes bazelbuild#18584.
  • Loading branch information
tjgq committed Jun 6, 2023
1 parent 50c16b3 commit 36cc2d1
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 57 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/google/devtools/build/lib/analysis/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2181,6 +2181,12 @@ java_library(
java_library(
name = "constraints/constraint_constants",
srcs = ["constraints/ConstraintConstants.java"],
deps = [
"//src/main/java/com/google/devtools/build/lib/analysis/platform",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/util:os",
"//third_party:guava",
],
)

java_library(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@

package com.google.devtools.build.lib.analysis;

import static com.google.devtools.build.lib.analysis.constraints.ConstraintConstants.OS_TO_CONSTRAINTS;

import com.google.common.base.Preconditions;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.vfs.PathFragment;
import javax.annotation.Nullable;

/** Class to work with the shell toolchain, e.g. get the shell interpreter's path. */
/**
* Class to work with the shell toolchain, e.g. get the shell interpreter's path.
*/
public final class ShToolchain {

private static PathFragment getHostOrDefaultPath() {
Expand Down Expand Up @@ -65,7 +69,7 @@ public static PathFragment getPathForPlatform(
for (OS os : ShellConfiguration.getShellExecutables().keySet()) {
if (platformInfo
.constraints()
.hasConstraintValue(ShellConfiguration.OS_TO_CONSTRAINTS.get(os))) {
.hasConstraintValue(OS_TO_CONSTRAINTS.get(os))) {
return ShellConfiguration.getShellExecutables().get(os);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@
// limitations under the License.
package com.google.devtools.build.lib.analysis;

import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.config.BuildOptions;
import com.google.devtools.build.lib.analysis.config.Fragment;
import com.google.devtools.build.lib.analysis.config.FragmentOptions;
import com.google.devtools.build.lib.analysis.config.RequiresOptions;
import com.google.devtools.build.lib.analysis.platform.ConstraintSettingInfo;
import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.util.OptionsUtils.PathFragmentConverter;
import com.google.devtools.build.lib.vfs.PathFragment;
Expand All @@ -37,10 +33,6 @@ public class ShellConfiguration extends Fragment {

private static Map<OS, PathFragment> shellExecutables;

private static final ConstraintSettingInfo OS_CONSTRAINT_SETTING =
ConstraintSettingInfo.create(
Label.parseCanonicalUnchecked("@platforms//os:os"));

private static Function<Options, PathFragment> optionsBasedDefault;

/**
Expand All @@ -65,35 +57,7 @@ public static void injectShellExecutableFinder(Map<OS, PathFragment> osToShellMa
optionsBasedDefault = (options) -> null;
shellExecutables = osToShellMap;
}
// Standard mapping between OS and the corresponding platform constraints.
static final ImmutableMap<OS, ConstraintValueInfo> OS_TO_CONSTRAINTS =
ImmutableMap.<OS, ConstraintValueInfo>builder()
.put(
OS.DARWIN,
ConstraintValueInfo.create(
OS_CONSTRAINT_SETTING,
Label.parseCanonicalUnchecked("@platforms//os:osx")))
.put(
OS.WINDOWS,
ConstraintValueInfo.create(
OS_CONSTRAINT_SETTING,
Label.parseCanonicalUnchecked("@platforms//os:windows")))
.put(
OS.FREEBSD,
ConstraintValueInfo.create(
OS_CONSTRAINT_SETTING,
Label.parseCanonicalUnchecked("@platforms//os:freebsd")))
.put(
OS.OPENBSD,
ConstraintValueInfo.create(
OS_CONSTRAINT_SETTING,
Label.parseCanonicalUnchecked("@platforms//os:openbsd")))
.put(
OS.UNKNOWN,
ConstraintValueInfo.create(
OS_CONSTRAINT_SETTING,
Label.parseCanonicalUnchecked("@platforms//os:none")))
.buildOrThrow();


private final boolean useShBinaryStubScript;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,54 @@
// limitations under the License.
package com.google.devtools.build.lib.analysis.constraints;

/** Constants needed for use of the constraints system. */
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.platform.ConstraintSettingInfo;
import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.util.OS;

/**
* Constants needed for use of the constraints system.
*/
public final class ConstraintConstants {

public static final String ENVIRONMENT_RULE = "environment";

private static final ConstraintSettingInfo OS_CONSTRAINT_SETTING =
ConstraintSettingInfo.create(
Label.parseCanonicalUnchecked("@platforms//os:os"));

// Standard mapping between OS and the corresponding platform constraints.
public static final ImmutableMap<OS, ConstraintValueInfo> OS_TO_CONSTRAINTS =
ImmutableMap.<OS, ConstraintValueInfo>builder()
.put(
OS.DARWIN,
ConstraintValueInfo.create(
OS_CONSTRAINT_SETTING,
Label.parseCanonicalUnchecked("@platforms//os:osx")))
.put(
OS.WINDOWS,
ConstraintValueInfo.create(
OS_CONSTRAINT_SETTING,
Label.parseCanonicalUnchecked("@platforms//os:windows")))
.put(
OS.FREEBSD,
ConstraintValueInfo.create(
OS_CONSTRAINT_SETTING,
Label.parseCanonicalUnchecked("@platforms//os:freebsd")))
.put(
OS.OPENBSD,
ConstraintValueInfo.create(
OS_CONSTRAINT_SETTING,
Label.parseCanonicalUnchecked("@platforms//os:openbsd")))
.put(
OS.UNKNOWN,
ConstraintValueInfo.create(
OS_CONSTRAINT_SETTING,
Label.parseCanonicalUnchecked("@platforms//os:none")))
.buildOrThrow();

// No-op constructor to keep this from being instantiated.
private ConstraintConstants() {}
private ConstraintConstants() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
"//src/main/java/com/google/devtools/build/lib/analysis:config/execution_transition_factory",
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
"//src/main/java/com/google/devtools/build/lib/analysis:constraints/constraint_constants",
"//src/main/java/com/google/devtools/build/lib/analysis:file_provider",
"//src/main/java/com/google/devtools/build/lib/analysis:make_variable_supplier",
"//src/main/java/com/google/devtools/build/lib/analysis:rule_definition_environment",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.devtools.build.lib.rules.genrule;

import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.devtools.build.lib.analysis.constraints.ConstraintConstants.OS_TO_CONSTRAINTS;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -94,8 +95,8 @@ enum CommandType {
private static Pair<CommandType, String> determineCommandTypeAndAttribute(
RuleContext ruleContext) {
AttributeMap attributeMap = ruleContext.attributes();
// TODO(pcloudy): This should match the execution platform instead of using OS.getCurrent()
if (OS.getCurrent() == OS.WINDOWS) {
if (ruleContext.getExecutionPlatform().constraints()
.hasConstraintValue(OS_TO_CONSTRAINTS.get(OS.WINDOWS))) {
if (attributeMap.isAttributeValueExplicitlySpecified("cmd_ps")) {
return Pair.of(CommandType.WINDOWS_POWERSHELL, "cmd_ps");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ filegroup(

java_library(
name = "GenruleTests_lib",
srcs = glob(
["*.java"],
exclude = ["GenRuleWindowsConfiguredTargetTest.java"],
) +
# If we are on windows add back the test
select({
"//conditions:default": [],
"//src/conditions:windows": ["GenRuleWindowsConfiguredTargetTest.java"],
}),
srcs = glob(["*.java"]),
deps = [
"//src/main/java/com/google/devtools/build/lib/actions",
"//src/main/java/com/google/devtools/build/lib/actions:artifacts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import com.google.devtools.build.lib.analysis.ShToolchain;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.util.OS;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -58,6 +58,13 @@ private static String getWindowsPath(Artifact artifact) {
return artifact.getExecPathString().replace('/', '\\');
}

@Before
public void setUp() throws Exception {
scratch.file("platforms/BUILD",
"platform(name = 'windows', constraint_values = ['@platforms//os:windows'])");
useConfiguration("--host_platform=//platforms:windows");
}

@Test
public void testCmdBatchIsPreferred() throws Exception {
scratch.file(
Expand Down Expand Up @@ -164,7 +171,8 @@ public void testCmdBashIsPreferred() throws Exception {

String expected = "echo \"Hello, Bash cmd.\" >" + messageArtifact.getExecPathString();
assertThat(shellAction.getArguments().get(0))
.isEqualTo(ShToolchain.getPathForHost(targetConfig).getPathString());
.isEqualTo(ShToolchain.getPathForPlatform(execConfig, shellAction.getExecutionPlatform())
.getPathString());
assertThat(shellAction.getArguments().get(1)).isEqualTo("-c");
assertBashCommandEquals(expected, shellAction.getArguments().get(2));
}
Expand All @@ -181,9 +189,10 @@ public void testMissingCmdAttributeError() throws Exception {

@Test
public void testMissingCmdAttributeErrorOnNonWindowsPlatform() throws Exception {
if (OS.getCurrent() == OS.WINDOWS) {
return;
}
scratch.overwriteFile("platforms/BUILD",
"platform(name = 'nonwindows', constraint_values = ['@platforms//os:linux'])");
useConfiguration("--host_platform=//platforms:nonwindows");

checkError(
"foo",
"bar",
Expand Down

0 comments on commit 36cc2d1

Please sign in to comment.