authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-09 21:20:09-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-09 21:20:09-04:00
log99f077baf96c0f19026ea3d7f9475aa41b720cb9
treef660e817ff5211d14f6e9f62fb3b58416869a6a3
parent304cfb7122a9f32d11b1f699fc9768ce81b7b9ca

zig build: allow calling b.standardReleaseOptions...

...multiple times. See #368

2 files changed, 17 insertions(+), 6 deletions(-)

std/build.zig+12-6
...@@ -44,6 +44,7 @@ pub const Builder = struct {...@@ -44,6 +44,7 @@ pub const Builder = struct {
44 installed_files: ArrayList([]const u8),44 installed_files: ArrayList([]const u8),
45 build_root: []const u8,45 build_root: []const u8,
46 cache_root: []const u8,46 cache_root: []const u8,
47 release_mode: ?builtin.Mode,
4748
48 const UserInputOptionsMap = HashMap([]const u8, UserInputOption, mem.hash_slice_u8, mem.eql_slice_u8);49 const UserInputOptionsMap = HashMap([]const u8, UserInputOption, mem.hash_slice_u8, mem.eql_slice_u8);
49 const AvailableOptionsMap = HashMap([]const u8, AvailableOption, mem.hash_slice_u8, mem.eql_slice_u8);50 const AvailableOptionsMap = HashMap([]const u8, AvailableOption, mem.hash_slice_u8, mem.eql_slice_u8);
...@@ -112,6 +113,7 @@ pub const Builder = struct {...@@ -112,6 +113,7 @@ pub const Builder = struct {
112 .description = "Copy build artifacts to prefix path",113 .description = "Copy build artifacts to prefix path",
113 },114 },
114 .have_install_step = false,115 .have_install_step = false,
116 .release_mode = null,
115 };117 };
116 self.processNixOSEnvVars();118 self.processNixOSEnvVars();
117 self.default_step = self.step("default", "Build the project");119 self.default_step = self.step("default", "Build the project");
...@@ -407,20 +409,24 @@ pub const Builder = struct {...@@ -407,20 +409,24 @@ pub const Builder = struct {
407 }409 }
408410
409 pub fn standardReleaseOptions(self: &Builder) -> builtin.Mode {411 pub fn standardReleaseOptions(self: &Builder) -> builtin.Mode {
412 if (self.release_mode) |mode| return mode;
413
410 const release_safe = self.option(bool, "release-safe", "optimizations on and safety on") ?? false;414 const release_safe = self.option(bool, "release-safe", "optimizations on and safety on") ?? false;
411 const release_fast = self.option(bool, "release-fast", "optimizations on and safety off") ?? false;415 const release_fast = self.option(bool, "release-fast", "optimizations on and safety off") ?? false;
412416
413 if (release_safe and !release_fast) {417 const mode = if (release_safe and !release_fast) {
414 return builtin.Mode.ReleaseSafe;418 builtin.Mode.ReleaseSafe
415 } else if (release_fast and !release_safe) {419 } else if (release_fast and !release_safe) {
416 return builtin.Mode.ReleaseFast;420 builtin.Mode.ReleaseFast
417 } else if (!release_fast and !release_safe) {421 } else if (!release_fast and !release_safe) {
418 return builtin.Mode.Debug;422 builtin.Mode.Debug
419 } else {423 } else {
420 %%io.stderr.printf("Both -Drelease-safe and -Drelease-fast specified");424 %%io.stderr.printf("Both -Drelease-safe and -Drelease-fast specified");
421 self.markInvalidUserInput();425 self.markInvalidUserInput();
422 return builtin.Mode.Debug;426 builtin.Mode.Debug
423 }427 };
428 self.release_mode = mode;
429 return mode;
424 }430 }
425431
426 pub fn addUserInputOption(self: &Builder, name: []const u8, value: []const u8) -> bool {432 pub fn addUserInputOption(self: &Builder, name: []const u8, value: []const u8) -> bool {
test/standalone/pkg_import/build.zig+5
...@@ -4,6 +4,11 @@ pub fn build(b: &Builder) {...@@ -4,6 +4,11 @@ pub fn build(b: &Builder) {
4 const exe = b.addExecutable("test", "test.zig");4 const exe = b.addExecutable("test", "test.zig");
5 exe.addPackagePath("my_pkg", "pkg.zig");5 exe.addPackagePath("my_pkg", "pkg.zig");
66
7 // This is duplicated to test that you are allowed to call
8 // b.standardReleaseOptions() twice.
9 exe.setBuildMode(b.standardReleaseOptions());
10 exe.setBuildMode(b.standardReleaseOptions());
11
7 const run = b.addCommand(".", b.env_map, exe.getOutputPath(), [][]const u8{});12 const run = b.addCommand(".", b.env_map, exe.getOutputPath(), [][]const u8{});
8 run.step.dependOn(&exe.step);13 run.step.dependOn(&exe.step);
914