History

macOS: get active SDK path from xcode-select

Sushant Pandurangi committed 5 years ago

Commit cca4ece1 parent d5e4b252 files · +37 −16Browse files

Unified · Split

2 files
  1. libtcc.c +28 −1
  2. tcc.h +9 −15
libtcc.c+28−1
@@ -855,6 +855,29 @@ LIBTCCAPI void tcc_delete(TCCState *s1) · 854 unmodified lines
855855 #endif
856856 }
857857
858+/* Looks for the active developer SDK set by xcode-select (or the default
859+ one set during installation.) */
860+#define SZPAIR(s) s "", sizeof(s)-1
861+ST_FUNC int tcc_add_macos_sdkpath(TCCState* s)
862+{
863+ char *sdkroot = NULL, *pos = NULL;
864+ void* xcs = dlopen("libxcselect.dylib", RTLD_GLOBAL | RTLD_LAZY);
865+ CString path = {};
866+ int (*f)(unsigned int, char**) = dlsym(xcs, "xcselect_host_sdk_path");
867+
868+ if (f) f(1, &sdkroot);
869+ if (!sdkroot) return -1;
870+ pos = strstr(sdkroot,"SDKs/MacOSX");
871+ if (!pos) return -1;
872+ cstr_cat(&path, sdkroot, pos-sdkroot);
873+ cstr_cat(&path, SZPAIR("SDKs/MacOSX.sdk/usr/lib\0") );
874+ tcc_add_library_path(s, (char*)path.data);
875+ cstr_free(&path);
876+ tcc_free(sdkroot);
877+ return 0;
878+}
879+#undef SZPAIR
880+
858881 LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
859882 {
860883 s->output_type = output_type;
@@ -881,7 +904,11 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) · 20 unmodified lines
881904 }
882905
883906 tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
884−
907+#ifdef TCC_TARGET_MACHO
908+ if (tcc_add_macos_sdkpath(s) != 0) {
909+ tcc_add_library_path(s, CONFIG_OSX_SDK_FALLBACK);
910+ }
911+#endif
885912 #ifdef TCC_TARGET_PE
886913 # ifdef _WIN32
887914 if (!s->nostdlib && output_type != TCC_OUTPUT_OBJ)
tcc.h+9−15
@@ -247,12 +247,6 @@ extern long double strtold (const char *__nptr, char **__endptr); · 246 unmodified lines
247247 # define ALSO_TRIPLET(s) s
248248 #endif
249249
250−/* FIXME: do this at runtime instead; check output of `xcode-select -p`. libxcselect provides this stuff */
251−#ifndef CONFIG_OSX_SDK1
252−# define CONFIG_OSX_SDK1 "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"
253−# define CONFIG_OSX_SDK2 "/Applications/Xcode.app/Developer/SDKs/MacOSX.sdk"
254−#endif
255−
256250 /* path to find crt1.o, crti.o and crtn.o */
257251 #ifndef CONFIG_TCC_CRTPREFIX
258252 # define CONFIG_TCC_CRTPREFIX USE_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR)
@@ -281,18 +275,18 @@ extern long double strtold (const char *__nptr, char **__endptr); · 22 unmodified lines
281275 # ifdef TCC_TARGET_PE
282276 # define CONFIG_TCC_LIBPATHS "{B}/lib"
283277 # else
284−# ifdef TCC_TARGET_MACHO
285−# define CONFIG_TCC_LIBPATHS \
286− ALSO_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \
287− ":" ALSO_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) \
288− ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR)\
289− ":" ALSO_TRIPLET(CONFIG_OSX_SDK1 "/usr/" CONFIG_LDDIR) \
290− ":" ALSO_TRIPLET(CONFIG_OSX_SDK2 "/usr/" CONFIG_LDDIR)
291−# else
292−# define CONFIG_TCC_LIBPATHS \
278+# define CONFIG_TCC_LIBPATHS \
293279 ALSO_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \
294280 ":" ALSO_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) \
295281 ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR)
282+# ifdef TCC_TARGET_MACHO
283+# define CONFIG_OSX_SDK1 \
284+ "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"
285+# define CONFIG_OSX_SDK2 \
286+ "/Applications/Xcode.app/Developer/SDKs/MacOSX.sdk"
287+# define CONFIG_OSX_SDK_FALLBACK \
288+ ALSO_TRIPLET(CONFIG_OSX_SDK1 "/usr/" CONFIG_LDDIR) \
289+ ":" ALSO_TRIPLET(CONFIG_OSX_SDK2 "/usr/" CONFIG_LDDIR)
296290 # endif
297291 # endif
298292 #endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t