authorgravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2025-04-08 13:04:02+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-04-09 15:04:24+02:00
log83e1ce1e00ec04c4311307adc809a1a86b874b05
treee830cb91f2d2eafc32c75eea2afdecc4a471e521
parent9397dc5af686c26fff7cb3f6e93e3151bb5786ca
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

Compilation: Fix logic in addCCArgs() for various file types and flags.

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>

1 files changed, 77 insertions(+), 54 deletions(-)

src/Compilation.zig+77-54
...@@ -5628,6 +5628,41 @@ pub fn addCCArgs(...@@ -5628,6 +5628,41 @@ pub fn addCCArgs(
5628 const llvm_triple = try @import("codegen/llvm.zig").targetTriple(arena, target);5628 const llvm_triple = try @import("codegen/llvm.zig").targetTriple(arena, target);
5629 try argv.appendSlice(&[_][]const u8{ "-target", llvm_triple });5629 try argv.appendSlice(&[_][]const u8{ "-target", llvm_triple });
56305630
5631 switch (target.os.tag) {
5632 .macos => {
5633 try argv.ensureUnusedCapacity(2);
5634 // Pass the proper -m<os>-version-min argument for darwin.
5635 const ver = target.os.version_range.semver.min;
5636 argv.appendAssumeCapacity(try std.fmt.allocPrint(arena, "-mmacos-version-min={d}.{d}.{d}", .{
5637 ver.major, ver.minor, ver.patch,
5638 }));
5639 // This avoids a warning that sometimes occurs when
5640 // providing both a -target argument that contains a
5641 // version as well as the -mmacosx-version-min argument.
5642 // Zig provides the correct value in both places, so it
5643 // doesn't matter which one gets overridden.
5644 argv.appendAssumeCapacity("-Wno-overriding-option");
5645 },
5646 .ios => switch (target.cpu.arch) {
5647 // Pass the proper -m<os>-version-min argument for darwin.
5648 .x86, .x86_64 => {
5649 const ver = target.os.version_range.semver.min;
5650 try argv.append(try std.fmt.allocPrint(
5651 arena,
5652 "-m{s}-simulator-version-min={d}.{d}.{d}",
5653 .{ @tagName(target.os.tag), ver.major, ver.minor, ver.patch },
5654 ));
5655 },
5656 else => {
5657 const ver = target.os.version_range.semver.min;
5658 try argv.append(try std.fmt.allocPrint(arena, "-m{s}-version-min={d}.{d}.{d}", .{
5659 @tagName(target.os.tag), ver.major, ver.minor, ver.patch,
5660 }));
5661 },
5662 },
5663 else => {},
5664 }
5665
5631 if (target.cpu.arch.isArm()) {5666 if (target.cpu.arch.isArm()) {
5632 try argv.append(if (target.cpu.arch.isThumb()) "-mthumb" else "-mno-thumb");5667 try argv.append(if (target.cpu.arch.isThumb()) "-mthumb" else "-mno-thumb");
5633 }5668 }
...@@ -5748,6 +5783,19 @@ pub fn addCCArgs(...@@ -5748,6 +5783,19 @@ pub fn addCCArgs(
5748 try argv.append("-D_SOFT_DOUBLE");5783 try argv.append("-D_SOFT_DOUBLE");
5749 }5784 }
57505785
5786 switch (mod.optimize_mode) {
5787 .Debug => {
5788 // windows c runtime requires -D_DEBUG if using debug libraries
5789 try argv.append("-D_DEBUG");
5790 },
5791 .ReleaseSafe => {
5792 try argv.append("-D_FORTIFY_SOURCE=2");
5793 },
5794 .ReleaseFast, .ReleaseSmall => {
5795 try argv.append("-DNDEBUG");
5796 },
5797 }
5798
5751 if (comp.config.link_libc) {5799 if (comp.config.link_libc) {
5752 if (target.isGnuLibC()) {5800 if (target.isGnuLibC()) {
5753 const target_version = target.os.versionRange().gnuLibCVersion().?;5801 const target_version = target.os.versionRange().gnuLibCVersion().?;
...@@ -5840,6 +5888,32 @@ pub fn addCCArgs(...@@ -5840,6 +5888,32 @@ pub fn addCCArgs(
5840 }5888 }
5841 }5889 }
58425890
5891 // Only C-family files support these flags.
5892 switch (ext) {
5893 .c,
5894 .h,
5895 .cpp,
5896 .hpp,
5897 .m,
5898 .hm,
5899 .mm,
5900 .hmm,
5901 => {
5902 try argv.append("-fno-spell-checking");
5903
5904 if (target.os.tag == .windows and target.abi.isGnu()) {
5905 // windows.h has files such as pshpack1.h which do #pragma packing,
5906 // triggering a clang warning. So for this target, we disable this warning.
5907 try argv.append("-Wno-pragma-pack");
5908 }
5909
5910 if (mod.optimize_mode != .Debug) {
5911 try argv.append("-Werror=date-time");
5912 }
5913 },
5914 else => {},
5915 }
5916
5843 // Only assembly files support these flags.5917 // Only assembly files support these flags.
5844 switch (ext) {5918 switch (ext) {
5845 .assembly,5919 .assembly,
...@@ -5914,7 +5988,7 @@ pub fn addCCArgs(...@@ -5914,7 +5988,7 @@ pub fn addCCArgs(
5914 else => {},5988 else => {},
5915 }5989 }
59165990
5917 // Only C-family files support these flags.5991 // Only compiled files support these flags.
5918 switch (ext) {5992 switch (ext) {
5919 .c,5993 .c,
5920 .h,5994 .h,
...@@ -5924,9 +5998,9 @@ pub fn addCCArgs(...@@ -5924,9 +5998,9 @@ pub fn addCCArgs(
5924 .hm,5998 .hm,
5925 .mm,5999 .mm,
5926 .hmm,6000 .hmm,
6001 .ll,
6002 .bc,
5927 => {6003 => {
5928 try argv.append("-fno-spell-checking");
5929
5930 if (target_util.clangSupportsTargetCpuArg(target)) {6004 if (target_util.clangSupportsTargetCpuArg(target)) {
5931 if (target.cpu.model.llvm_name) |llvm_name| {6005 if (target.cpu.model.llvm_name) |llvm_name| {
5932 try argv.appendSlice(&[_][]const u8{6006 try argv.appendSlice(&[_][]const u8{
...@@ -5953,48 +6027,6 @@ pub fn addCCArgs(...@@ -5953,48 +6027,6 @@ pub fn addCCArgs(
5953 }6027 }
5954 }6028 }
59556029
5956 switch (target.os.tag) {
5957 .windows => {
5958 // windows.h has files such as pshpack1.h which do #pragma packing,
5959 // triggering a clang warning. So for this target, we disable this warning.
5960 if (target.abi.isGnu()) {
5961 try argv.append("-Wno-pragma-pack");
5962 }
5963 },
5964 .macos => {
5965 try argv.ensureUnusedCapacity(2);
5966 // Pass the proper -m<os>-version-min argument for darwin.
5967 const ver = target.os.version_range.semver.min;
5968 argv.appendAssumeCapacity(try std.fmt.allocPrint(arena, "-mmacos-version-min={d}.{d}.{d}", .{
5969 ver.major, ver.minor, ver.patch,
5970 }));
5971 // This avoids a warning that sometimes occurs when
5972 // providing both a -target argument that contains a
5973 // version as well as the -mmacosx-version-min argument.
5974 // Zig provides the correct value in both places, so it
5975 // doesn't matter which one gets overridden.
5976 argv.appendAssumeCapacity("-Wno-overriding-option");
5977 },
5978 .ios => switch (target.cpu.arch) {
5979 // Pass the proper -m<os>-version-min argument for darwin.
5980 .x86, .x86_64 => {
5981 const ver = target.os.version_range.semver.min;
5982 try argv.append(try std.fmt.allocPrint(
5983 arena,
5984 "-m{s}-simulator-version-min={d}.{d}.{d}",
5985 .{ @tagName(target.os.tag), ver.major, ver.minor, ver.patch },
5986 ));
5987 },
5988 else => {
5989 const ver = target.os.version_range.semver.min;
5990 try argv.append(try std.fmt.allocPrint(arena, "-m{s}-version-min={d}.{d}.{d}", .{
5991 @tagName(target.os.tag), ver.major, ver.minor, ver.patch,
5992 }));
5993 },
5994 },
5995 else => {},
5996 }
5997
5998 {6030 {
5999 var san_arg: std.ArrayListUnmanaged(u8) = .empty;6031 var san_arg: std.ArrayListUnmanaged(u8) = .empty;
6000 const prefix = "-fsanitize=";6032 const prefix = "-fsanitize=";
...@@ -6055,8 +6087,6 @@ pub fn addCCArgs(...@@ -6055,8 +6087,6 @@ pub fn addCCArgs(
60556087
6056 switch (mod.optimize_mode) {6088 switch (mod.optimize_mode) {
6057 .Debug => {6089 .Debug => {
6058 // windows c runtime requires -D_DEBUG if using debug libraries
6059 try argv.append("-D_DEBUG");
6060 // Clang has -Og for compatibility with GCC, but currently it is just equivalent6090 // Clang has -Og for compatibility with GCC, but currently it is just equivalent
6061 // to -O1. Besides potentially impairing debugging, -O1/-Og significantly6091 // to -O1. Besides potentially impairing debugging, -O1/-Og significantly
6062 // increases compile times.6092 // increases compile times.
...@@ -6066,10 +6096,8 @@ pub fn addCCArgs(...@@ -6066,10 +6096,8 @@ pub fn addCCArgs(
6066 // See the comment in the BuildModeFastRelease case for why we pass -O2 rather6096 // See the comment in the BuildModeFastRelease case for why we pass -O2 rather
6067 // than -O3 here.6097 // than -O3 here.
6068 try argv.append("-O2");6098 try argv.append("-O2");
6069 try argv.append("-D_FORTIFY_SOURCE=2");
6070 },6099 },
6071 .ReleaseFast => {6100 .ReleaseFast => {
6072 try argv.append("-DNDEBUG");
6073 // Here we pass -O2 rather than -O3 because, although we do the equivalent of6101 // Here we pass -O2 rather than -O3 because, although we do the equivalent of
6074 // -O3 in Zig code, the justification for the difference here is that Zig6102 // -O3 in Zig code, the justification for the difference here is that Zig
6075 // has better detection and prevention of undefined behavior, so -O3 is safer for6103 // has better detection and prevention of undefined behavior, so -O3 is safer for
...@@ -6078,14 +6106,9 @@ pub fn addCCArgs(...@@ -6078,14 +6106,9 @@ pub fn addCCArgs(
6078 try argv.append("-O2");6106 try argv.append("-O2");
6079 },6107 },
6080 .ReleaseSmall => {6108 .ReleaseSmall => {
6081 try argv.append("-DNDEBUG");
6082 try argv.append("-Os");6109 try argv.append("-Os");
6083 },6110 },
6084 }6111 }
6085
6086 if (mod.optimize_mode != .Debug) {
6087 try argv.append("-Werror=date-time");
6088 }
6089 },6112 },
6090 else => {},6113 else => {},
6091 }6114 }