Commit 6ab9268a90

Andrew Kelley <andrew@ziglang.org>
2020-12-24 22:11:58
stage2: re-use compiler runtime libs across opt modes and strip flag
Previously Zig would need to recompile runtime libs if you changed the values of --strip or -O. Now, unless the `debug_compiler_runtime_libs` flag is set (which is currently not exposed to the CLI), Zig will always choose ReleaseFast or ReleaseSmall for compiler runtime libraries. When the main application chooses ReleaseFast or ReleaseSmall, that value is propagated to compiler runtime libraries. Otherwise a decision is made based on the target, which is currently ReleaseSmall for freestanding WebAssembly and ReleaseFast for everything else. Ultimately the purpose of this commit is to have Debug and ReleaseSafe builds of applications still get optimized builds of, e.g. libcxx and libunwind, as well as to spend less time unnecessarily rebuilding compiler runtime libraries.
1 parent 8944349
src/Compilation.zig
@@ -131,6 +131,7 @@ mutex: std.Mutex = .{},
 test_filter: ?[]const u8,
 test_name_prefix: ?[]const u8,
 test_evented_io: bool,
+debug_compiler_runtime_libs: bool,
 
 emit_asm: ?EmitLoc,
 emit_llvm_ir: ?EmitLoc,
@@ -429,6 +430,7 @@ pub const InitOptions = struct {
     verbose_llvm_cpu_features: bool = false,
     is_test: bool = false,
     test_evented_io: bool = false,
+    debug_compiler_runtime_libs: bool = false,
     /// Normally when you create a `Compilation`, Zig will automatically build
     /// and link in required dependencies, such as compiler-rt and libc. When
     /// building such dependencies themselves, this flag must be set to avoid
@@ -1025,6 +1027,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
             .test_filter = options.test_filter,
             .test_name_prefix = options.test_name_prefix,
             .test_evented_io = options.test_evented_io,
+            .debug_compiler_runtime_libs = options.debug_compiler_runtime_libs,
             .work_queue_wait_group = undefined,
         };
         break :comp comp;
@@ -2891,14 +2894,6 @@ fn buildOutputFromZig(
         .directory = null, // Put it in the cache directory.
         .basename = bin_basename,
     };
