Skip to content

Commit e0a1466

Browse files
committed
build: add --search-prefix option
1 parent 2031989 commit e0a1466

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

build.zig

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ fn dependOnLib(lib_exe_obj: &std.build.LibExeObjStep, dep: &const LibraryDep) {
8080
for (dep.libdirs.toSliceConst()) |lib_dir| {
8181
lib_exe_obj.addLibPath(lib_dir);
8282
}
83-
for (dep.libs.toSliceConst()) |lib| {
83+
for (dep.system_libs.toSliceConst()) |lib| {
8484
lib_exe_obj.linkSystemLibrary(lib);
8585
}
86+
for (dep.libs.toSliceConst()) |lib| {
87+
lib_exe_obj.addObjectFile(lib);
88+
}
8689
for (dep.includes.toSliceConst()) |include_path| {
8790
lib_exe_obj.addIncludeDir(include_path);
8891
}
@@ -91,18 +94,19 @@ fn dependOnLib(lib_exe_obj: &std.build.LibExeObjStep, dep: &const LibraryDep) {
9194
const LibraryDep = struct {
9295
libdirs: ArrayList([]const u8),
9396
libs: ArrayList([]const u8),
97+
system_libs: ArrayList([]const u8),
9498
includes: ArrayList([]const u8),
9599
};
96100

97101
fn findLLVM(b: &Builder) -> ?LibraryDep {
98102
const llvm_config_exe = b.findProgram(
99103
[][]const u8{"llvm-config-5.0", "llvm-config"},
100104
[][]const u8{
101-
"/usr/local/opt/llvm@5/bin",
102-
"/mingw64/bin",
105+
"C:/Libraries/llvm-5.0.0/bin",
103106
"/c/msys64/mingw64/bin",
104107
"c:/msys64/mingw64/bin",
105-
"C:/Libraries/llvm-5.0.0/bin",
108+
"/usr/local/opt/llvm@5/bin",
109+
"/mingw64/bin",
106110
}) %% |err|
107111
{
108112
warn("unable to find llvm-config: {}\n", err);
@@ -114,14 +118,17 @@ fn findLLVM(b: &Builder) -> ?LibraryDep {
114118

115119
var result = LibraryDep {
116120
.libs = ArrayList([]const u8).init(b.allocator),
121+
.system_libs = ArrayList([]const u8).init(b.allocator),
117122
.includes = ArrayList([]const u8).init(b.allocator),
118123
.libdirs = ArrayList([]const u8).init(b.allocator),
119124
};
120125
{
121126
var it = mem.split(libs_output, " \n");
122127
while (it.next()) |lib_arg| {
123128
if (mem.startsWith(u8, lib_arg, "-l")) {
124-
%%result.libs.append(lib_arg[2..]);
129+
%%result.system_libs.append(lib_arg[2..]);
130+
} else {
131+
%%result.libs.append(lib_arg);
125132
}
126133
}
127134
}

std/build.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub const Builder = struct {
4747
env_map: BufMap,
4848
top_level_steps: ArrayList(&TopLevelStep),
4949
prefix: []const u8,
50+
search_prefixes: ArrayList([]const u8),
5051
lib_dir: []const u8,
5152
exe_dir: []const u8,
5253
installed_files: ArrayList([]const u8),
@@ -114,6 +115,7 @@ pub const Builder = struct {
114115
.default_step = undefined,
115116
.env_map = %%os.getEnvMap(allocator),
116117
.prefix = undefined,
118+
.search_prefixes = ArrayList([]const u8).init(allocator),
117119
.lib_dir = undefined,
118120
.exe_dir = undefined,
119121
.installed_files = ArrayList([]const u8).init(allocator),
@@ -671,7 +673,22 @@ pub const Builder = struct {
671673
}
672674

673675
pub fn findProgram(self: &Builder, names: []const []const u8, paths: []const []const u8) -> %[]const u8 {
676+
// TODO report error for ambiguous situations
674677
const exe_extension = (Target { .Native = {}}).exeFileExt();
678+
for (self.search_prefixes.toSliceConst()) |search_prefix| {
679+
for (names) |name| {
680+
if (os.path.isAbsolute(name)) {
681+
return name;
682+
}
683+
const full_path = %return os.path.join(self.allocator, search_prefix, "bin",
684+
self.fmt("{}{}", name, exe_extension));
685+
if (os.path.real(self.allocator, full_path)) |real_path| {
686+
return real_path;
687+
} else |_| {
688+
continue;
689+
}
690+
}
691+
}
675692
if (self.env_map.get("PATH")) |PATH| {
676693
for (names) |name| {
677694
if (os.path.isAbsolute(name)) {
@@ -727,6 +744,10 @@ pub const Builder = struct {
727744
},
728745
}
729746
}
747+
748+
pub fn addSearchPrefix(self: &Builder, search_prefix: []const u8) {
749+
%%self.search_prefixes.append(search_prefix);
750+
}
730751
};
731752

732753
const Version = struct {

std/special/build_runner.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ pub fn main() -> %void {
8484
warn("Expected argument after --prefix\n\n");
8585
return usageAndErr(&builder, false, %return stderr_stream);
8686
});
87+
} else if (mem.eql(u8, arg, "--search-prefix")) {
88+
const search_prefix = %return unwrapArg(arg_it.next(allocator) ?? {
89+
warn("Expected argument after --search-prefix\n\n");
90+
return usageAndErr(&builder, false, %return stderr_stream);
91+
});
92+
builder.addSearchPrefix(search_prefix);
8793
} else if (mem.eql(u8, arg, "--verbose-tokenize")) {
8894
builder.verbose_tokenize = true;
8995
} else if (mem.eql(u8, arg, "--verbose-ast")) {
@@ -145,6 +151,7 @@ fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream)
145151
\\ --help Print this help and exit
146152
\\ --verbose Print commands before executing them
147153
\\ --prefix [path] Override default install prefix
154+
\\ --search-prefix [path] Add a path to look for binaries, libraries, headers
148155
\\
149156
\\Project-Specific Options:
150157
\\

0 commit comments

Comments
 (0)