Skip to content

Commit df53cbe

Browse files
Bug 1943149 - build(swgl): work around broken (upstream) cc::Tool detection of clang --driver-mode=cl r=gfx-reviewers,nical
With the current version of `cc`, we depend on behavior where, if `CC` is set to something like `clang --driver-mode=cl`, we expect to be able to use arguments on the command line a la MSVC's `cl.exe`. We were actually the original contributors of a heuristic to detect this in the `cc` crate, and it's served us well. In `cc` upstream since 1.0.89, a new heuristic for detecting compiler families in `cc::Tool` was introduced which does not have the desired behavior, and misclassifies the above case as being `clang`-like, rather than `cl`-like. The heuristic we originally submitted upstream is now in a fallback path which does not get used for our case. This causes `cc`'s default flags and APIs like `cc::Tool::is_like_msvc` to be incorrect. `swgl`, in particular, breaks because of this, since it's opinionated on the arguments it wants to provide to compilers. Work around the above regression by detecting checking `Tool`s' base command and "wrapper arguments" to see if we have a wrapper argument matching `--driver-mode=cl`. If so, provide `cl`-like arguments in `swgl`, rather than `clang`-like arguments. This behavior has been fixed upstream; see <rust-lang/cc-rs#1378>. Once released, we can consume it and revert this patch. Differential Revision: https://phabricator.services.mozilla.com/D236305
1 parent 9fab2c3 commit df53cbe

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

swgl/build.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn translate_shader(
116116
let mut build = cc::Build::new();
117117
build.no_default_flags(true);
118118
if let Ok(tool) = build.try_get_compiler() {
119-
if tool.is_like_msvc() {
119+
if is_like_msvc(&tool) {
120120
build.flag("/EP");
121121
if tool.path().to_str().is_some_and(|p| p.contains("clang")) {
122122
build.flag("/clang:-undef");
@@ -185,7 +185,7 @@ fn main() {
185185
build.cpp(true);
186186

187187
if let Ok(tool) = build.try_get_compiler() {
188-
if tool.is_like_msvc() {
188+
if is_like_msvc(&tool) {
189189
build
190190
.flag("/std:c++17")
191191
.flag("/EHs-")
@@ -212,7 +212,7 @@ fn main() {
212212
// instructions makes things easier on the processor and in places where it matters we can
213213
// probably explicitly use reciprocal instructions and avoid the refinement step.
214214
// Also, allow checks for non-finite values which fast-math may disable.
215-
if tool.is_like_msvc() {
215+
if is_like_msvc(&tool) {
216216
build
217217
.flag("/fp:fast")
218218
.flag("-Xclang")
@@ -252,3 +252,20 @@ impl Drop for EnvVarGuard {
252252
}
253253
}
254254
}
255+
256+
fn is_like_msvc(tool: &cc::Tool) -> bool {
257+
tool.is_like_msvc() || {
258+
tool.is_like_clang()
259+
&& tool.to_command().get_args().any(|arg| {
260+
// `mozilla-central` does this funky thing where it replaces `clang-cl.exe` with
261+
// `clang.exe --driver-mode=cl`, which isn't considered by `Tool::is_like_msvc`, _but_
262+
// it forces the CLI to adhere to a `cl`-like interface and reject naively `clang`-like
263+
// arguments.
264+
//
265+
// See also `config/static-checking-config.mk`:
266+
// <https://searchfox.org/mozilla-central/rev/dd8b64a6198ff599a5eb2ca096845ebd6997457f/config/static-checking-config.mk>
267+
arg.to_str()
268+
.is_some_and(|a| a.starts_with("--driver-mode=cl"))
269+
})
270+
}
271+
}

0 commit comments

Comments
 (0)