diff options
| author | zhylmzr <zhylmzr@gmail.com> | 2024-07-23 01:53:51 +0800 |
|---|---|---|
| committer | Alex Rønne Petersen <alex@alexrp.com> | 2025-01-29 10:48:36 +0100 |
| commit | 308ba80597864ab12b77cff1ce28f001ad551fdf (patch) | |
| tree | 3b8d28b3622a8ad508d54e31fef8baa1ba645af0 /src/main.zig | |
| parent | 7843deb16be3e28068398c1397345c8bb22ea6c6 (diff) | |
| download | zig-308ba80597864ab12b77cff1ce28f001ad551fdf.tar.gz zig-308ba80597864ab12b77cff1ce28f001ad551fdf.zip | |
fix(cc): make link and preprocessor logic to be more consistent with
clang's behavior.
1. `zig cc main.c -o /dev/null` shouldn't emit a.out
2. `zig cc -E main.c` and `zig cc -E main -o -` should output to stdout
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/main.zig b/src/main.zig index 1177a0baab..34d610bdd1 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2718,7 +2718,9 @@ fn buildOutputType( switch (c_out_mode orelse .link) { .link => { create_module.opts.output_mode = if (is_shared_lib) .Lib else .Exe; - emit_bin = if (out_path) |p| .{ .yes = p } else EmitBin.yes_a_out; + if (emit_bin != .no) { + emit_bin = if (out_path) |p| .{ .yes = p } else EmitBin.yes_a_out; + } if (emit_llvm) { fatal("-emit-llvm cannot be used when linking", .{}); } @@ -2768,10 +2770,13 @@ fn buildOutputType( emit_bin = if (out_path) |p| .{ .yes = p } else .yes_default_path; clang_preprocessor_mode = .pch; } else { - if (out_path) |p| { - emit_bin = .{ .yes = p }; + // If the output path is "-" (stdout), then we need to emit the preprocessed output to stdout + // like "clang -E main.c -o -" does. + if (out_path != null and !mem.eql(u8, out_path.?, "-")) { + emit_bin = .{ .yes = out_path.? }; clang_preprocessor_mode = .yes; } else { + emit_bin = .no; clang_preprocessor_mode = .stdout; } } |
