authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-03 16:19:10-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-03 16:19:10-04:00
logfc0f8d0359777580c771848361166f97a85deddd
treeb0089937d647fcffe00979f4c5d87ce149deea10
parentbe17a4b6c1be11eb4b75db1972a38fc50875ebed
signaturelock-open Commit is signed but in an unrecognized format.

zig build: make install prefix available to build.zig and

prevent accident of '/' showing up in application/library names

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

std/build.zig+18-14
......@@ -4,6 +4,7 @@ const io = std.io;
44const fs = std.fs;
55const mem = std.mem;
66const debug = std.debug;
7const panic = std.debug.panic;
78const assert = debug.assert;
89const warn = std.debug.warn;
910const ArrayList = std.ArrayList;
......@@ -42,8 +43,8 @@ pub const Builder = struct {
4243 top_level_steps: ArrayList(*TopLevelStep),
4344 install_prefix: ?[]const u8,
4445 dest_dir: ?[]const u8,
45 lib_dir: ?[]const u8,
46 exe_dir: ?[]const u8,
46 lib_dir: []const u8,
47 exe_dir: []const u8,
4748 install_path: []const u8,
4849 search_prefixes: ArrayList([]const u8),
4950 installed_files: ArrayList(InstalledFile),
......@@ -129,8 +130,8 @@ pub const Builder = struct {
129130 .env_map = env_map,
130131 .search_prefixes = ArrayList([]const u8).init(allocator),
131132 .install_prefix = null,
132 .lib_dir = null,
133 .exe_dir = null,
133 .lib_dir = undefined,
134 .exe_dir = undefined,
134135 .dest_dir = env_map.get("DESTDIR"),
135136 .installed_files = ArrayList(InstalledFile).init(allocator),
136137 .install_tls = TopLevelStep{
......@@ -163,11 +164,13 @@ pub const Builder = struct {
163164 self.allocator.destroy(self);
164165 }
165166
167 /// This function is intended to be called by std/special/build_runner.zig, not a build.zig file.
166168 pub fn setInstallPrefix(self: *Builder, optional_prefix: ?[]const u8) void {
167169 self.install_prefix = optional_prefix;
168170 }
169171
170 fn resolveInstallPrefix(self: *Builder) void {
172 /// This function is intended to be called by std/special/build_runner.zig, not a build.zig file.
173 pub fn resolveInstallPrefix(self: *Builder) void {
171174 if (self.dest_dir) |dest_dir| {
172175 const install_prefix = self.install_prefix orelse "/usr";
173176 self.install_path = fs.path.join(self.allocator, [_][]const u8{ dest_dir, install_prefix }) catch unreachable;
......@@ -437,7 +440,7 @@ pub const Builder = struct {
437440 .description = description,
438441 };
439442 if ((self.available_options_map.put(name, available_option) catch unreachable) != null) {
440 debug.panic("Option '{}' declared twice", name);
443 panic("Option '{}' declared twice", name);
441444 }
442445 self.available_options_list.append(available_option) catch unreachable;
443446
......@@ -463,8 +466,8 @@ pub const Builder = struct {
463466 return null;
464467 },
465468 },
466 TypeId.Int => debug.panic("TODO integer options to build script"),
467 TypeId.Float => debug.panic("TODO float options to build script"),
469 TypeId.Int => panic("TODO integer options to build script"),
470 TypeId.Float => panic("TODO float options to build script"),
468471 TypeId.String => switch (entry.value.value) {
469472 UserValue.Flag => {
470473 warn("Expected -D{} to be a string, but received a boolean.\n", name);
......@@ -478,7 +481,7 @@ pub const Builder = struct {
478481 },
479482 UserValue.Scalar => |s| return s,
480483 },
481 TypeId.List => debug.panic("TODO list options to build script"),
484 TypeId.List => panic("TODO list options to build script"),
482485 }
483486 }
484487
......@@ -644,8 +647,6 @@ pub const Builder = struct {
644647 }
645648
646649 pub fn validateUserInputDidItFail(self: *Builder) bool {
647 self.resolveInstallPrefix();
648
649650 // make sure all args are used
650651 var it = self.user_input_options.iterator();
651652 while (true) {
......@@ -855,7 +856,7 @@ pub const Builder = struct {
855856 var stdout_file_in_stream = child.stdout.?.inStream();
856857 try stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size);
857858
858 const term = child.wait() catch |err| std.debug.panic("unable to spawn {}: {}", argv[0], err);
859 const term = child.wait() catch |err| panic("unable to spawn {}: {}", argv[0], err);
859860 switch (term) {
860861 .Exited => |code| {
861862 if (code != 0) {
......@@ -882,8 +883,8 @@ pub const Builder = struct {
882883 fn getInstallPath(self: *Builder, dir: InstallDir, dest_rel_path: []const u8) []const u8 {
883884 const base_dir = switch (dir) {
884885 .Prefix => self.install_path,
885 .Bin => self.exe_dir.?,
886 .Lib => self.lib_dir.?,
886 .Bin => self.exe_dir,
887 .Lib => self.lib_dir,
887888 };
888889 return fs.path.resolve(
889890 self.allocator,
......@@ -1318,6 +1319,9 @@ pub const LibExeObjStep = struct {
13181319 }
13191320
13201321 fn initExtraArgs(builder: *Builder, name: []const u8, root_src: ?[]const u8, kind: Kind, is_dynamic: bool, ver: Version) LibExeObjStep {
1322 if (mem.indexOf(u8, name, "/") != null or mem.indexOf(u8, name, "\\") != null) {
1323 panic("invalid name: '{}'. It looks like a file path, but it is supposed to be the library or application name.", name);
1324 }
13211325 var self = LibExeObjStep{
13221326 .strip = false,
13231327 .builder = builder,
std/special/build_runner.zig+2
......@@ -123,6 +123,7 @@ pub fn main() !void {
123123 }
124124 }
125125
126 builder.resolveInstallPrefix();
126127 try runBuild(builder);
127128
128129 if (builder.validateUserInputDidItFail())
......@@ -151,6 +152,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void {
151152 // run the build script to collect the options
152153 if (!already_ran_build) {
153154 builder.setInstallPrefix(null);
155 builder.resolveInstallPrefix();
154156 try runBuild(builder);
155157 }
156158