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 10:12:42+02:00
log3830fc041b2ccfc989546ed1d689cde6a38dc647
tree79c2d640cc9e63e4691d225d3887cf26523bd232
parent1bfc71d4dab0a7347f76dc6f0afc1346bd3dc3bb

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
...@@ -5627,6 +5627,41 @@ pub fn addCCArgs(...@@ -5627,6 +5627,41 @@ pub fn addCCArgs(
5627 const llvm_triple = try @import("codegen/llvm.zig").targetTriple(arena, target);5627 const llvm_triple = try @import("codegen/llvm.zig").targetTriple(arena, target);
5628 try argv.appendSlice(&[_][]const u8{ "-target", llvm_triple });5628 try argv.appendSlice(&[_][]const u8{ "-target", llvm_triple });
56295629
5630 switch (target.os.tag) {
5631 .macos => {
5632 try argv.ensureUnusedCapacity(2);
5633 // Pass the proper -m<os>-version-min argument for darwin.
5634 const ver = target.os.version_range.semver.min;
5635 argv.appendAssumeCapacity(try std.fmt.allocPrint(arena, "-mmacos-version-min={d}.{d}.{d}", .{
5636 ver.major, ver.minor, ver.patch,
5637 }));
5638 // This avoids a warning that sometimes occurs when
5639 // providing both a -target argument that contains a
5640 // version as well as the -mmacosx-version-min argument.
5641 // Zig provides the correct value in both places, so it
5642 // doesn't matter which one gets overridden.
5643 argv.appendAssumeCapacity("-Wno-overriding-option");
5644 },
5645 .ios => switch (target.cpu.arch) {
5646 // Pass the proper -m<os>-version-min argument for darwin.
5647 .x86, .x86_64 => {
5648 const ver = target.os.version_range.semver.min;
5649 try argv.append(try std.fmt.allocPrint(
5650 arena,
5651 "-m{s}-simulator-version-min={d}.{d}.{d}",
5652 .{ @tagName(target.os.tag), ver.major, ver.minor, ver.patch },
5653 ));
5654 },
5655 else => {
5656 const ver = target.os.version_range.semver.min;
5657 try argv.append(try std.fmt.allocPrint(arena, "-m{s}-version-min={d}.{d}.{d}", .{
5658 @tagName(target.os.tag), ver.major, ver.minor, ver.patch,
5659 }));
5660 },
5661 },
5662 else => {},
5663 }
5664
5630 if (target.cpu.arch.isArm()) {5665 if (target.cpu.arch.isArm()) {
5631 try argv.append(if (target.cpu.arch.isThumb()) "-mthumb" else "-mno-thumb");5666 try argv.append(if (target.cpu.arch.isThumb()) "-mthumb" else "-mno-thumb");
5632 }5667 }
...@@ -5740,6 +5775,19 @@ pub fn addCCArgs(...@@ -5740,6 +5775,19 @@ pub fn addCCArgs(
57405775
5741 // LLVM IR files don't support these flags.5776 // LLVM IR files don't support these flags.
5742 if (ext != .ll and ext != .bc) {5777 if (ext != .ll and ext != .bc) {
5778 switch (mod.optimize_mode) {
5779 .Debug => {
5780 // windows c runtime requires -D_DEBUG if using debug libraries
5781 try argv.append("-D_DEBUG");
5782 },
5783 .ReleaseSafe => {
5784 try argv.append("-D_FORTIFY_SOURCE=2");
5785 },
5786 .ReleaseFast, .ReleaseSmall => {
5787 try argv.append("-DNDEBUG");
5788 },
5789 }
5790
5743 if (comp.config.link_libc) {5791 if (comp.config.link_libc) {
5744 if (target.isGnuLibC()) {5792 if (target.isGnuLibC()) {
5745 const target_version = target.os.versionRange().gnuLibCVersion().?;5793 const target_version = target.os.versionRange().gnuLibCVersion().?;
...@@ -5810,6 +5858,32 @@ pub fn addCCArgs(...@@ -5810,6 +5858,32 @@ pub fn addCCArgs(
5810 }5858 }
5811 }5859 }
58125860
5861 // Only C-family files support these flags.
5862 switch (ext) {
5863 .c,
5864 .h,
5865 .cpp,
5866 .hpp,
5867 .m,
5868 .hm,
5869 .mm,
5870 .hmm,
5871 => {
5872 try argv.append("-fno-spell-checking");
5873
5874 if (target.os.tag == .windows and target.abi.isGnu()) {
5875 // windows.h has files such as pshpack1.h which do #pragma packing,
5876 // triggering a clang warning. So for this target, we disable this warning.
5877 try argv.append("-Wno-pragma-pack");
5878 }
5879
5880 if (mod.optimize_mode != .Debug) {
5881 try argv.append("-Werror=date-time");
5882 }
5883 },
5884 else => {},
5885 }
5886
5813 // Only assembly files support these flags.5887 // Only assembly files support these flags.
5814 switch (ext) {5888 switch (ext) {
5815 .assembly,5889 .assembly,
...@@ -5884,7 +5958,7 @@ pub fn addCCArgs(...@@ -5884,7 +5958,7 @@ pub fn addCCArgs(
5884 else => {},5958 else => {},
5885 }5959 }
58865960
5887 // Only C-family files support these flags.5961 // Only compiled files support these flags.
5888 switch (ext) {5962 switch (ext) {
5889 .c,5963 .c,
5890 .h,5964 .h,
...@@ -5894,9 +5968,9 @@ pub fn addCCArgs(...@@ -5894,9 +5968,9 @@ pub fn addCCArgs(
5894 .hm,5968 .hm,
5895 .mm,5969 .mm,
5896 .hmm,5970 .hmm,
5971 .ll,
5972 .bc,
5897 => {5973 => {
5898 try argv.append("-fno-spell-checking");
5899
5900 if (target_util.clangSupportsTargetCpuArg(target)) {5974 if (target_util.clangSupportsTargetCpuArg(target)) {
5901 if (target.cpu.model.llvm_name) |llvm_name| {5975 if (target.cpu.model.llvm_name) |llvm_name| {
5902 try argv.appendSlice(&[_][]const u8{5976 try argv.appendSlice(&[_][]const u8{
...@@ -5925,48 +5999,6 @@ pub fn addCCArgs(...@@ -5925,48 +5999,6 @@ pub fn addCCArgs(
5925 }5999 }
5926 }6000 }
59276001
5928 switch (target.os.tag) {
5929 .windows => {
5930 // windows.h has files such as pshpack1.h which do #pragma packing,
5931 // triggering a clang warning. So for this target, we disable this warning.
5932 if (target.abi.isGnu()) {
5933 try argv.append("-Wno-pragma-pack");
5934 }
5935 },
5936 .macos => {
5937 try argv.ensureUnusedCapacity(2);
5938 // Pass the proper -m<os>-version-min argument for darwin.
5939 const ver = target.os.version_range.semver.min;
5940 argv.appendAssumeCapacity(try std.fmt.allocPrint(arena, "-mmacos-version-min={d}.{d}.{d}", .{
5941 ver.major, ver.minor, ver.patch,
5942 }));
5943 // This avoids a warning that sometimes occurs when
5944 // providing both a -target argument that contains a
5945 // version as well as the -mmacosx-version-min argument.
5946 // Zig provides the correct value in both places, so it
5947 // doesn't matter which one gets overridden.
5948 argv.appendAssumeCapacity("-Wno-overriding-option");
5949 },
5950 .ios => switch (target.cpu.arch) {
5951 // Pass the proper -m<os>-version-min argument for darwin.
5952 .x86, .x86_64 => {
5953 const ver = target.os.version_range.semver.min;
5954 try argv.append(try std.fmt.allocPrint(
5955 arena,
5956 "-m{s}-simulator-version-min={d}.{d}.{d}",
5957 .{ @tagName(target.os.tag), ver.major, ver.minor, ver.patch },
5958 ));
5959 },
5960 else => {
5961 const ver = target.os.version_range.semver.min;
5962 try argv.append(try std.fmt.allocPrint(arena, "-m{s}-version-min={d}.{d}.{d}", .{
5963 @tagName(target.os.tag), ver.major, ver.minor, ver.patch,
5964 }));
5965 },
5966 },
5967 else => {},
5968 }
5969
5970 {6002 {
5971 var san_arg: std.ArrayListUnmanaged(u8) = .empty;6003 var san_arg: std.ArrayListUnmanaged(u8) = .empty;
5972 const prefix = "-fsanitize=";6004 const prefix = "-fsanitize=";
...@@ -6027,8 +6059,6 @@ pub fn addCCArgs(...@@ -6027,8 +6059,6 @@ pub fn addCCArgs(
60276059
6028 switch (mod.optimize_mode) {6060 switch (mod.optimize_mode) {
6029 .Debug => {6061 .Debug => {
6030 // windows c runtime requires -D_DEBUG if using debug libraries
6031 try argv.append("-D_DEBUG");
6032 // Clang has -Og for compatibility with GCC, but currently it is just equivalent6062 // Clang has -Og for compatibility with GCC, but currently it is just equivalent
6033 // to -O1. Besides potentially impairing debugging, -O1/-Og significantly6063 // to -O1. Besides potentially impairing debugging, -O1/-Og significantly
6034 // increases compile times.6064 // increases compile times.
...@@ -6038,10 +6068,8 @@ pub fn addCCArgs(...@@ -6038,10 +6068,8 @@ pub fn addCCArgs(
6038 // See the comment in the BuildModeFastRelease case for why we pass -O2 rather6068 // See the comment in the BuildModeFastRelease case for why we pass -O2 rather
6039 // than -O3 here.6069 // than -O3 here.
6040 try argv.append("-O2");6070 try argv.append("-O2");
6041 try argv.append("-D_FORTIFY_SOURCE=2");
6042 },6071 },
6043 .ReleaseFast => {6072 .ReleaseFast => {
6044 try argv.append("-DNDEBUG");
6045 // Here we pass -O2 rather than -O3 because, although we do the equivalent of6073 // Here we pass -O2 rather than -O3 because, although we do the equivalent of
6046 // -O3 in Zig code, the justification for the difference here is that Zig6074 // -O3 in Zig code, the justification for the difference here is that Zig
6047 // has better detection and prevention of undefined behavior, so -O3 is safer for6075 // has better detection and prevention of undefined behavior, so -O3 is safer for
...@@ -6050,14 +6078,9 @@ pub fn addCCArgs(...@@ -6050,14 +6078,9 @@ pub fn addCCArgs(
6050 try argv.append("-O2");6078 try argv.append("-O2");
6051 },6079 },
6052 .ReleaseSmall => {6080 .ReleaseSmall => {
6053 try argv.append("-DNDEBUG");
6054 try argv.append("-Os");6081 try argv.append("-Os");
6055 },6082 },
6056 }6083 }
6057
6058 if (mod.optimize_mode != .Debug) {
6059 try argv.append("-Werror=date-time");
6060 }
6061 },6084 },
6062 else => {},6085 else => {},
6063 }6086 }