-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cmd_bash, cmd_ps, cmd_bat attributes to genrule
Based on #9022 Users now can use Batch or Powershell command in genrule on Windows. The attributes have the following priority: `cmd_ps` > `cmd_bat` > `cmd_bash` > `cmd`, but `cmd_ps` and `cmd_bat` will only be taken into consideration on Windows. Fixes #7503 Closes #9024. PiperOrigin-RevId: 261093981
- Loading branch information
1 parent
c35878a
commit d3bacfb
Showing
16 changed files
with
714 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/com/google/devtools/build/lib/analysis/WindowsBatchCommandConstructor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2019 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.analysis; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.devtools.build.lib.actions.Artifact; | ||
import com.google.devtools.build.lib.analysis.actions.FileWriteAction; | ||
|
||
/** The class for constructing command line for Batch on Windows. */ | ||
public final class WindowsBatchCommandConstructor implements CommandConstructor { | ||
private final String scriptNameSuffix; | ||
|
||
WindowsBatchCommandConstructor(String scriptNameSuffix) { | ||
this.scriptNameSuffix = scriptNameSuffix; | ||
} | ||
|
||
@Override | ||
public ImmutableList<String> asExecArgv(String command) { | ||
// `cmd.exe` exists at C:\Windows\System32, which is in the default PATH on Windows. | ||
return ImmutableList.of( | ||
"cmd.exe", "/S", // strip first and last quotes and execute everything else as is. | ||
"/E:ON", // enable extended command set. | ||
"/V:ON", // enable delayed variable expansion | ||
"/D", // ignore AutoRun registry entries. | ||
"/c", // execute command. This must be the last option before the command itself. | ||
command); | ||
} | ||
|
||
@Override | ||
public ImmutableList<String> asExecArgv(Artifact scriptFileArtifact) { | ||
return this.asExecArgv(scriptFileArtifact.getExecPathString().replace('/', '\\')); | ||
} | ||
|
||
@Override | ||
public Artifact commandAsScript(RuleContext ruleContext, String command) { | ||
String scriptFileName = ruleContext.getTarget().getName() + this.scriptNameSuffix; | ||
String scriptFileContents = "@echo off\n" + command; | ||
return FileWriteAction.createFile( | ||
ruleContext, scriptFileName, scriptFileContents, /*executable=*/ true); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...main/java/com/google/devtools/build/lib/analysis/WindowsPowershellCommandConstructor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2019 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.analysis; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.devtools.build.lib.actions.Artifact; | ||
import com.google.devtools.build.lib.analysis.actions.FileWriteAction; | ||
|
||
/** The class for constructing command line for Powershell on Windows. */ | ||
public final class WindowsPowershellCommandConstructor implements CommandConstructor { | ||
private static final String POWERSHELL_SETUP_COMMANDS = | ||
// 1. Use Set-ExecutionPolicy to allow users to run scripts unsigned. | ||
"Set-ExecutionPolicy -Scope CurrentUser RemoteSigned; " | ||
// 2. Set $errorActionPreference to Stop so that we exit immediately if a CmdLet | ||
// fails, | ||
// but when an external command fails, it will still continue. | ||
+ "$errorActionPreference='Stop'; " | ||
// 3. Change the default encoding to utf-8, by default it was utf-16. | ||
// https://stackoverflow.com/questions/40098771 | ||
+ "$PSDefaultParameterValues['*:Encoding'] = 'utf8'; "; | ||
private final String scriptNameSuffix; | ||
|
||
WindowsPowershellCommandConstructor(String scriptNameSuffix) { | ||
this.scriptNameSuffix = scriptNameSuffix; | ||
} | ||
|
||
@Override | ||
public ImmutableList<String> asExecArgv(String command) { | ||
// `powershell.exe` exists at C:\Windows\System32\WindowsPowerShell\v1.0, | ||
// which is in the default PATH on Windows. | ||
// We currently don't support Powershell on Linux/macOS, although Powershell is available on | ||
// those platforms. | ||
return ImmutableList.of("powershell.exe", "/c", POWERSHELL_SETUP_COMMANDS + command); | ||
} | ||
|
||
@Override | ||
public ImmutableList<String> asExecArgv(Artifact scriptFileArtifact) { | ||
// Powershell doesn't search for the current directory by default, we need to always add | ||
// ".\" prefix to a relative path. | ||
return this.asExecArgv(".\\" + scriptFileArtifact.getExecPathString().replace('/', '\\')); | ||
} | ||
|
||
@Override | ||
public Artifact commandAsScript(RuleContext ruleContext, String command) { | ||
String scriptFileName = ruleContext.getTarget().getName() + scriptNameSuffix; | ||
return FileWriteAction.createFile(ruleContext, scriptFileName, command, /*executable=*/ true); | ||
} | ||
} |
Oops, something went wrong.