hosted.c (4717B)
1 #include "os/hosted_internal.h" 2 3 /* The three Darwin OSes share one hosted-ops instance, so their per-OS SDK and 4 * profile names cannot be plain vtable fields; this single table is the source 5 * of truth. The first entry is the macOS / unknown-OS fallback. */ 6 static const struct { 7 KitOSKind os; 8 const char* sdk; 9 const char* profile; 10 } darwin_os_info[] = { 11 {KIT_OS_MACOS, "macosx", "macos-libSystem"}, 12 {KIT_OS_IOS, "iphoneos", "ios-libSystem"}, 13 {KIT_OS_IOS_SIMULATOR, "iphonesimulator", "ios-simulator-libSystem"}, 14 }; 15 16 static size_t darwin_os_info_index(KitOSKind os) { 17 size_t i; 18 for (i = 1; i < sizeof darwin_os_info / sizeof darwin_os_info[0]; ++i) 19 if (darwin_os_info[i].os == os) return i; 20 return 0; /* macOS / default fallback */ 21 } 22 23 static const char* darwin_sdk_name(KitOSKind os) { 24 return darwin_os_info[darwin_os_info_index(os)].sdk; 25 } 26 27 static const char* darwin_profile_name(KitOSKind os) { 28 return darwin_os_info[darwin_os_info_index(os)].profile; 29 } 30 31 static int hosted_add_darwin_defines(KitOsHostedPlan* plan, 32 KitTargetSpec target) { 33 if (kit_os_hosted_add_clang_compat_defines(plan) != 0 || 34 kit_os_hosted_add_define(plan, "__APPLE__", "1") != 0 || 35 kit_os_hosted_add_define(plan, "__APPLE_CC__", "6000") != 0 || 36 kit_os_hosted_add_define(plan, "__MACH__", "1") != 0 || 37 kit_os_hosted_add_define(plan, "__DYNAMIC__", "1") != 0) 38 return 1; 39 if (target.os == KIT_OS_IOS || target.os == KIT_OS_IOS_SIMULATOR) { 40 if (kit_os_hosted_add_define( 41 plan, "__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__", 42 "120000") != 0 || 43 kit_os_hosted_add_define(plan, 44 "__ENVIRONMENT_OS_VERSION_MIN_REQUIRED__", 45 "120000") != 0) 46 return 1; 47 } 48 if (target.os == KIT_OS_IOS_SIMULATOR) { 49 if (kit_os_hosted_add_define(plan, "__APPLE_EMBEDDED_SIMULATOR__", "1") != 50 0 || 51 kit_os_hosted_add_define(plan, "TARGET_IPHONE_SIMULATOR", "1") != 0) 52 return 1; 53 } 54 return 0; 55 } 56 57 static int hosted_resolve_darwin(const KitOsHostedRequest* req, 58 const KitOsHostedDirs* dirs, 59 KitOsHostedPlan* plan) { 60 if (dirs->nlibdirs == 0) { 61 kit_os_hosted_errf( 62 req->host, req->host_user, req->tool, 63 "Darwin hosted profile requires an Apple SDK; pass --sysroot, set " 64 "KIT_SYSROOT, or install Xcode/Command Line Tools (try: --sysroot " 65 "\"$(xcrun --sdk %s --show-sdk-path)\")", 66 darwin_sdk_name(req->target.os)); 67 return 1; 68 } 69 plan->profile_name = darwin_profile_name(req->target.os); 70 if (hosted_add_darwin_defines(plan, req->target) != 0 || 71 kit_os_hosted_add_incdirs(plan, req->host, req->host_user, dirs) != 0) { 72 kit_os_hosted_errf(req->host, req->host_user, req->tool, "out of memory"); 73 return 1; 74 } 75 if (!req->link_inputs) return 0; 76 { 77 size_t size = 0; 78 char* libsystem = kit_os_hosted_find_in_libdirs( 79 req->host, req->host_user, dirs, "libSystem.tbd", &size); 80 if (!libsystem) 81 libsystem = kit_os_hosted_find_in_libdirs( 82 req->host, req->host_user, dirs, "libSystem.dylib", &size); 83 if (!libsystem) { 84 kit_os_hosted_errf(req->host, req->host_user, req->tool, 85 "hosted profile missing required file: " 86 "libSystem.tbd or libSystem.dylib"); 87 return 1; 88 } 89 if (kit_os_hosted_add_input(plan->after, &plan->nafter, 90 KIT_OS_HOSTED_MAX_AFTER, 91 KIT_OS_HOSTED_INPUT_DSO, libsystem, size) != 92 0) { 93 req->host->free(req->host_user, libsystem, size); 94 kit_os_hosted_errf(req->host, req->host_user, req->tool, 95 "too many hosted inputs"); 96 return 1; 97 } 98 } 99 return 0; 100 } 101 102 static int hosted_sysroot_layout_darwin(KitOsHostedDirs* dirs, 103 KitTargetSpec target, 104 const char* sysroot) { 105 (void)target; 106 if (kit_os_hosted_dirs_add_inc_join(dirs, sysroot, "usr/include") != 0 || 107 kit_os_hosted_dirs_add_lib_join(dirs, sysroot, "usr/lib") != 0) 108 return 1; 109 return 0; 110 } 111 112 const KitOsHostedOps kit_os_macos_hosted_ops = { 113 .obj = KIT_OBJ_MACHO, 114 /* Native Darwin compilation is hosted by default. Driver-side host/OS 115 * gating prevents this policy from borrowing a Darwin SDK when kit runs 116 * on another operating system. */ 117 .default_profile = 1, 118 .needs_sysroot_libdir = 0, 119 .uses_framework_dirs = 1, 120 .resolve = hosted_resolve_darwin, 121 .sysroot_layout = hosted_sysroot_layout_darwin, 122 };