authorgravatar for zhylmzr@gmail.comzhylmzr <zhylmzr@gmail.com> 2024-07-23 01:53:51+08:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-01-29 10:48:36+01:00
log308ba80597864ab12b77cff1ce28f001ad551fdf
tree3b8d28b3622a8ad508d54e31fef8baa1ba645af0
parent7843deb16be3e28068398c1397345c8bb22ea6c6

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

1 files changed, 8 insertions(+), 3 deletions(-)

src/main.zig+8-3
...@@ -2718,7 +2718,9 @@ fn buildOutputType(...@@ -2718,7 +2718,9 @@ fn buildOutputType(
2718 switch (c_out_mode orelse .link) {2718 switch (c_out_mode orelse .link) {
2719 .link => {2719 .link => {
2720 create_module.opts.output_mode = if (is_shared_lib) .Lib else .Exe;2720 create_module.opts.output_mode = if (is_shared_lib) .Lib else .Exe;
2721 emit_bin = if (out_path) |p| .{ .yes = p } else EmitBin.yes_a_out;2721 if (emit_bin != .no) {
2722 emit_bin = if (out_path) |p| .{ .yes = p } else EmitBin.yes_a_out;
2723 }
2722 if (emit_llvm) {2724 if (emit_llvm) {
2723 fatal("-emit-llvm cannot be used when linking", .{});2725 fatal("-emit-llvm cannot be used when linking", .{});
2724 }2726 }
...@@ -2768,10 +2770,13 @@ fn buildOutputType(...@@ -2768,10 +2770,13 @@ fn buildOutputType(
2768 emit_bin = if (out_path) |p| .{ .yes = p } else .yes_default_path;2770 emit_bin = if (out_path) |p| .{ .yes = p } else .yes_default_path;
2769 clang_preprocessor_mode = .pch;2771 clang_preprocessor_mode = .pch;
2770 } else {2772 } else {
2771 if (out_path) |p| {2773 // If the output path is "-" (stdout), then we need to emit the preprocessed output to stdout
2772 emit_bin = .{ .yes = p };2774 // like "clang -E main.c -o -" does.
2775 if (out_path != null and !mem.eql(u8, out_path.?, "-")) {
2776 emit_bin = .{ .yes = out_path.? };
2773 clang_preprocessor_mode = .yes;2777 clang_preprocessor_mode = .yes;
2774 } else {2778 } else {
2779 emit_bin = .no;
2775 clang_preprocessor_mode = .stdout;2780 clang_preprocessor_mode = .stdout;
2776 }2781 }
2777 }2782 }