aboutsummaryrefslogtreecommitdiff
path: root/std/build.zig
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-05-02 17:34:21 -0400
committerAndrew Kelley <superjoe30@gmail.com>2017-05-02 17:34:21 -0400
commit24a9a42966dc9c0654314ad99866699208776025 (patch)
tree338bbf5f3aa39700e08eda107194b3ce93958059 /std/build.zig
parent7c236f6dd8194500f459b48621e2eb1997563caf (diff)
downloadzig-24a9a42966dc9c0654314ad99866699208776025.tar.gz
zig-24a9a42966dc9c0654314ad99866699208776025.zip
add safe release build mode
closes #288
Diffstat (limited to 'std/build.zig')
-rw-r--r--std/build.zig104
1 files changed, 69 insertions, 35 deletions
diff --git a/std/build.zig b/std/build.zig
index 939b4e8d57..4001aa4259 100644
--- a/std/build.zig
+++ b/std/build.zig
@@ -405,6 +405,23 @@ pub const Builder = struct {
return &step_info.step;
}
+ pub fn standardReleaseOptions(self: &Builder) -> builtin.Mode {
+ const release_safe = self.option(bool, "release-safe", "optimizations on and safety on") ?? false;
+ const release_fast = self.option(bool, "release-fast", "optimizations on and safety off") ?? false;
+
+ if (release_safe and !release_fast) {
+ return builtin.Mode.ReleaseSafe;
+ } else if (release_fast and !release_safe) {
+ return builtin.Mode.ReleaseFast;
+ } else if (!release_fast and !release_safe) {
+ return builtin.Mode.Debug;
+ } else {
+ %%io.stderr.printf("Both -Drelease-safe and -Drelease-fast specified");
+ self.markInvalidUserInput();
+ return builtin.Mode.Debug;
+ }
+ }
+
pub fn addUserInputOption(self: &Builder, name: []const u8, value: []const u8) -> bool {
test (%%self.user_input_options.put(name, UserInputOption {
.name = name,
@@ -667,7 +684,7 @@ pub const LibExeObjStep = struct {
linker_script: ?[]const u8,
link_libs: BufSet,
verbose: bool,
- release: bool,
+ build_mode: builtin.Mode,
static: bool,
output_path: ?[]const u8,
output_h_path: ?[]const u8,
@@ -724,7 +741,7 @@ pub const LibExeObjStep = struct {
var self = LibExeObjStep {
.builder = builder,
.verbose = false,
- .release = false,
+ .build_mode = builtin.Mode.Debug,
.static = static,
.kind = kind,
.root_src = root_src,
@@ -794,8 +811,8 @@ pub const LibExeObjStep = struct {
self.verbose = value;
}
- pub fn setRelease(self: &LibExeObjStep, value: bool) {
- self.release = value;
+ pub fn setBuildMode(self: &LibExeObjStep, mode: builtin.Mode) {
+ self.build_mode = mode;
}
pub fn setOutputPath(self: &LibExeObjStep, value: []const u8) {
@@ -886,8 +903,10 @@ pub const LibExeObjStep = struct {
%%zig_args.append("--verbose");
}
- if (self.release) {
- %%zig_args.append("--release");
+ switch (self.build_mode) {
+ builtin.Mode.Debug => {},
+ builtin.Mode.ReleaseSafe => %%zig_args.append("--release-safe"),
+ builtin.Mode.ReleaseFast => %%zig_args.append("--release-fast"),
}
%%zig_args.append("--cache-dir");
@@ -980,7 +999,7 @@ pub const TestStep = struct {
step: Step,
builder: &Builder,
root_src: []const u8,
- release: bool,
+ build_mode: builtin.Mode,
verbose: bool,
link_libs: BufSet,
name_prefix: []const u8,
@@ -992,7 +1011,7 @@ pub const TestStep = struct {
.step = Step.init(step_name, builder.allocator, make),
.builder = builder,
.root_src = root_src,
- .release = false,
+ .build_mode = builtin.Mode.Debug,
.verbose = false,
.name_prefix = "",
.filter = null,
@@ -1004,8 +1023,8 @@ pub const TestStep = struct {
self.verbose = value;
}
- pub fn setRelease(self: &TestStep, value: bool) {
- self.release = value;
+ pub fn setBuildMode(self: &TestStep, mode: builtin.Mode) {
+ self.build_mode = mode;
}
pub fn linkSystemLibrary(self: &TestStep, name: []const u8) {
@@ -1034,8 +1053,10 @@ pub const TestStep = struct {
%%zig_args.append("--verbose");
}
- if (self.release) {
- %%zig_args.append("--release");
+ switch (self.build_mode) {
+ builtin.Mode.Debug => {},
+ builtin.Mode.ReleaseSafe => %%zig_args.append("--release-safe"),
+ builtin.Mode.ReleaseFast => %%zig_args.append("--release-fast"),
}
test (self.filter) |filter| {
@@ -1095,6 +1116,8 @@ pub const CLibExeObjStep = struct {
name_only_filename: []const u8,
object_src: []const u8,
kind: Kind,
+ build_mode: builtin.Mode,
+ strip: bool,
const Kind = enum {
Exe,
@@ -1147,6 +1170,8 @@ pub const CLibExeObjStep = struct {
.major_only_filename = undefined,
.name_only_filename = undefined,
.object_src = undefined,
+ .build_mode = builtin.Mode.Debug,
+ .strip = false,
};
clib.computeOutFileNames();
return clib;
@@ -1233,13 +1258,8 @@ pub const CLibExeObjStep = struct {
%%self.include_dirs.append(path);
}
- pub fn addCompileFlagsForRelease(self: &CLibExeObjStep, release: bool) {
- if (release) {
- %%self.cflags.append("-g");
- %%self.cflags.append("-O2");
- } else {
- %%self.cflags.append("-g");
- }
+ pub fn setBuildMode(self: &CLibExeObjStep, build_mode: builtin.Mode) {
+ self.build_mode = build_mode;
}
pub fn addCompileFlags(self: &CLibExeObjStep, flags: []const []const u8) {
@@ -1248,6 +1268,34 @@ pub const CLibExeObjStep = struct {
}
}
+ fn appendCompileFlags(self: &CLibExeObjStep, args: &List([]const u8)) {
+ if (!self.strip) {
+ %%args.append("-g");
+ }
+ switch (self.build_mode) {
+ builtin.Mode.Debug => {},
+ builtin.Mode.ReleaseSafe => {
+ %%args.append("-O2");
+ %%args.append("-D_FORTIFY_SOURCE=2");
+ %%args.append("-fstack-protector-strong");
+ %%args.append("--param");
+ %%args.append("ssp-buffer-size=4");
+ },
+ builtin.Mode.ReleaseFast => {
+ %%args.append("-O2");
+ },
+ }
+
+ for (self.include_dirs.toSliceConst()) |dir| {
+ %%args.append("-I");
+ %%args.append(self.builder.pathFromRoot(dir));
+ }
+
+ for (self.cflags.toSliceConst()) |cflag| {
+ %%args.append(cflag);
+ }
+ }
+
fn make(step: &Step) -> %void {
const self = @fieldParentPtr(CLibExeObjStep, "step", step);
const cc = os.getEnv("CC") ?? "cc";
@@ -1265,14 +1313,7 @@ pub const CLibExeObjStep = struct {
%%cc_args.append("-o");
%%cc_args.append(output_path);
- for (self.cflags.toSliceConst()) |cflag| {
- %%cc_args.append(cflag);
- }
-
- for (self.include_dirs.toSliceConst()) |dir| {
- %%cc_args.append("-I");
- %%cc_args.append(builder.pathFromRoot(dir));
- }
+ self.appendCompileFlags(&cc_args);
%return builder.spawnChild(cc, cc_args.toSliceConst());
},
@@ -1295,14 +1336,7 @@ pub const CLibExeObjStep = struct {
%%cc_args.append("-o");
%%cc_args.append(cache_o_file);
- for (self.cflags.toSliceConst()) |cflag| {
- %%cc_args.append(cflag);
- }
-
- for (self.include_dirs.toSliceConst()) |dir| {
- %%cc_args.append("-I");
- %%cc_args.append(builder.pathFromRoot(dir));
- }
+ self.appendCompileFlags(&cc_args);
%return builder.spawnChild(cc, cc_args.toSliceConst());