Skip to content

Commit 3830fc0

Browse files
Techatrixalexrp
andcommitted
Compilation: Fix logic in addCCArgs() for various file types and flags.
Co-authored-by: Alex Rønne Petersen <[email protected]>
1 parent 1bfc71d commit 3830fc0

File tree

1 file changed

+77
-54
lines changed

1 file changed

+77
-54
lines changed

src/Compilation.zig

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5627,6 +5627,41 @@ pub fn addCCArgs(
56275627
const llvm_triple = try @import("codegen/llvm.zig").targetTriple(arena, target);
56285628
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+
56305665
if (target.cpu.arch.isArm()) {
56315666
try argv.append(if (target.cpu.arch.isThumb()) "-mthumb" else "-mno-thumb");
56325667
}
@@ -5740,6 +5775,19 @@ pub fn addCCArgs(
57405775

57415776
// LLVM IR files don't support these flags.
57425777
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+
57435791
if (comp.config.link_libc) {
57445792
if (target.isGnuLibC()) {
57455793
const target_version = target.os.versionRange().gnuLibCVersion().?;
@@ -5810,6 +5858,32 @@ pub fn addCCArgs(
58105858
}
58115859
}
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+
58135887
// Only assembly files support these flags.
58145888
switch (ext) {
58155889
.assembly,
@@ -5884,7 +5958,7 @@ pub fn addCCArgs(
58845958
else => {},
58855959
}
58865960

5887-
// Only C-family files support these flags.
5961+
// Only compiled files support these flags.
58885962
switch (ext) {
58895963
.c,
58905964
.h,
@@ -5894,9 +5968,9 @@ pub fn addCCArgs(
58945968
.hm,
58955969
.mm,
58965970
.hmm,
5971+
.ll,
5972+
.bc,
58975973
=> {
5898-
try argv.append("-fno-spell-checking");
5899-
59005974
if (target_util.clangSupportsTargetCpuArg(target)) {
59015975
if (target.cpu.model.llvm_name) |llvm_name| {
59025976
try argv.appendSlice(&[_][]const u8{
@@ -5925,48 +5999,6 @@ pub fn addCCArgs(
59255999
}
59266000
}
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-
59706002
{
59716003
var san_arg: std.ArrayListUnmanaged(u8) = .empty;
59726004
const prefix = "-fsanitize=";
@@ -6027,8 +6059,6 @@ pub fn addCCArgs(
60276059

60286060
switch (mod.optimize_mode) {
60296061
.Debug => {
6030-
// windows c runtime requires -D_DEBUG if using debug libraries
6031-
try argv.append("-D_DEBUG");
60326062
// Clang has -Og for compatibility with GCC, but currently it is just equivalent
60336063
// to -O1. Besides potentially impairing debugging, -O1/-Og significantly
60346064
// increases compile times.
@@ -6038,10 +6068,8 @@ pub fn addCCArgs(
60386068
// See the comment in the BuildModeFastRelease case for why we pass -O2 rather
60396069
// than -O3 here.
60406070
try argv.append("-O2");
6041-
try argv.append("-D_FORTIFY_SOURCE=2");
60426071
},
60436072
.ReleaseFast => {
6044-
try argv.append("-DNDEBUG");
60456073
// Here we pass -O2 rather than -O3 because, although we do the equivalent of
60466074
// -O3 in Zig code, the justification for the difference here is that Zig
60476075
// has better detection and prevention of undefined behavior, so -O3 is safer for
@@ -6050,14 +6078,9 @@ pub fn addCCArgs(
60506078
try argv.append("-O2");
60516079
},
60526080
.ReleaseSmall => {
6053-
try argv.append("-DNDEBUG");
60546081
try argv.append("-Os");
60556082
},
60566083
}
6057-
6058-
if (mod.optimize_mode != .Debug) {
6059-
try argv.append("-Werror=date-time");
6060-
}
60616084
},
60626085
else => {},
60636086
}

0 commit comments

Comments
 (0)