authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-03-22 13:11:45+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-03-24 12:29:50+01:00
log3bf72f2b3add70ad0671c668baf251f2db93abbf
treed93b2f65908d00a80bd8da93ed59374b0b098571
parent749c3f014ba36a6dc824419f835790f9c181aad3

std.build: make Builder.install_prefix non optional

This is useful for build.zig files to check in some cases, for example to adhere to the convention of installing config to /etc instead of /usr/etc on linux when using the /usr prefix. Perhaps std.build will handle such common cases eventually, but that is not yet the case.

2 files changed, 11 insertions(+), 20 deletions(-)

lib/std/build.zig+7-16
......@@ -51,7 +51,7 @@ pub const Builder = struct {
5151 default_step: *Step,
5252 env_map: *BufMap,
5353 top_level_steps: ArrayList(*TopLevelStep),
54 install_prefix: ?[]const u8,
54 install_prefix: []const u8,
5555 dest_dir: ?[]const u8,
5656 lib_dir: []const u8,
5757 exe_dir: []const u8,
......@@ -156,7 +156,7 @@ pub const Builder = struct {
156156 .default_step = undefined,
157157 .env_map = env_map,
158158 .search_prefixes = ArrayList([]const u8).init(allocator),
159 .install_prefix = null,
159 .install_prefix = undefined,
160160 .lib_dir = undefined,
161161 .exe_dir = undefined,
162162 .h_dir = undefined,
......@@ -190,22 +190,13 @@ pub const Builder = struct {
190190 }
191191
192192 /// This function is intended to be called by std/special/build_runner.zig, not a build.zig file.
193 pub fn setInstallPrefix(self: *Builder, optional_prefix: ?[]const u8) void {
194 self.install_prefix = optional_prefix;
195 }
196
197 /// This function is intended to be called by std/special/build_runner.zig, not a build.zig file.
198 pub fn resolveInstallPrefix(self: *Builder) void {
193 pub fn resolveInstallPrefix(self: *Builder, install_prefix: ?[]const u8) void {
199194 if (self.dest_dir) |dest_dir| {
200 const install_prefix = self.install_prefix orelse "/usr";
201 self.install_path = fs.path.join(self.allocator, &[_][]const u8{ dest_dir, install_prefix }) catch unreachable;
195 self.install_prefix = install_prefix orelse "/usr";
196 self.install_path = fs.path.join(self.allocator, &[_][]const u8{ dest_dir, self.install_prefix }) catch unreachable;
202197 } else {
203 const install_prefix = self.install_prefix orelse blk: {
204 const p = self.cache_root;
205 self.install_prefix = p;
206 break :blk p;
207 };
208 self.install_path = install_prefix;
198 self.install_prefix = install_prefix orelse self.cache_root;
199 self.install_path = self.install_prefix;
209200 }
210201 self.lib_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "lib" }) catch unreachable;
211202 self.exe_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "bin" }) catch unreachable;
lib/std/special/build_runner.zig+4-4
......@@ -60,6 +60,7 @@ pub fn main() !void {
6060 const stderr_stream = io.getStdErr().writer();
6161 const stdout_stream = io.getStdOut().writer();
6262
63 var install_prefix: ?[]const u8 = null;
6364 while (nextArg(args, &arg_idx)) |arg| {
6465 if (mem.startsWith(u8, arg, "-D")) {
6566 const option_contents = arg[2..];
......@@ -82,7 +83,7 @@ pub fn main() !void {
8283 } else if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
8384 return usage(builder, false, stdout_stream);
8485 } else if (mem.eql(u8, arg, "--prefix")) {
85 builder.install_prefix = nextArg(args, &arg_idx) orelse {
86 install_prefix = nextArg(args, &arg_idx) orelse {
8687 warn("Expected argument after --prefix\n\n", .{});
8788 return usageAndErr(builder, false, stderr_stream);
8889 };
......@@ -134,7 +135,7 @@ pub fn main() !void {
134135 }
135136 }
136137
137 builder.resolveInstallPrefix();
138 builder.resolveInstallPrefix(install_prefix);
138139 try runBuild(builder);
139140
140141 if (builder.validateUserInputDidItFail())
......@@ -162,8 +163,7 @@ fn runBuild(builder: *Builder) anyerror!void {
162163fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void {
163164 // run the build script to collect the options
164165 if (!already_ran_build) {
165 builder.setInstallPrefix(null);
166 builder.resolveInstallPrefix();
166 builder.resolveInstallPrefix(null);
167167 try runBuild(builder);
168168 }
169169