Commit d3a99c7bd5

Andrew Kelley <andrew@ziglang.org>
2020-09-29 09:50:20
add CLI options for darwin frameworks and -ffunction-sections
and add missing usage help text
1 parent cfbcb41
src/Compilation.zig
@@ -347,6 +347,7 @@ pub const InitOptions = struct {
     rdynamic: bool = false,
     strip: bool = false,
     single_threaded: bool = false,
+    function_sections: bool = false,
     is_native_os: bool,
     time_report: bool = false,
     stack_report: bool = false,
@@ -355,7 +356,6 @@ pub const InitOptions = struct {
     version_script: ?[]const u8 = null,
     override_soname: ?[]const u8 = null,
     linker_gc_sections: ?bool = null,
-    function_sections: ?bool = null,
     linker_allow_shlib_undefined: ?bool = null,
     linker_bind_global_refs_locally: ?bool = null,
     each_lib_rpath: ?bool = null,
@@ -538,7 +538,6 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
         };
 
         const single_threaded = options.single_threaded or target_util.isSingleThreaded(options.target);
-        const function_sections = options.function_sections orelse false;
 
         const llvm_cpu_features: ?[*:0]const u8 = if (build_options.have_llvm and use_llvm) blk: {
             var buf = std.ArrayList(u8).init(arena);
@@ -589,7 +588,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
         cache.hash.add(pic);
         cache.hash.add(stack_check);
         cache.hash.add(link_mode);
-        cache.hash.add(function_sections);
+        cache.hash.add(options.function_sections);
         cache.hash.add(strip);
         cache.hash.add(link_libc);
         cache.hash.add(options.link_libcpp);
@@ -757,7 +756,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
             .rpath_list = options.rpath_list,
             .strip = strip,
             .is_native_os = options.is_native_os,
-            .function_sections = options.function_sections orelse false,
+            .function_sections = options.function_sections,
             .allow_shlib_undefined = options.linker_allow_shlib_undefined,
             .bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false,
             .z_nodelete = options.linker_z_nodelete,
src/main.zig
@@ -260,11 +260,13 @@ const usage_build_generic =
     \\  -D[macro]=[value]         Define C [macro] to [value] (1 if [value] omitted)
     \\  --libc [file]             Provide a file which specifies libc paths
     \\  -cflags [flags] --        Set extra flags for the next positional C source files
+    \\  -ffunction-sections       Places each function in a separate section
     \\
     \\Link Options:
     \\  -l[lib], --library [lib]       Link against system library
     \\  -L[d], --library-directory [d] Add a directory to the library search path
-    \\  -T[script]                     Use a custom linker script
+    \\  -T[script], --script [script]  Use a custom linker script
+    \\  --version-script [path]        Provide a version .map file
     \\  --dynamic-linker [path]        Set the dynamic interpreter path (usually ld.so)
     \\  --each-lib-rpath               Add rpath for each used dynamic library
     \\  --version [ver]                Dynamic library semver
@@ -273,7 +275,11 @@ const usage_build_generic =
     \\  --eh-frame-hdr                 Enable C++ exception handling by passing --eh-frame-hdr to linker
     \\  -dynamic                       Force output to be dynamically linked
     \\  -static                        Force output to be statically linked
+    \\  -Bsymbolic                     Bind global references locally
     \\  --subsystem [subsystem]        (windows) /SUBSYSTEM:<subsystem> to the linker\n"
+    \\  --stack [size]                 Override default stack size
+    \\  -framework [name]              (darwin) link against framework
+    \\  -F[dir]                        (darwin) add search path for frameworks
     \\
     \\Test Options:
     \\  --test-filter [text]           Skip tests that do not match filter
@@ -381,6 +387,7 @@ fn buildOutputType(
     var have_version = false;
     var strip = false;
     var single_threaded = false;
+    var function_sections = false;
     var watch = false;
     var verbose_link = false;
     var verbose_cc = false;
@@ -634,7 +641,15 @@ fn buildOutputType(
                         if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
                         i += 1;
                         try lib_dirs.append(args[i]);
-                    } else if (mem.eql(u8, arg, "-T")) {
+                    } else if (mem.eql(u8, arg, "-F")) {
+                        if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
+                        i += 1;
+                        try framework_dirs.append(args[i]);
+                    } else if (mem.eql(u8, arg, "-framework")) {
+                        if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
+                        i += 1;
+                        try frameworks.append(args[i]);
+                    } else if (mem.eql(u8, arg, "-T") or mem.eql(u8, arg, "--script")) {
                         if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
                         i += 1;
                         linker_script = args[i];
@@ -819,6 +834,8 @@ fn buildOutputType(
                         strip = true;
                     } else if (mem.eql(u8, arg, "--single-threaded")) {
                         single_threaded = true;
+                    } else if (mem.eql(u8, arg, "-ffunction-sections")) {
+                        function_sections = true;
                     } else if (mem.eql(u8, arg, "--eh-frame-hdr")) {
                         link_eh_frame_hdr = true;
                     } else if (mem.eql(u8, arg, "-Bsymbolic")) {
@@ -843,6 +860,8 @@ fn buildOutputType(
                         linker_script = arg[2..];
                     } else if (mem.startsWith(u8, arg, "-L")) {
                         try lib_dirs.append(arg[2..]);
+                    } else if (mem.startsWith(u8, arg, "-F")) {
+                        try framework_dirs.append(arg[2..]);
                     } else if (mem.startsWith(u8, arg, "-l")) {
                         // We don't know whether this library is part of libc or libc++ until we resolve the target.
                         // So we simply append to the list for now.
@@ -1555,6 +1574,7 @@ fn buildOutputType(
         .stack_size_override = stack_size_override,
         .strip = strip,
         .single_threaded = single_threaded,
+        .function_sections = function_sections,
         .self_exe_path = self_exe_path,
         .rand = &default_prng.random,
         .clang_passthrough_mode = arg_mode != .build,
BRANCH_TODO
@@ -1,47 +1,3 @@
- * audit the CLI options for stage2
+ * building risc-v musl regression
+ * go ahead and use allocSentinel now that the stage1 bug is fixed
  * audit the base cache hash
- * try building some software with zig cc to make sure it didn't regress
-
- * On operating systems that support it, do an execve for `zig test` and `zig run` rather than child process.
- * implement proper parsing of clang stderr/stdout and exposing compile errors with the Compilation API
- * implement proper parsing of LLD stderr/stdout and exposing compile errors with the Compilation API
- * support cross compiling stage2 with `zig build`
- * implement proper compile errors for failing to build glibc crt files and shared libs
- * implement -fno-emit-bin
- * improve the stage2 tests to support testing with LLVM extensions enabled
- * implement emit-h in stage2
- * multi-thread building C objects
- * implement serialization/deserialization of incremental compilation metadata
- * incremental compilation - implement detection of which source files changed
- * improve the cache hash logic for c objects with respect to extra flags and file parameters
- * LLVM codegen backend: put a sub-arch in the triple in some cases
- * implement an LLVM backend for stage2
- * implement outputting dynamic libraries in self-hosted linker
- * implement outputting static libraries (archive files) in self-hosted linker
- * support linking against object files in self-hosted linker
- * avoid invoking lld when it's just 1 object file (the `zig cc -c` case)
- * `zig fmt --check` should output to stdout not stderr.
- * main.zig: If there was an argsAllocZ we could avoid this allocation
- * improve robustness of response file parsing
- * there are a couple panic("TODO") in clang options parsing
- * std.testing needs improvement to support exposing directory path for its tmp dir (look for "bogus")
- * integrate target features into building assembly code
- * libc_installation.zig: make it look for msvc only if msvc abi is chosen
- * switch the default C ABI for windows to be mingw-w64
-   - make it .obj instead of .o always for coff
- * change glibc log errors to normal exposed compile errors
- * improve Directory.join to only use 1 allocation in a clean way.
- * tracy builds with lc++
- * some kind of "zig identifier escape" function rather than unconditionally using @"" syntax
-   in builtin.zig
- * rename std.builtin.Mode to std.builtin.OptimizeMode
- * implement `zig run` and `zig test` when combined with `--watch`
- * close the --pkg-begin --pkg-end Package directory handles
- * make std.Progress support multithreaded
- * update musl.zig static data to use native path separator in static data rather than replacing '/' at runtime
- * linking hello world with LLD, lld is silently calling exit(1) instead of reporting ok=false. when run standalone the error message is: ld.lld: error: section [index 3] has a sh_offset (0x57000) + sh_size (0x68) that is greater than the file size (0x57060)
- * submit PR to godbolt and update the CLI options (see changes to test/cli.zig)
- * make proposal about log levels
- * proposal for changing fs Z/W functions to be native paths and have a way to do native path string literals
- * proposal for block { break x; }
- * generally look for the "TODO surface this as a real compile error message" and fix all that stuff