| ... | @@ -10,10 +10,9 @@ | ... | @@ -10,10 +10,9 @@ |
| 10 | #include "windows_sdk.h" | 10 | #include "windows_sdk.h" |
| 11 | #include "target.hpp" | 11 | #include "target.hpp" |
| 12 | | 12 | |
| 13 | static const size_t zig_libc_keys_len = 6; | | |
| 14 | | | |
| 15 | static const char *zig_libc_keys[] = { | 13 | static const char *zig_libc_keys[] = { |
| 16 | "include_dir", | 14 | "include_dir", |
| | 15 | "crt_dir", |
| 17 | "lib_dir", | 16 | "lib_dir", |
| 18 | "static_lib_dir", | 17 | "static_lib_dir", |
| 19 | "msvc_lib_dir", | 18 | "msvc_lib_dir", |
| ... | @@ -21,6 +20,8 @@ static const char *zig_libc_keys[] = { | ... | @@ -21,6 +20,8 @@ static const char *zig_libc_keys[] = { |
| 21 | "dynamic_linker_path", | 20 | "dynamic_linker_path", |
| 22 | }; | 21 | }; |
| 23 | | 22 | |
| | 23 | static const size_t zig_libc_keys_len = array_length(zig_libc_keys); |
| | 24 | |
| 24 | static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool *found_keys, | 25 | static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool *found_keys, |
| 25 | size_t index, Buf *field_ptr) | 26 | size_t index, Buf *field_ptr) |
| 26 | { | 27 | { |
| ... | @@ -33,6 +34,7 @@ static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool * | ... | @@ -33,6 +34,7 @@ static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool * |
| 33 | static void zig_libc_init_empty(ZigLibCInstallation *libc) { | 34 | static void zig_libc_init_empty(ZigLibCInstallation *libc) { |
| 34 | *libc = {}; | 35 | *libc = {}; |
| 35 | buf_init_from_str(&libc->include_dir, ""); | 36 | buf_init_from_str(&libc->include_dir, ""); |
| | 37 | buf_init_from_str(&libc->crt_dir, ""); |
| 36 | buf_init_from_str(&libc->lib_dir, ""); | 38 | buf_init_from_str(&libc->lib_dir, ""); |
| 37 | buf_init_from_str(&libc->static_lib_dir, ""); | 39 | buf_init_from_str(&libc->static_lib_dir, ""); |
| 38 | buf_init_from_str(&libc->msvc_lib_dir, ""); | 40 | buf_init_from_str(&libc->msvc_lib_dir, ""); |
| ... | @@ -44,7 +46,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget | ... | @@ -44,7 +46,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget |
| 44 | Error err; | 46 | Error err; |
| 45 | zig_libc_init_empty(libc); | 47 | zig_libc_init_empty(libc); |
| 46 | | 48 | |
| 47 | bool found_keys[6] = {}; // zig_libc_keys_len | 49 | bool found_keys[array_length(zig_libc_keys)] = {}; // zig_libc_keys_len |
| 48 | | 50 | |
| 49 | Buf *contents = buf_alloc(); | 51 | Buf *contents = buf_alloc(); |
| 50 | if ((err = os_fetch_file_path(libc_file, contents, false))) { | 52 | if ((err = os_fetch_file_path(libc_file, contents, false))) { |
| ... | @@ -74,11 +76,12 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget | ... | @@ -74,11 +76,12 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget |
| 74 | Slice<uint8_t> value = SplitIterator_rest(&line_it); | 76 | Slice<uint8_t> value = SplitIterator_rest(&line_it); |
| 75 | bool match = false; | 77 | bool match = false; |
| 76 | match = match || zig_libc_match_key(name, value, found_keys, 0, &libc->include_dir); | 78 | match = match || zig_libc_match_key(name, value, found_keys, 0, &libc->include_dir); |
| 77 | match = match || zig_libc_match_key(name, value, found_keys, 1, &libc->lib_dir); | 79 | match = match || zig_libc_match_key(name, value, found_keys, 1, &libc->crt_dir); |
| 78 | match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->static_lib_dir); | 80 | match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->lib_dir); |
| 79 | match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->msvc_lib_dir); | 81 | match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->static_lib_dir); |
| 80 | match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->kernel32_lib_dir); | 82 | match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->msvc_lib_dir); |
| 81 | match = match || zig_libc_match_key(name, value, found_keys, 5, &libc->dynamic_linker_path); | 83 | match = match || zig_libc_match_key(name, value, found_keys, 5, &libc->kernel32_lib_dir); |
| | 84 | match = match || zig_libc_match_key(name, value, found_keys, 6, &libc->dynamic_linker_path); |
| 82 | } | 85 | } |
| 83 | | 86 | |
| 84 | for (size_t i = 0; i < zig_libc_keys_len; i += 1) { | 87 | for (size_t i = 0; i < zig_libc_keys_len; i += 1) { |
| ... | @@ -97,8 +100,17 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget | ... | @@ -97,8 +100,17 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget |
| 97 | return ErrorSemanticAnalyzeFail; | 100 | return ErrorSemanticAnalyzeFail; |
| 98 | } | 101 | } |
| 99 | | 102 | |
| 100 | if (buf_len(&libc->lib_dir) == 0) { | 103 | if (buf_len(&libc->crt_dir) == 0) { |
| 101 | if (!target_is_darwin(target)) { | 104 | if (!target_is_darwin(target)) { |
| | 105 | if (verbose) { |
| | 106 | fprintf(stderr, "crt_dir may not be empty for %s\n", get_target_os_name(target->os)); |
| | 107 | } |
| | 108 | return ErrorSemanticAnalyzeFail; |
| | 109 | } |
| | 110 | } |
| | 111 | |
| | 112 | if (buf_len(&libc->lib_dir) == 0) { |
| | 113 | if (!target_is_darwin(target) && target->os != OsWindows) { |
| 102 | if (verbose) { | 114 | if (verbose) { |
| 103 | fprintf(stderr, "lib_dir may not be empty for %s\n", get_target_os_name(target->os)); | 115 | fprintf(stderr, "lib_dir may not be empty for %s\n", get_target_os_name(target->os)); |
| 104 | } | 116 | } |
| ... | @@ -134,7 +146,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget | ... | @@ -134,7 +146,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget |
| 134 | } | 146 | } |
| 135 | | 147 | |
| 136 | if (buf_len(&libc->dynamic_linker_path) == 0) { | 148 | if (buf_len(&libc->dynamic_linker_path) == 0) { |
| 137 | if (target->os == OsLinux) { | 149 | if (!target_is_darwin(target) && target->os != OsWindows) { |
| 138 | if (verbose) { | 150 | if (verbose) { |
| 139 | fprintf(stderr, "dynamic_linker_path may not be empty for %s\n", get_target_os_name(target->os)); | 151 | fprintf(stderr, "dynamic_linker_path may not be empty for %s\n", get_target_os_name(target->os)); |
| 140 | } | 152 | } |
| ... | @@ -156,11 +168,11 @@ static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self, | ... | @@ -156,11 +168,11 @@ static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self, |
| 156 | } | 168 | } |
| 157 | return ErrorNone; | 169 | return ErrorNone; |
| 158 | } | 170 | } |
| 159 | static Error zig_libc_find_lib_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target, | 171 | static Error zig_libc_find_crt_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target, |
| 160 | bool verbose) | 172 | bool verbose) |
| 161 | { | 173 | { |
| 162 | Error err; | 174 | Error err; |
| 163 | if ((err = os_get_win32_ucrt_lib_path(sdk, &self->lib_dir, target->arch.arch))) { | 175 | if ((err = os_get_win32_ucrt_lib_path(sdk, &self->crt_dir, target->arch.arch))) { |
| 164 | if (verbose) { | 176 | if (verbose) { |
| 165 | fprintf(stderr, "Unable to determine ucrt path: %s\n", err_str(err)); | 177 | fprintf(stderr, "Unable to determine ucrt path: %s\n", err_str(err)); |
| 166 | } | 178 | } |
| ... | @@ -291,8 +303,11 @@ static Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want | ... | @@ -291,8 +303,11 @@ static Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want |
| 291 | } | 303 | } |
| 292 | return ErrorNone; | 304 | return ErrorNone; |
| 293 | } | 305 | } |
| | 306 | static Error zig_libc_find_native_crt_dir_posix(ZigLibCInstallation *self, bool verbose) { |
| | 307 | return zig_libc_cc_print_file_name("crt1.o", &self->crt_dir, true, verbose); |
| | 308 | } |
| 294 | static Error zig_libc_find_native_lib_dir_posix(ZigLibCInstallation *self, bool verbose) { | 309 | static Error zig_libc_find_native_lib_dir_posix(ZigLibCInstallation *self, bool verbose) { |
| 295 | return zig_libc_cc_print_file_name("crt1.o", &self->lib_dir, true, verbose); | 310 | return zig_libc_cc_print_file_name("libgcc_s.so", &self->lib_dir, true, verbose); |
| 296 | } | 311 | } |
| 297 | | 312 | |
| 298 | static Error zig_libc_find_native_static_lib_dir_posix(ZigLibCInstallation *self, bool verbose) { | 313 | static Error zig_libc_find_native_static_lib_dir_posix(ZigLibCInstallation *self, bool verbose) { |
| ... | @@ -328,16 +343,21 @@ static Error zig_libc_find_native_dynamic_linker_posix(ZigLibCInstallation *self | ... | @@ -328,16 +343,21 @@ static Error zig_libc_find_native_dynamic_linker_posix(ZigLibCInstallation *self |
| 328 | void zig_libc_render(ZigLibCInstallation *self, FILE *file) { | 343 | void zig_libc_render(ZigLibCInstallation *self, FILE *file) { |
| 329 | fprintf(file, | 344 | fprintf(file, |
| 330 | "# The directory that contains `stdlib.h`.\n" | 345 | "# The directory that contains `stdlib.h`.\n" |
| 331 | "# On Linux, can be found with: `cc -E -Wp,-v -xc /dev/null`\n" | 346 | "# On POSIX, can be found with: `cc -E -Wp,-v -xc /dev/null`\n" |
| 332 | "include_dir=%s\n" | 347 | "include_dir=%s\n" |
| 333 | "\n" | 348 | "\n" |
| 334 | "# The directory that contains `crt1.o`.\n" | 349 | "# The directory that contains `crt1.o`.\n" |
| 335 | "# On Linux, can be found with `cc -print-file-name=crt1.o`.\n" | 350 | "# On POSIX, can be found with `cc -print-file-name=crt1.o`.\n" |
| 336 | "# Not needed when targeting MacOS.\n" | 351 | "# Not needed when targeting MacOS.\n" |
| | 352 | "crt_dir=%s\n" |
| | 353 | "\n" |
| | 354 | "# The directory that contains `libgcc_s.so`.\n" |
| | 355 | "# On POSIX, can be found with `cc -print-file-name=libgcc_s.so`.\n" |
| | 356 | "# Not needed when targeting MacOS or Windows.\n" |
| 337 | "lib_dir=%s\n" | 357 | "lib_dir=%s\n" |
| 338 | "\n" | 358 | "\n" |
| 339 | "# The directory that contains `crtbegin.o`.\n" | 359 | "# The directory that contains `crtbegin.o`.\n" |
| 340 | "# On Linux, can be found with `cc -print-file-name=crtbegin.o`.\n" | 360 | "# On POSIX, can be found with `cc -print-file-name=crtbegin.o`.\n" |
| 341 | "# Not needed when targeting MacOS or Windows.\n" | 361 | "# Not needed when targeting MacOS or Windows.\n" |
| 342 | "static_lib_dir=%s\n" | 362 | "static_lib_dir=%s\n" |
| 343 | "\n" | 363 | "\n" |
| ... | @@ -350,11 +370,12 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) { | ... | @@ -350,11 +370,12 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) { |
| 350 | "kernel32_lib_dir=%s\n" | 370 | "kernel32_lib_dir=%s\n" |
| 351 | "\n" | 371 | "\n" |
| 352 | "# The full path to the dynamic linker, on the target system.\n" | 372 | "# The full path to the dynamic linker, on the target system.\n" |
| 353 | "# Only needed when targeting Linux.\n" | 373 | "# Not needed when targeting MacOS or Windows.\n" |
| 354 | "dynamic_linker_path=%s\n" | 374 | "dynamic_linker_path=%s\n" |
| 355 | "\n" | 375 | "\n" |
| 356 | , | 376 | , |
| 357 | buf_ptr(&self->include_dir), | 377 | buf_ptr(&self->include_dir), |
| | 378 | buf_ptr(&self->crt_dir), |
| 358 | buf_ptr(&self->lib_dir), | 379 | buf_ptr(&self->lib_dir), |
| 359 | buf_ptr(&self->static_lib_dir), | 380 | buf_ptr(&self->static_lib_dir), |
| 360 | buf_ptr(&self->msvc_lib_dir), | 381 | buf_ptr(&self->msvc_lib_dir), |
| ... | @@ -378,7 +399,7 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) { | ... | @@ -378,7 +399,7 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) { |
| 378 | return err; | 399 | return err; |
| 379 | if ((err = zig_libc_find_native_include_dir_windows(self, sdk, verbose))) | 400 | if ((err = zig_libc_find_native_include_dir_windows(self, sdk, verbose))) |
| 380 | return err; | 401 | return err; |
| 381 | if ((err = zig_libc_find_lib_dir_windows(self, sdk, &native_target, verbose))) | 402 | if ((err = zig_libc_find_crt_dir_windows(self, sdk, &native_target, verbose))) |
| 382 | return err; | 403 | return err; |
| 383 | return ErrorNone; | 404 | return ErrorNone; |
| 384 | case ZigFindWindowsSdkErrorOutOfMemory: | 405 | case ZigFindWindowsSdkErrorOutOfMemory: |
| ... | @@ -393,9 +414,12 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) { | ... | @@ -393,9 +414,12 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) { |
| 393 | if ((err = zig_libc_find_native_include_dir_posix(self, verbose))) | 414 | if ((err = zig_libc_find_native_include_dir_posix(self, verbose))) |
| 394 | return err; | 415 | return err; |
| 395 | #if defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) | 416 | #if defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) |
| | 417 | buf_init_from_str(&self->crt_dir, "/usr/lib"); |
| 396 | buf_init_from_str(&self->lib_dir, "/usr/lib"); | 418 | buf_init_from_str(&self->lib_dir, "/usr/lib"); |
| 397 | buf_init_from_str(&self->static_lib_dir, "/usr/lib"); | 419 | buf_init_from_str(&self->static_lib_dir, "/usr/lib"); |
| 398 | #elif !defined(ZIG_OS_DARWIN) | 420 | #elif !defined(ZIG_OS_DARWIN) |
| | 421 | if ((err = zig_libc_find_native_crt_dir_posix(self, verbose))) |
| | 422 | return err; |
| 399 | if ((err = zig_libc_find_native_lib_dir_posix(self, verbose))) | 423 | if ((err = zig_libc_find_native_lib_dir_posix(self, verbose))) |
| 400 | return err; | 424 | return err; |
| 401 | if ((err = zig_libc_find_native_static_lib_dir_posix(self, verbose))) | 425 | if ((err = zig_libc_find_native_static_lib_dir_posix(self, verbose))) |