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(
56285628 const llvm_triple = try @import("codegen/llvm.zig").targetTriple(arena, target);
56295629 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
56315666 if (target.cpu.arch.isArm()) {
56325667 try argv.append(if (target.cpu.arch.isThumb()) "-mthumb" else "-mno-thumb");
56335668 }
......@@ -5748,6 +5783,19 @@ pub fn addCCArgs(
57485783 try argv.append("-D_SOFT_DOUBLE");
57495784 }
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
57515799 if (comp.config.link_libc) {
57525800 if (target.isGnuLibC()) {
57535801 const target_version = target.os.versionRange().gnuLibCVersion().?;
......@@ -5840,6 +5888,32 @@ pub fn addCCArgs(
58405888 }
58415889 }
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
58435917 // Only assembly files support these flags.
58445918 switch (ext) {
58455919 .assembly,
......@@ -5914,7 +5988,7 @@ pub fn addCCArgs(
59145988 else => {},
59155989 }
59165990
5917 // Only C-family files support these flags.
5991 // Only compiled files support these flags.
59185992 switch (ext) {
59195993 .c,
59205994 .h,
......@@ -5924,9 +5998,9 @@ pub fn addCCArgs(
59245998 .hm,
59255999 .mm,
59266000 .hmm,
6001 .ll,
6002 .bc,
59276003 => {
5928 try argv.append("-fno-spell-checking");
5929
59306004 if (target_util.clangSupportsTargetCpuArg(target)) {
59316005 if (target.cpu.model.llvm_name) |llvm_name| {
59326006 try argv.appendSlice(&[_][]const u8{
......@@ -5953,48 +6027,6 @@ pub fn addCCArgs(
59536027 }
59546028 }
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
59986030 {
59996031 var san_arg: std.ArrayListUnmanaged(u8) = .empty;
60006032 const prefix = "-fsanitize=";
......@@ -6055,8 +6087,6 @@ pub fn addCCArgs(
60556087
60566088 switch (mod.optimize_mode) {
60576089 .Debug => {
6058 // windows c runtime requires -D_DEBUG if using debug libraries
6059 try argv.append("-D_DEBUG");
60606090 // Clang has -Og for compatibility with GCC, but currently it is just equivalent
60616091 // to -O1. Besides potentially impairing debugging, -O1/-Og significantly
60626092 // increases compile times.
......@@ -6066,10 +6096,8 @@ pub fn addCCArgs(
60666096 // See the comment in the BuildModeFastRelease case for why we pass -O2 rather
60676097 // than -O3 here.
60686098 try argv.append("-O2");
6069 try argv.append("-D_FORTIFY_SOURCE=2");
60706099 },
60716100 .ReleaseFast => {
6072 try argv.append("-DNDEBUG");
60736101 // Here we pass -O2 rather than -O3 because, although we do the equivalent of
60746102 // -O3 in Zig code, the justification for the difference here is that Zig
60756103 // has better detection and prevention of undefined behavior, so -O3 is safer for
......@@ -6078,14 +6106,9 @@ pub fn addCCArgs(
60786106 try argv.append("-O2");
60796107 },
60806108 .ReleaseSmall => {
6081 try argv.append("-DNDEBUG");
60826109 try argv.append("-Os");
60836110 },
60846111 }
6085
6086 if (mod.optimize_mode != .Debug) {
6087 try argv.append("-Werror=date-time");
6088 }
60896112 },
60906113 else => {},
60916114 }