Commit b15d6b2a34

Lee Cannon <leecannon@leecannon.xyz>
2021-09-30 20:49:13
Add build.zig and command line flags
1 parent fdd11f6
lib/std/build.zig
@@ -1515,6 +1515,8 @@ pub const LibExeObjStep = struct {
 
     red_zone: ?bool = null,
 
+    omit_frame_pointer: ?bool = null,
+
     subsystem: ?std.Target.SubSystem = null,
 
     /// Overrides the default stack size
@@ -2406,6 +2408,13 @@ pub const LibExeObjStep = struct {
                 try zig_args.append("-mno-red-zone");
             }
         }
+        if (self.omit_frame_pointer) |omit_frame_pointer| {
+            if (omit_frame_pointer) {
+                try zig_args.append("-fomit-frame-pointer");
+            } else {
+                try zig_args.append("-fno-omit-frame-pointer");
+            }
+        }
         if (self.disable_sanitize_c) {
             try zig_args.append("-fno-sanitize-c");
         }
src/stage1/all_types.hpp
@@ -2175,6 +2175,7 @@ struct CodeGen {
     bool dll_export_fns;
     bool have_stack_probing;
     bool red_zone;
+    bool omit_frame_pointer;
     bool function_sections;
     bool include_compiler_rt;
     bool test_is_evented;
src/stage1/stage1.cpp
@@ -95,6 +95,7 @@ void zig_stage1_build_object(struct ZigStage1 *stage1) {
     g->unwind_tables = stage1->unwind_tables;
     g->have_stack_probing = stage1->enable_stack_probing;
     g->red_zone = stage1->red_zone;
+    g->omit_frame_pointer = stage1->omit_frame_pointer;
     g->is_single_threaded = stage1->is_single_threaded;
     g->valgrind_enabled = stage1->valgrind_enabled;
     g->tsan_enabled = stage1->tsan_enabled;
src/stage1/stage1.h
@@ -199,6 +199,7 @@ struct ZigStage1 {
     bool include_compiler_rt;
     bool enable_stack_probing;
     bool red_zone;
+    bool omit_frame_pointer;
     bool enable_time_report;
     bool enable_stack_report;
     bool test_is_evented;
src/clang_options_data.zig
@@ -3014,7 +3014,14 @@ flagpd1("fno-objc-infer-related-result-type"),
 flagpd1("fno-objc-legacy-dispatch"),
 flagpd1("fno-objc-nonfragile-abi"),
 flagpd1("fno-objc-weak"),
-flagpd1("fno-omit-frame-pointer"),
+.{
+    .name = "fno-omit-frame-pointer",
+    .syntax = .flag,
+    .zig_equivalent = .no_omit_frame_pointer,
+    .pd1 = true,
+    .pd2 = false,
+    .psl = false,
+},
 flagpd1("fno-openmp"),
 flagpd1("fno-openmp-cuda-force-full-runtime"),
 flagpd1("fno-openmp-cuda-mode"),
@@ -3235,7 +3242,14 @@ flagpd1("fobjc-runtime-has-weak"),
 flagpd1("fobjc-sender-dependent-dispatch"),
 flagpd1("fobjc-subscripting-legacy-runtime"),
 flagpd1("fobjc-weak"),
-flagpd1("fomit-frame-pointer"),
+.{
+    .name = "fomit-frame-pointer",
+    .syntax = .flag,
+    .zig_equivalent = .omit_frame_pointer,
+    .pd1 = true,
+    .pd2 = false,
+    .psl = false,
+},
 flagpd1("fopenmp"),
 flagpd1("fopenmp-cuda-force-full-runtime"),
 flagpd1("fopenmp-cuda-mode"),
src/Compilation.zig
@@ -680,6 +680,7 @@ pub const InitOptions = struct {
     want_sanitize_c: ?bool = null,
     want_stack_check: ?bool = null,
     want_red_zone: ?bool = null,
+    omit_frame_pointer: ?bool = null,
     want_valgrind: ?bool = null,
     want_tsan: ?bool = null,
     want_compiler_rt: ?bool = null,
@@ -1113,6 +1114,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
 
         const strip = options.strip or !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);
 
         // We put everything into the cache hash that *cannot be modified during an incremental update*.
         // For example, one cannot change the target between updates, but one can change source files,
@@ -1146,6 +1148,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
         cache.hash.add(tsan);
         cache.hash.add(stack_check);
         cache.hash.add(red_zone);
+        cache.hash.add(omit_frame_pointer);
         cache.hash.add(link_mode);
         cache.hash.add(options.function_sections);
         cache.hash.add(strip);
@@ -1416,6 +1419,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
             .tsan = tsan,
             .stack_check = stack_check,
             .red_zone = red_zone,
+            .omit_frame_pointer = omit_frame_pointer,
             .single_threaded = single_threaded,
             .verbose_link = options.verbose_link,
             .machine_code_model = options.machine_code_model,
@@ -3189,6 +3193,12 @@ pub fn addCCArgs(
                 try argv.append("-mno-red-zone");
             }
 
+            if (comp.bin_file.options.omit_frame_pointer) {
+                try argv.append("-fomit-frame-pointer");
+            } else {
+                try argv.append("-fno-omit-frame-pointer");
+            }
+
             switch (comp.bin_file.options.optimize_mode) {
                 .Debug => {
                     // windows c runtime requires -D_DEBUG if using debug libraries
@@ -4019,6 +4029,7 @@ fn buildOutputFromZig(
         .want_sanitize_c = false,
         .want_stack_check = false,
         .want_red_zone = comp.bin_file.options.red_zone,
+        .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
         .want_valgrind = false,
         .want_tsan = false,
         .want_pic = comp.bin_file.options.pic,
@@ -4302,6 +4313,7 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node
         .include_compiler_rt = include_compiler_rt,
         .enable_stack_probing = comp.bin_file.options.stack_check,
         .red_zone = comp.bin_file.options.red_zone,
+        .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
         .enable_time_report = comp.time_report,
         .enable_stack_report = comp.stack_report,
         .test_is_evented = comp.test_evented_io,
@@ -4451,6 +4463,7 @@ pub fn build_crt_file(
         .want_sanitize_c = false,
         .want_stack_check = false,
         .want_red_zone = comp.bin_file.options.red_zone,
+        .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
         .want_valgrind = false,
         .want_tsan = false,
         .want_pic = comp.bin_file.options.pic,
src/glibc.zig
@@ -953,6 +953,7 @@ fn buildSharedLib(
         .want_sanitize_c = false,
         .want_stack_check = false,
         .want_red_zone = comp.bin_file.options.red_zone,
+        .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
         .want_valgrind = false,
         .want_tsan = false,
         .emit_h = null,
src/libcxx.zig
@@ -189,6 +189,7 @@ pub fn buildLibCXX(comp: *Compilation) !void {
         .want_sanitize_c = false,
         .want_stack_check = false,
         .want_red_zone = comp.bin_file.options.red_zone,
+        .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
         .want_valgrind = false,
         .want_tsan = comp.bin_file.options.tsan,
         .want_pic = comp.bin_file.options.pic,
@@ -321,6 +322,7 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
         .want_sanitize_c = false,
         .want_stack_check = false,
         .want_red_zone = comp.bin_file.options.red_zone,
+        .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
         .want_valgrind = false,
         .want_tsan = comp.bin_file.options.tsan,
         .want_pic = comp.bin_file.options.pic,
src/libunwind.zig
@@ -113,6 +113,7 @@ pub fn buildStaticLib(comp: *Compilation) !void {
         .want_sanitize_c = false,
         .want_stack_check = false,
         .want_red_zone = comp.bin_file.options.red_zone,
+        .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
         .want_valgrind = false,
         .want_tsan = false,
         .want_pic = comp.bin_file.options.pic,
src/link.zig
@@ -90,6 +90,7 @@ pub const Options = struct {
     tsan: bool,
     stack_check: bool,
     red_zone: bool,
+    omit_frame_pointer: bool,
     single_threaded: bool,
     verbose_link: bool,
     dll_export_fns: bool,
src/main.zig
@@ -330,6 +330,8 @@ const usage_build_generic =
     \\            medium|large]
     \\  -mred-zone                Force-enable the "red-zone"
     \\  -mno-red-zone             Force-disable the "red-zone"
+    \\  -fomit-frame-pointer      Omit the stack frame pointer
+    \\  -fno-omit-frame-pointer   Store the stack frame pointer
     \\  -mexec-model=[value]      Execution model (WASI only)
     \\  --name [name]             Override root name (not a file path)
     \\  -O [mode]                 Choose what to optimize for
@@ -587,6 +589,7 @@ fn buildOutputType(
     var want_sanitize_c: ?bool = null;
     var want_stack_check: ?bool = null;
     var want_red_zone: ?bool = null;
+    var omit_frame_pointer: ?bool = null;
     var want_valgrind: ?bool = null;
     var want_tsan: ?bool = null;
     var want_compiler_rt: ?bool = null;
@@ -975,6 +978,10 @@ fn buildOutputType(
                         want_red_zone = true;
                     } else if (mem.eql(u8, arg, "-mno-red-zone")) {
                         want_red_zone = false;
+                    } else if (mem.eql(u8, arg, "-fomit-frame-pointer")) {
+                        omit_frame_pointer = true;
+                    } else if (mem.eql(u8, arg, "-fno-omit-frame-pointer")) {
+                        omit_frame_pointer = false;
                     } else if (mem.eql(u8, arg, "-fsanitize-c")) {
                         want_sanitize_c = true;
                     } else if (mem.eql(u8, arg, "-fno-sanitize-c")) {
@@ -1216,6 +1223,8 @@ fn buildOutputType(
                     .no_lto => want_lto = false,
                     .red_zone => want_red_zone = true,
                     .no_red_zone => want_red_zone = false,
+                    .omit_frame_pointer => omit_frame_pointer = true,
+                    .no_omit_frame_pointer => omit_frame_pointer = false,
                     .unwind_tables => want_unwind_tables = true,
                     .no_unwind_tables => want_unwind_tables = false,
                     .nostdlib => ensure_libc_on_non_freestanding = false,
@@ -2081,6 +2090,7 @@ fn buildOutputType(
         .want_sanitize_c = want_sanitize_c,
         .want_stack_check = want_stack_check,
         .want_red_zone = want_red_zone,
+        .omit_frame_pointer = omit_frame_pointer,
         .want_valgrind = want_valgrind,
         .want_tsan = want_tsan,
         .want_compiler_rt = want_compiler_rt,
@@ -3706,6 +3716,8 @@ pub const ClangArgIterator = struct {
         nostdlibinc,
         red_zone,
         no_red_zone,
+        omit_frame_pointer,
+        no_omit_frame_pointer,
         strip,
         exec_model,
         emit_llvm,
src/musl.zig
@@ -207,6 +207,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                 .want_sanitize_c = false,
                 .want_stack_check = false,
                 .want_red_zone = comp.bin_file.options.red_zone,
+                .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
                 .want_valgrind = false,
                 .want_tsan = false,
                 .emit_h = null,
src/stage1.zig
@@ -128,6 +128,7 @@ pub const Module = extern struct {
     include_compiler_rt: bool,
     enable_stack_probing: bool,
     red_zone: bool,
+    omit_frame_pointer: bool,
     enable_time_report: bool,
     enable_stack_report: bool,
     test_is_evented: bool,
tools/update_clang_options.zig
@@ -300,6 +300,14 @@ const known_options = [_]KnownOpt{
         .name = "mno-red-zone",
         .ident = "no_red_zone",
     },
+    .{
+        .name = "fomit-frame-pointer",
+        .ident = "omit_frame_pointer",
+    },
+    .{
+        .name = "fno-omit-frame-pointer",
+        .ident = "no_omit_frame_pointer",
+    },
     .{
         .name = "MD",
         .ident = "dep_file",