Skip to content

Compilation: strip debug info from ReleaseSmall by default #13067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,15 @@ pub fn build(b: *Builder) !void {
.Debug => {},
.ReleaseFast => {
zig1_obj.addArg("-OReleaseFast");
zig1_obj.addArg("--strip");
zig1_obj.addArg("-fstrip");
},
.ReleaseSafe => {
zig1_obj.addArg("-OReleaseSafe");
zig1_obj.addArg("--strip");
zig1_obj.addArg("-fstrip");
},
.ReleaseSmall => {
zig1_obj.addArg("-OReleaseSmall");
zig1_obj.addArg("--strip");
zig1_obj.addArg("-fstrip");
},
}
if (single_threaded orelse false) {
Expand Down
14 changes: 9 additions & 5 deletions lib/std/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,7 @@ pub const LibExeObjStep = struct {
kind: Kind,
major_only_filename: ?[]const u8,
name_only_filename: ?[]const u8,
strip: bool,
strip: ?bool,
// keep in sync with src/link.zig:CompressDebugSections
compress_debug_sections: enum { none, zlib } = .none,
lib_paths: ArrayList([]const u8),
Expand Down Expand Up @@ -1738,7 +1738,7 @@ pub const LibExeObjStep = struct {

const self = builder.allocator.create(LibExeObjStep) catch unreachable;
self.* = LibExeObjStep{
.strip = false,
.strip = null,
.builder = builder,
.verbose_link = false,
.verbose_cc = false,
Expand Down Expand Up @@ -1953,7 +1953,7 @@ pub const LibExeObjStep = struct {

pub fn producesPdbFile(self: *LibExeObjStep) bool {
if (!self.target.isWindows() and !self.target.isUefi()) return false;
if (self.strip) return false;
if (self.strip != null and self.strip.?) return false;
return self.isDynamicLibrary() or self.kind == .exe or self.kind == .test_exe;
}

Expand Down Expand Up @@ -2690,8 +2690,12 @@ pub const LibExeObjStep = struct {

if (self.emit_h) try zig_args.append("-femit-h");

if (self.strip) {
try zig_args.append("--strip");
if (self.strip) |strip| {
if (strip) {
try zig_args.append("-fstrip");
} else {
try zig_args.append("-fno-strip");
}
}

switch (self.compress_debug_sections) {
Expand Down
4 changes: 2 additions & 2 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,8 @@ pub const InitOptions = struct {
use_clang: ?bool = null,
use_stage1: ?bool = null,
single_threaded: ?bool = null,
strip: ?bool = null,
rdynamic: bool = false,
strip: bool = false,
function_sections: bool = false,
no_builtin: bool = false,
is_native_os: bool,
Expand Down Expand Up @@ -1422,7 +1422,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
break :blk buf.items[0 .. buf.items.len - 1 :0].ptr;
} else null;

const strip = options.strip or !target_util.hasDebugInfo(options.target);
const strip = options.strip orelse !target_util.hasDebugInfo(options.target);
const red_zone = options.want_red_zone orelse target_util.hasRedZone(options.target);
const omit_frame_pointer = options.omit_frame_pointer orelse (options.optimize_mode != .Debug);
const linker_optimization: u8 = options.linker_optimization orelse switch (options.optimize_mode) {
Expand Down
13 changes: 9 additions & 4 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ const usage_build_generic =
\\ -fno-builtin Disable implicit builtin knowledge of functions
\\ -ffunction-sections Places each function in a separate section
\\ -fno-function-sections All functions go into same section
\\ --strip Omit debug symbols
\\ -fstrip Omit debug symbols
\\ -fno-strip Keep debug symbols
\\ -ofmt=[mode] Override target object format
\\ elf Executable and Linking Format
\\ c C source code
Expand Down Expand Up @@ -630,7 +631,7 @@ fn buildOutputType(
var version: std.builtin.Version = .{ .major = 0, .minor = 0, .patch = 0 };
var have_version = false;
var compatibility_version: ?std.builtin.Version = null;
var strip = false;
var strip: ?bool = null;
var function_sections = false;
var no_builtin = false;
var watch = false;
Expand Down Expand Up @@ -1296,8 +1297,10 @@ fn buildOutputType(
} else if (mem.eql(u8, arg, "--show-builtin")) {
show_builtin = true;
emit_bin = .no;
} else if (mem.eql(u8, arg, "--strip")) {
} else if (mem.eql(u8, arg, "-fstrip")) {
strip = true;
} else if (mem.eql(u8, arg, "-fno-strip")) {
strip = false;
} else if (mem.eql(u8, arg, "-fsingle-threaded")) {
single_threaded = true;
} else if (mem.eql(u8, arg, "-fno-single-threaded")) {
Expand Down Expand Up @@ -1432,7 +1435,6 @@ fn buildOutputType(
.cc, .cpp => {
emit_h = .no;
soname = .no;
strip = false;
ensure_libc_on_non_freestanding = true;
ensure_libcpp_on_non_freestanding = arg_mode == .cpp;
want_native_include_dirs = true;
Expand Down Expand Up @@ -2186,6 +2188,9 @@ fn buildOutputType(
},
}

if (arg_mode == .build and optimize_mode == .ReleaseSmall and strip == null)
strip = true;

if (arg_mode == .translate_c and c_source_files.items.len != 1) {
fatal("translate-c expects exactly 1 source file (found {d})", .{c_source_files.items.len});
}
Expand Down
2 changes: 1 addition & 1 deletion test/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
"--cache-dir", dir_path,
"--name", "example",
"-fno-emit-bin", "-fno-emit-h",
"--strip", "-OReleaseFast",
"-fstrip", "-OReleaseFast",
example_zig_path,
});

Expand Down
1 change: 1 addition & 0 deletions test/link/wasm/archive/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn build(b: *Builder) void {
lib.use_llvm = false;
lib.use_stage1 = false;
lib.use_lld = false;
lib.strip = false;

const check = lib.checkObject(.wasm);
check.checkStart("Section import");
Expand Down
1 change: 1 addition & 0 deletions test/link/wasm/bss/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn build(b: *Builder) void {
lib.use_llvm = false;
lib.use_stage1 = false;
lib.use_lld = false;
lib.strip = false;
// to make sure the bss segment is emitted, we must import memory
lib.import_memory = true;
lib.install();
Expand Down
1 change: 1 addition & 0 deletions test/link/wasm/segments/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn build(b: *Builder) void {
lib.use_llvm = false;
lib.use_stage1 = false;
lib.use_lld = false;
lib.strip = false;
lib.install();

const check_lib = lib.checkObject(.wasm);
Expand Down
1 change: 1 addition & 0 deletions test/link/wasm/stack_pointer/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn build(b: *Builder) void {
lib.use_llvm = false;
lib.use_stage1 = false;
lib.use_lld = false;
lib.strip = false;
lib.stack_size = std.wasm.page_size * 2; // set an explicit stack size
lib.install();

Expand Down
1 change: 1 addition & 0 deletions test/link/wasm/type/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn build(b: *Builder) void {
lib.use_llvm = false;
lib.use_stage1 = false;
lib.use_lld = false;
lib.strip = false;
lib.install();

const check_lib = lib.checkObject(.wasm);
Expand Down