From c1ecb97befdb6a1106c1651da69f08cfb34f754a Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Sat, 12 Jun 2021 11:23:04 +0200 Subject: [PATCH 1/2] specify the output lib, exe and include paths with flags Signed-off-by: Jan200101 --- lib/std/build.zig | 34 ++++++++++++++++++++++++++++---- lib/std/special/build_runner.zig | 24 ++++++++++++++++++++-- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/lib/std/build.zig b/lib/std/build.zig index 572f2b2be..a12186464 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -122,6 +122,12 @@ pub const Builder = struct { description: []const u8, }; + pub const DirList = struct { + lib_dir: ?[]const u8 = null, + exe_dir: ?[]const u8 = null, + include_dir: ?[]const u8 = null, + }; + pub fn create( allocator: *Allocator, zig_exe: []const u8, @@ -190,7 +196,7 @@ pub const Builder = struct { } /// This function is intended to be called by std/special/build_runner.zig, not a build.zig file. - pub fn resolveInstallPrefix(self: *Builder, install_prefix: ?[]const u8) void { + pub fn resolveInstallPrefix(self: *Builder, install_prefix: ?[]const u8, dir_list: DirList) void { if (self.dest_dir) |dest_dir| { self.install_prefix = install_prefix orelse "/usr"; self.install_path = fs.path.join(self.allocator, &[_][]const u8{ dest_dir, self.install_prefix }) catch unreachable; @@ -199,9 +205,29 @@ pub const Builder = struct { (fs.path.join(self.allocator, &[_][]const u8{ self.build_root, "zig-out" }) catch unreachable); self.install_path = self.install_prefix; } - self.lib_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "lib" }) catch unreachable; - self.exe_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "bin" }) catch unreachable; - self.h_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "include" }) catch unreachable; + + var lib_list = [_][]const u8{ self.install_path, "lib" }; + var exe_list = [_][]const u8{ self.install_path, "bin" }; + var h_list = [_][]const u8{ self.install_path, "include" }; + + if (dir_list.lib_dir) |dir| { + if (std.fs.path.isAbsolute(dir)) lib_list[0] = self.dest_dir orelse ""; + lib_list[1] = dir; + } + + if (dir_list.exe_dir) |dir| { + if (std.fs.path.isAbsolute(dir)) exe_list[0] = self.dest_dir orelse ""; + exe_list[1] = dir; + } + + if (dir_list.include_dir) |dir| { + if (std.fs.path.isAbsolute(dir)) h_list[0] = self.dest_dir orelse ""; + h_list[1] = dir; + } + + self.lib_dir = fs.path.join(self.allocator, &lib_list) catch unreachable; + self.exe_dir = fs.path.join(self.allocator, &exe_list) catch unreachable; + self.h_dir = fs.path.join(self.allocator, &h_list) catch unreachable; } pub fn addExecutable(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep { diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index c6185ef09..da50548c6 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -61,6 +61,8 @@ pub fn main() !void { const stdout_stream = io.getStdOut().writer(); var install_prefix: ?[]const u8 = null; + var dir_list = Builder.DirList{}; + while (nextArg(args, &arg_idx)) |arg| { if (mem.startsWith(u8, arg, "-D")) { const option_contents = arg[2..]; @@ -87,6 +89,21 @@ pub fn main() !void { warn("Expected argument after {s}\n\n", .{arg}); return usageAndErr(builder, false, stderr_stream); }; + } else if (mem.eql(u8, arg, "--lib-dir")) { + dir_list.lib_dir = nextArg(args, &arg_idx) orelse { + warn("Expected argument after {s}\n\n", .{arg}); + return usageAndErr(builder, false, stderr_stream); + }; + } else if (mem.eql(u8, arg, "--exe-dir")) { + dir_list.exe_dir = nextArg(args, &arg_idx) orelse { + warn("Expected argument after {s}\n\n", .{arg}); + return usageAndErr(builder, false, stderr_stream); + }; + } else if (mem.eql(u8, arg, "--include-dir")) { + dir_list.include_dir = nextArg(args, &arg_idx) orelse { + warn("Expected argument after {s}\n\n", .{arg}); + return usageAndErr(builder, false, stderr_stream); + }; } else if (mem.eql(u8, arg, "--search-prefix")) { const search_prefix = nextArg(args, &arg_idx) orelse { warn("Expected argument after --search-prefix\n\n", .{}); @@ -135,7 +152,7 @@ pub fn main() !void { } } - builder.resolveInstallPrefix(install_prefix); + builder.resolveInstallPrefix(install_prefix, dir_list); try runBuild(builder); if (builder.validateUserInputDidItFail()) @@ -163,7 +180,7 @@ fn runBuild(builder: *Builder) anyerror!void { fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void { // run the build script to collect the options if (!already_ran_build) { - builder.resolveInstallPrefix(null); + builder.resolveInstallPrefix(null, .{}); try runBuild(builder); } @@ -189,6 +206,9 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void \\ -h, --help Print this help and exit \\ --verbose Print commands before executing them \\ -p, --prefix [path] Override default install prefix + \\ --lib-dir [path] Override default library directory path + \\ --exe-dir [path] Override default executable directory path + \\ --include-dir [path] Override default include directory path \\ --search-prefix [path] Add a path to look for binaries, libraries, headers \\ --color [auto|off|on] Enable or disable colored error messages \\ -- 2.31.1