Skip to content

Commit b21ce26

Browse files
committed
Filter unsupported -Wp,-U preprocessor args for zig cc
zig cc doesn't support the `-Wp,-U` pass-through form (e.g. `-Wp,-U_FORTIFY_SOURCE` added by CMake). Strip these args in `filter_linker_arg` to fix builds of crates like `aws-lc-sys` that use CMake internally.
1 parent 6a06943 commit b21ce26

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

.github/workflows/CI.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ jobs:
206206
207207
cargo run run --target aarch64-unknown-linux-gnu --manifest-path tests/hello-rustls/Cargo.toml
208208
cargo run test --target aarch64-unknown-linux-gnu --manifest-path tests/hello-rustls/Cargo.toml
209+
- name: Test aws-lc-rs build
210+
if: matrix.os != 'windows-latest'
211+
env:
212+
# See https://github.com/aws/aws-lc-rs/issues/512
213+
AWS_LC_SYS_NO_ASM: "1"
214+
run: |
215+
cargo run zigbuild --target aarch64-unknown-linux-gnu --manifest-path tests/hello-aws-lc-rs/Cargo.toml
216+
cargo run zigbuild --target aarch64-apple-darwin --manifest-path tests/hello-aws-lc-rs/Cargo.toml
209217
- name: Linux - Test glibc build openssl
210218
if: matrix.os != 'windows-latest'
211219
run: |

src/zig.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,16 @@ fn filter_linker_arg(
542542
return FilteredArg::Skip;
543543
}
544544
}
545+
// zig cc only supports -Wp,-MD, -Wp,-MMD, and -Wp,-MT;
546+
// strip all other -Wp, args (e.g. -Wp,-U_FORTIFY_SOURCE from CMake)
547+
// https://github.com/ziglang/zig/blob/0.15.2/src/main.zig#L2798
548+
if arg.starts_with("-Wp,")
549+
&& !arg.starts_with("-Wp,-MD")
550+
&& !arg.starts_with("-Wp,-MMD")
551+
&& !arg.starts_with("-Wp,-MT")
552+
{
553+
return FilteredArg::Skip;
554+
}
545555
if arg.starts_with("-march=") {
546556
if target_info.is_arm() || target_info.is_i386() {
547557
return FilteredArg::Skip;
@@ -2288,6 +2298,32 @@ mod tests {
22882298
}
22892299
}
22902300

2301+
#[test]
2302+
fn test_filter_wp_args() {
2303+
let linux = Some("x86_64-unknown-linux-gnu");
2304+
// Unsupported -Wp, args should be removed
2305+
for arg in &[
2306+
"-Wp,-U_FORTIFY_SOURCE",
2307+
"-Wp,-DFOO=1",
2308+
"-Wp,-MF,/tmp/t.d",
2309+
"-Wp,-MQ,foo",
2310+
"-Wp,-MP",
2311+
] {
2312+
let result = run_filter_one(arg, linux, (13, 0));
2313+
assert!(result.is_empty(), "{arg} should be removed");
2314+
}
2315+
// Supported -Wp, args should be kept (-MD, -MMD, -MT)
2316+
for arg in &["-Wp,-MD,/tmp/test.d", "-Wp,-MMD,/tmp/test.d", "-Wp,-MT,foo"] {
2317+
let result = run_filter_one(arg, linux, (13, 0));
2318+
assert_eq!(result, vec![*arg], "{arg} should be kept");
2319+
}
2320+
// bare -U and -D should be kept (zig cc supports them directly)
2321+
let result = run_filter_one("-U_FORTIFY_SOURCE", linux, (13, 0));
2322+
assert_eq!(result, vec!["-U_FORTIFY_SOURCE"]);
2323+
let result = run_filter_one("-DFOO=1", linux, (13, 0));
2324+
assert_eq!(result, vec!["-DFOO=1"]);
2325+
}
2326+
22912327
#[test]
22922328
fn test_filter_musl_args() {
22932329
let musl = Some("x86_64-unknown-linux-musl");

tests/hello-aws-lc-rs/Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)