| ... | ... | @@ -111,7 +111,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | if (buf_len(&libc->msvc_lib_dir) == 0) { |
| 114 | | if (target->os == OsWindows) { |
| 114 | if (target->os == OsWindows && !target_abi_is_gnu(target->abi)) { |
| 115 | 115 | if (verbose) { |
| 116 | 116 | fprintf(stderr, "msvc_lib_dir may not be empty for %s\n", target_os_name(target->os)); |
| 117 | 117 | } |
| ... | ... | @@ -120,7 +120,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | if (buf_len(&libc->kernel32_lib_dir) == 0) { |
| 123 | | if (target->os == OsWindows) { |
| 123 | if (target->os == OsWindows && !target_abi_is_gnu(target->abi)) { |
| 124 | 124 | if (verbose) { |
| 125 | 125 | fprintf(stderr, "kernel32_lib_dir may not be empty for %s\n", target_os_name(target->os)); |
| 126 | 126 | } |
| ... | ... | @@ -132,86 +132,24 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | #if defined(ZIG_OS_WINDOWS) |
| 135 | | static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) { |
| 136 | | Error err; |
| 137 | | if ((err = os_get_win32_ucrt_include_path(sdk, &self->include_dir))) { |
| 138 | | if (verbose) { |
| 139 | | fprintf(stderr, "Unable to determine libc include path: %s\n", err_str(err)); |
| 140 | | } |
| 141 | | return err; |
| 142 | | } |
| 143 | | return ErrorNone; |
| 144 | | } |
| 145 | | static Error zig_libc_find_crt_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target, |
| 146 | | bool verbose) |
| 147 | | { |
| 148 | | Error err; |
| 149 | | if ((err = os_get_win32_ucrt_lib_path(sdk, &self->crt_dir, target->arch))) { |
| 150 | | if (verbose) { |
| 151 | | fprintf(stderr, "Unable to determine ucrt path: %s\n", err_str(err)); |
| 152 | | } |
| 153 | | return err; |
| 154 | | } |
| 155 | | return ErrorNone; |
| 156 | | } |
| 157 | | static Error zig_libc_find_kernel32_lib_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target, |
| 158 | | bool verbose) |
| 159 | | { |
| 160 | | Error err; |
| 161 | | if ((err = os_get_win32_kern32_path(sdk, &self->kernel32_lib_dir, target->arch))) { |
| 162 | | if (verbose) { |
| 163 | | fprintf(stderr, "Unable to determine kernel32 path: %s\n", err_str(err)); |
| 164 | | } |
| 165 | | return err; |
| 166 | | } |
| 167 | | return ErrorNone; |
| 168 | | } |
| 169 | | static Error zig_libc_find_native_msvc_lib_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) { |
| 170 | | if (sdk->msvc_lib_dir_ptr == nullptr) { |
| 171 | | if (verbose) { |
| 172 | | fprintf(stderr, "Unable to determine vcruntime.lib path\n"); |
| 173 | | } |
| 174 | | return ErrorFileNotFound; |
| 175 | | } |
| 176 | | buf_init_from_mem(&self->msvc_lib_dir, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len); |
| 177 | | return ErrorNone; |
| 178 | | } |
| 179 | | static Error zig_libc_find_native_msvc_include_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) { |
| 180 | | Error err; |
| 181 | | if (sdk->msvc_lib_dir_ptr == nullptr) { |
| 182 | | if (verbose) { |
| 183 | | fprintf(stderr, "Unable to determine vcruntime.h path\n"); |
| 184 | | } |
| 185 | | return ErrorFileNotFound; |
| 186 | | } |
| 187 | | Buf search_path = BUF_INIT; |
| 188 | | buf_init_from_mem(&search_path, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len); |
| 189 | | buf_append_str(&search_path, "\\..\\..\\include"); |
| 190 | | |
| 191 | | Buf *vcruntime_path = buf_sprintf("%s\\vcruntime.h", buf_ptr(&search_path)); |
| 192 | | bool exists; |
| 193 | | if ((err = os_file_exists(vcruntime_path, &exists))) { |
| 194 | | exists = false; |
| 195 | | } |
| 196 | | if (exists) { |
| 197 | | self->sys_include_dir = search_path; |
| 198 | | return ErrorNone; |
| 199 | | } |
| 200 | | |
| 201 | | if (verbose) { |
| 202 | | fprintf(stderr, "Unable to determine vcruntime.h path\n"); |
| 203 | | } |
| 204 | | return ErrorFileNotFound; |
| 205 | | } |
| 135 | #define CC_EXE "cc.exe" |
| 206 | 136 | #else |
| 137 | #define CC_EXE "cc" |
| 138 | #endif |
| 139 | |
| 207 | 140 | static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, bool verbose) { |
| 208 | 141 | const char *cc_exe = getenv("CC"); |
| 209 | | cc_exe = (cc_exe == nullptr) ? "cc" : cc_exe; |
| 142 | cc_exe = (cc_exe == nullptr) ? CC_EXE : cc_exe; |
| 210 | 143 | ZigList<const char *> args = {}; |
| 211 | 144 | args.append("-E"); |
| 212 | 145 | args.append("-Wp,-v"); |
| 213 | 146 | args.append("-xc"); |
| 147 | #if defined(ZIG_OS_WINDOWS) |
| 148 | args.append("nul"); |
| 149 | #else |
| 214 | 150 | args.append("/dev/null"); |
| 151 | #endif |
| 152 | |
| 215 | 153 | Termination term; |
| 216 | 154 | Buf *out_stderr = buf_alloc(); |
| 217 | 155 | Buf *out_stdout = buf_alloc(); |
| ... | ... | @@ -235,7 +173,12 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b |
| 235 | 173 | if (newline == nullptr) { |
| 236 | 174 | break; |
| 237 | 175 | } |
| 176 | |
| 177 | #if defined(ZIG_OS_WINDOWS) |
| 178 | *(newline - 1) = 0; |
| 179 | #endif |
| 238 | 180 | *newline = 0; |
| 181 | |
| 239 | 182 | if (prev_newline[0] == ' ') { |
| 240 | 183 | search_paths.append(prev_newline); |
| 241 | 184 | } |
| ... | ... | @@ -255,6 +198,28 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b |
| 255 | 198 | search_path += 1; |
| 256 | 199 | } |
| 257 | 200 | |
| 201 | #if defined(ZIG_OS_WINDOWS) |
| 202 | if (buf_len(&self->include_dir) == 0) { |
| 203 | Buf *stdlib_path = buf_sprintf("%s\\stdlib.h", search_path); |
| 204 | bool exists; |
| 205 | if ((err = os_file_exists(stdlib_path, &exists))) { |
| 206 | exists = false; |
| 207 | } |
| 208 | if (exists) { |
| 209 | buf_init_from_str(&self->include_dir, search_path); |
| 210 | } |
| 211 | } |
| 212 | if (buf_len(&self->sys_include_dir) == 0) { |
| 213 | Buf *stdlib_path = buf_sprintf("%s\\sys\\types.h", search_path); |
| 214 | bool exists; |
| 215 | if ((err = os_file_exists(stdlib_path, &exists))) { |
| 216 | exists = false; |
| 217 | } |
| 218 | if (exists) { |
| 219 | buf_init_from_str(&self->sys_include_dir, search_path); |
| 220 | } |
| 221 | } |
| 222 | #else |
| 258 | 223 | if (buf_len(&self->include_dir) == 0) { |
| 259 | 224 | Buf *stdlib_path = buf_sprintf("%s/stdlib.h", search_path); |
| 260 | 225 | bool exists; |
| ... | ... | @@ -275,6 +240,8 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b |
| 275 | 240 | buf_init_from_str(&self->sys_include_dir, search_path); |
| 276 | 241 | } |
| 277 | 242 | } |
| 243 | #endif |
| 244 | |
| 278 | 245 | if (buf_len(&self->include_dir) != 0 && buf_len(&self->sys_include_dir) != 0) { |
| 279 | 246 | return ErrorNone; |
| 280 | 247 | } |
| ... | ... | @@ -284,15 +251,19 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b |
| 284 | 251 | fprintf(stderr, "unable to determine libc include path: stdlib.h not found in '%s' search paths\n", cc_exe); |
| 285 | 252 | } |
| 286 | 253 | if (buf_len(&self->sys_include_dir) == 0) { |
| 254 | #if defined(ZIG_OS_WINDOWS) |
| 255 | fprintf(stderr, "unable to determine libc include path: sys/types.h not found in '%s' search paths\n", cc_exe); |
| 256 | #else |
| 287 | 257 | fprintf(stderr, "unable to determine libc include path: sys/errno.h not found in '%s' search paths\n", cc_exe); |
| 258 | #endif |
| 288 | 259 | } |
| 289 | 260 | } |
| 290 | 261 | return ErrorFileNotFound; |
| 291 | 262 | } |
| 292 | | #if defined(ZIG_OS_LINUX) |
| 263 | |
| 293 | 264 | Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose) { |
| 294 | 265 | const char *cc_exe = getenv("CC"); |
| 295 | | cc_exe = (cc_exe == nullptr) ? "cc" : cc_exe; |
| 266 | cc_exe = (cc_exe == nullptr) ? CC_EXE : cc_exe; |
| 296 | 267 | ZigList<const char *> args = {}; |
| 297 | 268 | args.append(buf_ptr(buf_sprintf("-print-file-name=%s", o_file))); |
| 298 | 269 | Termination term; |
| ... | ... | @@ -301,19 +272,25 @@ Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirnam |
| 301 | 272 | Error err; |
| 302 | 273 | if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) { |
| 303 | 274 | if (verbose) { |
| 304 | | fprintf(stderr, "unable to determine libc include path: executing '%s': %s\n", cc_exe, err_str(err)); |
| 275 | fprintf(stderr, "unable to determine libc library path: executing '%s': %s\n", cc_exe, err_str(err)); |
| 305 | 276 | } |
| 306 | 277 | return err; |
| 307 | 278 | } |
| 308 | 279 | if (term.how != TerminationIdClean || term.code != 0) { |
| 309 | 280 | if (verbose) { |
| 310 | | fprintf(stderr, "unable to determine libc include path: executing '%s' failed\n", cc_exe); |
| 281 | fprintf(stderr, "unable to determine libc library path: executing '%s' failed\n", cc_exe); |
| 311 | 282 | } |
| 312 | 283 | return ErrorCCompileErrors; |
| 313 | 284 | } |
| 285 | #if defined(ZIG_OS_WINDOWS) |
| 286 | if (buf_ends_with_str(out_stdout, "\r\n")) { |
| 287 | buf_resize(out_stdout, buf_len(out_stdout) - 2); |
| 288 | } |
| 289 | #else |
| 314 | 290 | if (buf_ends_with_str(out_stdout, "\n")) { |
| 315 | 291 | buf_resize(out_stdout, buf_len(out_stdout) - 1); |
| 316 | 292 | } |
| 293 | #endif |
| 317 | 294 | if (buf_len(out_stdout) == 0 || buf_eql_str(out_stdout, o_file)) { |
| 318 | 295 | return ErrorCCompilerCannotFindFile; |
| 319 | 296 | } |
| ... | ... | @@ -324,16 +301,95 @@ Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirnam |
| 324 | 301 | } |
| 325 | 302 | return ErrorNone; |
| 326 | 303 | } |
| 304 | |
| 305 | #undef CC_EXE |
| 306 | |
| 327 | 307 | static Error zig_libc_find_native_crt_dir_posix(ZigLibCInstallation *self, bool verbose) { |
| 328 | 308 | return zig_libc_cc_print_file_name("crt1.o", &self->crt_dir, true, verbose); |
| 329 | 309 | } |
| 330 | | #endif |
| 310 | |
| 311 | #if defined(ZIG_OS_WINDOWS) |
| 312 | static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) { |
| 313 | Error err; |
| 314 | if ((err = os_get_win32_ucrt_include_path(sdk, &self->include_dir))) { |
| 315 | if (verbose) { |
| 316 | fprintf(stderr, "Unable to determine libc include path: %s\n", err_str(err)); |
| 317 | } |
| 318 | return err; |
| 319 | } |
| 320 | return ErrorNone; |
| 321 | } |
| 322 | |
| 323 | static Error zig_libc_find_crt_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target, |
| 324 | bool verbose) |
| 325 | { |
| 326 | Error err; |
| 327 | if ((err = os_get_win32_ucrt_lib_path(sdk, &self->crt_dir, target->arch))) { |
| 328 | if (verbose) { |
| 329 | fprintf(stderr, "Unable to determine ucrt path: %s\n", err_str(err)); |
| 330 | } |
| 331 | return err; |
| 332 | } |
| 333 | return ErrorNone; |
| 334 | } |
| 335 | |
| 336 | static Error zig_libc_find_kernel32_lib_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target, |
| 337 | bool verbose) |
| 338 | { |
| 339 | Error err; |
| 340 | if ((err = os_get_win32_kern32_path(sdk, &self->kernel32_lib_dir, target->arch))) { |
| 341 | if (verbose) { |
| 342 | fprintf(stderr, "Unable to determine kernel32 path: %s\n", err_str(err)); |
| 343 | } |
| 344 | return err; |
| 345 | } |
| 346 | return ErrorNone; |
| 347 | } |
| 348 | |
| 349 | static Error zig_libc_find_native_msvc_lib_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) { |
| 350 | if (sdk->msvc_lib_dir_ptr == nullptr) { |
| 351 | if (verbose) { |
| 352 | fprintf(stderr, "Unable to determine vcruntime.lib path\n"); |
| 353 | } |
| 354 | return ErrorFileNotFound; |
| 355 | } |
| 356 | buf_init_from_mem(&self->msvc_lib_dir, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len); |
| 357 | return ErrorNone; |
| 358 | } |
| 359 | |
| 360 | static Error zig_libc_find_native_msvc_include_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) { |
| 361 | Error err; |
| 362 | if (sdk->msvc_lib_dir_ptr == nullptr) { |
| 363 | if (verbose) { |
| 364 | fprintf(stderr, "Unable to determine vcruntime.h path\n"); |
| 365 | } |
| 366 | return ErrorFileNotFound; |
| 367 | } |
| 368 | Buf search_path = BUF_INIT; |
| 369 | buf_init_from_mem(&search_path, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len); |
| 370 | buf_append_str(&search_path, "\\..\\..\\include"); |
| 371 | |
| 372 | Buf *vcruntime_path = buf_sprintf("%s\\vcruntime.h", buf_ptr(&search_path)); |
| 373 | bool exists; |
| 374 | if ((err = os_file_exists(vcruntime_path, &exists))) { |
| 375 | exists = false; |
| 376 | } |
| 377 | if (exists) { |
| 378 | self->sys_include_dir = search_path; |
| 379 | return ErrorNone; |
| 380 | } |
| 381 | |
| 382 | if (verbose) { |
| 383 | fprintf(stderr, "Unable to determine vcruntime.h path\n"); |
| 384 | } |
| 385 | return ErrorFileNotFound; |
| 386 | } |
| 331 | 387 | #endif |
| 332 | 388 | |
| 333 | 389 | void zig_libc_render(ZigLibCInstallation *self, FILE *file) { |
| 334 | 390 | fprintf(file, |
| 335 | 391 | "# The directory that contains `stdlib.h`.\n" |
| 336 | | "# On POSIX, include directories be found with: `cc -E -Wp,-v -xc /dev/null`\n" |
| 392 | "# On POSIX-like systems, include directories be found with: `cc -E -Wp,-v -xc /dev/null`\n" |
| 337 | 393 | "include_dir=%s\n" |
| 338 | 394 | "# The system-specific include directory. May be the same as `include_dir`.\n" |
| 339 | 395 | "# On Windows it's the directory that includes `vcruntime.h`.\n" |
| ... | ... | @@ -346,11 +402,11 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) { |
| 346 | 402 | "crt_dir=%s\n" |
| 347 | 403 | "\n" |
| 348 | 404 | "# The directory that contains `vcruntime.lib`.\n" |
| 349 | | "# Only needed when targeting Windows.\n" |
| 405 | "# Only needed when targeting MSVC on Windows.\n" |
| 350 | 406 | "msvc_lib_dir=%s\n" |
| 351 | 407 | "\n" |
| 352 | 408 | "# The directory that contains `kernel32.lib`.\n" |
| 353 | | "# Only needed when targeting Windows.\n" |
| 409 | "# Only needed when targeting MSVC on Windows.\n" |
| 354 | 410 | "kernel32_lib_dir=%s\n" |
| 355 | 411 | "\n" |
| 356 | 412 | , |
| ... | ... | @@ -368,26 +424,34 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) { |
| 368 | 424 | #if defined(ZIG_OS_WINDOWS) |
| 369 | 425 | ZigTarget native_target; |
| 370 | 426 | get_native_target(&native_target); |
| 371 | | ZigWindowsSDK *sdk; |
| 372 | | switch (zig_find_windows_sdk(&sdk)) { |
| 373 | | case ZigFindWindowsSdkErrorNone: |
| 374 | | if ((err = zig_libc_find_native_msvc_include_dir(self, sdk, verbose))) |
| 375 | | return err; |
| 376 | | if ((err = zig_libc_find_native_msvc_lib_dir(self, sdk, verbose))) |
| 377 | | return err; |
| 378 | | if ((err = zig_libc_find_kernel32_lib_dir(self, sdk, &native_target, verbose))) |
| 379 | | return err; |
| 380 | | if ((err = zig_libc_find_native_include_dir_windows(self, sdk, verbose))) |
| 381 | | return err; |
| 382 | | if ((err = zig_libc_find_crt_dir_windows(self, sdk, &native_target, verbose))) |
| 383 | | return err; |
| 384 | | return ErrorNone; |
| 385 | | case ZigFindWindowsSdkErrorOutOfMemory: |
| 386 | | return ErrorNoMem; |
| 387 | | case ZigFindWindowsSdkErrorNotFound: |
| 388 | | return ErrorFileNotFound; |
| 389 | | case ZigFindWindowsSdkErrorPathTooLong: |
| 390 | | return ErrorPathTooLong; |
| 427 | if (target_abi_is_gnu(native_target.abi)) { |
| 428 | if ((err = zig_libc_find_native_include_dir_posix(self, verbose))) |
| 429 | return err; |
| 430 | if ((err = zig_libc_find_native_crt_dir_posix(self, verbose))) |
| 431 | return err; |
| 432 | return ErrorNone; |
| 433 | } else { |
| 434 | ZigWindowsSDK *sdk; |
| 435 | switch (zig_find_windows_sdk(&sdk)) { |
| 436 | case ZigFindWindowsSdkErrorNone: |
| 437 | if ((err = zig_libc_find_native_msvc_include_dir(self, sdk, verbose))) |
| 438 | return err; |
| 439 | if ((err = zig_libc_find_native_msvc_lib_dir(self, sdk, verbose))) |
| 440 | return err; |
| 441 | if ((err = zig_libc_find_kernel32_lib_dir(self, sdk, &native_target, verbose))) |
| 442 | return err; |
| 443 | if ((err = zig_libc_find_native_include_dir_windows(self, sdk, verbose))) |
| 444 | return err; |
| 445 | if ((err = zig_libc_find_crt_dir_windows(self, sdk, &native_target, verbose))) |
| 446 | return err; |
| 447 | return ErrorNone; |
| 448 | case ZigFindWindowsSdkErrorOutOfMemory: |
| 449 | return ErrorNoMem; |
| 450 | case ZigFindWindowsSdkErrorNotFound: |
| 451 | return ErrorFileNotFound; |
| 452 | case ZigFindWindowsSdkErrorPathTooLong: |
| 453 | return ErrorPathTooLong; |
| 454 | } |
| 391 | 455 | } |
| 392 | 456 | zig_unreachable(); |
| 393 | 457 | #else |