From 6904cd828edb34217cd4d54e8f6fe5bb91cb4858 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 10 Nov 2020 15:23:18 -0700 Subject: [PATCH] add missing -m-version-min CLI args to clang This fixes some code generation issues when targeting macOS and compiling C/C++ code. --- src/Compilation.zig | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 65fa296ca4..b3cb9e31f0 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -613,6 +613,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { cache.hash.addBytes(options.target.cpu.model.name); cache.hash.add(options.target.cpu.features.ints); cache.hash.add(options.target.os.tag); + cache.hash.add(options.target.os.getVersionRange()); cache.hash.add(options.is_native_os); cache.hash.add(options.target.abi); cache.hash.add(ofmt); @@ -642,7 +643,6 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { hash.addOptionalBytes(root_pkg.root_src_directory.path); hash.add(valgrind); hash.add(single_threaded); - hash.add(options.target.os.getVersionRange()); hash.add(dll_export_fns); hash.add(options.is_test); hash.add(options.is_compiler_rt_or_libc); @@ -1619,7 +1619,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_comp_progress_node: * const out_dep_path: ?[]const u8 = if (comp.disable_c_depfile or !ext.clangSupportsDepFile()) null else - try std.fmt.allocPrint(arena, "{}.d", .{out_obj_path}); + try std.fmt.allocPrint(arena, "{s}.d", .{out_obj_path}); try comp.addCCArgs(arena, &argv, ext, out_dep_path); try argv.ensureCapacity(argv.items.len + 3); @@ -1849,10 +1849,39 @@ pub fn addCCArgs( try argv.append(try std.fmt.allocPrint(arena, "-mcmodel={}", .{@tagName(mcmodel)})); } - // 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.os.tag == .windows and target.abi.isGnu()) { - try argv.append("-Wno-pragma-pack"); + 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 => { + // Pass the proper -m-version-min argument for darwin. + const ver = target.os.version_range.semver.min; + try argv.append(try std.fmt.allocPrint(arena, "-mmacos-version-min={d}.{d}.{d}", .{ + ver.major, ver.minor, ver.patch, + })); + }, + .ios, .tvos, .watchos => switch (target.cpu.arch) { + // Pass the proper -m-version-min argument for darwin. + .i386, .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 (!comp.bin_file.options.strip) {