From 3cbc244e982da2189acfe1de9d60b478d2e60de4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 23 Dec 2017 20:21:57 -0500 Subject: build: add --search-prefix option --- std/build.zig | 21 +++++++++++++++++++++ std/special/build_runner.zig | 7 +++++++ 2 files changed, 28 insertions(+) (limited to 'std') diff --git a/std/build.zig b/std/build.zig index 0a23a77f80..3a2079db15 100644 --- a/std/build.zig +++ b/std/build.zig @@ -47,6 +47,7 @@ pub const Builder = struct { env_map: BufMap, top_level_steps: ArrayList(&TopLevelStep), prefix: []const u8, + search_prefixes: ArrayList([]const u8), lib_dir: []const u8, exe_dir: []const u8, installed_files: ArrayList([]const u8), @@ -114,6 +115,7 @@ pub const Builder = struct { .default_step = undefined, .env_map = %%os.getEnvMap(allocator), .prefix = undefined, + .search_prefixes = ArrayList([]const u8).init(allocator), .lib_dir = undefined, .exe_dir = undefined, .installed_files = ArrayList([]const u8).init(allocator), @@ -671,7 +673,22 @@ pub const Builder = struct { } pub fn findProgram(self: &Builder, names: []const []const u8, paths: []const []const u8) -> %[]const u8 { + // TODO report error for ambiguous situations const exe_extension = (Target { .Native = {}}).exeFileExt(); + for (self.search_prefixes.toSliceConst()) |search_prefix| { + for (names) |name| { + if (os.path.isAbsolute(name)) { + return name; + } + const full_path = %return os.path.join(self.allocator, search_prefix, "bin", + self.fmt("{}{}", name, exe_extension)); + if (os.path.real(self.allocator, full_path)) |real_path| { + return real_path; + } else |_| { + continue; + } + } + } if (self.env_map.get("PATH")) |PATH| { for (names) |name| { if (os.path.isAbsolute(name)) { @@ -727,6 +744,10 @@ pub const Builder = struct { }, } } + + pub fn addSearchPrefix(self: &Builder, search_prefix: []const u8) { + %%self.search_prefixes.append(search_prefix); + } }; const Version = struct { diff --git a/std/special/build_runner.zig b/std/special/build_runner.zig index e54d85e6ef..0ae76a560b 100644 --- a/std/special/build_runner.zig +++ b/std/special/build_runner.zig @@ -84,6 +84,12 @@ pub fn main() -> %void { warn("Expected argument after --prefix\n\n"); return usageAndErr(&builder, false, %return stderr_stream); }); + } else if (mem.eql(u8, arg, "--search-prefix")) { + const search_prefix = %return unwrapArg(arg_it.next(allocator) ?? { + warn("Expected argument after --search-prefix\n\n"); + return usageAndErr(&builder, false, %return stderr_stream); + }); + builder.addSearchPrefix(search_prefix); } else if (mem.eql(u8, arg, "--verbose-tokenize")) { builder.verbose_tokenize = true; } else if (mem.eql(u8, arg, "--verbose-ast")) { @@ -145,6 +151,7 @@ fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) \\ --help Print this help and exit \\ --verbose Print commands before executing them \\ --prefix [path] Override default install prefix + \\ --search-prefix [path] Add a path to look for binaries, libraries, headers \\ \\Project-Specific Options: \\ -- cgit v1.2.3