| ... | ... | @@ -174,16 +174,23 @@ pub const LibCInstallation = struct { |
| 174 | 174 | }); |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | pub const FindNativeOptions = struct { |
| 178 | allocator: *Allocator, |
| 179 | |
| 180 | /// If enabled, will print human-friendly errors to stderr. |
| 181 | verbose: bool = false, |
| 182 | }; |
| 183 | |
| 177 | 184 | /// Finds the default, native libc. |
| 178 | | pub fn findNative(allocator: *Allocator) FindError!LibCInstallation { |
| 185 | pub fn findNative(args: FindNativeOptions) FindError!LibCInstallation { |
| 179 | 186 | var self: LibCInstallation = .{}; |
| 180 | 187 | |
| 181 | 188 | if (is_windows) { |
| 182 | 189 | if (is_gnu) { |
| 183 | 190 | var batch = Batch(FindError!void, 3, .auto_async).init(); |
| 184 | | batch.add(&async self.findNativeIncludeDirPosix(allocator)); |
| 185 | | batch.add(&async self.findNativeCrtDirPosix(allocator)); |
| 186 | | batch.add(&async self.findNativeStaticCrtDirPosix(allocator)); |
| 191 | batch.add(&async self.findNativeIncludeDirPosix(args)); |
| 192 | batch.add(&async self.findNativeCrtDirPosix(args)); |
| 193 | batch.add(&async self.findNativeStaticCrtDirPosix(args)); |
| 187 | 194 | try batch.wait(); |
| 188 | 195 | } else { |
| 189 | 196 | var sdk: *ZigWindowsSDK = undefined; |
| ... | ... | @@ -192,11 +199,11 @@ pub const LibCInstallation = struct { |
| 192 | 199 | defer zig_free_windows_sdk(sdk); |
| 193 | 200 | |
| 194 | 201 | var batch = Batch(FindError!void, 5, .auto_async).init(); |
| 195 | | batch.add(&async self.findNativeMsvcIncludeDir(allocator, sdk)); |
| 196 | | batch.add(&async self.findNativeMsvcLibDir(allocator, sdk)); |
| 197 | | batch.add(&async self.findNativeKernel32LibDir(allocator, sdk)); |
| 198 | | batch.add(&async self.findNativeIncludeDirWindows(allocator, sdk)); |
| 199 | | batch.add(&async self.findNativeCrtDirWindows(allocator, sdk)); |
| 202 | batch.add(&async self.findNativeMsvcIncludeDir(args, sdk)); |
| 203 | batch.add(&async self.findNativeMsvcLibDir(args, sdk)); |
| 204 | batch.add(&async self.findNativeKernel32LibDir(args, sdk)); |
| 205 | batch.add(&async self.findNativeIncludeDirWindows(args, sdk)); |
| 206 | batch.add(&async self.findNativeCrtDirWindows(args, sdk)); |
| 200 | 207 | try batch.wait(); |
| 201 | 208 | }, |
| 202 | 209 | .OutOfMemory => return error.OutOfMemory, |
| ... | ... | @@ -208,11 +215,11 @@ pub const LibCInstallation = struct { |
| 208 | 215 | try blk: { |
| 209 | 216 | var batch = Batch(FindError!void, 2, .auto_async).init(); |
| 210 | 217 | errdefer batch.wait() catch {}; |
| 211 | | batch.add(&async self.findNativeIncludeDirPosix(allocator)); |
| 218 | batch.add(&async self.findNativeIncludeDirPosix(args)); |
| 212 | 219 | if (is_freebsd or is_netbsd) { |
| 213 | | self.crt_dir = try std.mem.dupeZ(allocator, u8, "/usr/lib"); |
| 220 | self.crt_dir = try std.mem.dupeZ(args.allocator, u8, "/usr/lib"); |
| 214 | 221 | } else if (is_linux or is_dragonfly) { |
| 215 | | batch.add(&async self.findNativeCrtDirPosix(allocator)); |
| 222 | batch.add(&async self.findNativeCrtDirPosix(args)); |
| 216 | 223 | } |
| 217 | 224 | break :blk batch.wait(); |
| 218 | 225 | }; |
| ... | ... | @@ -231,7 +238,8 @@ pub const LibCInstallation = struct { |
| 231 | 238 | self.* = undefined; |
| 232 | 239 | } |
| 233 | 240 | |
| 234 | | fn findNativeIncludeDirPosix(self: *LibCInstallation, allocator: *Allocator) FindError!void { |
| 241 | fn findNativeIncludeDirPosix(self: *LibCInstallation, args: FindNativeOptions) FindError!void { |
| 242 | const allocator = args.allocator; |
| 235 | 243 | const dev_null = if (is_windows) "nul" else "/dev/null"; |
| 236 | 244 | const cc_exe = std.os.getenvZ("CC") orelse default_cc_exe; |
| 237 | 245 | const argv = [_][]const u8{ |
| ... | ... | @@ -252,15 +260,24 @@ pub const LibCInstallation = struct { |
| 252 | 260 | .expand_arg0 = .expand, |
| 253 | 261 | }) catch |err| switch (err) { |
| 254 | 262 | error.OutOfMemory => return error.OutOfMemory, |
| 255 | | else => return error.UnableToSpawnCCompiler, |
| 263 | else => { |
| 264 | printVerboseInvocation(&argv, null, args.verbose, null); |
| 265 | return error.UnableToSpawnCCompiler; |
| 266 | }, |
| 256 | 267 | }; |
| 257 | 268 | defer { |
| 258 | 269 | allocator.free(exec_res.stdout); |
| 259 | 270 | allocator.free(exec_res.stderr); |
| 260 | 271 | } |
| 261 | 272 | switch (exec_res.term) { |
| 262 | | .Exited => |code| if (code != 0) return error.CCompilerExitCode, |
| 263 | | else => return error.CCompilerCrashed, |
| 273 | .Exited => |code| if (code != 0) { |
| 274 | printVerboseInvocation(&argv, null, args.verbose, exec_res.stderr); |
| 275 | return error.CCompilerExitCode; |
| 276 | }, |
| 277 | else => { |
| 278 | printVerboseInvocation(&argv, null, args.verbose, exec_res.stderr); |
| 279 | return error.CCompilerCrashed; |
| 280 | }, |
| 264 | 281 | } |
| 265 | 282 | |
| 266 | 283 | var it = std.mem.tokenize(exec_res.stderr, "\n\r"); |
| ... | ... | @@ -322,9 +339,11 @@ pub const LibCInstallation = struct { |
| 322 | 339 | |
| 323 | 340 | fn findNativeIncludeDirWindows( |
| 324 | 341 | self: *LibCInstallation, |
| 325 | | allocator: *Allocator, |
| 342 | args: FindNativeOptions, |
| 326 | 343 | sdk: *ZigWindowsSDK, |
| 327 | 344 | ) FindError!void { |
| 345 | const allocator = args.allocator; |
| 346 | |
| 328 | 347 | var search_buf: [2]Search = undefined; |
| 329 | 348 | const searches = fillSearch(&search_buf, sdk); |
| 330 | 349 | |
| ... | ... | @@ -358,7 +377,13 @@ pub const LibCInstallation = struct { |
| 358 | 377 | return error.LibCStdLibHeaderNotFound; |
| 359 | 378 | } |
| 360 | 379 | |
| 361 | | fn findNativeCrtDirWindows(self: *LibCInstallation, allocator: *Allocator, sdk: *ZigWindowsSDK) FindError!void { |
| 380 | fn findNativeCrtDirWindows( |
| 381 | self: *LibCInstallation, |
| 382 | args: FindNativeOptions, |
| 383 | sdk: *ZigWindowsSDK, |
| 384 | ) FindError!void { |
| 385 | const allocator = args.allocator; |
| 386 | |
| 362 | 387 | var search_buf: [2]Search = undefined; |
| 363 | 388 | const searches = fillSearch(&search_buf, sdk); |
| 364 | 389 | |
| ... | ... | @@ -398,15 +423,31 @@ pub const LibCInstallation = struct { |
| 398 | 423 | return error.LibCRuntimeNotFound; |
| 399 | 424 | } |
| 400 | 425 | |
| 401 | | fn findNativeCrtDirPosix(self: *LibCInstallation, allocator: *Allocator) FindError!void { |
| 402 | | self.crt_dir = try ccPrintFileName(allocator, "crt1.o", .only_dir); |
| 426 | fn findNativeCrtDirPosix(self: *LibCInstallation, args: FindNativeOptions) FindError!void { |
| 427 | self.crt_dir = try ccPrintFileName(.{ |
| 428 | .allocator = args.allocator, |
| 429 | .search_basename = "crt1.o", |
| 430 | .want_dirname = .only_dir, |
| 431 | .verbose = args.verbose, |
| 432 | }); |
| 403 | 433 | } |
| 404 | 434 | |
| 405 | | fn findNativeStaticCrtDirPosix(self: *LibCInstallation, allocator: *Allocator) FindError!void { |
| 406 | | self.static_crt_dir = try ccPrintFileName(allocator, "crtbegin.o", .only_dir); |
| 435 | fn findNativeStaticCrtDirPosix(self: *LibCInstallation, args: FindNativeOptions) FindError!void { |
| 436 | self.static_crt_dir = try ccPrintFileName(.{ |
| 437 | .allocator = args.allocator, |
| 438 | .search_basename = "crtbegin.o", |
| 439 | .want_dirname = .only_dir, |
| 440 | .verbose = args.verbose, |
| 441 | }); |
| 407 | 442 | } |
| 408 | 443 | |
| 409 | | fn findNativeKernel32LibDir(self: *LibCInstallation, allocator: *Allocator, sdk: *ZigWindowsSDK) FindError!void { |
| 444 | fn findNativeKernel32LibDir( |
| 445 | self: *LibCInstallation, |
| 446 | args: FindNativeOptions, |
| 447 | sdk: *ZigWindowsSDK, |
| 448 | ) FindError!void { |
| 449 | const allocator = args.allocator; |
| 450 | |
| 410 | 451 | var search_buf: [2]Search = undefined; |
| 411 | 452 | const searches = fillSearch(&search_buf, sdk); |
| 412 | 453 | |
| ... | ... | @@ -448,9 +489,11 @@ pub const LibCInstallation = struct { |
| 448 | 489 | |
| 449 | 490 | fn findNativeMsvcIncludeDir( |
| 450 | 491 | self: *LibCInstallation, |
| 451 | | allocator: *Allocator, |
| 492 | args: FindNativeOptions, |
| 452 | 493 | sdk: *ZigWindowsSDK, |
| 453 | 494 | ) FindError!void { |
| 495 | const allocator = args.allocator; |
| 496 | |
| 454 | 497 | const msvc_lib_dir_ptr = sdk.msvc_lib_dir_ptr orelse return error.LibCStdLibHeaderNotFound; |
| 455 | 498 | const msvc_lib_dir = msvc_lib_dir_ptr[0..sdk.msvc_lib_dir_len]; |
| 456 | 499 | const up1 = fs.path.dirname(msvc_lib_dir) orelse return error.LibCStdLibHeaderNotFound; |
| ... | ... | @@ -481,9 +524,10 @@ pub const LibCInstallation = struct { |
| 481 | 524 | |
| 482 | 525 | fn findNativeMsvcLibDir( |
| 483 | 526 | self: *LibCInstallation, |
| 484 | | allocator: *Allocator, |
| 527 | args: FindNativeOptions, |
| 485 | 528 | sdk: *ZigWindowsSDK, |
| 486 | 529 | ) FindError!void { |
| 530 | const allocator = args.allocator; |
| 487 | 531 | const msvc_lib_dir_ptr = sdk.msvc_lib_dir_ptr orelse return error.LibCRuntimeNotFound; |
| 488 | 532 | self.msvc_lib_dir = try std.mem.dupeZ(allocator, u8, msvc_lib_dir_ptr[0..sdk.msvc_lib_dir_len]); |
| 489 | 533 | } |
| ... | ... | @@ -491,14 +535,19 @@ pub const LibCInstallation = struct { |
| 491 | 535 | |
| 492 | 536 | const default_cc_exe = if (is_windows) "cc.exe" else "cc"; |
| 493 | 537 | |
| 494 | | /// caller owns returned memory |
| 495 | | fn ccPrintFileName( |
| 538 | pub const CCPrintFileNameOptions = struct { |
| 496 | 539 | allocator: *Allocator, |
| 497 | | o_file: []const u8, |
| 540 | search_basename: []const u8, |
| 498 | 541 | want_dirname: enum { full_path, only_dir }, |
| 499 | | ) ![:0]u8 { |
| 542 | verbose: bool = false, |
| 543 | }; |
| 544 | |
| 545 | /// caller owns returned memory |
| 546 | fn ccPrintFileName(args: CCPrintFileNameOptions) ![:0]u8 { |
| 547 | const allocator = args.allocator; |
| 548 | |
| 500 | 549 | const cc_exe = std.os.getenvZ("CC") orelse default_cc_exe; |
| 501 | | const arg1 = try std.fmt.allocPrint(allocator, "-print-file-name={}", .{o_file}); |
| 550 | const arg1 = try std.fmt.allocPrint(allocator, "-print-file-name={}", .{args.search_basename}); |
| 502 | 551 | defer allocator.free(arg1); |
| 503 | 552 | const argv = [_][]const u8{ cc_exe, arg1 }; |
| 504 | 553 | |
| ... | ... | @@ -520,16 +569,22 @@ fn ccPrintFileName( |
| 520 | 569 | allocator.free(exec_res.stderr); |
| 521 | 570 | } |
| 522 | 571 | switch (exec_res.term) { |
| 523 | | .Exited => |code| if (code != 0) return error.CCompilerExitCode, |
| 524 | | else => return error.CCompilerCrashed, |
| 572 | .Exited => |code| if (code != 0) { |
| 573 | printVerboseInvocation(&argv, args.search_basename, args.verbose, exec_res.stderr); |
| 574 | return error.CCompilerExitCode; |
| 575 | }, |
| 576 | else => { |
| 577 | printVerboseInvocation(&argv, args.search_basename, args.verbose, exec_res.stderr); |
| 578 | return error.CCompilerCrashed; |
| 579 | }, |
| 525 | 580 | } |
| 526 | 581 | |
| 527 | 582 | var it = std.mem.tokenize(exec_res.stdout, "\n\r"); |
| 528 | 583 | const line = it.next() orelse return error.LibCRuntimeNotFound; |
| 529 | 584 | // When this command fails, it returns exit code 0 and duplicates the input file name. |
| 530 | 585 | // So we detect failure by checking if the output matches exactly the input. |
| 531 | | if (std.mem.eql(u8, line, o_file)) return error.LibCRuntimeNotFound; |
| 532 | | switch (want_dirname) { |
| 586 | if (std.mem.eql(u8, line, args.search_basename)) return error.LibCRuntimeNotFound; |
| 587 | switch (args.want_dirname) { |
| 533 | 588 | .full_path => return std.mem.dupeZ(allocator, u8, line), |
| 534 | 589 | .only_dir => { |
| 535 | 590 | const dirname = fs.path.dirname(line) orelse return error.LibCRuntimeNotFound; |
| ... | ... | @@ -538,6 +593,29 @@ fn ccPrintFileName( |
| 538 | 593 | } |
| 539 | 594 | } |
| 540 | 595 | |
| 596 | fn printVerboseInvocation( |
| 597 | argv: []const []const u8, |
| 598 | search_basename: ?[]const u8, |
| 599 | verbose: bool, |
| 600 | stderr: ?[]const u8, |
| 601 | ) void { |
| 602 | if (!verbose) return; |
| 603 | |
| 604 | if (search_basename) |s| { |
| 605 | std.debug.warn("Zig attempted to find the file '{}' by executing this command:\n", .{s}); |
| 606 | } else { |
| 607 | std.debug.warn("Zig attempted to find the path to native system libc headers by executing this command:\n", .{}); |
| 608 | } |
| 609 | for (argv) |arg, i| { |
| 610 | if (i != 0) std.debug.warn(" ", .{}); |
| 611 | std.debug.warn("{}", .{arg}); |
| 612 | } |
| 613 | std.debug.warn("\n", .{}); |
| 614 | if (stderr) |s| { |
| 615 | std.debug.warn("Output:\n==========\n{}\n==========\n", .{s}); |
| 616 | } |
| 617 | } |
| 618 | |
| 541 | 619 | /// Caller owns returned memory. |
| 542 | 620 | pub fn detectNativeDynamicLinker(allocator: *Allocator) error{ |
| 543 | 621 | OutOfMemory, |
| ... | ... | @@ -617,7 +695,11 @@ pub fn detectNativeDynamicLinker(allocator: *Allocator) error{ |
| 617 | 695 | for (ld_info_list.toSlice()) |ld_info| { |
| 618 | 696 | const standard_ld_basename = fs.path.basename(ld_info.ld_path); |
| 619 | 697 | |
| 620 | | const full_ld_path = ccPrintFileName(allocator, standard_ld_basename, .full_path) catch |err| switch (err) { |
| 698 | const full_ld_path = ccPrintFileName(.{ |
| 699 | .allocator = allocator, |
| 700 | .search_basename = standard_ld_basename, |
| 701 | .want_dirname = .full_path, |
| 702 | }) catch |err| switch (err) { |
| 621 | 703 | error.OutOfMemory => return error.OutOfMemory, |
| 622 | 704 | error.LibCRuntimeNotFound, |
| 623 | 705 | error.CCompilerExitCode, |