-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Linux to Windows cross compilation of py_binary, java_binary and sh_binary using MinGW #16019
Closed
jesseschalken
wants to merge
11
commits into
bazelbuild:master
from
jesseschalken:fix-windows-launcher-on-mingw
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7b8bb0d
Remove incorrect exec config transition for java_binary, py_binary an…
jesseschalken 29be9a1
Fix Windows detection when cross compiling java_binary, py_binary and…
jesseschalken 3ad5b34
Fix Windows java launcher MinGW compatibility
jesseschalken bfbb0be
Make the Windows Java launcher fully statically linked
jesseschalken 7abb6b1
Set C++ standard version to C++17 on all launcher targets
jesseschalken 05ebbf5
Allow backslashes in input to AsWindowsPathImpl(), they will be norma…
jesseschalken 3dfd8f5
Move compiler constraints on Windows toolchains from exec platform to…
jesseschalken 8f03b44
Add msvc compiler constraint to MSVC toolchains
jesseschalken 694e0f3
Fix missing l modifier when formatting wchar_t*
jesseschalken 55995fe
Link launcher binary with -municode on MinGW
jesseschalken 7e567bb
Merge remote-tracking branch 'origin/master' into fix-windows-launche…
jesseschalken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
|
@@ -37,25 +37,24 @@ private AnalysisTestActionBuilder() {} | |
*/ | ||
public static void writeAnalysisTestAction( | ||
RuleContext ruleContext, AnalysisTestResultInfo testResultInfo) { | ||
// TODO(laszlocsomor): Use the execution platform, not the host platform. | ||
boolean isExecutedOnWindows = OS.getCurrent() == OS.WINDOWS; | ||
boolean isTargetOsWindows = ruleContext.isTargetOsWindows(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @brandjon @tetromino Do you know why we need special casing |
||
String escapedMessage = | ||
isExecutedOnWindows | ||
isTargetOsWindows | ||
? testResultInfo.getMessage().replace("%", "%%") | ||
// Prefix each character with \ (double-escaped; once in the string, once in the | ||
// replacement sequence, which allows backslash-escaping literal "$"). "." is put in | ||
// parentheses because ErrorProne is overly vigorous about objecting to "." as an | ||
// always-matching regex (b/201772278). | ||
: testResultInfo.getMessage().replaceAll("(.)", "\\\\$1"); | ||
StringBuilder sb = new StringBuilder(); | ||
if (isExecutedOnWindows) { | ||
if (isTargetOsWindows) { | ||
sb.append("@echo off\n"); | ||
} | ||
for (String line : Splitter.on("\n").split(escapedMessage)) { | ||
sb.append("echo ").append(line).append("\n"); | ||
} | ||
sb.append("exit "); | ||
if (isExecutedOnWindows) { | ||
if (isTargetOsWindows) { | ||
sb.append("/b "); | ||
} | ||
sb.append(testResultInfo.getSuccess() ? "0" : "1"); | ||
|
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will probably fail with Bzlmod as it doesn't use a repository mapping - with Bzlmod, the
platforms
module will manifest as a repository named something like@platforms~0.0.4
.You should be able to use:
but that won't work from a static context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Should I just parse the labels directly in
isTargetOsWindows
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I unfortunately don't know enough about constraint handling in Java code to answer that question - I would leave it for the reviewer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this some more, it's more involved and what I suggested above is incorrect: The rule may be instantiated in any repository, not necessarily the main repository. But these repositories may not depend on the
platforms
module and thus may not even see@platforms
in their repository mapping.@Wyverald can probably help here, but may still be OOO.
Edit: A possible solution could be to introduce
alias
es for the constraint values inbazel_tools
, which AFAIK is always available under that name. This would probably allow you to keep usingLabel.parseAbsolute
, so better keep that code around until someone more knowledgeable suggests a solution.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Wyverald Do you happen to know the best way to test for the
@platforms//os:windows
constraint on the target platform inside a Java rule implementation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@comius would be the best person to answer this, I think. I really don't know whether we're supposed to grab labels like this in Java code.
With that said, what @fmeum pointed out still stands -- we can't rely on the current repository having visibility into the
@platforms
repo. Depending on the answer to the previous question, we might have ways to work around this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move all references to Windows out of RuleContext.
RuleContext should remain a general device, and is no place for specifics. You can either put the code into into each rule or providing a helper class under
rules
orbazel/rules
package.This will allso make it possible to Starlarkify the rule's your modifying. There's already available API in Starlark
ctx.target_platform_has_constraint
.Creating a providers instead of using an implicit dependency to
@platforms//os:windows
is also not rewritable to Starlark (you can't create a ConstraintInfoProvider). Please use the example you found in copy_file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@comius Thanks. How do I get a
Label
for@platforms//os:windows
to use inBazel{Sh,Py,Java}BinaryRule.build
? It looks likeRuleDefinitionEnvironment
can only give Labels for things in@bazel_tools
. Will this work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the above will likely not work with Bzlmod (same reason as @fmeum described earlier).
@bazel_tools
is special since it's a "built-in module" which is shipped with the Bazel binary;@platforms
is not.You would be able to use labels from
@platforms
if you were defining these rules in Starlark (in the builtins_bzl stuff, for example). Not sure where we are on that front -- I thinkjava_binary
is already Starlarkified?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It think this would work, except if I'm missing some bzlmod machinery that is invoked on Labels in Starlark. It's not different from other implicit dependencies - which also point at various different location outside of Bazel.
There's another problem: it will only work in Bazel, not in a monorepo. We use getToolsRepository to map it to absolute label. Can you extend it with
getPlatfromsRepository()
? This should make it possible to remap @platforms as well.