authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-04 15:32:44-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-04 15:32:44-04:00
loga8b3b5f11cc792391065ed7235887f01d668926d
tree5795cd2d450dc7ce651ce7319c99caf3358f186f
parent79e1fcfddac681128698acd2ef5667b233015c58
signaturelock-open Commit is signed but in an unrecognized format.

zig build: install is now the default step; default prefix is zig-cache

closes #2817

4 files changed, 202 insertions(+), 107 deletions(-)

std/build.zig+177-80
......@@ -18,10 +18,8 @@ const File = std.fs.File;
1818pub const FmtStep = @import("build/fmt.zig").FmtStep;
1919
2020pub const Builder = struct {
21 uninstall_tls: TopLevelStep,
2221 install_tls: TopLevelStep,
23 have_uninstall_step: bool,
24 have_install_step: bool,
22 uninstall_tls: TopLevelStep,
2523 allocator: *Allocator,
2624 native_system_lib_paths: ArrayList([]const u8),
2725 native_system_include_dirs: ArrayList([]const u8),
......@@ -42,14 +40,15 @@ pub const Builder = struct {
4240 default_step: *Step,
4341 env_map: *BufMap,
4442 top_level_steps: ArrayList(*TopLevelStep),
45 prefix: []const u8,
43 install_prefix: ?[]const u8,
4644 search_prefixes: ArrayList([]const u8),
47 lib_dir: []const u8,
48 exe_dir: []const u8,
49 installed_files: ArrayList([]const u8),
45 lib_dir: ?[]const u8,
46 exe_dir: ?[]const u8,
47 installed_files: ArrayList(InstalledFile),
5048 build_root: []const u8,
5149 cache_root: []const u8,
5250 release_mode: ?builtin.Mode,
51 is_release: bool,
5352 override_std_dir: ?[]const u8,
5453 override_lib_dir: ?[]const u8,
5554
......@@ -93,13 +92,20 @@ pub const Builder = struct {
9392 description: []const u8,
9493 };
9594
96 pub fn init(allocator: *Allocator, zig_exe: []const u8, build_root: []const u8, cache_root: []const u8) Builder {
97 const env_map = allocator.create(BufMap) catch unreachable;
98 env_map.* = process.getEnvMap(allocator) catch unreachable;
99 var self = Builder{
95 pub fn create(
96 allocator: *Allocator,
97 zig_exe: []const u8,
98 build_root: []const u8,
99 cache_root: []const u8,
100 ) !*Builder {
101 const env_map = try allocator.create(BufMap);
102 env_map.* = try process.getEnvMap(allocator);
103
104 const self = try allocator.create(Builder);
105 self.* = Builder{
100106 .zig_exe = zig_exe,
101107 .build_root = build_root,
102 .cache_root = fs.path.relative(allocator, build_root, cache_root) catch unreachable,
108 .cache_root = try fs.path.relative(allocator, build_root, cache_root),
103109 .verbose = false,
104110 .verbose_tokenize = false,
105111 .verbose_ast = false,
......@@ -119,42 +125,53 @@ pub const Builder = struct {
119125 .top_level_steps = ArrayList(*TopLevelStep).init(allocator),
120126 .default_step = undefined,
121127 .env_map = env_map,
122 .prefix = undefined,
128 .install_prefix = null,
123129 .search_prefixes = ArrayList([]const u8).init(allocator),
124 .lib_dir = undefined,
125 .exe_dir = undefined,
126 .installed_files = ArrayList([]const u8).init(allocator),
127 .uninstall_tls = TopLevelStep{
128 .step = Step.init("uninstall", allocator, makeUninstall),
129 .description = "Remove build artifacts from prefix path",
130 },
131 .have_uninstall_step = false,
130 .lib_dir = null,
131 .exe_dir = null,
132 .installed_files = ArrayList(InstalledFile).init(allocator),
132133 .install_tls = TopLevelStep{
133134 .step = Step.initNoOp("install", allocator),
134135 .description = "Copy build artifacts to prefix path",
135136 },
136 .have_install_step = false,
137 .uninstall_tls = TopLevelStep{
138 .step = Step.init("uninstall", allocator, makeUninstall),
139 .description = "Remove build artifacts from prefix path",
140 },
137141 .release_mode = null,
142 .is_release = false,
138143 .override_std_dir = null,
139144 .override_lib_dir = null,
140145 };
146 try self.top_level_steps.append(&self.install_tls);
147 try self.top_level_steps.append(&self.uninstall_tls);
141148 self.detectNativeSystemPaths();
142 self.default_step = self.step("default", "Build the project");
149 self.default_step = &self.install_tls.step;
143150 return self;
144151 }
145152
146 pub fn deinit(self: *Builder) void {
153 pub fn destroy(self: *Builder) void {
147154 self.native_system_lib_paths.deinit();
148155 self.native_system_include_dirs.deinit();
149156 self.native_system_rpaths.deinit();
150157 self.env_map.deinit();
151158 self.top_level_steps.deinit();
159 self.allocator.destroy(self);
152160 }
153161
154 pub fn setInstallPrefix(self: *Builder, maybe_prefix: ?[]const u8) void {
155 self.prefix = maybe_prefix orelse "/usr/local"; // TODO better default
156 self.lib_dir = fs.path.join(self.allocator, [_][]const u8{ self.prefix, "lib" }) catch unreachable;
157 self.exe_dir = fs.path.join(self.allocator, [_][]const u8{ self.prefix, "bin" }) catch unreachable;
162 pub fn setInstallPrefix(self: *Builder, optional_prefix: ?[]const u8) void {
163 self.install_prefix = optional_prefix;
164 }
165
166 fn resolveInstallPrefix(self: *Builder) void {
167 const prefix = if (self.install_prefix) |prefix| prefix else blk: {
168 const prefix = self.cache_root;
169 self.install_prefix = prefix;
170 break :blk prefix;
171 };
172
173 self.lib_dir = fs.path.join(self.allocator, [_][]const u8{ prefix, "lib" }) catch unreachable;
174 self.exe_dir = fs.path.join(self.allocator, [_][]const u8{ prefix, "bin" }) catch unreachable;
158175 }
159176
160177 pub fn addExecutable(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep {
......@@ -263,18 +280,10 @@ pub const Builder = struct {
263280 }
264281
265282 pub fn getInstallStep(self: *Builder) *Step {
266 if (self.have_install_step) return &self.install_tls.step;
267
268 self.top_level_steps.append(&self.install_tls) catch unreachable;
269 self.have_install_step = true;
270283 return &self.install_tls.step;
271284 }
272285
273286 pub fn getUninstallStep(self: *Builder) *Step {
274 if (self.have_uninstall_step) return &self.uninstall_tls.step;
275
276 self.top_level_steps.append(&self.uninstall_tls) catch unreachable;
277 self.have_uninstall_step = true;
278287 return &self.uninstall_tls.step;
279288 }
280289
......@@ -283,10 +292,11 @@ pub const Builder = struct {
283292 const self = @fieldParentPtr(Builder, "uninstall_tls", uninstall_tls);
284293
285294 for (self.installed_files.toSliceConst()) |installed_file| {
295 const full_path = self.getInstallPath(installed_file.dir, installed_file.path);
286296 if (self.verbose) {
287 warn("rm {}\n", installed_file);
297 warn("rm {}\n", full_path);
288298 }
289 fs.deleteFile(installed_file) catch {};
299 fs.deleteFile(full_path) catch {};
290300 }
291301
292302 // TODO remove empty directories
......@@ -460,6 +470,18 @@ pub const Builder = struct {
460470 return &step_info.step;
461471 }
462472
473 /// This provides the -Drelease option to the build user and does not give them the choice.
474 pub fn setPreferredReleaseMode(self: *Builder, mode: builtin.Mode) void {
475 if (self.release_mode != null) {
476 @panic("setPreferredReleaseMode must be called before standardReleaseOptions and may not be called twice");
477 }
478 const description = self.fmt("create a release build ({})", @tagName(mode));
479 self.is_release = self.option(bool, "release", description) orelse false;
480 self.release_mode = if (is_release) mode else builtin.Mode.Debug;
481 }
482
483 /// If you call this without first calling `setPreferredReleaseMode` then it gives the build user
484 /// the choice of what kind of release.
463485 pub fn standardReleaseOptions(self: *Builder) builtin.Mode {
464486 if (self.release_mode) |mode| return mode;
465487
......@@ -467,11 +489,20 @@ pub const Builder = struct {
467489 const release_fast = self.option(bool, "release-fast", "optimizations on and safety off") orelse false;
468490 const release_small = self.option(bool, "release-small", "size optimizations on and safety off") orelse false;
469491
470 const mode = if (release_safe and !release_fast and !release_small) builtin.Mode.ReleaseSafe else if (release_fast and !release_safe and !release_small) builtin.Mode.ReleaseFast else if (release_small and !release_fast and !release_safe) builtin.Mode.ReleaseSmall else if (!release_fast and !release_safe and !release_small) builtin.Mode.Debug else x: {
492 const mode = if (release_safe and !release_fast and !release_small)
493 builtin.Mode.ReleaseSafe
494 else if (release_fast and !release_safe and !release_small)
495 builtin.Mode.ReleaseFast
496 else if (release_small and !release_fast and !release_safe)
497 builtin.Mode.ReleaseSmall
498 else if (!release_fast and !release_safe and !release_small)
499 builtin.Mode.Debug
500 else x: {
471501 warn("Multiple release modes (of -Drelease-safe, -Drelease-fast and -Drelease-small)");
472502 self.markInvalidUserInput();
473503 break :x builtin.Mode.Debug;
474504 };
505 self.is_release = mode != .Debug;
475506 self.release_mode = mode;
476507 return mode;
477508 }
......@@ -571,6 +602,8 @@ pub const Builder = struct {
571602 }
572603
573604 pub fn validateUserInputDidItFail(self: *Builder) bool {
605 self.resolveInstallPrefix();
606
574607 // make sure all args are used
575608 var it = self.user_input_options.iterator();
576609 while (true) {
......@@ -644,27 +677,52 @@ pub const Builder = struct {
644677 return InstallArtifactStep.create(self, artifact);
645678 }
646679
647 ///::dest_rel_path is relative to prefix path or it can be an absolute path
680 ///`dest_rel_path` is relative to prefix path
648681 pub fn installFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) void {
649 self.getInstallStep().dependOn(&self.addInstallFile(src_path, dest_rel_path).step);
682 self.getInstallStep().dependOn(&self.addInstallFileWithDir(src_path, .Prefix, dest_rel_path).step);
683 }
684
685 ///`dest_rel_path` is relative to bin path
686 pub fn installBinFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) void {
687 self.getInstallStep().dependOn(&self.addInstallFileWithDir(src_path, .Bin, dest_rel_path).step);
650688 }
651689
652 ///::dest_rel_path is relative to prefix path or it can be an absolute path
690 ///`dest_rel_path` is relative to lib path
691 pub fn installLibFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) void {
692 self.getInstallStep().dependOn(&self.addInstallFileWithDir(src_path, .Lib, dest_rel_path).step);
693 }
694
695 ///`dest_rel_path` is relative to install prefix path
653696 pub fn addInstallFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep {
654 const full_dest_path = fs.path.resolve(
655 self.allocator,
656 [_][]const u8{ self.prefix, dest_rel_path },
657 ) catch unreachable;
658 self.pushInstalledFile(full_dest_path);
697 return self.addInstallFileWithDir(src_path, .Prefix, dest_rel_path);
698 }
699
700 ///`dest_rel_path` is relative to bin path
701 pub fn addInstallBinFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep {
702 return self.addInstallFileWithDir(src_path, .Bin, dest_rel_path);
703 }
659704
705 ///`dest_rel_path` is relative to lib path
706 pub fn addInstallLibFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep {
707 return self.addInstallFileWithDir(src_path, .Lib, dest_rel_path);
708 }
709
710 pub fn addInstallFileWithDir(
711 self: *Builder,
712 src_path: []const u8,
713 install_dir: InstallDir,
714 dest_rel_path: []const u8,
715 ) *InstallFileStep {
660716 const install_step = self.allocator.create(InstallFileStep) catch unreachable;
661 install_step.* = InstallFileStep.init(self, src_path, full_dest_path);
717 install_step.* = InstallFileStep.init(self, src_path, install_dir, dest_rel_path);
662718 return install_step;
663719 }
664720
665 pub fn pushInstalledFile(self: *Builder, full_path: []const u8) void {
666 _ = self.getUninstallStep();
667 self.installed_files.append(full_path) catch unreachable;
721 pub fn pushInstalledFile(self: *Builder, dir: InstallDir, dest_rel_path: []const u8) void {
722 self.installed_files.append(InstalledFile{
723 .dir = dir,
724 .path = dest_rel_path,
725 }) catch unreachable;
668726 }
669727
670728 fn copyFile(self: *Builder, source_path: []const u8, dest_path: []const u8) !void {
......@@ -786,6 +844,18 @@ pub const Builder = struct {
786844 pub fn addSearchPrefix(self: *Builder, search_prefix: []const u8) void {
787845 self.search_prefixes.append(search_prefix) catch unreachable;
788846 }
847
848 fn getInstallPath(self: *Builder, dir: InstallDir, dest_rel_path: []const u8) []const u8 {
849 const base_dir = switch (dir) {
850 .Prefix => self.install_prefix.?,
851 .Bin => self.exe_dir.?,
852 .Lib => self.lib_dir.?,
853 };
854 return fs.path.resolve(
855 self.allocator,
856 [_][]const u8{ base_dir, dest_rel_path },
857 ) catch unreachable;
858 }
789859};
790860
791861const Version = struct {
......@@ -980,6 +1050,9 @@ pub const LibExeObjStep = struct {
9801050 output_dir: ?[]const u8,
9811051 need_system_paths: bool,
9821052
1053 installed_path: ?[]const u8,
1054 install_step: ?*InstallArtifactStep,
1055
9831056 const LinkObject = union(enum) {
9841057 StaticPath: []const u8,
9851058 OtherStep: *LibExeObjStep,
......@@ -1071,6 +1144,8 @@ pub const LibExeObjStep = struct {
10711144 .output_dir = null,
10721145 .need_system_paths = false,
10731146 .single_threaded = false,
1147 .installed_path = null,
1148 .install_step = null,
10741149 };
10751150 self.computeOutFileNames();
10761151 return self;
......@@ -1146,10 +1221,15 @@ pub const LibExeObjStep = struct {
11461221 self.output_dir = self.builder.dupe(dir);
11471222 }
11481223
1224 pub fn install(self: *LibExeObjStep) void {
1225 self.builder.installArtifact(self);
1226 }
1227
11491228 /// Creates a `RunStep` with an executable built with `addExecutable`.
11501229 /// Add command line arguments with `addArg`.
11511230 pub fn run(exe: *LibExeObjStep) *RunStep {
11521231 assert(exe.kind == Kind.Exe);
1232
11531233 // It doesn't have to be native. We catch that if you actually try to run it.
11541234 // Consider that this is declarative; the run step may not be run unless a user
11551235 // option is supplied.
......@@ -1692,7 +1772,8 @@ pub const RunStep = struct {
16921772 // On Windows we don't have rpaths so we have to add .dll search paths to PATH
16931773 self.addPathForDynLibs(artifact);
16941774 }
1695 try argv.append(artifact.getOutputPath());
1775 const executable_path = artifact.installed_path orelse artifact.getOutputPath();
1776 try argv.append(executable_path);
16961777 },
16971778 }
16981779 }
......@@ -1719,38 +1800,32 @@ const InstallArtifactStep = struct {
17191800 step: Step,
17201801 builder: *Builder,
17211802 artifact: *LibExeObjStep,
1722 dest_file: []const u8,
1803 dest_dir: InstallDir,
17231804
17241805 const Self = @This();
17251806
17261807 pub fn create(builder: *Builder, artifact: *LibExeObjStep) *Self {
1727 const dest_dir = switch (artifact.kind) {
1728 LibExeObjStep.Kind.Obj => unreachable,
1729 LibExeObjStep.Kind.Test => unreachable,
1730 LibExeObjStep.Kind.Exe => builder.exe_dir,
1731 LibExeObjStep.Kind.Lib => builder.lib_dir,
1732 };
1808 if (artifact.install_step) |s| return s;
1809
17331810 const self = builder.allocator.create(Self) catch unreachable;
17341811 self.* = Self{
17351812 .builder = builder,
17361813 .step = Step.init(builder.fmt("install {}", artifact.step.name), builder.allocator, make),
17371814 .artifact = artifact,
1738 .dest_file = fs.path.join(
1739 builder.allocator,
1740 [_][]const u8{ dest_dir, artifact.out_filename },
1741 ) catch unreachable,
1815 .dest_dir = switch (artifact.kind) {
1816 .Obj => unreachable,
1817 .Test => unreachable,
1818 .Exe => InstallDir.Bin,
1819 .Lib => InstallDir.Lib,
1820 },
17421821 };
17431822 self.step.dependOn(&artifact.step);
1744 builder.pushInstalledFile(self.dest_file);
1745 if (self.artifact.kind == LibExeObjStep.Kind.Lib and self.artifact.is_dynamic) {
1746 builder.pushInstalledFile(fs.path.join(
1747 builder.allocator,
1748 [_][]const u8{ builder.lib_dir, artifact.major_only_filename },
1749 ) catch unreachable);
1750 builder.pushInstalledFile(fs.path.join(
1751 builder.allocator,
1752 [_][]const u8{ builder.lib_dir, artifact.name_only_filename },
1753 ) catch unreachable);
1823 artifact.install_step = self;
1824
1825 builder.pushInstalledFile(self.dest_dir, artifact.out_filename);
1826 if (self.artifact.isDynamicLibrary()) {
1827 builder.pushInstalledFile(.Lib, artifact.major_only_filename);
1828 builder.pushInstalledFile(.Lib, artifact.name_only_filename);
17541829 }
17551830 return self;
17561831 }
......@@ -1768,10 +1843,12 @@ const InstallArtifactStep = struct {
17681843 .Lib => if (!self.artifact.is_dynamic) u32(0o666) else u32(0o755),
17691844 },
17701845 };
1771 try builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode);
1846 const full_dest_path = builder.getInstallPath(self.dest_dir, self.artifact.out_filename);
1847 try builder.copyFileMode(self.artifact.getOutputPath(), full_dest_path, mode);
17721848 if (self.artifact.isDynamicLibrary()) {
1773 try doAtomicSymLinks(builder.allocator, self.dest_file, self.artifact.major_only_filename, self.artifact.name_only_filename);
1849 try doAtomicSymLinks(builder.allocator, full_dest_path, self.artifact.major_only_filename, self.artifact.name_only_filename);
17741850 }
1851 self.artifact.installed_path = full_dest_path;
17751852 }
17761853};
17771854
......@@ -1779,20 +1856,29 @@ pub const InstallFileStep = struct {
17791856 step: Step,
17801857 builder: *Builder,
17811858 src_path: []const u8,
1782 dest_path: []const u8,
1783
1784 pub fn init(builder: *Builder, src_path: []const u8, dest_path: []const u8) InstallFileStep {
1859 dir: InstallDir,
1860 dest_rel_path: []const u8,
1861
1862 pub fn init(
1863 builder: *Builder,
1864 src_path: []const u8,
1865 dir: InstallDir,
1866 dest_rel_path: []const u8,
1867 ) InstallFileStep {
1868 builder.pushInstalledFile(dir, dest_rel_path);
17851869 return InstallFileStep{
17861870 .builder = builder,
17871871 .step = Step.init(builder.fmt("install {}", src_path), builder.allocator, make),
17881872 .src_path = src_path,
1789 .dest_path = dest_path,
1873 .dir = dir,
1874 .dest_rel_path = dest_rel_path,
17901875 };
17911876 }
17921877
17931878 fn make(step: *Step) !void {
17941879 const self = @fieldParentPtr(InstallFileStep, "step", step);
1795 try self.builder.copyFile(self.src_path, self.dest_path);
1880 const full_dest_path = self.builder.getInstallPath(self.dir, self.dest_rel_path);
1881 try self.builder.copyFile(self.src_path, full_dest_path);
17961882 }
17971883};
17981884
......@@ -1925,3 +2011,14 @@ fn doAtomicSymLinks(allocator: *Allocator, output_path: []const u8, filename_maj
19252011 return err;
19262012 };
19272013}
2014
2015pub const InstallDir = enum {
2016 Prefix,
2017 Lib,
2018 Bin,
2019};
2020
2021pub const InstalledFile = struct {
2022 dir: InstallDir,
2023 path: []const u8,
2024};
std/special/build_runner.zig+22-21
......@@ -38,13 +38,11 @@ pub fn main() !void {
3838 return error.InvalidArgs;
3939 });
4040
41 var builder = Builder.init(allocator, zig_exe, build_root, cache_root);
42 defer builder.deinit();
41 const builder = try Builder.create(allocator, zig_exe, build_root, cache_root);
42 defer builder.destroy();
4343
4444 var targets = ArrayList([]const u8).init(allocator);
4545
46 var prefix: ?[]const u8 = null;
47
4846 var stderr_file = io.getStdErr();
4947 var stderr_file_stream: File.OutStream = undefined;
5048 var stderr_stream = if (stderr_file) |f| x: {
......@@ -65,42 +63,42 @@ pub fn main() !void {
6563 const option_contents = arg[2..];
6664 if (option_contents.len == 0) {
6765 warn("Expected option name after '-D'\n\n");
68 return usageAndErr(&builder, false, try stderr_stream);
66 return usageAndErr(builder, false, try stderr_stream);
6967 }
7068 if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| {
7169 const option_name = option_contents[0..name_end];
7270 const option_value = option_contents[name_end + 1 ..];
7371 if (try builder.addUserInputOption(option_name, option_value))
74 return usageAndErr(&builder, false, try stderr_stream);
72 return usageAndErr(builder, false, try stderr_stream);
7573 } else {
7674 if (try builder.addUserInputFlag(option_contents))
77 return usageAndErr(&builder, false, try stderr_stream);
75 return usageAndErr(builder, false, try stderr_stream);
7876 }
7977 } else if (mem.startsWith(u8, arg, "-")) {
8078 if (mem.eql(u8, arg, "--verbose")) {
8179 builder.verbose = true;
8280 } else if (mem.eql(u8, arg, "--help")) {
83 return usage(&builder, false, try stdout_stream);
81 return usage(builder, false, try stdout_stream);
8482 } else if (mem.eql(u8, arg, "--prefix")) {
85 prefix = try unwrapArg(arg_it.next(allocator) orelse {
83 builder.install_prefix = try unwrapArg(arg_it.next(allocator) orelse {
8684 warn("Expected argument after --prefix\n\n");
87 return usageAndErr(&builder, false, try stderr_stream);
85 return usageAndErr(builder, false, try stderr_stream);
8886 });
8987 } else if (mem.eql(u8, arg, "--search-prefix")) {
9088 const search_prefix = try unwrapArg(arg_it.next(allocator) orelse {
9189 warn("Expected argument after --search-prefix\n\n");
92 return usageAndErr(&builder, false, try stderr_stream);
90 return usageAndErr(builder, false, try stderr_stream);
9391 });
9492 builder.addSearchPrefix(search_prefix);
9593 } else if (mem.eql(u8, arg, "--override-std-dir")) {
9694 builder.override_std_dir = try unwrapArg(arg_it.next(allocator) orelse {
9795 warn("Expected argument after --override-std-dir\n\n");
98 return usageAndErr(&builder, false, try stderr_stream);
96 return usageAndErr(builder, false, try stderr_stream);
9997 });
10098 } else if (mem.eql(u8, arg, "--override-lib-dir")) {
10199 builder.override_lib_dir = try unwrapArg(arg_it.next(allocator) orelse {
102100 warn("Expected argument after --override-lib-dir\n\n");
103 return usageAndErr(&builder, false, try stderr_stream);
101 return usageAndErr(builder, false, try stderr_stream);
104102 });
105103 } else if (mem.eql(u8, arg, "--verbose-tokenize")) {
106104 builder.verbose_tokenize = true;
......@@ -118,23 +116,22 @@ pub fn main() !void {
118116 builder.verbose_cc = true;
119117 } else {
120118 warn("Unrecognized argument: {}\n\n", arg);
121 return usageAndErr(&builder, false, try stderr_stream);
119 return usageAndErr(builder, false, try stderr_stream);
122120 }
123121 } else {
124122 try targets.append(arg);
125123 }
126124 }
127125
128 builder.setInstallPrefix(prefix);
129 try runBuild(&builder);
126 try runBuild(builder);
130127
131128 if (builder.validateUserInputDidItFail())
132 return usageAndErr(&builder, true, try stderr_stream);
129 return usageAndErr(builder, true, try stderr_stream);
133130
134131 builder.make(targets.toSliceConst()) catch |err| {
135132 switch (err) {
136133 error.InvalidStepName => {
137 return usageAndErr(&builder, true, try stderr_stream);
134 return usageAndErr(builder, true, try stderr_stream);
138135 },
139136 error.UncleanExit => process.exit(1),
140137 else => return err,
......@@ -144,8 +141,8 @@ pub fn main() !void {
144141
145142fn runBuild(builder: *Builder) anyerror!void {
146143 switch (@typeId(@typeOf(root.build).ReturnType)) {
147 builtin.TypeId.Void => root.build(builder),
148 builtin.TypeId.ErrorUnion => try root.build(builder),
144 .Void => root.build(builder),
145 .ErrorUnion => try root.build(builder),
149146 else => @compileError("expected return type of build to be 'void' or '!void'"),
150147 }
151148}
......@@ -167,7 +164,11 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void {
167164
168165 const allocator = builder.allocator;
169166 for (builder.top_level_steps.toSliceConst()) |top_level_step| {
170 try out_stream.print(" {s:22} {}\n", top_level_step.step.name, top_level_step.description);
167 const name = if (&top_level_step.step == builder.default_step)
168 try fmt.allocPrint(allocator, "{} (default)", top_level_step.step.name)
169 else
170 top_level_step.step.name;
171 try out_stream.print(" {s:22} {}\n", name, top_level_step.description);
171172 }
172173
173174 try out_stream.write(
std/special/init-exe/build.zig+2-3
......@@ -4,12 +4,11 @@ pub fn build(b: *Builder) void {
44 const mode = b.standardReleaseOptions();
55 const exe = b.addExecutable("$", "src/main.zig");
66 exe.setBuildMode(mode);
7 exe.install();
78
89 const run_cmd = exe.run();
10 run_cmd.step.dependOn(b.getInstallStep());
911
1012 const run_step = b.step("run", "Run the app");
1113 run_step.dependOn(&run_cmd.step);
12
13 b.default_step.dependOn(&exe.step);
14 b.installArtifact(exe);
1514}
std/special/init-lib/build.zig+1-3
......@@ -4,13 +4,11 @@ pub fn build(b: *Builder) void {
44 const mode = b.standardReleaseOptions();
55 const lib = b.addStaticLibrary("$", "src/main.zig");
66 lib.setBuildMode(mode);
7 lib.install();
78
89 var main_tests = b.addTest("src/main.zig");
910 main_tests.setBuildMode(mode);
1011
1112 const test_step = b.step("test", "Run library tests");
1213 test_step.dependOn(&main_tests.step);
13
14 b.default_step.dependOn(&lib.step);
15 b.installArtifact(lib);
1614}