Skip to content

Commit 594d224

Browse files
committed
Allow setting library paths directly for compile steps
`LibraryPath` is implemented by analogy with `RPath`. We need this to add a test case to `test/link/elf.zig` for checking `-l`/`-L` behavior compatibility with Clang/GCC for ELF.
1 parent 087dbab commit 594d224

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

lib/std/Build/Module.zig

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dwarf_format: ?std.dwarf.Format,
1616

1717
c_macros: std.ArrayListUnmanaged([]const u8),
1818
include_dirs: std.ArrayListUnmanaged(IncludeDir),
19-
lib_paths: std.ArrayListUnmanaged(LazyPath),
19+
lib_paths: std.ArrayListUnmanaged(LibraryPath),
2020
rpaths: std.ArrayListUnmanaged(RPath),
2121
frameworks: std.StringArrayHashMapUnmanaged(LinkFrameworkOptions),
2222
link_objects: std.ArrayListUnmanaged(LinkObject),
@@ -41,6 +41,11 @@ link_libcpp: ?bool,
4141
/// Symbols to be exported when compiling to WebAssembly.
4242
export_symbol_names: []const []const u8 = &.{},
4343

44+
pub const LibraryPath = union(enum) {
45+
lazy_path: LazyPath,
46+
special: []const u8,
47+
};
48+
4449
pub const RPath = union(enum) {
4550
lazy_path: LazyPath,
4651
special: []const u8,
@@ -274,7 +279,10 @@ pub fn addImport(m: *Module, name: []const u8, module: *Module) void {
274279
/// dependencies on `m`'s `depending_steps`.
275280
fn addShallowDependencies(m: *Module, dependee: *Module) void {
276281
if (dependee.root_source_file) |lazy_path| addLazyPathDependencies(m, dependee, lazy_path);
277-
for (dependee.lib_paths.items) |lib_path| addLazyPathDependencies(m, dependee, lib_path);
282+
for (dependee.lib_paths.items) |lib_path| switch (lib_path) {
283+
.lazy_path => |lp| addLazyPathDependencies(m, dependee, lp),
284+
.special => {},
285+
};
278286
for (dependee.rpaths.items) |rpath| switch (rpath) {
279287
.lazy_path => |lp| addLazyPathDependencies(m, dependee, lp),
280288
.special => {},
@@ -603,10 +611,15 @@ pub fn addFrameworkPath(m: *Module, directory_path: LazyPath) void {
603611

604612
pub fn addLibraryPath(m: *Module, directory_path: LazyPath) void {
605613
const b = m.owner;
606-
m.lib_paths.append(b.allocator, directory_path.dupe(b)) catch @panic("OOM");
614+
m.lib_paths.append(b.allocator, .{ .lazy_path = directory_path.dupe(b) }) catch @panic("OOM");
607615
addLazyPathDependenciesOnly(m, directory_path);
608616
}
609617

618+
pub fn addLibraryPathSpecial(m: *Module, bytes: []const u8) void {
619+
const b = m.owner;
620+
m.lib_paths.append(b.allocator, .{ .special = b.dupe(bytes) }) catch @panic("OOM");
621+
}
622+
610623
pub fn addRPath(m: *Module, directory_path: LazyPath) void {
611624
const b = m.owner;
612625
m.rpaths.append(b.allocator, .{ .lazy_path = directory_path.dupe(b) }) catch @panic("OOM");
@@ -725,10 +738,16 @@ pub fn appendZigProcessFlags(
725738
try zig_args.appendSlice(m.c_macros.items);
726739

727740
try zig_args.ensureUnusedCapacity(2 * m.lib_paths.items.len);
728-
for (m.lib_paths.items) |lib_path| {
729-
zig_args.appendAssumeCapacity("-L");
730-
zig_args.appendAssumeCapacity(lib_path.getPath2(b, asking_step));
731-
}
741+
for (m.lib_paths.items) |lib_path| switch (lib_path) {
742+
.lazy_path => |lp| {
743+
zig_args.appendAssumeCapacity("-L");
744+
zig_args.appendAssumeCapacity(lp.getPath2(b, asking_step));
745+
},
746+
.special => |bytes| {
747+
zig_args.appendAssumeCapacity("-L");
748+
zig_args.appendAssumeCapacity(bytes);
749+
},
750+
};
732751

733752
try zig_args.ensureUnusedCapacity(2 * m.rpaths.items.len);
734753
for (m.rpaths.items) |rpath| switch (rpath) {

lib/std/Build/Step/Compile.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,10 +924,18 @@ pub fn addLibraryPath(compile: *Compile, directory_path: LazyPath) void {
924924
compile.root_module.addLibraryPath(directory_path);
925925
}
926926

927+
pub fn addLibraryPathSpecial(compile: *Compile, bytes: []const u8) void {
928+
compile.root_module.addLibraryPathSpecial(bytes);
929+
}
930+
927931
pub fn addRPath(compile: *Compile, directory_path: LazyPath) void {
928932
compile.root_module.addRPath(directory_path);
929933
}
930934

935+
pub fn addRPathSpecial(compile: *Compile, bytes: []const u8) void {
936+
compile.root_module.addRPathSpecial(bytes);
937+
}
938+
931939
pub fn addSystemFrameworkPath(compile: *Compile, directory_path: LazyPath) void {
932940
compile.root_module.addSystemFrameworkPath(directory_path);
933941
}

0 commit comments

Comments
 (0)