authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2022-11-09 00:50:29-05:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-01-04 21:45:06-05:00
logb97a68c529b5db15705f4d542d8ead616d27c880
tree4a88814249684640687fbde316fe0ad0bf9256d6
parent0471eea0e2cac49946449efe4698d5ac18c0dc0d

windows: supporting changes for boostrapping via msvc

- add support for passing through .def files to the linker, required for building libLTO.dll in LLVM - fixup libcpp linking conditionals - add option to skip linking zstd for use in bootstrapping (when building against an LLVM with LLVM_ENABLE_ZSTD=OFF)

5 files changed, 32 insertions(+), 8 deletions(-)

build.zig+8-6
...@@ -99,6 +99,7 @@ pub fn build(b: *Builder) !void {...@@ -99,6 +99,7 @@ pub fn build(b: *Builder) !void {
99 const enable_macos_sdk = b.option(bool, "enable-macos-sdk", "Run tests requiring presence of macOS SDK and frameworks") orelse false;99 const enable_macos_sdk = b.option(bool, "enable-macos-sdk", "Run tests requiring presence of macOS SDK and frameworks") orelse false;
100 const enable_symlinks_windows = b.option(bool, "enable-symlinks-windows", "Run tests requiring presence of symlinks on Windows") orelse false;100 const enable_symlinks_windows = b.option(bool, "enable-symlinks-windows", "Run tests requiring presence of symlinks on Windows") orelse false;
101 const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h");101 const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h");
102 const disable_zstd = b.option(bool, "disable-zstd", "Skip linking zstd") orelse false;
102103
103 if (!skip_install_lib_files) {104 if (!skip_install_lib_files) {
104 b.installDirectory(InstallDirectoryOptions{105 b.installDirectory(InstallDirectoryOptions{
...@@ -277,8 +278,8 @@ pub fn build(b: *Builder) !void {...@@ -277,8 +278,8 @@ pub fn build(b: *Builder) !void {
277 try addCmakeCfgOptionsToExe(b, cfg, test_cases, use_zig_libcxx);278 try addCmakeCfgOptionsToExe(b, cfg, test_cases, use_zig_libcxx);
278 } else {279 } else {
279 // Here we are -Denable-llvm but no cmake integration.280 // Here we are -Denable-llvm but no cmake integration.
280 try addStaticLlvmOptionsToExe(exe);281 try addStaticLlvmOptionsToExe(exe, !disable_zstd);
281 try addStaticLlvmOptionsToExe(test_cases);282 try addStaticLlvmOptionsToExe(test_cases, !disable_zstd);
282 }283 }
283 if (target.isWindows()) {284 if (target.isWindows()) {
284 inline for (.{ exe, test_cases }) |artifact| {285 inline for (.{ exe, test_cases }) |artifact| {
...@@ -606,7 +607,7 @@ fn addCmakeCfgOptionsToExe(...@@ -606,7 +607,7 @@ fn addCmakeCfgOptionsToExe(
606 }607 }
607}608}
608609
609fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep) !void {610fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep, link_zstd: bool) !void {
610 // Adds the Zig C++ sources which both stage1 and stage2 need.611 // Adds the Zig C++ sources which both stage1 and stage2 need.
611 //612 //
612 // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling613 // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling
...@@ -629,10 +630,11 @@ fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep) !void {...@@ -629,10 +630,11 @@ fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep) !void {
629630
630 exe.linkSystemLibrary("z");631 exe.linkSystemLibrary("z");
631632
632 if (exe.target.getOs().tag != .windows and exe.target.getAbi() != .msvc) {633 if (link_zstd) {
633 // TODO: Support this on msvc
634 exe.linkSystemLibrary("zstd");634 exe.linkSystemLibrary("zstd");
635 }
635636
637 if (exe.target.getOs().tag != .windows or exe.target.getAbi() != .msvc) {
636 // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.638 // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.
637 exe.linkSystemLibrary("c++");639 exe.linkSystemLibrary("c++");
638 }640 }
...@@ -704,7 +706,7 @@ fn addCMakeSystemLibraryList(exe: *std.build.LibExeObjStep, list: []const u8) vo...@@ -704,7 +706,7 @@ fn addCMakeSystemLibraryList(exe: *std.build.LibExeObjStep, list: []const u8) vo
704 end_offset = ".lib".len;706 end_offset = ".lib".len;
705 }707 }
706708
707 exe.linkSystemLibrary(lib[start_offset..lib.len - end_offset]);709 exe.linkSystemLibrary(lib[start_offset .. lib.len - end_offset]);
708 }710 }
709}711}
710712
src/Compilation.zig+7-1
...@@ -984,6 +984,7 @@ pub const InitOptions = struct {...@@ -984,6 +984,7 @@ pub const InitOptions = struct {
984 linker_dynamicbase: bool = false,984 linker_dynamicbase: bool = false,
985 linker_optimization: ?u8 = null,985 linker_optimization: ?u8 = null,
986 linker_compress_debug_sections: ?link.CompressDebugSections = null,986 linker_compress_debug_sections: ?link.CompressDebugSections = null,
987 linker_module_definition_file: ?[]const u8 = null,
987 major_subsystem_version: ?u32 = null,988 major_subsystem_version: ?u32 = null,
988 minor_subsystem_version: ?u32 = null,989 minor_subsystem_version: ?u32 = null,
989 clang_passthrough_mode: bool = false,990 clang_passthrough_mode: bool = false,
...@@ -1815,6 +1816,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1815,6 +1816,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1815 .allow_shlib_undefined = options.linker_allow_shlib_undefined,1816 .allow_shlib_undefined = options.linker_allow_shlib_undefined,
1816 .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false,1817 .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false,
1817 .compress_debug_sections = options.linker_compress_debug_sections orelse .none,1818 .compress_debug_sections = options.linker_compress_debug_sections orelse .none,
1819 .module_definition_file = options.linker_module_definition_file,
1818 .import_memory = options.linker_import_memory orelse false,1820 .import_memory = options.linker_import_memory orelse false,
1819 .import_symbols = options.linker_import_symbols,1821 .import_symbols = options.linker_import_symbols,
1820 .import_table = options.linker_import_table,1822 .import_table = options.linker_import_table,
...@@ -4379,7 +4381,7 @@ pub fn addCCArgs(...@@ -4379,7 +4381,7 @@ pub fn addCCArgs(
4379 try argv.append("-fno-unwind-tables");4381 try argv.append("-fno-unwind-tables");
4380 }4382 }
4381 },4383 },
4382 .shared_library, .ll, .bc, .unknown, .static_library, .object, .zig => {},4384 .shared_library, .ll, .bc, .unknown, .static_library, .object, .def, .zig => {},
4383 .assembly => {4385 .assembly => {
4384 // The Clang assembler does not accept the list of CPU features like the4386 // The Clang assembler does not accept the list of CPU features like the
4385 // compiler frontend does. Therefore we must hard-code the -m flags for4387 // compiler frontend does. Therefore we must hard-code the -m flags for
...@@ -4524,6 +4526,7 @@ pub const FileExt = enum {...@@ -4524,6 +4526,7 @@ pub const FileExt = enum {
4524 object,4526 object,
4525 static_library,4527 static_library,
4526 zig,4528 zig,
4529 def,
4527 unknown,4530 unknown,
45284531
4529 pub fn clangSupportsDepFile(ext: FileExt) bool {4532 pub fn clangSupportsDepFile(ext: FileExt) bool {
...@@ -4537,6 +4540,7 @@ pub const FileExt = enum {...@@ -4537,6 +4540,7 @@ pub const FileExt = enum {
4537 .object,4540 .object,
4538 .static_library,4541 .static_library,
4539 .zig,4542 .zig,
4543 .def,
4540 .unknown,4544 .unknown,
4541 => false,4545 => false,
4542 };4546 };
...@@ -4629,6 +4633,8 @@ pub fn classifyFileExt(filename: []const u8) FileExt {...@@ -4629,6 +4633,8 @@ pub fn classifyFileExt(filename: []const u8) FileExt {
4629 return .object;4633 return .object;
4630 } else if (mem.endsWith(u8, filename, ".cu")) {4634 } else if (mem.endsWith(u8, filename, ".cu")) {
4631 return .cu;4635 return .cu;
4636 } else if (mem.endsWith(u8, filename, ".def")) {
4637 return .def;
4632 } else {4638 } else {
4633 return .unknown;4639 return .unknown;
4634 }4640 }
src/link.zig+7
...@@ -219,6 +219,13 @@ pub const Options = struct {...@@ -219,6 +219,13 @@ pub const Options = struct {
219 /// (Darwin) remove dylibs that are unreachable by the entry point or exported symbols219 /// (Darwin) remove dylibs that are unreachable by the entry point or exported symbols
220 dead_strip_dylibs: bool = false,220 dead_strip_dylibs: bool = false,
221221
222 /// (Windows) PDB source path prefix to instruct the linker how to resolve relative
223 /// paths when consolidating CodeView streams into a single PDB file.
224 pdb_source_path: ?[]const u8 = null,
225
226 /// (Windows) .def file to specify when linking
227 module_definition_file: ?[] const u8 = null,
228
222 pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode {229 pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode {
223 return if (options.use_lld) .Obj else options.output_mode;230 return if (options.use_lld) .Obj else options.output_mode;
224 }231 }
src/link/Coff/lld.zig+4
...@@ -260,6 +260,10 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod...@@ -260,6 +260,10 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
260 try argv.append(p);260 try argv.append(p);
261 }261 }
262262
263 if (self.base.options.module_definition_file) |def| {
264 try argv.append(try allocPrint(arena, "-DEF:{s}", .{ def }));
265 }
266
263 const resolved_subsystem: ?std.Target.SubSystem = blk: {267 const resolved_subsystem: ?std.Target.SubSystem = blk: {
264 if (self.base.options.subsystem) |explicit| break :blk explicit;268 if (self.base.options.subsystem) |explicit| break :blk explicit;
265 switch (target.os.tag) {269 switch (target.os.tag) {
src/main.zig+6-1
...@@ -742,6 +742,7 @@ fn buildOutputType(...@@ -742,6 +742,7 @@ fn buildOutputType(
742 var linker_nxcompat = false;742 var linker_nxcompat = false;
743 var linker_dynamicbase = false;743 var linker_dynamicbase = false;
744 var linker_optimization: ?u8 = null;744 var linker_optimization: ?u8 = null;
745 var linker_module_definition_file: ?[]const u8 = null;
745 var test_evented_io = false;746 var test_evented_io = false;
746 var test_no_exec = false;747 var test_no_exec = false;
747 var entry: ?[]const u8 = null;748 var entry: ?[]const u8 = null;
...@@ -1404,7 +1405,7 @@ fn buildOutputType(...@@ -1404,7 +1405,7 @@ fn buildOutputType(
1404 root_src_file = arg;1405 root_src_file = arg;
1405 }1406 }
1406 },1407 },
1407 .unknown => {1408 .def, .unknown => {
1408 fatal("unrecognized file extension of parameter '{s}'", .{arg});1409 fatal("unrecognized file extension of parameter '{s}'", .{arg});
1409 },1410 },
1410 }1411 }
...@@ -1478,6 +1479,9 @@ fn buildOutputType(...@@ -1478,6 +1479,9 @@ fn buildOutputType(
1478 .must_link = must_link,1479 .must_link = must_link,
1479 });1480 });
1480 },1481 },
1482 .def => {
1483 linker_module_definition_file = it.only_arg;
1484 },
1481 .zig => {1485 .zig => {
1482 if (root_src_file) |other| {1486 if (root_src_file) |other| {
1483 fatal("found another zig file '{s}' after root source file '{s}'", .{ it.only_arg, other });1487 fatal("found another zig file '{s}' after root source file '{s}'", .{ it.only_arg, other });
...@@ -3015,6 +3019,7 @@ fn buildOutputType(...@@ -3015,6 +3019,7 @@ fn buildOutputType(
3015 .linker_dynamicbase = linker_dynamicbase,3019 .linker_dynamicbase = linker_dynamicbase,
3016 .linker_optimization = linker_optimization,3020 .linker_optimization = linker_optimization,
3017 .linker_compress_debug_sections = linker_compress_debug_sections,3021 .linker_compress_debug_sections = linker_compress_debug_sections,
3022 .linker_module_definition_file = linker_module_definition_file,
3018 .major_subsystem_version = major_subsystem_version,3023 .major_subsystem_version = major_subsystem_version,
3019 .minor_subsystem_version = minor_subsystem_version,3024 .minor_subsystem_version = minor_subsystem_version,
3020 .link_eh_frame_hdr = link_eh_frame_hdr,3025 .link_eh_frame_hdr = link_eh_frame_hdr,