| author | |
| committer | |
| log | 0dd28920daa5127ffe5a3691343fa519f7547cfd |
| tree | 949f6078a42f12415508667377b40ea0e84eac0b |
| parent | efc5c97bff87d4c28ae9642fe69d9bc2c7e9eeb7 |
MachO linker now handles `-needed-l<name>`, `-needed_library=<name>`
and `-needed_framework=<name>`. While on macOS `-l` is equivalent
to `-needed-l`, and `-framework` to `-needed_framework`, it can be
used to the same effect as on Linux if combined with `-dead_strip_dylibs`.
This commit also adds handling for `-needed_library` which is macOS
specific flag only (in addition to `-needed-l`).
Finally, in order to leverage new linker testing harness, this commit
added ability to specify lowering to those flags via `build.zig`:
`linkSystemLibraryNeeded` (and related), and `linkFrameworkNeeded`.15 files changed, 227 insertions(+), 56 deletions(-)
lib/std/build.zig+61-11| ... | ... | @@ -11,7 +11,6 @@ const ArrayList = std.ArrayList; |
| 11 | 11 | const StringHashMap = std.StringHashMap; |
| 12 | 12 | const Allocator = mem.Allocator; |
| 13 | 13 | const process = std.process; |
| 14 | const BufSet = std.BufSet; | |
| 15 | 14 | const EnvMap = std.process.EnvMap; |
| 16 | 15 | const fmt_lib = std.fmt; |
| 17 | 16 | const File = std.fs.File; |
| ... | ... | @@ -1484,7 +1483,7 @@ pub const LibExeObjStep = struct { |
| 1484 | 1483 | lib_paths: ArrayList([]const u8), |
| 1485 | 1484 | rpaths: ArrayList([]const u8), |
| 1486 | 1485 | framework_dirs: ArrayList([]const u8), |
| 1487 | frameworks: BufSet, | |
| 1486 | frameworks: StringHashMap(bool), | |
| 1488 | 1487 | verbose_link: bool, |
| 1489 | 1488 | verbose_cc: bool, |
| 1490 | 1489 | emit_analysis: EmitOption = .default, |
| ... | ... | @@ -1643,6 +1642,7 @@ pub const LibExeObjStep = struct { |
| 1643 | 1642 | |
| 1644 | 1643 | pub const SystemLib = struct { |
| 1645 | 1644 | name: []const u8, |
| 1645 | needed: bool, | |
| 1646 | 1646 | use_pkg_config: enum { |
| 1647 | 1647 | /// Don't use pkg-config, just pass -lfoo where foo is name. |
| 1648 | 1648 | no, |
| ... | ... | @@ -1744,7 +1744,7 @@ pub const LibExeObjStep = struct { |
| 1744 | 1744 | .kind = kind, |
| 1745 | 1745 | .root_src = root_src, |
| 1746 | 1746 | .name = name, |
| 1747 | .frameworks = BufSet.init(builder.allocator), | |
| 1747 | .frameworks = StringHashMap(bool).init(builder.allocator), | |
| 1748 | 1748 | .step = Step.init(base_id, name, builder.allocator, make), |
| 1749 | 1749 | .version = ver, |
| 1750 | 1750 | .out_filename = undefined, |
| ... | ... | @@ -1893,8 +1893,11 @@ pub const LibExeObjStep = struct { |
| 1893 | 1893 | } |
| 1894 | 1894 | |
| 1895 | 1895 | pub fn linkFramework(self: *LibExeObjStep, framework_name: []const u8) void { |
| 1896 | // Note: No need to dupe because frameworks dupes internally. | |
| 1897 | self.frameworks.insert(framework_name) catch unreachable; | |
| 1896 | self.frameworks.put(self.builder.dupe(framework_name), false) catch unreachable; | |
| 1897 | } | |
| 1898 | ||
| 1899 | pub fn linkFrameworkNeeded(self: *LibExeObjStep, framework_name: []const u8) void { | |
| 1900 | self.frameworks.put(self.builder.dupe(framework_name), true) catch unreachable; | |
| 1898 | 1901 | } |
| 1899 | 1902 | |
| 1900 | 1903 | /// Returns whether the library, executable, or object depends on a particular system library. |
| ... | ... | @@ -1935,6 +1938,7 @@ pub const LibExeObjStep = struct { |
| 1935 | 1938 | self.link_objects.append(.{ |
| 1936 | 1939 | .system_lib = .{ |
| 1937 | 1940 | .name = "c", |
| 1941 | .needed = false, | |
| 1938 | 1942 | .use_pkg_config = .no, |
| 1939 | 1943 | }, |
| 1940 | 1944 | }) catch unreachable; |
| ... | ... | @@ -1947,6 +1951,7 @@ pub const LibExeObjStep = struct { |
| 1947 | 1951 | self.link_objects.append(.{ |
| 1948 | 1952 | .system_lib = .{ |
| 1949 | 1953 | .name = "c++", |
| 1954 | .needed = false, | |
| 1950 | 1955 | .use_pkg_config = .no, |
| 1951 | 1956 | }, |
| 1952 | 1957 | }) catch unreachable; |
| ... | ... | @@ -1971,6 +1976,19 @@ pub const LibExeObjStep = struct { |
| 1971 | 1976 | self.link_objects.append(.{ |
| 1972 | 1977 | .system_lib = .{ |
| 1973 | 1978 | .name = self.builder.dupe(name), |
| 1979 | .needed = false, | |
| 1980 | .use_pkg_config = .no, | |
| 1981 | }, | |
| 1982 | }) catch unreachable; | |
| 1983 | } | |
| 1984 | ||
| 1985 | /// This one has no integration with anything, it just puts -needed-lname on the command line. | |
| 1986 | /// Prefer to use `linkSystemLibraryNeeded` instead. | |
| 1987 | pub fn linkSystemLibraryNeededName(self: *LibExeObjStep, name: []const u8) void { | |
| 1988 | self.link_objects.append(.{ | |
| 1989 | .system_lib = .{ | |
| 1990 | .name = self.builder.dupe(name), | |
| 1991 | .needed = true, | |
| 1974 | 1992 | .use_pkg_config = .no, |
| 1975 | 1993 | }, |
| 1976 | 1994 | }) catch unreachable; |
| ... | ... | @@ -1982,6 +2000,19 @@ pub const LibExeObjStep = struct { |
| 1982 | 2000 | self.link_objects.append(.{ |
| 1983 | 2001 | .system_lib = .{ |
| 1984 | 2002 | .name = self.builder.dupe(lib_name), |
| 2003 | .needed = false, | |
| 2004 | .use_pkg_config = .force, | |
| 2005 | }, | |
| 2006 | }) catch unreachable; | |
| 2007 | } | |
| 2008 | ||
| 2009 | /// This links against a system library, exclusively using pkg-config to find the library. | |
| 2010 | /// Prefer to use `linkSystemLibraryNeeded` instead. | |
| 2011 | pub fn linkSystemLibraryNeededPkgConfigOnly(self: *LibExeObjStep, lib_name: []const u8) void { | |
| 2012 | self.link_objects.append(.{ | |
| 2013 | .system_lib = .{ | |
| 2014 | .name = self.builder.dupe(lib_name), | |
| 2015 | .needed = true, | |
| 1985 | 2016 | .use_pkg_config = .force, |
| 1986 | 2017 | }, |
| 1987 | 2018 | }) catch unreachable; |
| ... | ... | @@ -2084,6 +2115,14 @@ pub const LibExeObjStep = struct { |
| 2084 | 2115 | } |
| 2085 | 2116 | |
| 2086 | 2117 | pub fn linkSystemLibrary(self: *LibExeObjStep, name: []const u8) void { |
| 2118 | self.linkSystemLibraryInner(name, false); | |
| 2119 | } | |
| 2120 | ||
| 2121 | pub fn linkSystemLibraryNeeded(self: *LibExeObjStep, name: []const u8) void { | |
| 2122 | self.linkSystemLibraryInner(name, true); | |
| 2123 | } | |
| 2124 | ||
| 2125 | fn linkSystemLibraryInner(self: *LibExeObjStep, name: []const u8, needed: bool) void { | |
| 2087 | 2126 | if (isLibCLibrary(name)) { |
| 2088 | 2127 | self.linkLibC(); |
| 2089 | 2128 | return; |
| ... | ... | @@ -2096,6 +2135,7 @@ pub const LibExeObjStep = struct { |
| 2096 | 2135 | self.link_objects.append(.{ |
| 2097 | 2136 | .system_lib = .{ |
| 2098 | 2137 | .name = self.builder.dupe(name), |
| 2138 | .needed = needed, | |
| 2099 | 2139 | .use_pkg_config = .yes, |
| 2100 | 2140 | }, |
| 2101 | 2141 | }) catch unreachable; |
| ... | ... | @@ -2437,7 +2477,7 @@ pub const LibExeObjStep = struct { |
| 2437 | 2477 | if (!other.isDynamicLibrary()) { |
| 2438 | 2478 | var it = other.frameworks.iterator(); |
| 2439 | 2479 | while (it.next()) |framework| { |
| 2440 | self.frameworks.insert(framework.*) catch unreachable; | |
| 2480 | self.frameworks.put(framework.key_ptr.*, framework.value_ptr.*) catch unreachable; | |
| 2441 | 2481 | } |
| 2442 | 2482 | } |
| 2443 | 2483 | }, |
| ... | ... | @@ -2473,8 +2513,9 @@ pub const LibExeObjStep = struct { |
| 2473 | 2513 | }, |
| 2474 | 2514 | |
| 2475 | 2515 | .system_lib => |system_lib| { |
| 2516 | const prefix: []const u8 = if (system_lib.needed) "-needed-l" else "-l"; | |
| 2476 | 2517 | switch (system_lib.use_pkg_config) { |
| 2477 | .no => try zig_args.append(builder.fmt("-l{s}", .{system_lib.name})), | |
| 2518 | .no => try zig_args.append(builder.fmt("{s}{s}", .{ prefix, system_lib.name })), | |
| 2478 | 2519 | .yes, .force => { |
| 2479 | 2520 | if (self.runPkgConfig(system_lib.name)) |args| { |
| 2480 | 2521 | try zig_args.appendSlice(args); |
| ... | ... | @@ -2488,7 +2529,10 @@ pub const LibExeObjStep = struct { |
| 2488 | 2529 | .yes => { |
| 2489 | 2530 | // pkg-config failed, so fall back to linking the library |
| 2490 | 2531 | // by name directly. |
| 2491 | try zig_args.append(builder.fmt("-l{s}", .{system_lib.name})); | |
| 2532 | try zig_args.append(builder.fmt("{s}{s}", .{ | |
| 2533 | prefix, | |
| 2534 | system_lib.name, | |
| 2535 | })); | |
| 2492 | 2536 | }, |
| 2493 | 2537 | .force => { |
| 2494 | 2538 | panic("pkg-config failed for library {s}", .{system_lib.name}); |
| ... | ... | @@ -2972,9 +3016,15 @@ pub const LibExeObjStep = struct { |
| 2972 | 3016 | } |
| 2973 | 3017 | |
| 2974 | 3018 | var it = self.frameworks.iterator(); |
| 2975 | while (it.next()) |framework| { | |
| 2976 | zig_args.append("-framework") catch unreachable; | |
| 2977 | zig_args.append(framework.*) catch unreachable; | |
| 3019 | while (it.next()) |entry| { | |
| 3020 | const name = entry.key_ptr.*; | |
| 3021 | const needed = entry.value_ptr.*; | |
| 3022 | if (needed) { | |
| 3023 | zig_args.append("-needed_framework") catch unreachable; | |
| 3024 | } else { | |
| 3025 | zig_args.append("-framework") catch unreachable; | |
| 3026 | } | |
| 3027 | zig_args.append(name) catch unreachable; | |
| 2978 | 3028 | } |
| 2979 | 3029 | } else { |
| 2980 | 3030 | if (self.framework_dirs.items.len > 0) { |
src/Compilation.zig+4-4| ... | ... | @@ -791,7 +791,7 @@ pub const InitOptions = struct { |
| 791 | 791 | c_source_files: []const CSourceFile = &[0]CSourceFile{}, |
| 792 | 792 | link_objects: []LinkObject = &[0]LinkObject{}, |
| 793 | 793 | framework_dirs: []const []const u8 = &[0][]const u8{}, |
| 794 | frameworks: []const []const u8 = &[0][]const u8{}, | |
| 794 | frameworks: std.StringArrayHashMapUnmanaged(SystemLib) = .{}, | |
| 795 | 795 | system_lib_names: []const []const u8 = &.{}, |
| 796 | 796 | system_lib_infos: []const SystemLib = &.{}, |
| 797 | 797 | /// These correspond to the WASI libc emulated subcomponents including: |
| ... | ... | @@ -1097,7 +1097,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1097 | 1097 | // Our linker can't handle objects or most advanced options yet. |
| 1098 | 1098 | if (options.link_objects.len != 0 or |
| 1099 | 1099 | options.c_source_files.len != 0 or |
| 1100 | options.frameworks.len != 0 or | |
| 1100 | options.frameworks.count() != 0 or | |
| 1101 | 1101 | options.system_lib_names.len != 0 or |
| 1102 | 1102 | options.link_libc or options.link_libcpp or |
| 1103 | 1103 | link_eh_frame_hdr or |
| ... | ... | @@ -1215,7 +1215,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1215 | 1215 | options.target, |
| 1216 | 1216 | options.is_native_abi, |
| 1217 | 1217 | link_libc, |
| 1218 | options.system_lib_names.len != 0 or options.frameworks.len != 0, | |
| 1218 | options.system_lib_names.len != 0 or options.frameworks.count() != 0, | |
| 1219 | 1219 | options.libc_installation, |
| 1220 | 1220 | options.native_darwin_sdk != null, |
| 1221 | 1221 | ); |
| ... | ... | @@ -2485,7 +2485,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes |
| 2485 | 2485 | |
| 2486 | 2486 | // Mach-O specific stuff |
| 2487 | 2487 | man.hash.addListOfBytes(comp.bin_file.options.framework_dirs); |
| 2488 | man.hash.addListOfBytes(comp.bin_file.options.frameworks); | |
| 2488 | link.hashAddSystemLibs(&man.hash, comp.bin_file.options.frameworks); | |
| 2489 | 2489 | try man.addOptionalFile(comp.bin_file.options.entitlements); |
| 2490 | 2490 | man.hash.addOptional(comp.bin_file.options.pagezero_size); |
| 2491 | 2491 | man.hash.addOptional(comp.bin_file.options.search_strategy); |
src/link.zig+1-1| ... | ... | @@ -162,7 +162,7 @@ pub const Options = struct { |
| 162 | 162 | |
| 163 | 163 | objects: []Compilation.LinkObject, |
| 164 | 164 | framework_dirs: []const []const u8, |
| 165 | frameworks: []const []const u8, | |
| 165 | frameworks: std.StringArrayHashMapUnmanaged(SystemLib), | |
| 166 | 166 | system_libs: std.StringArrayHashMapUnmanaged(SystemLib), |
| 167 | 167 | wasi_emulated_libs: []const wasi_libc.CRTFile, |
| 168 | 168 | lib_dirs: []const []const u8, |
src/link/MachO.zig+47-27| ... | ... | @@ -561,7 +561,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 561 | 561 | man.hash.add(self.base.options.dead_strip_dylibs); |
| 562 | 562 | man.hash.addListOfBytes(self.base.options.lib_dirs); |
| 563 | 563 | man.hash.addListOfBytes(self.base.options.framework_dirs); |
| 564 | man.hash.addListOfBytes(self.base.options.frameworks); | |
| 564 | link.hashAddSystemLibs(&man.hash, self.base.options.frameworks); | |
| 565 | 565 | man.hash.addListOfBytes(self.base.options.rpath_list); |
| 566 | 566 | if (is_dyn_lib) { |
| 567 | 567 | man.hash.addOptionalBytes(self.base.options.install_name); |
| ... | ... | @@ -768,19 +768,20 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 768 | 768 | } |
| 769 | 769 | |
| 770 | 770 | // Shared and static libraries passed via `-l` flag. |
| 771 | var search_lib_names = std.ArrayList([]const u8).init(arena); | |
| 771 | var candidate_libs = std.StringArrayHashMap(Compilation.SystemLib).init(arena); | |
| 772 | 772 | |
| 773 | const system_libs = self.base.options.system_libs.keys(); | |
| 774 | for (system_libs) |link_lib| { | |
| 773 | const system_lib_names = self.base.options.system_libs.keys(); | |
| 774 | for (system_lib_names) |system_lib_name| { | |
| 775 | 775 | // By this time, we depend on these libs being dynamically linked libraries and not static libraries |
| 776 | 776 | // (the check for that needs to be earlier), but they could be full paths to .dylib files, in which |
| 777 | 777 | // case we want to avoid prepending "-l". |
| 778 | if (Compilation.classifyFileExt(link_lib) == .shared_library) { | |
| 779 | try positionals.append(link_lib); | |
| 778 | if (Compilation.classifyFileExt(system_lib_name) == .shared_library) { | |
| 779 | try positionals.append(system_lib_name); | |
| 780 | 780 | continue; |
| 781 | 781 | } |
| 782 | 782 | |
| 783 | try search_lib_names.append(link_lib); | |
| 783 | const system_lib_info = self.base.options.system_libs.get(system_lib_name).?; | |
| 784 | try candidate_libs.put(system_lib_name, system_lib_info); | |
| 784 | 785 | } |
| 785 | 786 | |
| 786 | 787 | var lib_dirs = std.ArrayList([]const u8).init(arena); |
| ... | ... | @@ -792,18 +793,18 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 792 | 793 | } |
| 793 | 794 | } |
| 794 | 795 | |
| 795 | var libs = std.ArrayList([]const u8).init(arena); | |
| 796 | var libs = std.StringArrayHashMap(Compilation.SystemLib).init(arena); | |
| 796 | 797 | |
| 797 | 798 | // Assume ld64 default -search_paths_first if no strategy specified. |
| 798 | 799 | const search_strategy = self.base.options.search_strategy orelse .paths_first; |
| 799 | outer: for (search_lib_names.items) |lib_name| { | |
| 800 | outer: for (candidate_libs.keys()) |lib_name| { | |
| 800 | 801 | switch (search_strategy) { |
| 801 | 802 | .paths_first => { |
| 802 | 803 | // Look in each directory for a dylib (stub first), and then for archive |
| 803 | 804 | for (lib_dirs.items) |dir| { |
| 804 | 805 | for (&[_][]const u8{ ".tbd", ".dylib", ".a" }) |ext| { |
| 805 | 806 | if (try resolveLib(arena, dir, lib_name, ext)) |full_path| { |
| 806 | try libs.append(full_path); | |
| 807 | try libs.put(full_path, candidate_libs.get(lib_name).?); | |
| 807 | 808 | continue :outer; |
| 808 | 809 | } |
| 809 | 810 | } |
| ... | ... | @@ -817,13 +818,13 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 817 | 818 | for (lib_dirs.items) |dir| { |
| 818 | 819 | for (&[_][]const u8{ ".tbd", ".dylib" }) |ext| { |
| 819 | 820 | if (try resolveLib(arena, dir, lib_name, ext)) |full_path| { |
| 820 | try libs.append(full_path); | |
| 821 | try libs.put(full_path, candidate_libs.get(lib_name).?); | |
| 821 | 822 | continue :outer; |
| 822 | 823 | } |
| 823 | 824 | } |
| 824 | 825 | } else for (lib_dirs.items) |dir| { |
| 825 | 826 | if (try resolveLib(arena, dir, lib_name, ".a")) |full_path| { |
| 826 | try libs.append(full_path); | |
| 827 | try libs.put(full_path, candidate_libs.get(lib_name).?); | |
| 827 | 828 | } else { |
| 828 | 829 | log.warn("library not found for '-l{s}'", .{lib_name}); |
| 829 | 830 | lib_not_found = true; |
| ... | ... | @@ -847,7 +848,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 847 | 848 | // re-exports every single symbol definition. |
| 848 | 849 | for (lib_dirs.items) |dir| { |
| 849 | 850 | if (try resolveLib(arena, dir, "System", ".tbd")) |full_path| { |
| 850 | try libs.append(full_path); | |
| 851 | try libs.put(full_path, .{ .needed = false }); | |
| 851 | 852 | libsystem_available = true; |
| 852 | 853 | break :blk; |
| 853 | 854 | } |
| ... | ... | @@ -857,8 +858,8 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 857 | 858 | for (lib_dirs.items) |dir| { |
| 858 | 859 | if (try resolveLib(arena, dir, "System", ".dylib")) |libsystem_path| { |
| 859 | 860 | if (try resolveLib(arena, dir, "c", ".dylib")) |libc_path| { |
| 860 | try libs.append(libsystem_path); | |
| 861 | try libs.append(libc_path); | |
| 861 | try libs.put(libsystem_path, .{ .needed = false }); | |
| 862 | try libs.put(libc_path, .{ .needed = false }); | |
| 862 | 863 | libsystem_available = true; |
| 863 | 864 | break :blk; |
| 864 | 865 | } |
| ... | ... | @@ -872,7 +873,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 872 | 873 | const full_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 873 | 874 | "libc", "darwin", libsystem_name, |
| 874 | 875 | }); |
| 875 | try libs.append(full_path); | |
| 876 | try libs.put(full_path, .{ .needed = false }); | |
| 876 | 877 | } |
| 877 | 878 | |
| 878 | 879 | // frameworks |
| ... | ... | @@ -885,16 +886,16 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 885 | 886 | } |
| 886 | 887 | } |
| 887 | 888 | |
| 888 | outer: for (self.base.options.frameworks) |framework| { | |
| 889 | outer: for (self.base.options.frameworks.keys()) |f_name| { | |
| 889 | 890 | for (framework_dirs.items) |dir| { |
| 890 | 891 | for (&[_][]const u8{ ".tbd", ".dylib", "" }) |ext| { |
| 891 | if (try resolveFramework(arena, dir, framework, ext)) |full_path| { | |
| 892 | try libs.append(full_path); | |
| 892 | if (try resolveFramework(arena, dir, f_name, ext)) |full_path| { | |
| 893 | try libs.put(full_path, self.base.options.frameworks.get(f_name).?); | |
| 893 | 894 | continue :outer; |
| 894 | 895 | } |
| 895 | 896 | } |
| 896 | 897 | } else { |
| 897 | log.warn("framework not found for '-framework {s}'", .{framework}); | |
| 898 | log.warn("framework not found for '-framework {s}'", .{f_name}); | |
| 898 | 899 | framework_not_found = true; |
| 899 | 900 | } |
| 900 | 901 | } |
| ... | ... | @@ -1025,15 +1026,25 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 1025 | 1026 | try argv.append("-lc"); |
| 1026 | 1027 | |
| 1027 | 1028 | for (self.base.options.system_libs.keys()) |l_name| { |
| 1028 | try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{l_name})); | |
| 1029 | const needed = self.base.options.system_libs.get(l_name).?.needed; | |
| 1030 | const arg = if (needed) | |
| 1031 | try std.fmt.allocPrint(arena, "-needed-l{s}", .{l_name}) | |
| 1032 | else | |
| 1033 | try std.fmt.allocPrint(arena, "-l{s}", .{l_name}); | |
| 1034 | try argv.append(arg); | |
| 1029 | 1035 | } |
| 1030 | 1036 | |
| 1031 | 1037 | for (self.base.options.lib_dirs) |lib_dir| { |
| 1032 | 1038 | try argv.append(try std.fmt.allocPrint(arena, "-L{s}", .{lib_dir})); |
| 1033 | 1039 | } |
| 1034 | 1040 | |
| 1035 | for (self.base.options.frameworks) |framework| { | |
| 1036 | try argv.append(try std.fmt.allocPrint(arena, "-framework {s}", .{framework})); | |
| 1041 | for (self.base.options.frameworks.keys()) |framework| { | |
| 1042 | const needed = self.base.options.frameworks.get(framework).?.needed; | |
| 1043 | const arg = if (needed) | |
| 1044 | try std.fmt.allocPrint(arena, "-needed_framework {s}", .{framework}) | |
| 1045 | else | |
| 1046 | try std.fmt.allocPrint(arena, "-framework {s}", .{framework}); | |
| 1047 | try argv.append(arg); | |
| 1037 | 1048 | } |
| 1038 | 1049 | |
| 1039 | 1050 | for (self.base.options.framework_dirs) |framework_dir| { |
| ... | ... | @@ -1056,7 +1067,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 1056 | 1067 | defer dependent_libs.deinit(); |
| 1057 | 1068 | try self.parseInputFiles(positionals.items, self.base.options.sysroot, &dependent_libs); |
| 1058 | 1069 | try self.parseAndForceLoadStaticArchives(must_link_archives.keys()); |
| 1059 | try self.parseLibs(libs.items, self.base.options.sysroot, &dependent_libs); | |
| 1070 | try self.parseLibs(libs.keys(), libs.values(), self.base.options.sysroot, &dependent_libs); | |
| 1060 | 1071 | try self.parseDependentLibs(self.base.options.sysroot, &dependent_libs); |
| 1061 | 1072 | } |
| 1062 | 1073 | |
| ... | ... | @@ -1381,6 +1392,7 @@ const DylibCreateOpts = struct { |
| 1381 | 1392 | dependent_libs: *std.fifo.LinearFifo(Dylib.Id, .Dynamic), |
| 1382 | 1393 | id: ?Dylib.Id = null, |
| 1383 | 1394 | is_dependent: bool = false, |
| 1395 | is_needed: bool = false, | |
| 1384 | 1396 | }; |
| 1385 | 1397 | |
| 1386 | 1398 | pub fn parseDylib(self: *MachO, path: []const u8, opts: DylibCreateOpts) ParseDylibError!bool { |
| ... | ... | @@ -1431,7 +1443,7 @@ pub fn parseDylib(self: *MachO, path: []const u8, opts: DylibCreateOpts) ParseDy |
| 1431 | 1443 | try self.dylibs_map.putNoClobber(self.base.allocator, dylib.id.?.name, dylib_id); |
| 1432 | 1444 | |
| 1433 | 1445 | const should_link_dylib_even_if_unreachable = blk: { |
| 1434 | if (self.base.options.dead_strip_dylibs) break :blk false; | |
| 1446 | if (self.base.options.dead_strip_dylibs and !opts.is_needed) break :blk false; | |
| 1435 | 1447 | break :blk !(opts.is_dependent or self.referenced_dylibs.contains(dylib_id)); |
| 1436 | 1448 | }; |
| 1437 | 1449 | |
| ... | ... | @@ -1479,12 +1491,20 @@ fn parseAndForceLoadStaticArchives(self: *MachO, files: []const []const u8) !voi |
| 1479 | 1491 | } |
| 1480 | 1492 | } |
| 1481 | 1493 | |
| 1482 | fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8, dependent_libs: anytype) !void { | |
| 1483 | for (libs) |lib| { | |
| 1494 | fn parseLibs( | |
| 1495 | self: *MachO, | |
| 1496 | lib_names: []const []const u8, | |
| 1497 | lib_infos: []const Compilation.SystemLib, | |
| 1498 | syslibroot: ?[]const u8, | |
| 1499 | dependent_libs: anytype, | |
| 1500 | ) !void { | |
| 1501 | for (lib_names) |lib, i| { | |
| 1502 | const lib_info = lib_infos[i]; | |
| 1484 | 1503 | log.debug("parsing lib path '{s}'", .{lib}); |
| 1485 | 1504 | if (try self.parseDylib(lib, .{ |
| 1486 | 1505 | .syslibroot = syslibroot, |
| 1487 | 1506 | .dependent_libs = dependent_libs, |
| 1507 | .is_needed = lib_info.needed, | |
| 1488 | 1508 | })) continue; |
| 1489 | 1509 | if (try self.parseArchive(lib, false)) continue; |
| 1490 | 1510 |
src/main.zig+31-9| ... | ... | @@ -444,6 +444,8 @@ const usage_build_generic = |
| 444 | 444 | \\ --stack [size] Override default stack size |
| 445 | 445 | \\ --image-base [addr] Set base address for executable image |
| 446 | 446 | \\ -framework [name] (Darwin) link against framework |
| 447 | \\ -needed_framework [name] (Darwin) link against framework (even if unused) | |
| 448 | \\ -needed_library [lib] (Darwin) link against system library (even if unused) | |
| 447 | 449 | \\ -F[dir] (Darwin) add search path for frameworks |
| 448 | 450 | \\ -install_name=[value] (Darwin) add dylib's install name |
| 449 | 451 | \\ --entitlements [path] (Darwin) add path to entitlements file for embedding in code signature |
| ... | ... | @@ -750,8 +752,7 @@ fn buildOutputType( |
| 750 | 752 | var framework_dirs = std.ArrayList([]const u8).init(gpa); |
| 751 | 753 | defer framework_dirs.deinit(); |
| 752 | 754 | |
| 753 | var frameworks = std.ArrayList([]const u8).init(gpa); | |
| 754 | defer frameworks.deinit(); | |
| 755 | var frameworks: std.StringArrayHashMapUnmanaged(Compilation.SystemLib) = .{}; | |
| 755 | 756 | |
| 756 | 757 | // null means replace with the test executable binary |
| 757 | 758 | var test_exec_args = std.ArrayList(?[]const u8).init(gpa); |
| ... | ... | @@ -912,9 +913,15 @@ fn buildOutputType( |
| 912 | 913 | fatal("expected parameter after {s}", .{arg}); |
| 913 | 914 | }); |
| 914 | 915 | } else if (mem.eql(u8, arg, "-framework")) { |
| 915 | try frameworks.append(args_iter.next() orelse { | |
| 916 | const path = args_iter.next() orelse { | |
| 916 | 917 | fatal("expected parameter after {s}", .{arg}); |
| 917 | }); | |
| 918 | }; | |
| 919 | try frameworks.put(gpa, path, .{ .needed = false }); | |
| 920 | } else if (mem.eql(u8, arg, "-needed_framework")) { | |
| 921 | const path = args_iter.next() orelse { | |
| 922 | fatal("expected parameter after {s}", .{arg}); | |
| 923 | }; | |
| 924 | try frameworks.put(gpa, path, .{ .needed = true }); | |
| 918 | 925 | } else if (mem.eql(u8, arg, "-install_name")) { |
| 919 | 926 | install_name = args_iter.next() orelse { |
| 920 | 927 | fatal("expected parameter after {s}", .{arg}); |
| ... | ... | @@ -956,7 +963,10 @@ fn buildOutputType( |
| 956 | 963 | // We don't know whether this library is part of libc or libc++ until |
| 957 | 964 | // we resolve the target, so we simply append to the list for now. |
| 958 | 965 | try system_libs.put(next_arg, .{ .needed = false }); |
| 959 | } else if (mem.eql(u8, arg, "--needed-library") or mem.eql(u8, arg, "-needed-l")) { | |
| 966 | } else if (mem.eql(u8, arg, "--needed-library") or | |
| 967 | mem.eql(u8, arg, "-needed-l") or | |
| 968 | mem.eql(u8, arg, "--needed_library")) | |
| 969 | { | |
| 960 | 970 | const next_arg = args_iter.next() orelse { |
| 961 | 971 | fatal("expected parameter after {s}", .{arg}); |
| 962 | 972 | }; |
| ... | ... | @@ -1586,7 +1596,7 @@ fn buildOutputType( |
| 1586 | 1596 | try clang_argv.appendSlice(it.other_args); |
| 1587 | 1597 | }, |
| 1588 | 1598 | .framework_dir => try framework_dirs.append(it.only_arg), |
| 1589 | .framework => try frameworks.append(it.only_arg), | |
| 1599 | .framework => try frameworks.put(gpa, it.only_arg, .{ .needed = false }), | |
| 1590 | 1600 | .nostdlibinc => want_native_include_dirs = false, |
| 1591 | 1601 | .strip => strip = true, |
| 1592 | 1602 | .exec_model => { |
| ... | ... | @@ -1874,7 +1884,19 @@ fn buildOutputType( |
| 1874 | 1884 | if (i >= linker_args.items.len) { |
| 1875 | 1885 | fatal("expected linker arg after '{s}'", .{arg}); |
| 1876 | 1886 | } |
| 1877 | try frameworks.append(linker_args.items[i]); | |
| 1887 | try frameworks.put(gpa, linker_args.items[i], .{ .needed = false }); | |
| 1888 | } else if (mem.eql(u8, arg, "-needed_framework")) { | |
| 1889 | i += 1; | |
| 1890 | if (i >= linker_args.items.len) { | |
| 1891 | fatal("expected linker arg after '{s}'", .{arg}); | |
| 1892 | } | |
| 1893 | try frameworks.put(gpa, linker_args.items[i], .{ .needed = true }); | |
| 1894 | } else if (mem.eql(u8, arg, "-needed_library")) { | |
| 1895 | i += 1; | |
| 1896 | if (i >= linker_args.items.len) { | |
| 1897 | fatal("expected linker arg after '{s}'", .{arg}); | |
| 1898 | } | |
| 1899 | try system_libs.put(linker_args.items[i], .{ .needed = true }); | |
| 1878 | 1900 | } else if (mem.eql(u8, arg, "-compatibility_version")) { |
| 1879 | 1901 | i += 1; |
| 1880 | 1902 | if (i >= linker_args.items.len) { |
| ... | ... | @@ -2244,7 +2266,7 @@ fn buildOutputType( |
| 2244 | 2266 | |
| 2245 | 2267 | if (comptime builtin.target.isDarwin()) { |
| 2246 | 2268 | // If we want to link against frameworks, we need system headers. |
| 2247 | if (framework_dirs.items.len > 0 or frameworks.items.len > 0) | |
| 2269 | if (framework_dirs.items.len > 0 or frameworks.count() > 0) | |
| 2248 | 2270 | want_native_include_dirs = true; |
| 2249 | 2271 | } |
| 2250 | 2272 | |
| ... | ... | @@ -2734,7 +2756,7 @@ fn buildOutputType( |
| 2734 | 2756 | .c_source_files = c_source_files.items, |
| 2735 | 2757 | .link_objects = link_objects.items, |
| 2736 | 2758 | .framework_dirs = framework_dirs.items, |
| 2737 | .frameworks = frameworks.items, | |
| 2759 | .frameworks = frameworks, | |
| 2738 | 2760 | .system_lib_names = system_libs.keys(), |
| 2739 | 2761 | .system_lib_infos = system_libs.values(), |
| 2740 | 2762 | .wasi_emulated_libs = wasi_emulated_libs.items, |
test/link.zig+9| ... | ... | @@ -45,6 +45,15 @@ pub fn addCases(cases: *tests.StandaloneContext) void { |
| 45 | 45 | .requires_macos_sdk = true, |
| 46 | 46 | }); |
| 47 | 47 | |
| 48 | cases.addBuildFile("test/link/macho/needed_l/build.zig", .{ | |
| 49 | .build_modes = true, | |
| 50 | }); | |
| 51 | ||
| 52 | cases.addBuildFile("test/link/macho/needed_framework/build.zig", .{ | |
| 53 | .build_modes = true, | |
| 54 | .requires_macos_sdk = true, | |
| 55 | }); | |
| 56 | ||
| 48 | 57 | // Try to build and run an Objective-C executable. |
| 49 | 58 | cases.addBuildFile("test/link/macho/objc/build.zig", .{ |
| 50 | 59 | .build_modes = true, |
test/link/macho/dead_strip_dylibs/build.zig+1-1| ... | ... | @@ -6,6 +6,7 @@ pub fn build(b: *Builder) void { |
| 6 | 6 | const mode = b.standardReleaseOptions(); |
| 7 | 7 | |
| 8 | 8 | const test_step = b.step("test", "Test the program"); |
| 9 | test_step.dependOn(b.getInstallStep()); | |
| 9 | 10 | |
| 10 | 11 | { |
| 11 | 12 | // Without -dead_strip_dylibs we expect `-la` to include liba.dylib in the final executable |
| ... | ... | @@ -37,7 +38,6 @@ pub fn build(b: *Builder) void { |
| 37 | 38 | |
| 38 | 39 | fn createScenario(b: *Builder, mode: std.builtin.Mode) *LibExeObjectStep { |
| 39 | 40 | const exe = b.addExecutable("test", null); |
| 40 | b.default_step.dependOn(&exe.step); | |
| 41 | 41 | exe.addCSourceFile("main.c", &[0][]const u8{}); |
| 42 | 42 | exe.setBuildMode(mode); |
| 43 | 43 | exe.linkLibC(); |
test/link/macho/dead_strip_dylibs/main.c+2-1| ... | ... | @@ -1,10 +1,11 @@ |
| 1 | 1 | #include <objc/runtime.h> |
| 2 | 2 | |
| 3 | int main() { | |
| 3 | int main(int argc, char* argv[]) { | |
| 4 | 4 | if (objc_getClass("NSObject") == 0) { |
| 5 | 5 | return -1; |
| 6 | 6 | } |
| 7 | 7 | if (objc_getClass("NSApplication") == 0) { |
| 8 | 8 | return -2; |
| 9 | 9 | } |
| 10 | return 0; | |
| 10 | 11 | } |
test/link/macho/dylib/main.c+1-1| ... | ... | @@ -3,7 +3,7 @@ |
| 3 | 3 | char* hello(); |
| 4 | 4 | extern char world[]; |
| 5 | 5 | |
| 6 | int main() { | |
| 6 | int main(int argc, char* argv[]) { | |
| 7 | 7 | printf("%s %s", hello(), world); |
| 8 | 8 | return 0; |
| 9 | 9 | } |
test/link/macho/needed_framework/build.zig created+27| ... | ... | @@ -0,0 +1,27 @@ |
| 1 | const std = @import("std"); | |
| 2 | const Builder = std.build.Builder; | |
| 3 | const LibExeObjectStep = std.build.LibExeObjStep; | |
| 4 | ||
| 5 | pub fn build(b: *Builder) void { | |
| 6 | const mode = b.standardReleaseOptions(); | |
| 7 | ||
| 8 | const test_step = b.step("test", "Test the program"); | |
| 9 | test_step.dependOn(b.getInstallStep()); | |
| 10 | ||
| 11 | // -dead_strip_dylibs | |
| 12 | // -needed_framework Cocoa | |
| 13 | const exe = b.addExecutable("test", null); | |
| 14 | exe.addCSourceFile("main.c", &[0][]const u8{}); | |
| 15 | exe.setBuildMode(mode); | |
| 16 | exe.linkLibC(); | |
| 17 | exe.linkFrameworkNeeded("Cocoa"); | |
| 18 | exe.dead_strip_dylibs = true; | |
| 19 | ||
| 20 | const check = exe.checkObject(.macho); | |
| 21 | check.checkStart("cmd LOAD_DYLIB"); | |
| 22 | check.checkNext("name {*}Cocoa"); | |
| 23 | test_step.dependOn(&check.step); | |
| 24 | ||
| 25 | const run_cmd = exe.run(); | |
| 26 | test_step.dependOn(&run_cmd.step); | |
| 27 | } |
test/link/macho/needed_framework/main.c created+3| ... | ... | @@ -0,0 +1,3 @@ |
| 1 | int main(int argc, char* argv[]) { | |
| 2 | return 0; | |
| 3 | } |
test/link/macho/needed_l/a.c created+1| ... | ... | @@ -0,0 +1 @@ |
| 1 | int a = 42; |
test/link/macho/needed_l/build.zig created+35| ... | ... | @@ -0,0 +1,35 @@ |
| 1 | const std = @import("std"); | |
| 2 | const Builder = std.build.Builder; | |
| 3 | const LibExeObjectStep = std.build.LibExeObjStep; | |
| 4 | ||
| 5 | pub fn build(b: *Builder) void { | |
| 6 | const mode = b.standardReleaseOptions(); | |
| 7 | ||
| 8 | const test_step = b.step("test", "Test the program"); | |
| 9 | test_step.dependOn(b.getInstallStep()); | |
| 10 | ||
| 11 | const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); | |
| 12 | dylib.setBuildMode(mode); | |
| 13 | dylib.addCSourceFile("a.c", &.{}); | |
| 14 | dylib.linkLibC(); | |
| 15 | dylib.install(); | |
| 16 | ||
| 17 | // -dead_strip_dylibs | |
| 18 | // -needed-la | |
| 19 | const exe = b.addExecutable("test", null); | |
| 20 | exe.addCSourceFile("main.c", &[0][]const u8{}); | |
| 21 | exe.setBuildMode(mode); | |
| 22 | exe.linkLibC(); | |
| 23 | exe.linkSystemLibraryNeeded("a"); | |
| 24 | exe.addLibraryPath(b.pathFromRoot("zig-out/lib")); | |
| 25 | exe.addRPath(b.pathFromRoot("zig-out/lib")); | |
| 26 | exe.dead_strip_dylibs = true; | |
| 27 | ||
| 28 | const check = exe.checkObject(.macho); | |
| 29 | check.checkStart("cmd LOAD_DYLIB"); | |
| 30 | check.checkNext("name @rpath/liba.dylib"); | |
| 31 | test_step.dependOn(&check.step); | |
| 32 | ||
| 33 | const run_cmd = exe.run(); | |
| 34 | test_step.dependOn(&run_cmd.step); | |
| 35 | } |
test/link/macho/needed_l/main.c created+3| ... | ... | @@ -0,0 +1,3 @@ |
| 1 | int main(int argc, char* argv[]) { | |
| 2 | return 0; | |
| 3 | } |
test/link/macho/search_strategy/main.c+1-1| ... | ... | @@ -3,7 +3,7 @@ |
| 3 | 3 | char* hello(); |
| 4 | 4 | extern char world[]; |
| 5 | 5 | |
| 6 | int main() { | |
| 6 | int main(int argc, char* argv[]) { | |
| 7 | 7 | printf("%s %s", hello(), world); |
| 8 | 8 | return 0; |
| 9 | 9 | } |