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

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>
This commit is contained in:
Techatrix 2025-04-08 13:04:02 +02:00 committed by Alex Rønne Petersen
parent 9397dc5af6
commit 83e1ce1e00
No known key found for this signature in database

View file

@ -5628,6 +5628,41 @@ pub fn addCCArgs(
const llvm_triple = try @import("codegen/llvm.zig").targetTriple(arena, target);
try argv.appendSlice(&[_][]const u8{ "-target", llvm_triple });
switch (target.os.tag) {
.macos => {
try argv.ensureUnusedCapacity(2);
// Pass the proper -m<os>-version-min argument for darwin.
const ver = target.os.version_range.semver.min;
argv.appendAssumeCapacity(try std.fmt.allocPrint(arena, "-mmacos-version-min={d}.{d}.{d}", .{
ver.major, ver.minor, ver.patch,
}));
// This avoids a warning that sometimes occurs when
// providing both a -target argument that contains a
// version as well as the -mmacosx-version-min argument.
// Zig provides the correct value in both places, so it
// doesn't matter which one gets overridden.
argv.appendAssumeCapacity("-Wno-overriding-option");
},
.ios => switch (target.cpu.arch) {
// Pass the proper -m<os>-version-min argument for darwin.
.x86, .x86_64 => {
const ver = target.os.version_range.semver.min;
try argv.append(try std.fmt.allocPrint(
arena,
"-m{s}-simulator-version-min={d}.{d}.{d}",
.{ @tagName(target.os.tag), ver.major, ver.minor, ver.patch },
));
},
else => {
const ver = target.os.version_range.semver.min;
try argv.append(try std.fmt.allocPrint(arena, "-m{s}-version-min={d}.{d}.{d}", .{
@tagName(target.os.tag), ver.major, ver.minor, ver.patch,
}));
},
},
else => {},
}
if (target.cpu.arch.isArm()) {
try argv.append(if (target.cpu.arch.isThumb()) "-mthumb" else "-mno-thumb");
}
@ -5748,6 +5783,19 @@ pub fn addCCArgs(
try argv.append("-D_SOFT_DOUBLE");
}
switch (mod.optimize_mode) {
.Debug => {
// windows c runtime requires -D_DEBUG if using debug libraries
try argv.append("-D_DEBUG");
},
.ReleaseSafe => {
try argv.append("-D_FORTIFY_SOURCE=2");
},
.ReleaseFast, .ReleaseSmall => {
try argv.append("-DNDEBUG");
},
}
if (comp.config.link_libc) {
if (target.isGnuLibC()) {
const target_version = target.os.versionRange().gnuLibCVersion().?;
@ -5840,6 +5888,32 @@ pub fn addCCArgs(
}
}
// Only C-family files support these flags.
switch (ext) {
.c,
.h,
.cpp,
.hpp,
.m,
.hm,
.mm,
.hmm,
=> {
try argv.append("-fno-spell-checking");
if (target.os.tag == .windows and target.abi.isGnu()) {
// windows.h has files such as pshpack1.h which do #pragma packing,
// triggering a clang warning. So for this target, we disable this warning.
try argv.append("-Wno-pragma-pack");
}
if (mod.optimize_mode != .Debug) {
try argv.append("-Werror=date-time");
}
},
else => {},
}
// Only assembly files support these flags.
switch (ext) {
.assembly,
@ -5914,7 +5988,7 @@ pub fn addCCArgs(
else => {},
}
// Only C-family files support these flags.
// Only compiled files support these flags.
switch (ext) {
.c,
.h,
@ -5924,9 +5998,9 @@ pub fn addCCArgs(
.hm,
.mm,
.hmm,
.ll,
.bc,
=> {
try argv.append("-fno-spell-checking");
if (target_util.clangSupportsTargetCpuArg(target)) {
if (target.cpu.model.llvm_name) |llvm_name| {
try argv.appendSlice(&[_][]const u8{
@ -5953,48 +6027,6 @@ pub fn addCCArgs(
}
}
switch (target.os.tag) {
.windows => {
// windows.h has files such as pshpack1.h which do #pragma packing,
// triggering a clang warning. So for this target, we disable this warning.
if (target.abi.isGnu()) {
try argv.append("-Wno-pragma-pack");
}
},
.macos => {
try argv.ensureUnusedCapacity(2);
// Pass the proper -m<os>-version-min argument for darwin.
const ver = target.os.version_range.semver.min;
argv.appendAssumeCapacity(try std.fmt.allocPrint(arena, "-mmacos-version-min={d}.{d}.{d}", .{
ver.major, ver.minor, ver.patch,
}));
// This avoids a warning that sometimes occurs when
// providing both a -target argument that contains a
// version as well as the -mmacosx-version-min argument.
// Zig provides the correct value in both places, so it
// doesn't matter which one gets overridden.
argv.appendAssumeCapacity("-Wno-overriding-option");
},
.ios => switch (target.cpu.arch) {
// Pass the proper -m<os>-version-min argument for darwin.
.x86, .x86_64 => {
const ver = target.os.version_range.semver.min;
try argv.append(try std.fmt.allocPrint(
arena,
"-m{s}-simulator-version-min={d}.{d}.{d}",
.{ @tagName(target.os.tag), ver.major, ver.minor, ver.patch },
));
},
else => {
const ver = target.os.version_range.semver.min;
try argv.append(try std.fmt.allocPrint(arena, "-m{s}-version-min={d}.{d}.{d}", .{
@tagName(target.os.tag), ver.major, ver.minor, ver.patch,
}));
},
},
else => {},
}
{
var san_arg: std.ArrayListUnmanaged(u8) = .empty;
const prefix = "-fsanitize=";
@ -6055,8 +6087,6 @@ pub fn addCCArgs(
switch (mod.optimize_mode) {
.Debug => {
// windows c runtime requires -D_DEBUG if using debug libraries
try argv.append("-D_DEBUG");
// Clang has -Og for compatibility with GCC, but currently it is just equivalent
// to -O1. Besides potentially impairing debugging, -O1/-Og significantly
// increases compile times.
@ -6066,10 +6096,8 @@ pub fn addCCArgs(
// See the comment in the BuildModeFastRelease case for why we pass -O2 rather
// than -O3 here.
try argv.append("-O2");
try argv.append("-D_FORTIFY_SOURCE=2");
},
.ReleaseFast => {
try argv.append("-DNDEBUG");
// Here we pass -O2 rather than -O3 because, although we do the equivalent of
// -O3 in Zig code, the justification for the difference here is that Zig
// has better detection and prevention of undefined behavior, so -O3 is safer for
@ -6078,14 +6106,9 @@ pub fn addCCArgs(
try argv.append("-O2");
},
.ReleaseSmall => {
try argv.append("-DNDEBUG");
try argv.append("-Os");
},
}
if (mod.optimize_mode != .Debug) {
try argv.append("-Werror=date-time");
}
},
else => {},
}