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 {...@@ -51,7 +51,7 @@ pub const Builder = struct {
51 default_step: *Step,51 default_step: *Step,
52 env_map: *BufMap,52 env_map: *BufMap,
53 top_level_steps: ArrayList(*TopLevelStep),53 top_level_steps: ArrayList(*TopLevelStep),
54 install_prefix: ?[]const u8,54 install_prefix: []const u8,
55 dest_dir: ?[]const u8,55 dest_dir: ?[]const u8,
56 lib_dir: []const u8,56 lib_dir: []const u8,
57 exe_dir: []const u8,57 exe_dir: []const u8,
...@@ -156,7 +156,7 @@ pub const Builder = struct {...@@ -156,7 +156,7 @@ pub const Builder = struct {
156 .default_step = undefined,156 .default_step = undefined,
157 .env_map = env_map,157 .env_map = env_map,
158 .search_prefixes = ArrayList([]const u8).init(allocator),158 .search_prefixes = ArrayList([]const u8).init(allocator),
159 .install_prefix = null,159 .install_prefix = undefined,
160 .lib_dir = undefined,160 .lib_dir = undefined,
161 .exe_dir = undefined,161 .exe_dir = undefined,
162 .h_dir = undefined,162 .h_dir = undefined,
...@@ -190,22 +190,13 @@ pub const Builder = struct {...@@ -190,22 +190,13 @@ pub const Builder = struct {
190 }190 }
191191
192 /// This function is intended to be called by std/special/build_runner.zig, not a build.zig file.192 /// 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 {193 pub fn resolveInstallPrefix(self: *Builder, install_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 {
199 if (self.dest_dir) |dest_dir| {194 if (self.dest_dir) |dest_dir| {
200 const install_prefix = self.install_prefix orelse "/usr";195 self.install_prefix = install_prefix orelse "/usr";
201 self.install_path = fs.path.join(self.allocator, &[_][]const u8{ dest_dir, install_prefix }) catch unreachable;196 self.install_path = fs.path.join(self.allocator, &[_][]const u8{ dest_dir, self.install_prefix }) catch unreachable;
202 } else {197 } else {
203 const install_prefix = self.install_prefix orelse blk: {198 self.install_prefix = install_prefix orelse self.cache_root;
204 const p = self.cache_root;199 self.install_path = self.install_prefix;
205 self.install_prefix = p;
206 break :blk p;
207 };
208 self.install_path = install_prefix;
209 }200 }
210 self.lib_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "lib" }) catch unreachable;201 self.lib_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "lib" }) catch unreachable;
211 self.exe_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "bin" }) catch unreachable;202 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 {...@@ -60,6 +60,7 @@ pub fn main() !void {
60 const stderr_stream = io.getStdErr().writer();60 const stderr_stream = io.getStdErr().writer();
61 const stdout_stream = io.getStdOut().writer();61 const stdout_stream = io.getStdOut().writer();
6262
63 var install_prefix: ?[]const u8 = null;
63 while (nextArg(args, &arg_idx)) |arg| {64 while (nextArg(args, &arg_idx)) |arg| {
64 if (mem.startsWith(u8, arg, "-D")) {65 if (mem.startsWith(u8, arg, "-D")) {
65 const option_contents = arg[2..];66 const option_contents = arg[2..];
...@@ -82,7 +83,7 @@ pub fn main() !void {...@@ -82,7 +83,7 @@ pub fn main() !void {
82 } else if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {83 } else if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
83 return usage(builder, false, stdout_stream);84 return usage(builder, false, stdout_stream);
84 } else if (mem.eql(u8, arg, "--prefix")) {85 } else if (mem.eql(u8, arg, "--prefix")) {
85 builder.install_prefix = nextArg(args, &arg_idx) orelse {86 install_prefix = nextArg(args, &arg_idx) orelse {
86 warn("Expected argument after --prefix\n\n", .{});87 warn("Expected argument after --prefix\n\n", .{});
87 return usageAndErr(builder, false, stderr_stream);88 return usageAndErr(builder, false, stderr_stream);
88 };89 };
...@@ -134,7 +135,7 @@ pub fn main() !void {...@@ -134,7 +135,7 @@ pub fn main() !void {
134 }135 }
135 }136 }
136137
137 builder.resolveInstallPrefix();138 builder.resolveInstallPrefix(install_prefix);
138 try runBuild(builder);139 try runBuild(builder);
139140
140 if (builder.validateUserInputDidItFail())141 if (builder.validateUserInputDidItFail())
...@@ -162,8 +163,7 @@ fn runBuild(builder: *Builder) anyerror!void {...@@ -162,8 +163,7 @@ fn runBuild(builder: *Builder) anyerror!void {
162fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void {163fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void {
163 // run the build script to collect the options164 // run the build script to collect the options
164 if (!already_ran_build) {165 if (!already_ran_build) {
165 builder.setInstallPrefix(null);166 builder.resolveInstallPrefix(null);
166 builder.resolveInstallPrefix();
167 try runBuild(builder);167 try runBuild(builder);
168 }168 }
169169