From 0e245fb40f12f0dd4ac53cc9c535e354a0540ea6 Mon Sep 17 00:00:00 2001 From: Harold Brenes Date: Sun, 13 Jul 2025 20:17:30 -0400 Subject: Updated iOS/iPhoneSimulator build support --- src/linker.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) (limited to 'src/linker.cpp') diff --git a/src/linker.cpp b/src/linker.cpp index bf2ba6fe0..01fa7065a 100644 --- a/src/linker.cpp +++ b/src/linker.cpp @@ -591,7 +591,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 +772,54 @@ 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"; + + // 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_iOS: + darwin_platform_name = "iPhoneOS"; + darwin_xcrun_sdk_name = "iphoneos"; + darwin_min_version_id = "ios"; + 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"; + clang_path = "xcrun --sdk iphonesimulator clang"; + break; + } + + const char* original_clang_path = original_clang_path; + + 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); @@ -797,7 +841,7 @@ 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)); + 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) { -- cgit v1.2.3 From 4d2d890b4c87f3376626b88782e64f97e749eaa8 Mon Sep 17 00:00:00 2001 From: Harold Brenes Date: Sun, 13 Jul 2025 20:29:54 -0400 Subject: Fix accidental incorrect assignment --- src/linker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/linker.cpp') diff --git a/src/linker.cpp b/src/linker.cpp index 01fa7065a..bf7705282 100644 --- a/src/linker.cpp +++ b/src/linker.cpp @@ -797,7 +797,7 @@ try_cross_linking:; break; } - const char* original_clang_path = original_clang_path; + const char* original_clang_path = clang_path; 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); -- cgit v1.2.3 From 070943aa9865bda8c163dc76fde70a2602d447c4 Mon Sep 17 00:00:00 2001 From: Harold Brenes Date: Mon, 14 Jul 2025 12:53:01 -0400 Subject: Provide default minimum version for iOS and apply its target triplet. - Fix incorrect clang_path override for iOS during link stage. --- src/build_settings.cpp | 12 ++++++++---- src/linker.cpp | 14 ++++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) (limited to 'src/linker.cpp') diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 312512568..094cf07f0 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -1907,12 +1907,16 @@ gb_internal void init_build_context(TargetMetrics *cross_target, Subtarget subta // does not annoy the user with version warnings. if (metrics->os == TargetOs_darwin) { if (!bc->minimum_os_version_string_given) { - bc->minimum_os_version_string = str_lit("11.0.0"); + if (subtarget == Subtarget_Default) { + bc->minimum_os_version_string = str_lit("11.0.0"); + } else if (subtarget == Subtarget_iOS || subtarget == Subtarget_iPhoneSimulator) { + // NOTE(harold): We default to 17.4 on iOS because that's when os_sync_wait_on_address was added and + // we'd like to avoid any potential App Store issues by using the private ulock_* there. + bc->minimum_os_version_string = str_lit("17.4"); + } } - if (subtarget == Subtarget_Default) { - bc->metrics.target_triplet = concatenate_strings(permanent_allocator(), bc->metrics.target_triplet, bc->minimum_os_version_string); - } + bc->metrics.target_triplet = concatenate_strings(permanent_allocator(), bc->metrics.target_triplet, bc->minimum_os_version_string); } else if (selected_subtarget == Subtarget_Android) { init_android_values(bc->build_mode == BuildMode_Executable && (bc->command_kind & Command__does_build) != 0); } diff --git a/src/linker.cpp b/src/linker.cpp index bf7705282..413995e0d 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 @@ -779,6 +781,8 @@ try_cross_linking:; 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. @@ -787,18 +791,20 @@ try_cross_linking:; darwin_platform_name = "iPhoneOS"; darwin_xcrun_sdk_name = "iphoneos"; darwin_min_version_id = "ios"; - clang_path = "xcrun --sdk iphoneos clang"; + 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"; - clang_path = "xcrun --sdk iphonesimulator clang"; + if (!has_odin_clang_path_env) { + clang_path = "xcrun --sdk iphonesimulator clang"; + } break; } - const char* original_clang_path = clang_path; - 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); -- cgit v1.2.3 From 77e5c7141448219a6a0fc40f281951c4e8a5c0eb Mon Sep 17 00:00:00 2001 From: Harold Brenes Date: Mon, 14 Jul 2025 14:24:15 -0400 Subject: Fix correct versioned target triplet for iphonesimulator subtarget - Always set the `-m*-version-min` linker flag for non-macOS Darwin subtargets --- src/build_settings.cpp | 11 ++++++++++- src/linker.cpp | 4 +++- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'src/linker.cpp') diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 094cf07f0..d98340844 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -1916,7 +1916,16 @@ gb_internal void init_build_context(TargetMetrics *cross_target, Subtarget subta } } - bc->metrics.target_triplet = concatenate_strings(permanent_allocator(), bc->metrics.target_triplet, bc->minimum_os_version_string); + if (subtarget == Subtarget_iPhoneSimulator) { + // For the iOS simulator subtarget, the version must be between 'ios' and '-simulator'. + String suffix = str_lit("-simulator"); + GB_ASSERT(string_ends_with(bc->metrics.target_triplet, suffix)); + + String prefix = substring(bc->metrics.target_triplet, 0, bc->metrics.target_triplet.len - suffix.len); + bc->metrics.target_triplet = concatenate3_strings(permanent_allocator(), prefix, bc->minimum_os_version_string, suffix); + } else { + bc->metrics.target_triplet = concatenate_strings(permanent_allocator(), bc->metrics.target_triplet, bc->minimum_os_version_string); + } } else if (selected_subtarget == Subtarget_Android) { init_android_values(bc->build_mode == BuildMode_Executable && (bc->command_kind & Command__does_build) != 0); } diff --git a/src/linker.cpp b/src/linker.cpp index 413995e0d..7647ed872 100644 --- a/src/linker.cpp +++ b/src/linker.cpp @@ -846,7 +846,9 @@ 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) { + // 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)); } -- cgit v1.2.3 From bab4ce11fc1d52e2a63ed950fcf3cb0510cbe642 Mon Sep 17 00:00:00 2001 From: Harold Brenes Date: Mon, 14 Jul 2025 21:55:28 -0400 Subject: Rename `iOS` subtarget to `iPhone` for consistency. Add `ODIN_PLATFORM_SUBTARGET_IOS` builtin constant which evaluated to `true` when the platform is `Darwin` and the subtarget it either `iPhone` or `iPhoneSimulator` --- base/builtin/builtin.odin | 2 +- base/runtime/core.odin | 4 +++- core/sys/darwin/sync.odin | 20 +++++--------------- core/sys/info/platform_darwin.odin | 2 +- src/build_settings.cpp | 10 +++++----- src/checker.cpp | 2 +- src/linker.cpp | 2 +- 7 files changed, 17 insertions(+), 25 deletions(-) (limited to 'src/linker.cpp') diff --git a/base/builtin/builtin.odin b/base/builtin/builtin.odin index af102ee0b..2dd214321 100644 --- a/base/builtin/builtin.odin +++ b/base/builtin/builtin.odin @@ -145,7 +145,7 @@ ODIN_OS_STRING :: ODIN_OS_STRING /* An `enum` value indicating the platform subtarget, chosen using the `-subtarget` switch. - Possible values are: `.Default` `.iOS`, .iPhoneSimulator, and `.Android`. + Possible values are: `.Default` `.iPhone`, .iPhoneSimulator, and `.Android`. */ ODIN_PLATFORM_SUBTARGET :: ODIN_PLATFORM_SUBTARGET diff --git a/base/runtime/core.odin b/base/runtime/core.odin index 6c43e6c16..090d1a65b 100644 --- a/base/runtime/core.odin +++ b/base/runtime/core.odin @@ -557,7 +557,7 @@ ALL_ODIN_OS_TYPES :: Odin_OS_Types{ // Defined internally by the compiler Odin_Platform_Subtarget_Type :: enum int { Default, - iOS, + iPhone, iPhoneSimulator Android, } @@ -566,6 +566,8 @@ Odin_Platform_Subtarget_Type :: type_of(ODIN_PLATFORM_SUBTARGET) Odin_Platform_Subtarget_Types :: bit_set[Odin_Platform_Subtarget_Type] +@(builtin) +ODIN_PLATFORM_SUBTARGET_IOS :: ODIN_PLATFORM_SUBTARGET == .iPhone || ODIN_PLATFORM_SUBTARGET == .iPhoneSimulator /* // Defined internally by the compiler diff --git a/core/sys/darwin/sync.odin b/core/sys/darwin/sync.odin index 6d68dc8f8..5f4f16fc3 100644 --- a/core/sys/darwin/sync.odin +++ b/core/sys/darwin/sync.odin @@ -3,23 +3,13 @@ package darwin // #define OS_WAIT_ON_ADDR_AVAILABILITY \ // __API_AVAILABLE(macos(14.4), ios(17.4), tvos(17.4), watchos(10.4)) when ODIN_OS == .Darwin { - - when ODIN_PLATFORM_SUBTARGET == .iOS && ODIN_MINIMUM_OS_VERSION >= 17_04_00 { - WAIT_ON_ADDRESS_AVAILABLE :: true - } else when ODIN_MINIMUM_OS_VERSION >= 14_04_00 { - WAIT_ON_ADDRESS_AVAILABLE :: true + when ODIN_PLATFORM_SUBTARGET_IOS { + WAIT_ON_ADDRESS_AVAILABLE :: ODIN_MINIMUM_OS_VERSION >= 17_04_00 + ULOCK_WAIT_2_AVAILABLE :: ODIN_MINIMUM_OS_VERSION >= 14_00_00 } else { - WAIT_ON_ADDRESS_AVAILABLE :: false + WAIT_ON_ADDRESS_AVAILABLE :: ODIN_MINIMUM_OS_VERSION >= 14_04_00 + ULOCK_WAIT_2_AVAILABLE :: ODIN_MINIMUM_OS_VERSION >= 11_00_00 } - - when ODIN_PLATFORM_SUBTARGET == .iOS && ODIN_MINIMUM_OS_VERSION >= 14_00_00 { - ULOCK_WAIT_2_AVAILABLE :: true - } else when ODIN_MINIMUM_OS_VERSION >= 11_00_00 { - ULOCK_WAIT_2_AVAILABLE :: true - } else { - ULOCK_WAIT_2_AVAILABLE :: false - } - } else { WAIT_ON_ADDRESS_AVAILABLE :: false ULOCK_WAIT_2_AVAILABLE :: false diff --git a/core/sys/info/platform_darwin.odin b/core/sys/info/platform_darwin.odin index dd7f0fa03..3fc8064ec 100644 --- a/core/sys/info/platform_darwin.odin +++ b/core/sys/info/platform_darwin.odin @@ -28,7 +28,7 @@ init_platform :: proc() { macos_version = {int(version.majorVersion), int(version.minorVersion), int(version.patchVersion)} - when ODIN_PLATFORM_SUBTARGET == .iOS { + when ODIN_PLATFORM_SUBTARGET_IOS { os_version.platform = .iOS ws(&b, "iOS") } else { diff --git a/src/build_settings.cpp b/src/build_settings.cpp index d98340844..46e7ecb4e 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -171,7 +171,7 @@ struct TargetMetrics { enum Subtarget : u32 { Subtarget_Default, - Subtarget_iOS, + Subtarget_iPhone, Subtarget_iPhoneSimulator, Subtarget_Android, @@ -180,7 +180,7 @@ enum Subtarget : u32 { gb_global String subtarget_strings[Subtarget_COUNT] = { str_lit(""), - str_lit("ios"), + str_lit("iphone"), str_lit("iphonesimulator"), str_lit("android"), }; @@ -1828,7 +1828,7 @@ gb_internal void init_build_context(TargetMetrics *cross_target, Subtarget subta if (metrics->os == TargetOs_darwin) { switch (subtarget) { - case Subtarget_iOS: + case Subtarget_iPhone: switch (metrics->arch) { case TargetArch_arm64: bc->metrics.target_triplet = str_lit("arm64-apple-ios"); @@ -1909,7 +1909,7 @@ gb_internal void init_build_context(TargetMetrics *cross_target, Subtarget subta if (!bc->minimum_os_version_string_given) { if (subtarget == Subtarget_Default) { bc->minimum_os_version_string = str_lit("11.0.0"); - } else if (subtarget == Subtarget_iOS || subtarget == Subtarget_iPhoneSimulator) { + } else if (subtarget == Subtarget_iPhone || subtarget == Subtarget_iPhoneSimulator) { // NOTE(harold): We default to 17.4 on iOS because that's when os_sync_wait_on_address was added and // we'd like to avoid any potential App Store issues by using the private ulock_* there. bc->minimum_os_version_string = str_lit("17.4"); @@ -1917,7 +1917,7 @@ gb_internal void init_build_context(TargetMetrics *cross_target, Subtarget subta } if (subtarget == Subtarget_iPhoneSimulator) { - // For the iOS simulator subtarget, the version must be between 'ios' and '-simulator'. + // For the iPhoneSimulator subtarget, the version must be between 'ios' and '-simulator'. String suffix = str_lit("-simulator"); GB_ASSERT(string_ends_with(bc->metrics.target_triplet, suffix)); diff --git a/src/checker.cpp b/src/checker.cpp index 625536caa..1821cbd4d 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1172,7 +1172,7 @@ gb_internal void init_universal(void) { { GlobalEnumValue values[Subtarget_COUNT] = { {"Default", Subtarget_Default}, - {"iOS", Subtarget_iOS}, + {"iPhone", Subtarget_iPhone}, {"iPhoneSimulator", Subtarget_iPhoneSimulator}, {"Android", Subtarget_Android}, }; diff --git a/src/linker.cpp b/src/linker.cpp index 7647ed872..41333a3c9 100644 --- a/src/linker.cpp +++ b/src/linker.cpp @@ -787,7 +787,7 @@ try_cross_linking:; // 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_iOS: + case Subtarget_iPhone: darwin_platform_name = "iPhoneOS"; darwin_xcrun_sdk_name = "iphoneos"; darwin_min_version_id = "ios"; -- cgit v1.2.3