authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-05 10:33:42+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-10 13:40:39+02:00
log049ff45430f7d556d1c6947c0bc44f3df67a8b00
tree751aa2f9481ac12b00e3c5b0ab21591db92719d9
parentf2bf1390a29a9decaa5ca49d3ae720b360583b35

macho: add basic support for building iOS binaries

* ensure we correctly transfer `-iwithsysroot` and `-iframeworkwithsysroot` flags with values from `build.zig` and that they are correctly transferred forward to `zig cc` * try to look for `libSystem.tbd` in the provided syslibroot - one caveat that the user will have to specify library search paths too

3 files changed, 34 insertions(+), 14 deletions(-)

lib/std/build.zig+11-1
......@@ -2672,7 +2672,11 @@ pub const LibExeObjStep = struct {
26722672 try zig_args.append(self.builder.pathFromRoot(include_path));
26732673 },
26742674 .raw_path_system => |include_path| {
2675 try zig_args.append("-isystem");
2675 if (builder.sysroot != null) {
2676 try zig_args.append("-iwithsysroot");
2677 } else {
2678 try zig_args.append("-isystem");
2679 }
26762680 try zig_args.append(self.builder.pathFromRoot(include_path));
26772681 },
26782682 .other_step => |other| if (other.emit_h) {
......@@ -2700,6 +2704,12 @@ pub const LibExeObjStep = struct {
27002704
27012705 if (self.target.isDarwin()) {
27022706 for (self.framework_dirs.items) |dir| {
2707 if (builder.sysroot != null) {
2708 try zig_args.append("-iframeworkwithsysroot");
2709 } else {
2710 try zig_args.append("-iframework");
2711 }
2712 try zig_args.append(dir);
27032713 try zig_args.append("-F");
27042714 try zig_args.append(dir);
27052715 }
src/link/MachO.zig+17-12
......@@ -794,15 +794,14 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
794794 }
795795 }
796796
797 // If we're compiling native and we can find libSystem.B.{dylib, tbd},
798 // we link against that instead of embedded libSystem.B.tbd file.
799 var native_libsystem_available = false;
800 if (self.base.options.is_native_os) blk: {
797 // If we were given the sysroot, try to look there first for libSystem.B.{dylib, tbd}.
798 var libsystem_available = false;
799 if (self.base.options.sysroot != null) blk: {
801800 // Try stub file first. If we hit it, then we're done as the stub file
802801 // re-exports every single symbol definition.
803802 if (try resolveLib(arena, lib_dirs.items, "System", ".tbd")) |full_path| {
804803 try libs.append(full_path);
805 native_libsystem_available = true;
804 libsystem_available = true;
806805 break :blk;
807806 }
808807 // If we didn't hit the stub file, try .dylib next. However, libSystem.dylib
......@@ -811,12 +810,12 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
811810 if (try resolveLib(arena, lib_dirs.items, "c", ".dylib")) |libc_path| {
812811 try libs.append(libsystem_path);
813812 try libs.append(libc_path);
814 native_libsystem_available = true;
813 libsystem_available = true;
815814 break :blk;
816815 }
817816 }
818817 }
819 if (!native_libsystem_available) {
818 if (!libsystem_available) {
820819 const full_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
821820 "libc", "darwin", "libSystem.B.tbd",
822821 });
......@@ -841,7 +840,7 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
841840 break;
842841 }
843842 } else {
844 log.warn("framework not found for '-f{s}'", .{framework});
843 log.warn("framework not found for '-framework {s}'", .{framework});
845844 framework_not_found = true;
846845 }
847846 }
......@@ -901,10 +900,8 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
901900 try argv.append("-o");
902901 try argv.append(full_out_path);
903902
904 if (native_libsystem_available) {
905 try argv.append("-lSystem");
906 try argv.append("-lc");
907 }
903 try argv.append("-lSystem");
904 try argv.append("-lc");
908905
909906 for (search_lib_names.items) |l_name| {
910907 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{l_name}));
......@@ -914,6 +911,14 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
914911 try argv.append(try std.fmt.allocPrint(arena, "-L{s}", .{lib_dir}));
915912 }
916913
914 for (self.base.options.frameworks) |framework| {
915 try argv.append(try std.fmt.allocPrint(arena, "-framework {s}", .{framework}));
916 }
917
918 for (self.base.options.framework_dirs) |framework_dir| {
919 try argv.append(try std.fmt.allocPrint(arena, "-F{s}", .{framework_dir}));
920 }
921
917922 Compilation.dump_argv(argv.items);
918923 }
919924
src/main.zig+6-1
......@@ -830,7 +830,10 @@ fn buildOutputType(
830830 } else if (mem.eql(u8, arg, "-D") or
831831 mem.eql(u8, arg, "-isystem") or
832832 mem.eql(u8, arg, "-I") or
833 mem.eql(u8, arg, "-dirafter"))
833 mem.eql(u8, arg, "-dirafter") or
834 mem.eql(u8, arg, "-iwithsysroot") or
835 mem.eql(u8, arg, "-iframework") or
836 mem.eql(u8, arg, "-iframeworkwithsysroot"))
834837 {
835838 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
836839 i += 1;
......@@ -873,6 +876,8 @@ fn buildOutputType(
873876 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
874877 i += 1;
875878 sysroot = args[i];
879 try clang_argv.append("-isysroot");
880 try clang_argv.append(args[i]);
876881 } else if (mem.eql(u8, arg, "--libc")) {
877882 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
878883 i += 1;