aboutsummaryrefslogtreecommitdiff
path: root/src/linker.cpp
diff options
context:
space:
mode:
authorColin Davidson <colrdavidson@gmail.com>2025-07-28 14:24:46 -0700
committerColin Davidson <colrdavidson@gmail.com>2025-07-28 14:24:46 -0700
commitb88f9194d0d25bd5121f45eb3696b0e1725dfd41 (patch)
tree8f15e33fa41dd191f786b4ad414ab96062d96cbf /src/linker.cpp
parent2dae1d8a4134226887a6e6a0aad0318e46e40cde (diff)
parentbe3006dbf26fbe6b51bb489f346793823968aedf (diff)
Merge remote-tracking branch 'live/master' into macharena
Diffstat (limited to 'src/linker.cpp')
-rw-r--r--src/linker.cpp64
1 files changed, 58 insertions, 6 deletions
diff --git a/src/linker.cpp b/src/linker.cpp
index bf2ba6fe0..41333a3c9 100644
--- a/src/linker.cpp
+++ b/src/linker.cpp
@@ -431,8 +431,10 @@ try_cross_linking:;
// Link using `clang`, unless overridden by `ODIN_CLANG_PATH` environment variable.
const char* clang_path = gb_get_env("ODIN_CLANG_PATH", permanent_allocator());
+ bool has_odin_clang_path_env = true;
if (clang_path == NULL) {
clang_path = "clang";
+ has_odin_clang_path_env = false;
}
// NOTE(vassvik): needs to add the root to the library search paths, so that the full filenames of the library
@@ -591,7 +593,7 @@ try_cross_linking:;
// Do not add libc again, this is added later already, and omitted with
// the `-no-crt` flag, not skipping here would cause duplicate library
// warnings when linking on darwin and might link libc silently even with `-no-crt`.
- if (lib == str_lit("System.framework") || lib == str_lit("c")) {
+ if (lib == str_lit("System.framework") || lib == str_lit("System") || lib == str_lit("c")) {
continue;
}
@@ -772,10 +774,58 @@ try_cross_linking:;
gbString platform_lib_str = gb_string_make(heap_allocator(), "");
defer (gb_string_free(platform_lib_str));
if (build_context.metrics.os == TargetOs_darwin) {
- // Get the MacOSX SDK path.
+ // Get the SDK path.
gbString darwin_sdk_path = gb_string_make(temporary_allocator(), "");
- if (!system_exec_command_line_app_output("xcrun --sdk macosx --show-sdk-path", &darwin_sdk_path)) {
- darwin_sdk_path = gb_string_set(darwin_sdk_path, "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk");
+
+ char const* darwin_platform_name = "MacOSX";
+ char const* darwin_xcrun_sdk_name = "macosx";
+ char const* darwin_min_version_id = "macosx";
+
+ const char* original_clang_path = clang_path;
+
+ // NOTE(harold): We set the clang_path to run through xcrun because otherwise it complaints about the the sysroot
+ // being set to 'MacOSX' even though we've set the sysroot to the correct SDK (-Wincompatible-sysroot).
+ // This is because it is likely not using the SDK's toolchain Apple Clang but another one installed in the system.
+ switch (selected_subtarget) {
+ case Subtarget_iPhone:
+ darwin_platform_name = "iPhoneOS";
+ darwin_xcrun_sdk_name = "iphoneos";
+ darwin_min_version_id = "ios";
+ if (!has_odin_clang_path_env) {
+ clang_path = "xcrun --sdk iphoneos clang";
+ }
+ break;
+ case Subtarget_iPhoneSimulator:
+ darwin_platform_name = "iPhoneSimulator";
+ darwin_xcrun_sdk_name = "iphonesimulator";
+ darwin_min_version_id = "ios-simulator";
+ if (!has_odin_clang_path_env) {
+ clang_path = "xcrun --sdk iphonesimulator clang";
+ }
+ break;
+ }
+
+ gbString darwin_find_sdk_cmd = gb_string_make(temporary_allocator(), "");
+ darwin_find_sdk_cmd = gb_string_append_fmt(darwin_find_sdk_cmd, "xcrun --sdk %s --show-sdk-path", darwin_xcrun_sdk_name);
+
+ if (!system_exec_command_line_app_output(darwin_find_sdk_cmd, &darwin_sdk_path)) {
+
+ // Fallback to default clang, since `xcrun --sdk` did not work.
+ clang_path = original_clang_path;
+
+ // Best-effort fallback to known locations
+ gbString darwin_sdk_path = gb_string_make(temporary_allocator(), "");
+ darwin_sdk_path = gb_string_append_fmt(darwin_sdk_path, "/Library/Developer/CommandLineTools/SDKs/%s.sdk", darwin_platform_name);
+
+ if (!path_is_directory(make_string_c(darwin_sdk_path))) {
+ gb_string_clear(darwin_sdk_path);
+ darwin_sdk_path = gb_string_append_fmt(darwin_sdk_path, "/Applications/Xcode.app/Contents/Developer/Platforms/%s.platform/Developer/SDKs/%s.sdk", darwin_platform_name);
+
+ if (!path_is_directory(make_string_c(darwin_sdk_path))) {
+ gb_printf_err("Failed to find %s SDK\n", darwin_platform_name);
+ return -1;
+ }
+ }
} else {
// Trim the trailing newline.
darwin_sdk_path = gb_string_trim_space(darwin_sdk_path);
@@ -796,8 +846,10 @@ try_cross_linking:;
// Only specify this flag if the user has given a minimum version to target.
// This will cause warnings to show up for mismatched libraries.
- if (build_context.minimum_os_version_string_given) {
- link_settings = gb_string_append_fmt(link_settings, "-mmacosx-version-min=%.*s ", LIT(build_context.minimum_os_version_string));
+ // NOTE(harold): For device subtargets we have to explicitly set the default version to
+ // avoid the same warning since we configure our own minimum version when compiling for devices.
+ if (build_context.minimum_os_version_string_given || selected_subtarget != Subtarget_Default) {
+ link_settings = gb_string_append_fmt(link_settings, "-m%s-version-min=%.*s ", darwin_min_version_id, LIT(build_context.minimum_os_version_string));
}
if (build_context.build_mode != BuildMode_DynamicLibrary) {