From 0471eea0e2cac49946449efe4698d5ac18c0dc0d Mon Sep 17 00:00:00 2001 From: kcbanner Date: Tue, 8 Nov 2022 02:14:39 -0500 Subject: build: first pass on geting stage3 building under x64_64-windows-msvc --- src/Compilation.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Compilation.zig') diff --git a/src/Compilation.zig b/src/Compilation.zig index e365d1dab2..eeecf58751 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -3297,8 +3297,8 @@ fn processOneJob(comp: *Compilation, job: Job) !void { // TODO Surface more error details. comp.lockAndSetMiscFailure( .windows_import_lib, - "unable to generate DLL import .lib file: {s}", - .{@errorName(err)}, + "unable to generate DLL import .lib file for {s}: {s}", + .{link_lib, @errorName(err)}, ); }; }, -- cgit v1.2.3 From b97a68c529b5db15705f4d542d8ead616d27c880 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Wed, 9 Nov 2022 00:50:29 -0500 Subject: 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) --- build.zig | 14 ++++++++------ src/Compilation.zig | 8 +++++++- src/link.zig | 7 +++++++ src/link/Coff/lld.zig | 4 ++++ src/main.zig | 7 ++++++- 5 files changed, 32 insertions(+), 8 deletions(-) (limited to 'src/Compilation.zig') diff --git a/build.zig b/build.zig index b33596ae52..1c814e203a 100644 --- a/build.zig +++ b/build.zig @@ -99,6 +99,7 @@ pub fn build(b: *Builder) !void { const enable_macos_sdk = b.option(bool, "enable-macos-sdk", "Run tests requiring presence of macOS SDK and frameworks") orelse false; const enable_symlinks_windows = b.option(bool, "enable-symlinks-windows", "Run tests requiring presence of symlinks on Windows") orelse false; const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h"); + const disable_zstd = b.option(bool, "disable-zstd", "Skip linking zstd") orelse false; if (!skip_install_lib_files) { b.installDirectory(InstallDirectoryOptions{ @@ -277,8 +278,8 @@ pub fn build(b: *Builder) !void { try addCmakeCfgOptionsToExe(b, cfg, test_cases, use_zig_libcxx); } else { // Here we are -Denable-llvm but no cmake integration. - try addStaticLlvmOptionsToExe(exe); - try addStaticLlvmOptionsToExe(test_cases); + try addStaticLlvmOptionsToExe(exe, !disable_zstd); + try addStaticLlvmOptionsToExe(test_cases, !disable_zstd); } if (target.isWindows()) { inline for (.{ exe, test_cases }) |artifact| { @@ -606,7 +607,7 @@ fn addCmakeCfgOptionsToExe( } } -fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep) !void { +fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep, link_zstd: bool) !void { // Adds the Zig C++ sources which both stage1 and stage2 need. // // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling @@ -629,10 +630,11 @@ fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep) !void { exe.linkSystemLibrary("z"); - if (exe.target.getOs().tag != .windows and exe.target.getAbi() != .msvc) { - // TODO: Support this on msvc + if (link_zstd) { exe.linkSystemLibrary("zstd"); + } + if (exe.target.getOs().tag != .windows or exe.target.getAbi() != .msvc) { // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries. exe.linkSystemLibrary("c++"); } @@ -704,7 +706,7 @@ fn addCMakeSystemLibraryList(exe: *std.build.LibExeObjStep, list: []const u8) vo end_offset = ".lib".len; } - exe.linkSystemLibrary(lib[start_offset..lib.len - end_offset]); + exe.linkSystemLibrary(lib[start_offset .. lib.len - end_offset]); } } diff --git a/src/Compilation.zig b/src/Compilation.zig index eeecf58751..98eb15c437 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -984,6 +984,7 @@ pub const InitOptions = struct { linker_dynamicbase: bool = false, linker_optimization: ?u8 = null, linker_compress_debug_sections: ?link.CompressDebugSections = null, + linker_module_definition_file: ?[]const u8 = null, major_subsystem_version: ?u32 = null, minor_subsystem_version: ?u32 = null, clang_passthrough_mode: bool = false, @@ -1815,6 +1816,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { .allow_shlib_undefined = options.linker_allow_shlib_undefined, .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false, .compress_debug_sections = options.linker_compress_debug_sections orelse .none, + .module_definition_file = options.linker_module_definition_file, .import_memory = options.linker_import_memory orelse false, .import_symbols = options.linker_import_symbols, .import_table = options.linker_import_table, @@ -4379,7 +4381,7 @@ pub fn addCCArgs( try argv.append("-fno-unwind-tables"); } }, - .shared_library, .ll, .bc, .unknown, .static_library, .object, .zig => {}, + .shared_library, .ll, .bc, .unknown, .static_library, .object, .def, .zig => {}, .assembly => { // The Clang assembler does not accept the list of CPU features like the // compiler frontend does. Therefore we must hard-code the -m flags for @@ -4524,6 +4526,7 @@ pub const FileExt = enum { object, static_library, zig, + def, unknown, pub fn clangSupportsDepFile(ext: FileExt) bool { @@ -4537,6 +4540,7 @@ pub const FileExt = enum { .object, .static_library, .zig, + .def, .unknown, => false, }; @@ -4629,6 +4633,8 @@ pub fn classifyFileExt(filename: []const u8) FileExt { return .object; } else if (mem.endsWith(u8, filename, ".cu")) { return .cu; + } else if (mem.endsWith(u8, filename, ".def")) { + return .def; } else { return .unknown; } diff --git a/src/link.zig b/src/link.zig index 0a526db0de..410d1232af 100644 --- a/src/link.zig +++ b/src/link.zig @@ -219,6 +219,13 @@ pub const Options = struct { /// (Darwin) remove dylibs that are unreachable by the entry point or exported symbols dead_strip_dylibs: bool = false, + /// (Windows) PDB source path prefix to instruct the linker how to resolve relative + /// paths when consolidating CodeView streams into a single PDB file. + pdb_source_path: ?[]const u8 = null, + + /// (Windows) .def file to specify when linking + module_definition_file: ?[] const u8 = null, + pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode { return if (options.use_lld) .Obj else options.output_mode; } diff --git a/src/link/Coff/lld.zig b/src/link/Coff/lld.zig index dfa56527bf..367dde3e88 100644 --- a/src/link/Coff/lld.zig +++ b/src/link/Coff/lld.zig @@ -260,6 +260,10 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod try argv.append(p); } + if (self.base.options.module_definition_file) |def| { + try argv.append(try allocPrint(arena, "-DEF:{s}", .{ def })); + } + const resolved_subsystem: ?std.Target.SubSystem = blk: { if (self.base.options.subsystem) |explicit| break :blk explicit; switch (target.os.tag) { diff --git a/src/main.zig b/src/main.zig index 421164de1c..d718f299c7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -742,6 +742,7 @@ fn buildOutputType( var linker_nxcompat = false; var linker_dynamicbase = false; var linker_optimization: ?u8 = null; + var linker_module_definition_file: ?[]const u8 = null; var test_evented_io = false; var test_no_exec = false; var entry: ?[]const u8 = null; @@ -1404,7 +1405,7 @@ fn buildOutputType( root_src_file = arg; } }, - .unknown => { + .def, .unknown => { fatal("unrecognized file extension of parameter '{s}'", .{arg}); }, } @@ -1478,6 +1479,9 @@ fn buildOutputType( .must_link = must_link, }); }, + .def => { + linker_module_definition_file = it.only_arg; + }, .zig => { if (root_src_file) |other| { fatal("found another zig file '{s}' after root source file '{s}'", .{ it.only_arg, other }); @@ -3015,6 +3019,7 @@ fn buildOutputType( .linker_dynamicbase = linker_dynamicbase, .linker_optimization = linker_optimization, .linker_compress_debug_sections = linker_compress_debug_sections, + .linker_module_definition_file = linker_module_definition_file, .major_subsystem_version = major_subsystem_version, .minor_subsystem_version = minor_subsystem_version, .link_eh_frame_hdr = link_eh_frame_hdr, -- cgit v1.2.3 From a03c8ef4bf526888518d13de1794c28375900cc2 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Fri, 11 Nov 2022 13:31:10 -0500 Subject: fixup formatting --- src/Compilation.zig | 2 +- src/link.zig | 2 +- src/link/Coff/lld.zig | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/Compilation.zig') diff --git a/src/Compilation.zig b/src/Compilation.zig index 98eb15c437..aa352fb9ab 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -3300,7 +3300,7 @@ fn processOneJob(comp: *Compilation, job: Job) !void { comp.lockAndSetMiscFailure( .windows_import_lib, "unable to generate DLL import .lib file for {s}: {s}", - .{link_lib, @errorName(err)}, + .{ link_lib, @errorName(err) }, ); }; }, diff --git a/src/link.zig b/src/link.zig index 410d1232af..ee9efd83ed 100644 --- a/src/link.zig +++ b/src/link.zig @@ -224,7 +224,7 @@ pub const Options = struct { pdb_source_path: ?[]const u8 = null, /// (Windows) .def file to specify when linking - module_definition_file: ?[] const u8 = null, + module_definition_file: ?[]const u8 = null, pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode { return if (options.use_lld) .Obj else options.output_mode; diff --git a/src/link/Coff/lld.zig b/src/link/Coff/lld.zig index cc89c8d344..be6c90d11d 100644 --- a/src/link/Coff/lld.zig +++ b/src/link/Coff/lld.zig @@ -261,7 +261,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod } if (self.base.options.module_definition_file) |def| { - try argv.append(try allocPrint(arena, "-DEF:{s}", .{ def })); + try argv.append(try allocPrint(arena, "-DEF:{s}", .{def})); } const resolved_subsystem: ?std.Target.SubSystem = blk: { -- cgit v1.2.3 From b42442f5b4913938b04f580b1d13a1fd7318514d Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sun, 13 Nov 2022 03:42:51 -0500 Subject: windows: fixes to support using zig cc/c++ with CMake on Windows Using zig cc with CMake on Windows was failing during compiler detection. -nostdinc was causing the crt not to be linked, and Coff/lld.zig assumed that wWinMainCRTStartup would be present in this case. -nostdlib did not prevent the default behaviour of linking libc++ when zig c++ was used. This caused libc++ to be built when CMake ran ABI detection using zig c++, which fails as libcxxabi cannot compile under MSVC. - Change the behaviour of COFF -nostdinc to set /entry to the function that the default CRT method for the specified subsystem would have called. - Fix -ENTRY being passed twice if it was specified explicitly and -nostdlib was present. - Add support for /pdb, /version, /implib, and /subsystem as linker args (passed by CMake) - Remove -Ddisable-zstd, no longer needed - Add -Ddisable-libcpp for use when bootstrapping on msvc --- CMakeLists.txt | 1 + build.zig | 13 +++++-------- src/Compilation.zig | 7 +++++++ src/link.zig | 3 +++ src/link/Coff/lld.zig | 35 ++++++++++++++++++++++++++++++++--- src/main.zig | 25 ++++++++++++++++++++++++- 6 files changed, 72 insertions(+), 12 deletions(-) (limited to 'src/Compilation.zig') diff --git a/CMakeLists.txt b/CMakeLists.txt index 7340572bdc..a92878781b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,6 +92,7 @@ set(ZIG_SHARED_LLVM off CACHE BOOL "Prefer linking against shared LLVM libraries set(ZIG_STATIC_LLVM off CACHE BOOL "Prefer linking against static LLVM libraries") set(ZIG_STATIC_ZLIB off CACHE BOOL "Prefer linking against static zlib") set(ZIG_ENABLE_ZSTD on CACHE BOOL "Enable linking zstd") +set(ZIG_ENABLE_LIBCPP on CACHE BOOL "Enable linking libcpp") set(ZIG_STATIC_ZSTD off CACHE BOOL "Prefer linking against static zstd") set(ZIG_USE_CCACHE off CACHE BOOL "Use ccache") diff --git a/build.zig b/build.zig index 1c814e203a..6eba66db42 100644 --- a/build.zig +++ b/build.zig @@ -99,7 +99,7 @@ pub fn build(b: *Builder) !void { const enable_macos_sdk = b.option(bool, "enable-macos-sdk", "Run tests requiring presence of macOS SDK and frameworks") orelse false; const enable_symlinks_windows = b.option(bool, "enable-symlinks-windows", "Run tests requiring presence of symlinks on Windows") orelse false; const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h"); - const disable_zstd = b.option(bool, "disable-zstd", "Skip linking zstd") orelse false; + const disable_libcpp = b.option(bool, "disable-libcpp", "Skip building/linking libcpp") orelse false; if (!skip_install_lib_files) { b.installDirectory(InstallDirectoryOptions{ @@ -278,8 +278,8 @@ pub fn build(b: *Builder) !void { try addCmakeCfgOptionsToExe(b, cfg, test_cases, use_zig_libcxx); } else { // Here we are -Denable-llvm but no cmake integration. - try addStaticLlvmOptionsToExe(exe, !disable_zstd); - try addStaticLlvmOptionsToExe(test_cases, !disable_zstd); + try addStaticLlvmOptionsToExe(exe); + try addStaticLlvmOptionsToExe(test_cases); } if (target.isWindows()) { inline for (.{ exe, test_cases }) |artifact| { @@ -607,7 +607,7 @@ fn addCmakeCfgOptionsToExe( } } -fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep, link_zstd: bool) !void { +fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep) !void { // Adds the Zig C++ sources which both stage1 and stage2 need. // // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling @@ -629,10 +629,7 @@ fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep, link_zstd: bool) !vo } exe.linkSystemLibrary("z"); - - if (link_zstd) { - exe.linkSystemLibrary("zstd"); - } + exe.linkSystemLibrary("zstd"); if (exe.target.getOs().tag != .windows or exe.target.getAbi() != .msvc) { // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries. diff --git a/src/Compilation.zig b/src/Compilation.zig index aa352fb9ab..5d0a9308af 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1042,6 +1042,11 @@ pub const InitOptions = struct { /// (Darwin) remove dylibs that are unreachable by the entry point or exported symbols dead_strip_dylibs: bool = false, libcxx_abi_version: libcxx.AbiVersion = libcxx.AbiVersion.default, + /// (Windows) PDB source path prefix to instruct the linker how to resolve relative + /// paths when consolidating CodeView streams into a single PDB file. + pdb_source_path: ?[]const u8 = null, + /// (Windows) PDB output path + pdb_out_path: ?[]const u8 = null, }; fn addPackageTableToCacheHash( @@ -1892,6 +1897,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { .headerpad_max_install_names = options.headerpad_max_install_names, .dead_strip_dylibs = options.dead_strip_dylibs, .force_undefined_symbols = .{}, + .pdb_source_path = pdb_source_path, + .pdb_out_path = options.pdb_out_path, }); errdefer bin_file.destroy(); comp.* = .{ diff --git a/src/link.zig b/src/link.zig index ee9efd83ed..222c0e4918 100644 --- a/src/link.zig +++ b/src/link.zig @@ -223,6 +223,9 @@ pub const Options = struct { /// paths when consolidating CodeView streams into a single PDB file. pdb_source_path: ?[]const u8 = null, + /// (Windows) PDB output path + pdb_out_path: ?[]const u8 = null, + /// (Windows) .def file to specify when linking module_definition_file: ?[]const u8 = null, diff --git a/src/link/Coff/lld.zig b/src/link/Coff/lld.zig index be6c90d11d..4f1124115e 100644 --- a/src/link/Coff/lld.zig +++ b/src/link/Coff/lld.zig @@ -166,12 +166,16 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod try argv.append("-DEBUG"); const out_ext = std.fs.path.extension(full_out_path); - const out_pdb = try allocPrint(arena, "{s}.pdb", .{ + const out_pdb = self.base.options.pdb_out_path orelse try allocPrint(arena, "{s}.pdb", .{ full_out_path[0 .. full_out_path.len - out_ext.len], }); + try argv.append(try allocPrint(arena, "-PDB:{s}", .{out_pdb})); try argv.append(try allocPrint(arena, "-PDBALTPATH:{s}", .{out_pdb})); } + if (self.base.options.version) |version| { + try argv.append(try allocPrint(arena, "-VERSION:{}.{}", .{ version.major, version.minor })); + } if (self.base.options.lto) { switch (self.base.options.optimize_mode) { .Debug => {}, @@ -427,7 +431,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod } } else { try argv.append("-NODEFAULTLIB"); - if (!is_lib) { + if (!is_lib and self.base.options.entry == null) { if (self.base.options.module) |module| { if (module.stage1_flags.have_winmain_crt_startup) { try argv.append("-ENTRY:WinMainCRTStartup"); @@ -435,7 +439,32 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod try argv.append("-ENTRY:wWinMainCRTStartup"); } } else { - try argv.append("-ENTRY:wWinMainCRTStartup"); + // If the crt isn't being linked, it won't provide the CRT startup methods that + // call through to the user-provided entrypoint. Instead, choose the entry point + // that the CRT methods would have called. Note that this differs from the behaviour + // of link.exe (which still tries to use the CRT methods in this case), but this + // fixes CMake compiler checks when using zig cc on Windows, as Windows-Clang.cmake + // does not specify /entry:main + + // TODO: I think the correct thing to do in this case would be to inspect the object + // being linked (like link.exe / lld-link does) and detect which symbols are available. + // This would allow detection of the w variants, as well as the crt methods. + if (resolved_subsystem) |subsystem| { + switch (subsystem) { + .Console => { + // The default is to call mainCRTStartup/wmainCRTStartup, which calls main/wmain + try argv.append("-ENTRY:main"); + }, + .Windows => { + // The default is to call WinMainCRTStartup/wWinMainCRTStartup, which calls WinMain/wWinMain + try argv.append("-ENTRY:WinMain"); + }, + else => {} + } + } + + // when no /entry is specified, lld-link will infer it based on which functions + // are present in the object being linked - see lld/COFF/Driver.cpp#LinkerDriver::findDefaultEntry } } } diff --git a/src/main.zig b/src/main.zig index d718f299c7..5e2377637c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -782,6 +782,7 @@ fn buildOutputType( var headerpad_max_install_names: bool = false; var dead_strip_dylibs: bool = false; var reference_trace: ?u32 = null; + var pdb_out_path: ?[]const u8 = null; // e.g. -m3dnow or -mno-outline-atomics. They correspond to std.Target llvm cpu feature names. // This array is populated by zig cc frontend and then has to be converted to zig-style @@ -1541,7 +1542,10 @@ fn buildOutputType( .no_stack_protector => want_stack_protector = 0, .unwind_tables => want_unwind_tables = true, .no_unwind_tables => want_unwind_tables = false, - .nostdlib => ensure_libc_on_non_freestanding = false, + .nostdlib => { + ensure_libc_on_non_freestanding = false; + ensure_libcpp_on_non_freestanding = false; + }, .nostdlib_cpp => ensure_libcpp_on_non_freestanding = false, .shared => { link_mode = .Dynamic; @@ -2120,6 +2124,24 @@ fn buildOutputType( next_arg, }); }; + } else if (mem.startsWith(u8, arg, "/subsystem:")) { + var split_it = mem.splitBackwards(u8, arg, ":"); + subsystem = try parseSubSystem(split_it.first()); + } else if (mem.startsWith(u8, arg, "/implib:")) { + var split_it = mem.splitBackwards(u8, arg, ":"); + emit_implib = .{ .yes = split_it.first() }; + emit_implib_arg_provided = true; + } else if (mem.startsWith(u8, arg, "/pdb:")) { + var split_it = mem.splitBackwards(u8, arg, ":"); + pdb_out_path = split_it.first(); + } else if (mem.startsWith(u8, arg, "/version:")) { + var split_it = mem.splitBackwards(u8, arg, ":"); + const version_arg = split_it.first(); + version = std.builtin.Version.parse(version_arg) catch |err| { + fatal("unable to parse /version '{s}': {s}", .{ arg, @errorName(err) }); + }; + + have_version = true; } else { warn("unsupported linker arg: {s}", .{arg}); } @@ -3069,6 +3091,7 @@ fn buildOutputType( .headerpad_max_install_names = headerpad_max_install_names, .dead_strip_dylibs = dead_strip_dylibs, .reference_trace = reference_trace, + .pdb_out_path = pdb_out_path, }) catch |err| switch (err) { error.LibCUnavailable => { const target = target_info.target; -- cgit v1.2.3 From 9c0a41f88afadc29fa46565da5df8cdda177b497 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Tue, 13 Dec 2022 01:34:13 -0500 Subject: rebase fixup --- src/Compilation.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Compilation.zig') diff --git a/src/Compilation.zig b/src/Compilation.zig index 5d0a9308af..f90b8f28d1 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1897,7 +1897,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { .headerpad_max_install_names = options.headerpad_max_install_names, .dead_strip_dylibs = options.dead_strip_dylibs, .force_undefined_symbols = .{}, - .pdb_source_path = pdb_source_path, + .pdb_source_path = options.pdb_source_path, .pdb_out_path = options.pdb_out_path, }); errdefer bin_file.destroy(); -- cgit v1.2.3