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
This commit is contained in:
zhylmzr 2024-07-23 01:53:51 +08:00 committed by Jacob Young
parent 9a12905a2d
commit 425da178e8

View File

@ -2555,7 +2555,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", .{});
}
@ -2605,10 +2607,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;
}
}