-    const optimize_mode: std.builtin.Mode = blk: {
-        if (comp.bin_file.options.is_test)
-            break :blk comp.bin_file.options.optimize_mode;
-        switch (comp.bin_file.options.optimize_mode) {
-            .Debug, .ReleaseFast, .ReleaseSafe => break :blk .ReleaseFast,
-            .ReleaseSmall => break :blk .ReleaseSmall,
-        }
-    };
     const sub_compilation = try Compilation.create(comp.gpa, .{
         .global_cache_directory = comp.global_cache_directory,
         .local_cache_directory = comp.global_cache_directory,
@@ -2910,7 +2905,7 @@ fn buildOutputFromZig(
         .thread_pool = comp.thread_pool,
         .libc_installation = comp.bin_file.options.libc_installation,
         .emit_bin = emit_bin,
-        .optimize_mode = optimize_mode,
+        .optimize_mode = comp.compilerRtOptMode(),
         .link_mode = .Static,
         .function_sections = true,
         .want_sanitize_c = false,
@@ -2920,7 +2915,7 @@ fn buildOutputFromZig(
         .want_pic = comp.bin_file.options.pic,
         .want_pie = comp.bin_file.options.pie,
         .emit_h = null,
-        .strip = comp.bin_file.options.strip,
+        .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
         .is_native_abi = comp.bin_file.options.is_native_abi,
         .self_exe_path = comp.self_exe_path,
@@ -3289,7 +3284,7 @@ pub fn build_crt_file(
         .thread_pool = comp.thread_pool,
         .libc_installation = comp.bin_file.options.libc_installation,
         .emit_bin = emit_bin,
-        .optimize_mode = comp.bin_file.options.optimize_mode,
+        .optimize_mode = comp.compilerRtOptMode(),
         .want_sanitize_c = false,
         .want_stack_check = false,
         .want_valgrind = false,
@@ -3297,7 +3292,7 @@ pub fn build_crt_file(
         .want_pic = comp.bin_file.options.pic,
         .want_pie = comp.bin_file.options.pie,
         .emit_h = null,
-        .strip = comp.bin_file.options.strip,
+        .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
         .is_native_abi = comp.bin_file.options.is_native_abi,
         .self_exe_path = comp.self_exe_path,
@@ -3344,3 +3339,26 @@ pub fn stage1AddLinkLib(comp: *Compilation, lib_name: []const u8) !void {
         });
     }
 }
+
+/// This decides the optimization mode for all zig-provided libraries, including
+/// compiler-rt, libcxx, libc, libunwind, etc.
+pub fn compilerRtOptMode(comp: Compilation) std.builtin.Mode {
+    if (comp.debug_compiler_runtime_libs) {
+        return comp.bin_file.options.optimize_mode;
+    }
+    switch (comp.bin_file.options.optimize_mode) {
+        .Debug, .ReleaseSafe => return target_util.defaultCompilerRtOptimizeMode(comp.getTarget()),
+        .ReleaseFast => return .ReleaseFast,
+        .ReleaseSmall => return .ReleaseSmall,
+    }
+}
+
+/// This decides whether to strip debug info for all zig-provided libraries, including
+/// compiler-rt, libcxx, libc, libunwind, etc.
+pub fn compilerRtStrip(comp: Compilation) bool {
+    if (comp.debug_compiler_runtime_libs) {
+        return comp.bin_file.options.strip;
+    } else {
+        return true;
+    }
+}
src/glibc.zig
@@ -271,7 +271,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                 try lib_path(comp, arena, lib_libc_glibc ++ "include" ++ path.sep_str ++ "libc-symbols.h"),
                 "-DTOP_NAMESPACE=glibc",
                 "-DASSEMBLER",
-                "-g",
                 "-Wa,--noexecstack",
             });
             return comp.build_crt_file("crti", .Obj, &[1]Compilation.CSourceFile{
@@ -291,7 +290,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                 try lib_path(comp, arena, lib_libc_glibc ++ "include" ++ path.sep_str ++ "libc-symbols.h"),
                 "-DTOP_NAMESPACE=glibc",
                 "-DASSEMBLER",
-                "-g",
                 "-Wa,--noexecstack",
             });
             return comp.build_crt_file("crtn", .Obj, &[1]Compilation.CSourceFile{
@@ -317,7 +315,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                     "-DSHARED",
                     "-DTOP_NAMESPACE=glibc",
                     "-DASSEMBLER",
-                    "-g",
                     "-Wa,--noexecstack",
                 });
                 break :blk .{
@@ -337,7 +334,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                     "-DMODULE_NAME=libc",
                     "-DTOP_NAMESPACE=glibc",
                     "-DASSEMBLER",
-                    "-g",
                     "-Wa,--noexecstack",
                 });
                 break :blk .{
@@ -372,8 +368,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                 try args.appendSlice(&[_][]const u8{
                     "-std=gnu11",
                     "-fgnu89-inline",
-                    "-g",
-                    "-O2",
                     "-fmerge-all-constants",
                     "-fno-stack-protector",
                     "-fmath-errno",
@@ -409,8 +403,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                 try args.appendSlice(&[_][]const u8{
                     "-std=gnu11",
                     "-fgnu89-inline",
-                    "-g",
-                    "-O2",
                     "-fmerge-all-constants",
                     "-fno-stack-protector",
                     "-fmath-errno",
@@ -939,13 +931,13 @@ fn buildSharedLib(
         .thread_pool = comp.thread_pool,
         .libc_installation = comp.bin_file.options.libc_installation,
         .emit_bin = emit_bin,
-        .optimize_mode = comp.bin_file.options.optimize_mode,
+        .optimize_mode = comp.compilerRtOptMode(),
         .want_sanitize_c = false,
         .want_stack_check = false,
         .want_valgrind = false,
         .want_tsan = false,
         .emit_h = null,
-        .strip = comp.bin_file.options.strip,
+        .strip = comp.compilerRtStrip(),
         .is_native_os = false,
         .is_native_abi = false,
         .self_exe_path = comp.self_exe_path,
src/libcxx.zig
@@ -138,7 +138,6 @@ pub fn buildLibCXX(comp: *Compilation) !void {
         try cflags.append("-I");
         try cflags.append(cxxabi_include_path);
 
-        try cflags.append("-O3");
         if (target_util.supports_fpic(target)) {
             try cflags.append("-fPIC");
         }
@@ -164,7 +163,7 @@ pub fn buildLibCXX(comp: *Compilation) !void {
         .thread_pool = comp.thread_pool,
         .libc_installation = comp.bin_file.options.libc_installation,
         .emit_bin = emit_bin,
-        .optimize_mode = comp.bin_file.options.optimize_mode,
+        .optimize_mode = comp.compilerRtOptMode(),
         .link_mode = link_mode,
         .want_sanitize_c = false,
         .want_stack_check = false,
@@ -173,7 +172,7 @@ pub fn buildLibCXX(comp: *Compilation) !void {
         .want_pic = comp.bin_file.options.pic,
         .want_pie = comp.bin_file.options.pie,
         .emit_h = null,
-        .strip = comp.bin_file.options.strip,
+        .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
         .is_native_abi = comp.bin_file.options.is_native_abi,
         .self_exe_path = comp.self_exe_path,
@@ -256,16 +255,12 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
         try cflags.append("-I");
         try cflags.append(cxx_include_path);
 
-        try cflags.append("-O3");
-        try cflags.append("-DNDEBUG");
         if (target_util.supports_fpic(target)) {
             try cflags.append("-fPIC");
         }
         try cflags.append("-nostdinc++");
         try cflags.append("-fstrict-aliasing");
         try cflags.append("-funwind-tables");
-        try cflags.append("-D_DEBUG");
-        try cflags.append("-UNDEBUG");
         try cflags.append("-std=c++11");
 
         c_source_files[i] = .{
@@ -285,7 +280,7 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
         .thread_pool = comp.thread_pool,
         .libc_installation = comp.bin_file.options.libc_installation,
         .emit_bin = emit_bin,
-        .optimize_mode = comp.bin_file.options.optimize_mode,
+        .optimize_mode = comp.compilerRtOptMode(),
         .link_mode = link_mode,
         .want_sanitize_c = false,
         .want_stack_check = false,
@@ -294,7 +289,7 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
         .want_pic = comp.bin_file.options.pic,
         .want_pie = comp.bin_file.options.pie,
         .emit_h = null,
-        .strip = comp.bin_file.options.strip,
+        .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
         .is_native_abi = comp.bin_file.options.is_native_abi,
         .self_exe_path = comp.self_exe_path,
src/libtsan.zig
@@ -43,8 +43,6 @@ pub fn buildTsan(comp: *Compilation) !void {
         try cflags.append("-I");
         try cflags.append(tsan_include_path);
 
-        try cflags.append("-O3");
-        try cflags.append("-DNDEBUG");
         try cflags.append("-nostdinc++");
         try cflags.append("-fvisibility-inlines-hidden");
         try cflags.append("-std=c++14");
@@ -67,8 +65,6 @@ pub fn buildTsan(comp: *Compilation) !void {
         try cflags.append("-I");
         try cflags.append(tsan_include_path);
 
-        try cflags.append("-O3");
-        try cflags.append("-DNDEBUG");
         try cflags.append("-nostdinc++");
         try cflags.append("-fvisibility-inlines-hidden");
         try cflags.append("-std=c++14");
@@ -110,8 +106,6 @@ pub fn buildTsan(comp: *Compilation) !void {
         try cflags.append("-I");
         try cflags.append(sanitizer_common_include_path);
 
-        try cflags.append("-O3");
-        try cflags.append("-DNDEBUG");
         try cflags.append("-nostdinc++");
         try cflags.append("-fvisibility-inlines-hidden");
         try cflags.append("-std=c++14");
@@ -136,8 +130,6 @@ pub fn buildTsan(comp: *Compilation) !void {
         try cflags.append("-I");
         try cflags.append(sanitizer_common_include_path);
 
-        try cflags.append("-O3");
-        try cflags.append("-DNDEBUG");
         try cflags.append("-nostdinc++");
         try cflags.append("-fvisibility-inlines-hidden");
         try cflags.append("-std=c++14");
@@ -158,8 +150,6 @@ pub fn buildTsan(comp: *Compilation) !void {
         try cflags.append("-I");
         try cflags.append(tsan_include_path);
 
-        try cflags.append("-O3");
-        try cflags.append("-DNDEBUG");
         try cflags.append("-nostdinc++");
         try cflags.append("-fvisibility-inlines-hidden");
         try cflags.append("-std=c++14");
@@ -188,8 +178,6 @@ pub fn buildTsan(comp: *Compilation) !void {
         try cflags.append("-I");
         try cflags.append(tsan_include_path);
 
-        try cflags.append("-O3");
-        try cflags.append("-DNDEBUG");
         try cflags.append("-nostdinc++");
         try cflags.append("-fvisibility-inlines-hidden");
         try cflags.append("-std=c++14");
@@ -218,7 +206,7 @@ pub fn buildTsan(comp: *Compilation) !void {
         .thread_pool = comp.thread_pool,
         .libc_installation = comp.bin_file.options.libc_installation,
         .emit_bin = emit_bin,
-        .optimize_mode = comp.bin_file.options.optimize_mode,
+        .optimize_mode = comp.compilerRtOptMode(),
         .link_mode = link_mode,
         .want_sanitize_c = false,
         .want_stack_check = false,
@@ -227,7 +215,7 @@ pub fn buildTsan(comp: *Compilation) !void {
         .want_pic = true,
         .want_pie = true,
         .emit_h = null,
-        .strip = comp.bin_file.options.strip,
+        .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
         .is_native_abi = comp.bin_file.options.is_native_abi,
         .self_exe_path = comp.self_exe_path,
src/libunwind.zig
@@ -104,7 +104,7 @@ pub fn buildStaticLib(comp: *Compilation) !void {
         .thread_pool = comp.thread_pool,
         .libc_installation = comp.bin_file.options.libc_installation,
         .emit_bin = emit_bin,
-        .optimize_mode = comp.bin_file.options.optimize_mode,
+        .optimize_mode = comp.compilerRtOptMode(),
         .link_mode = link_mode,
         .want_sanitize_c = false,
         .want_stack_check = false,
@@ -113,7 +113,7 @@ pub fn buildStaticLib(comp: *Compilation) !void {
         .want_pic = comp.bin_file.options.pic,
         .want_pie = comp.bin_file.options.pie,
         .emit_h = null,
-        .strip = comp.bin_file.options.strip,
+        .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
         .is_native_abi = comp.bin_file.options.is_native_abi,
         .self_exe_path = comp.self_exe_path,
src/mingw.zig
@@ -91,8 +91,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                     "-D_CRTBLD",
                     "-D_WIN32_WINNT=0x0f00",
                     "-D__MSVCRT_VERSION__=0x700",
-                    "-g",
-                    "-O2",
                 });
                 c_source_files[i] = .{
                     .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
@@ -119,9 +117,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
 
                 "-isystem",
                 try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libc", "include", "any-windows-any" }),
-
-                "-g",
-                "-O2",
             });
             var c_source_files = std.ArrayList(Compilation.CSourceFile).init(arena);
 
@@ -167,8 +162,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                 "-D_CRTBLD",
                 "-D_WIN32_WINNT=0x0f00",
                 "-D__MSVCRT_VERSION__=0x700",
-                "-g",
-                "-O2",
 
                 "-isystem",
                 try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libc", "include", "any-windows-any" }),
@@ -233,8 +226,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                 "-D_CRTBLD",
                 "-D_WIN32_WINNT=0x0f00",
                 "-D__MSVCRT_VERSION__=0x700",
-                "-g",
-                "-O2",
 
                 "-isystem",
                 try comp.zig_lib_directory.join(arena, &[_][]const u8{
src/musl.zig
@@ -203,13 +203,13 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                 .thread_pool = comp.thread_pool,
                 .libc_installation = comp.bin_file.options.libc_installation,
                 .emit_bin = Compilation.EmitLoc{ .directory = null, .basename = "libc.so" },
-                .optimize_mode = comp.bin_file.options.optimize_mode,
+                .optimize_mode = comp.compilerRtOptMode(),
                 .want_sanitize_c = false,
                 .want_stack_check = false,
                 .want_valgrind = false,
                 .want_tsan = false,
                 .emit_h = null,
-                .strip = comp.bin_file.options.strip,
+                .strip = comp.compilerRtStrip(),
                 .is_native_os = false,
                 .is_native_abi = false,
                 .self_exe_path = comp.self_exe_path,
src/target.zig
@@ -344,3 +344,11 @@ pub fn is_libcpp_lib_name(target: std.Target, name: []const u8) bool {
 pub fn hasDebugInfo(target: std.Target) bool {
     return !target.cpu.arch.isWasm();
 }
+
+pub fn defaultCompilerRtOptimizeMode(target: std.Target) std.builtin.Mode {
+    if (target.cpu.arch.isWasm() and target.os.tag == .freestanding) {
+        return .ReleaseSmall;
+    } else {
+        return .ReleaseFast;
+    }
+}