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;...@@ -4,6 +4,7 @@ const io = std.io;
4const fs = std.fs;4const fs = std.fs;
5const mem = std.mem;5const mem = std.mem;
6const debug = std.debug;6const debug = std.debug;
7const panic = std.debug.panic;
7const assert = debug.assert;8const assert = debug.assert;
8const warn = std.debug.warn;9const warn = std.debug.warn;
9const ArrayList = std.ArrayList;10const ArrayList = std.ArrayList;
...@@ -42,8 +43,8 @@ pub const Builder = struct {...@@ -42,8 +43,8 @@ pub const Builder = struct {
42 top_level_steps: ArrayList(*TopLevelStep),43 top_level_steps: ArrayList(*TopLevelStep),
43 install_prefix: ?[]const u8,44 install_prefix: ?[]const u8,
44 dest_dir: ?[]const u8,45 dest_dir: ?[]const u8,
45 lib_dir: ?[]const u8,46 lib_dir: []const u8,
46 exe_dir: ?[]const u8,47 exe_dir: []const u8,
47 install_path: []const u8,48 install_path: []const u8,
48 search_prefixes: ArrayList([]const u8),49 search_prefixes: ArrayList([]const u8),
49 installed_files: ArrayList(InstalledFile),50 installed_files: ArrayList(InstalledFile),
...@@ -129,8 +130,8 @@ pub const Builder = struct {...@@ -129,8 +130,8 @@ pub const Builder = struct {
129 .env_map = env_map,130 .env_map = env_map,
130 .search_prefixes = ArrayList([]const u8).init(allocator),131 .search_prefixes = ArrayList([]const u8).init(allocator),
131 .install_prefix = null,132 .install_prefix = null,
132 .lib_dir = null,133 .lib_dir = undefined,
133 .exe_dir = null,134 .exe_dir = undefined,
134 .dest_dir = env_map.get("DESTDIR"),135 .dest_dir = env_map.get("DESTDIR"),
135 .installed_files = ArrayList(InstalledFile).init(allocator),136 .installed_files = ArrayList(InstalledFile).init(allocator),
136 .install_tls = TopLevelStep{137 .install_tls = TopLevelStep{
...@@ -163,11 +164,13 @@ pub const Builder = struct {...@@ -163,11 +164,13 @@ pub const Builder = struct {
163 self.allocator.destroy(self);164 self.allocator.destroy(self);
164 }165 }
165166
167 /// This function is intended to be called by std/special/build_runner.zig, not a build.zig file.
166 pub fn setInstallPrefix(self: *Builder, optional_prefix: ?[]const u8) void {168 pub fn setInstallPrefix(self: *Builder, optional_prefix: ?[]const u8) void {
167 self.install_prefix = optional_prefix;169 self.install_prefix = optional_prefix;
168 }170 }
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 {
171 if (self.dest_dir) |dest_dir| {174 if (self.dest_dir) |dest_dir| {
172 const install_prefix = self.install_prefix orelse "/usr";175 const install_prefix = self.install_prefix orelse "/usr";
173 self.install_path = fs.path.join(self.allocator, [_][]const u8{ dest_dir, install_prefix }) catch unreachable;176 self.install_path = fs.path.join(self.allocator, [_][]const u8{ dest_dir, install_prefix }) catch unreachable;
...@@ -437,7 +440,7 @@ pub const Builder = struct {...@@ -437,7 +440,7 @@ pub const Builder = struct {
437 .description = description,440 .description = description,
438 };441 };
439 if ((self.available_options_map.put(name, available_option) catch unreachable) != null) {442 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);
441 }444 }
442 self.available_options_list.append(available_option) catch unreachable;445 self.available_options_list.append(available_option) catch unreachable;
443446
...@@ -463,8 +466,8 @@ pub const Builder = struct {...@@ -463,8 +466,8 @@ pub const Builder = struct {
463 return null;466 return null;
464 },467 },
465 },468 },
466 TypeId.Int => debug.panic("TODO integer options to build script"),469 TypeId.Int => panic("TODO integer options to build script"),
467 TypeId.Float => debug.panic("TODO float options to build script"),470 TypeId.Float => panic("TODO float options to build script"),
468 TypeId.String => switch (entry.value.value) {471 TypeId.String => switch (entry.value.value) {
469 UserValue.Flag => {472 UserValue.Flag => {
470 warn("Expected -D{} to be a string, but received a boolean.\n", name);473 warn("Expected -D{} to be a string, but received a boolean.\n", name);
...@@ -478,7 +481,7 @@ pub const Builder = struct {...@@ -478,7 +481,7 @@ pub const Builder = struct {
478 },481 },
479 UserValue.Scalar => |s| return s,482 UserValue.Scalar => |s| return s,
480 },483 },
481 TypeId.List => debug.panic("TODO list options to build script"),484 TypeId.List => panic("TODO list options to build script"),
482 }485 }
483 }486 }
484487
...@@ -644,8 +647,6 @@ pub const Builder = struct {...@@ -644,8 +647,6 @@ pub const Builder = struct {
644 }647 }
645648
646 pub fn validateUserInputDidItFail(self: *Builder) bool {649 pub fn validateUserInputDidItFail(self: *Builder) bool {
647 self.resolveInstallPrefix();
648
649 // make sure all args are used650 // make sure all args are used
650 var it = self.user_input_options.iterator();651 var it = self.user_input_options.iterator();
651 while (true) {652 while (true) {
...@@ -855,7 +856,7 @@ pub const Builder = struct {...@@ -855,7 +856,7 @@ pub const Builder = struct {
855 var stdout_file_in_stream = child.stdout.?.inStream();856 var stdout_file_in_stream = child.stdout.?.inStream();
856 try stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size);857 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);
859 switch (term) {860 switch (term) {
860 .Exited => |code| {861 .Exited => |code| {
861 if (code != 0) {862 if (code != 0) {
...@@ -882,8 +883,8 @@ pub const Builder = struct {...@@ -882,8 +883,8 @@ pub const Builder = struct {
882 fn getInstallPath(self: *Builder, dir: InstallDir, dest_rel_path: []const u8) []const u8 {883 fn getInstallPath(self: *Builder, dir: InstallDir, dest_rel_path: []const u8) []const u8 {
883 const base_dir = switch (dir) {884 const base_dir = switch (dir) {
884 .Prefix => self.install_path,885 .Prefix => self.install_path,
885 .Bin => self.exe_dir.?,886 .Bin => self.exe_dir,
886 .Lib => self.lib_dir.?,887 .Lib => self.lib_dir,
887 };888 };
888 return fs.path.resolve(889 return fs.path.resolve(
889 self.allocator,890 self.allocator,
...@@ -1318,6 +1319,9 @@ pub const LibExeObjStep = struct {...@@ -1318,6 +1319,9 @@ pub const LibExeObjStep = struct {
1318 }1319 }
13191320
1320 fn initExtraArgs(builder: *Builder, name: []const u8, root_src: ?[]const u8, kind: Kind, is_dynamic: bool, ver: Version) LibExeObjStep {1321 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 }
1321 var self = LibExeObjStep{1325 var self = LibExeObjStep{
1322 .strip = false,1326 .strip = false,
1323 .builder = builder,1327 .builder = builder,
std/special/build_runner.zig+2
...@@ -123,6 +123,7 @@ pub fn main() !void {...@@ -123,6 +123,7 @@ pub fn main() !void {
123 }123 }
124 }124 }
125125
126 builder.resolveInstallPrefix();
126 try runBuild(builder);127 try runBuild(builder);
127128
128 if (builder.validateUserInputDidItFail())129 if (builder.validateUserInputDidItFail())
...@@ -151,6 +152,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void {...@@ -151,6 +152,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void {
151 // run the build script to collect the options152 // run the build script to collect the options
152 if (!already_ran_build) {153 if (!already_ran_build) {
153 builder.setInstallPrefix(null);154 builder.setInstallPrefix(null);
155 builder.resolveInstallPrefix();
154 try runBuild(builder);156 try runBuild(builder);
155 }157 }
156158