Skip to content

Commit b97a68c

Browse files
committed
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)
1 parent 0471eea commit b97a68c

5 files changed

Lines changed: 32 additions & 8 deletions

File tree

build.zig

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub fn build(b: *Builder) !void {
9999
const enable_macos_sdk = b.option(bool, "enable-macos-sdk", "Run tests requiring presence of macOS SDK and frameworks") orelse false;
100100
const enable_symlinks_windows = b.option(bool, "enable-symlinks-windows", "Run tests requiring presence of symlinks on Windows") orelse false;
101101
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

103104
if (!skip_install_lib_files) {
104105
b.installDirectory(InstallDirectoryOptions{
@@ -277,8 +278,8 @@ pub fn build(b: *Builder) !void {
277278
try addCmakeCfgOptionsToExe(b, cfg, test_cases, use_zig_libcxx);
278279
} else {
279280
// Here we are -Denable-llvm but no cmake integration.
280-
try addStaticLlvmOptionsToExe(exe);
281-
try addStaticLlvmOptionsToExe(test_cases);
281+
try addStaticLlvmOptionsToExe(exe, !disable_zstd);
282+
try addStaticLlvmOptionsToExe(test_cases, !disable_zstd);
282283
}
283284
if (target.isWindows()) {
284285
inline for (.{ exe, test_cases }) |artifact| {
@@ -606,7 +607,7 @@ fn addCmakeCfgOptionsToExe(
606607
}
607608
}
608609

609-
fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep) !void {
610+
fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep, link_zstd: bool) !void {
610611
// Adds the Zig C++ sources which both stage1 and stage2 need.
611612
//
612613
// We need this because otherwise zig_clang_cc1_main.cpp ends up pulling
@@ -629,10 +630,11 @@ fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep) !void {
629630

630631
exe.linkSystemLibrary("z");
631632

632-
if (exe.target.getOs().tag != .windows and exe.target.getAbi() != .msvc) {
633-
// TODO: Support this on msvc
633+
if (link_zstd) {
634634
exe.linkSystemLibrary("zstd");
635+
}
635636

637+
if (exe.target.getOs().tag != .windows or exe.target.getAbi() != .msvc) {
636638
// This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.
637639
exe.linkSystemLibrary("c++");
638640
}
@@ -704,7 +706,7 @@ fn addCMakeSystemLibraryList(exe: *std.build.LibExeObjStep, list: []const u8) vo
704706
end_offset = ".lib".len;
705707
}
706708

707-
exe.linkSystemLibrary(lib[start_offset..lib.len - end_offset]);
709+
exe.linkSystemLibrary(lib[start_offset .. lib.len - end_offset]);
708710
}
709711
}
710712

src/Compilation.zig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ pub const InitOptions = struct {
984984
linker_dynamicbase: bool = false,
985985
linker_optimization: ?u8 = null,
986986
linker_compress_debug_sections: ?link.CompressDebugSections = null,
987+
linker_module_definition_file: ?[]const u8 = null,
987988
major_subsystem_version: ?u32 = null,
988989
minor_subsystem_version: ?u32 = null,
989990
clang_passthrough_mode: bool = false,
@@ -1815,6 +1816,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
18151816
.allow_shlib_undefined = options.linker_allow_shlib_undefined,
18161817
.bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false,
18171818
.compress_debug_sections = options.linker_compress_debug_sections orelse .none,
1819+
.module_definition_file = options.linker_module_definition_file,
18181820
.import_memory = options.linker_import_memory orelse false,
18191821
.import_symbols = options.linker_import_symbols,
18201822
.import_table = options.linker_import_table,
@@ -4379,7 +4381,7 @@ pub fn addCCArgs(
43794381
try argv.append("-fno-unwind-tables");
43804382
}
43814383
},
4382-
.shared_library, .ll, .bc, .unknown, .static_library, .object, .zig => {},
4384+
.shared_library, .ll, .bc, .unknown, .static_library, .object, .def, .zig => {},
43834385
.assembly => {
43844386
// The Clang assembler does not accept the list of CPU features like the
43854387
// compiler frontend does. Therefore we must hard-code the -m flags for
@@ -4524,6 +4526,7 @@ pub const FileExt = enum {
45244526
object,
45254527
static_library,
45264528
zig,
4529+
def,
45274530
unknown,
45284531

45294532
pub fn clangSupportsDepFile(ext: FileExt) bool {
@@ -4537,6 +4540,7 @@ pub const FileExt = enum {
45374540
.object,
45384541
.static_library,
45394542
.zig,
4543+
.def,
45404544
.unknown,
45414545
=> false,
45424546
};
@@ -4629,6 +4633,8 @@ pub fn classifyFileExt(filename: []const u8) FileExt {
46294633
return .object;
46304634
} else if (mem.endsWith(u8, filename, ".cu")) {
46314635
return .cu;
4636+
} else if (mem.endsWith(u8, filename, ".def")) {
4637+
return .def;
46324638
} else {
46334639
return .unknown;
46344640
}

src/link.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ pub const Options = struct {
219219
/// (Darwin) remove dylibs that are unreachable by the entry point or exported symbols
220220
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+
222229
pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode {
223230
return if (options.use_lld) .Obj else options.output_mode;
224231
}

src/link/Coff/lld.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
260260
try argv.append(p);
261261
}
262262

263+
if (self.base.options.module_definition_file) |def| {
264+
try argv.append(try allocPrint(arena, "-DEF:{s}", .{ def }));
265+
}
266+
263267
const resolved_subsystem: ?std.Target.SubSystem = blk: {
264268
if (self.base.options.subsystem) |explicit| break :blk explicit;
265269
switch (target.os.tag) {

src/main.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ fn buildOutputType(
742742
var linker_nxcompat = false;
743743
var linker_dynamicbase = false;
744744
var linker_optimization: ?u8 = null;
745+
var linker_module_definition_file: ?[]const u8 = null;
745746
var test_evented_io = false;
746747
var test_no_exec = false;
747748
var entry: ?[]const u8 = null;
@@ -1404,7 +1405,7 @@ fn buildOutputType(
14041405
root_src_file = arg;
14051406
}
14061407
},
1407-
.unknown => {
1408+
.def, .unknown => {
14081409
fatal("unrecognized file extension of parameter '{s}'", .{arg});
14091410
},
14101411
}
@@ -1478,6 +1479,9 @@ fn buildOutputType(
14781479
.must_link = must_link,
14791480
});
14801481
},
1482+
.def => {
1483+
linker_module_definition_file = it.only_arg;
1484+
},
14811485
.zig => {
14821486
if (root_src_file) |other| {
14831487
fatal("found another zig file '{s}' after root source file '{s}'", .{ it.only_arg, other });
@@ -3015,6 +3019,7 @@ fn buildOutputType(
30153019
.linker_dynamicbase = linker_dynamicbase,
30163020
.linker_optimization = linker_optimization,
30173021
.linker_compress_debug_sections = linker_compress_debug_sections,
3022+
.linker_module_definition_file = linker_module_definition_file,
30183023
.major_subsystem_version = major_subsystem_version,
30193024
.minor_subsystem_version = minor_subsystem_version,
30203025
.link_eh_frame_hdr = link_eh_frame_hdr,

0 commit comments

Comments
 (0)