| ... | ... | @@ -12,6 +12,7 @@ |
| 12 | 12 | |
| 13 | 13 | static const char *zig_libc_keys[] = { |
| 14 | 14 | "include_dir", |
| 15 | "sys_include_dir", |
| 15 | 16 | "crt_dir", |
| 16 | 17 | "lib_dir", |
| 17 | 18 | "static_lib_dir", |
| ... | ... | @@ -34,6 +35,7 @@ static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool * |
| 34 | 35 | static void zig_libc_init_empty(ZigLibCInstallation *libc) { |
| 35 | 36 | *libc = {}; |
| 36 | 37 | buf_init_from_str(&libc->include_dir, ""); |
| 38 | buf_init_from_str(&libc->sys_include_dir, ""); |
| 37 | 39 | buf_init_from_str(&libc->crt_dir, ""); |
| 38 | 40 | buf_init_from_str(&libc->lib_dir, ""); |
| 39 | 41 | buf_init_from_str(&libc->static_lib_dir, ""); |
| ... | ... | @@ -46,7 +48,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget |
| 46 | 48 | Error err; |
| 47 | 49 | zig_libc_init_empty(libc); |
| 48 | 50 | |
| 49 | | bool found_keys[array_length(zig_libc_keys)] = {}; // zig_libc_keys_len |
| 51 | bool found_keys[array_length(zig_libc_keys)] = {}; |
| 50 | 52 | |
| 51 | 53 | Buf *contents = buf_alloc(); |
| 52 | 54 | if ((err = os_fetch_file_path(libc_file, contents, false))) { |
| ... | ... | @@ -76,12 +78,13 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget |
| 76 | 78 | Slice<uint8_t> value = SplitIterator_rest(&line_it); |
| 77 | 79 | bool match = false; |
| 78 | 80 | match = match || zig_libc_match_key(name, value, found_keys, 0, &libc->include_dir); |
| 79 | | match = match || zig_libc_match_key(name, value, found_keys, 1, &libc->crt_dir); |
| 80 | | match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->lib_dir); |
| 81 | | match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->static_lib_dir); |
| 82 | | match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->msvc_lib_dir); |
| 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); |
| 81 | match = match || zig_libc_match_key(name, value, found_keys, 1, &libc->sys_include_dir); |
| 82 | match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->crt_dir); |
| 83 | match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->lib_dir); |
| 84 | match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->static_lib_dir); |
| 85 | match = match || zig_libc_match_key(name, value, found_keys, 5, &libc->msvc_lib_dir); |
| 86 | match = match || zig_libc_match_key(name, value, found_keys, 6, &libc->kernel32_lib_dir); |
| 87 | match = match || zig_libc_match_key(name, value, found_keys, 7, &libc->dynamic_linker_path); |
| 85 | 88 | } |
| 86 | 89 | |
| 87 | 90 | for (size_t i = 0; i < zig_libc_keys_len; i += 1) { |
| ... | ... | @@ -100,6 +103,13 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget |
| 100 | 103 | return ErrorSemanticAnalyzeFail; |
| 101 | 104 | } |
| 102 | 105 | |
| 106 | if (buf_len(&libc->sys_include_dir) == 0) { |
| 107 | if (verbose) { |
| 108 | fprintf(stderr, "sys_include_dir may not be empty\n"); |
| 109 | } |
| 110 | return ErrorSemanticAnalyzeFail; |
| 111 | } |
| 112 | |
| 103 | 113 | if (buf_len(&libc->crt_dir) == 0) { |
| 104 | 114 | if (!target_is_darwin(target)) { |
| 105 | 115 | if (verbose) { |
| ... | ... | @@ -195,13 +205,40 @@ static Error zig_libc_find_kernel32_lib_dir(ZigLibCInstallation *self, ZigWindow |
| 195 | 205 | static Error zig_libc_find_native_msvc_lib_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) { |
| 196 | 206 | if (sdk->msvc_lib_dir_ptr == nullptr) { |
| 197 | 207 | if (verbose) { |
| 198 | | fprintf(stderr, "Unable to determine vcruntime path\n"); |
| 208 | fprintf(stderr, "Unable to determine vcruntime.lib path\n"); |
| 199 | 209 | } |
| 200 | 210 | return ErrorFileNotFound; |
| 201 | 211 | } |
| 202 | 212 | buf_init_from_mem(&self->msvc_lib_dir, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len); |
| 203 | 213 | return ErrorNone; |
| 204 | 214 | } |
| 215 | static Error zig_libc_find_native_msvc_include_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) { |
| 216 | Error err; |
| 217 | if (sdk->msvc_lib_dir_ptr == nullptr) { |
| 218 | if (verbose) { |
| 219 | fprintf(stderr, "Unable to determine vcruntime.h path\n"); |
| 220 | } |
| 221 | return ErrorFileNotFound; |
| 222 | } |
| 223 | Buf search_path = BUF_INIT; |
| 224 | buf_init_from_mem(&search_path, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len); |
| 225 | buf_append_str(&search_path, "\\..\\..\\include"); |
| 226 | |
| 227 | Buf *vcruntime_path = buf_sprintf("%s\\vcruntime.h", buf_ptr(&search_path)); |
| 228 | bool exists; |
| 229 | if ((err = os_file_exists(vcruntime_path, &exists))) { |
| 230 | exists = false; |
| 231 | } |
| 232 | if (exists) { |
| 233 | self->sys_include_dir = search_path; |
| 234 | return ErrorNone; |
| 235 | } |
| 236 | |
| 237 | if (verbose) { |
| 238 | fprintf(stderr, "Unable to determine vcruntime.h path\n"); |
| 239 | } |
| 240 | return ErrorFileNotFound; |
| 241 | } |
| 205 | 242 | #else |
| 206 | 243 | static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, bool verbose) { |
| 207 | 244 | const char *cc_exe = getenv("CC"); |
| ... | ... | @@ -253,18 +290,38 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b |
| 253 | 290 | while (*search_path == ' ') { |
| 254 | 291 | search_path += 1; |
| 255 | 292 | } |
| 256 | | Buf *stdlib_path = buf_sprintf("%s/stdlib.h", search_path); |
| 257 | | bool exists; |
| 258 | | if ((err = os_file_exists(stdlib_path, &exists))) { |
| 259 | | exists = false; |
| 293 | |
| 294 | if (buf_len(&self->include_dir) == 0) { |
| 295 | Buf *stdlib_path = buf_sprintf("%s/stdlib.h", search_path); |
| 296 | bool exists; |
| 297 | if ((err = os_file_exists(stdlib_path, &exists))) { |
| 298 | exists = false; |
| 299 | } |
| 300 | if (exists) { |
| 301 | buf_init_from_str(&self->include_dir, search_path); |
| 302 | } |
| 303 | } |
| 304 | if (buf_len(&self->sys_include_dir) == 0) { |
| 305 | Buf *stdlib_path = buf_sprintf("%s/sys/errno.h", search_path); |
| 306 | bool exists; |
| 307 | if ((err = os_file_exists(stdlib_path, &exists))) { |
| 308 | exists = false; |
| 309 | } |
| 310 | if (exists) { |
| 311 | buf_init_from_str(&self->sys_include_dir, search_path); |
| 312 | } |
| 260 | 313 | } |
| 261 | | if (exists) { |
| 262 | | buf_init_from_str(&self->include_dir, search_path); |
| 314 | if (buf_len(&self->include_dir) != 0 && buf_len(&self->sys_include_dir) != 0) { |
| 263 | 315 | return ErrorNone; |
| 264 | 316 | } |
| 265 | 317 | } |
| 266 | 318 | if (verbose) { |
| 267 | | fprintf(stderr, "unable to determine libc include path: stdlib.h not found in '%s' search paths\n", cc_exe); |
| 319 | if (buf_len(&self->include_dir) == 0) { |
| 320 | fprintf(stderr, "unable to determine libc include path: stdlib.h not found in '%s' search paths\n", cc_exe); |
| 321 | } |
| 322 | if (buf_len(&self->sys_include_dir) == 0) { |
| 323 | fprintf(stderr, "unable to determine libc include path: sys/errno.h not found in '%s' search paths\n", cc_exe); |
| 324 | } |
| 268 | 325 | } |
| 269 | 326 | return ErrorFileNotFound; |
| 270 | 327 | } |
| ... | ... | @@ -345,8 +402,12 @@ static Error zig_libc_find_native_dynamic_linker_posix(ZigLibCInstallation *self |
| 345 | 402 | void zig_libc_render(ZigLibCInstallation *self, FILE *file) { |
| 346 | 403 | fprintf(file, |
| 347 | 404 | "# The directory that contains `stdlib.h`.\n" |
| 348 | | "# On POSIX, can be found with: `cc -E -Wp,-v -xc /dev/null`\n" |
| 405 | "# On POSIX, include directories be found with: `cc -E -Wp,-v -xc /dev/null`\n" |
| 349 | 406 | "include_dir=%s\n" |
| 407 | "# The system-specific include directory. May be the same as `include_dir`.\n" |
| 408 | "# On Windows it's the directory that includes `vcruntime.h`.\n" |
| 409 | "# On POSIX it's the directory that includes `sys/errno.h`.\n" |
| 410 | "sys_include_dir=%s\n" |
| 350 | 411 | "\n" |
| 351 | 412 | "# The directory that contains `crt1.o`.\n" |
| 352 | 413 | "# On POSIX, can be found with `cc -print-file-name=crt1.o`.\n" |
| ... | ... | @@ -377,6 +438,7 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) { |
| 377 | 438 | "\n" |
| 378 | 439 | , |
| 379 | 440 | buf_ptr(&self->include_dir), |
| 441 | buf_ptr(&self->sys_include_dir), |
| 380 | 442 | buf_ptr(&self->crt_dir), |
| 381 | 443 | buf_ptr(&self->lib_dir), |
| 382 | 444 | buf_ptr(&self->static_lib_dir), |
| ... | ... | @@ -395,6 +457,8 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) { |
| 395 | 457 | ZigWindowsSDK *sdk; |
| 396 | 458 | switch (zig_find_windows_sdk(&sdk)) { |
| 397 | 459 | case ZigFindWindowsSdkErrorNone: |
| 460 | if ((err = zig_libc_find_native_msvc_include_dir(self, sdk, verbose))) |
| 461 | return err; |
| 398 | 462 | if ((err = zig_libc_find_native_msvc_lib_dir(self, sdk, verbose))) |
| 399 | 463 | return err; |
| 400 | 464 | if ((err = zig_libc_find_kernel32_lib_dir(self, sdk, &native_target, verbose))) |