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 {...@@ -2672,7 +2672,11 @@ pub const LibExeObjStep = struct {
2672 try zig_args.append(self.builder.pathFromRoot(include_path));2672 try zig_args.append(self.builder.pathFromRoot(include_path));
2673 },2673 },
2674 .raw_path_system => |include_path| {2674 .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 }
2676 try zig_args.append(self.builder.pathFromRoot(include_path));2680 try zig_args.append(self.builder.pathFromRoot(include_path));
2677 },2681 },
2678 .other_step => |other| if (other.emit_h) {2682 .other_step => |other| if (other.emit_h) {
...@@ -2700,6 +2704,12 @@ pub const LibExeObjStep = struct {...@@ -2700,6 +2704,12 @@ pub const LibExeObjStep = struct {
27002704
2701 if (self.target.isDarwin()) {2705 if (self.target.isDarwin()) {
2702 for (self.framework_dirs.items) |dir| {2706 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);
2703 try zig_args.append("-F");2713 try zig_args.append("-F");
2704 try zig_args.append(dir);2714 try zig_args.append(dir);
2705 }2715 }
src/link/MachO.zig+17-12
...@@ -794,15 +794,14 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {...@@ -794,15 +794,14 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
794 }794 }
795 }795 }
796796
797 // If we're compiling native and we can find libSystem.B.{dylib, tbd},797 // If we were given the sysroot, try to look there first for libSystem.B.{dylib, tbd}.
798 // we link against that instead of embedded libSystem.B.tbd file.798 var libsystem_available = false;
799 var native_libsystem_available = false;799 if (self.base.options.sysroot != null) blk: {
800 if (self.base.options.is_native_os) blk: {
801 // Try stub file first. If we hit it, then we're done as the stub file800 // Try stub file first. If we hit it, then we're done as the stub file
802 // re-exports every single symbol definition.801 // re-exports every single symbol definition.
803 if (try resolveLib(arena, lib_dirs.items, "System", ".tbd")) |full_path| {802 if (try resolveLib(arena, lib_dirs.items, "System", ".tbd")) |full_path| {
804 try libs.append(full_path);803 try libs.append(full_path);
805 native_libsystem_available = true;804 libsystem_available = true;
806 break :blk;805 break :blk;
807 }806 }
808 // If we didn't hit the stub file, try .dylib next. However, libSystem.dylib807 // 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 {...@@ -811,12 +810,12 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
811 if (try resolveLib(arena, lib_dirs.items, "c", ".dylib")) |libc_path| {810 if (try resolveLib(arena, lib_dirs.items, "c", ".dylib")) |libc_path| {
812 try libs.append(libsystem_path);811 try libs.append(libsystem_path);
813 try libs.append(libc_path);812 try libs.append(libc_path);
814 native_libsystem_available = true;813 libsystem_available = true;
815 break :blk;814 break :blk;
816 }815 }
817 }816 }
818 }817 }
819 if (!native_libsystem_available) {818 if (!libsystem_available) {
820 const full_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{819 const full_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
821 "libc", "darwin", "libSystem.B.tbd",820 "libc", "darwin", "libSystem.B.tbd",
822 });821 });
...@@ -841,7 +840,7 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {...@@ -841,7 +840,7 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
841 break;840 break;
842 }841 }
843 } else {842 } else {
844 log.warn("framework not found for '-f{s}'", .{framework});843 log.warn("framework not found for '-framework {s}'", .{framework});
845 framework_not_found = true;844 framework_not_found = true;
846 }845 }
847 }846 }
...@@ -901,10 +900,8 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {...@@ -901,10 +900,8 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
901 try argv.append("-o");900 try argv.append("-o");
902 try argv.append(full_out_path);901 try argv.append(full_out_path);
903902
904 if (native_libsystem_available) {903 try argv.append("-lSystem");
905 try argv.append("-lSystem");904 try argv.append("-lc");
906 try argv.append("-lc");
907 }
908905
909 for (search_lib_names.items) |l_name| {906 for (search_lib_names.items) |l_name| {
910 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{l_name}));907 try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{l_name}));
...@@ -914,6 +911,14 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {...@@ -914,6 +911,14 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
914 try argv.append(try std.fmt.allocPrint(arena, "-L{s}", .{lib_dir}));911 try argv.append(try std.fmt.allocPrint(arena, "-L{s}", .{lib_dir}));
915 }912 }
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
917 Compilation.dump_argv(argv.items);922 Compilation.dump_argv(argv.items);
918 }923 }
919924
src/main.zig+6-1
...@@ -830,7 +830,10 @@ fn buildOutputType(...@@ -830,7 +830,10 @@ fn buildOutputType(
830 } else if (mem.eql(u8, arg, "-D") or830 } else if (mem.eql(u8, arg, "-D") or
831 mem.eql(u8, arg, "-isystem") or831 mem.eql(u8, arg, "-isystem") or
832 mem.eql(u8, arg, "-I") or832 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"))
834 {837 {
835 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});838 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
836 i += 1;839 i += 1;
...@@ -873,6 +876,8 @@ fn buildOutputType(...@@ -873,6 +876,8 @@ fn buildOutputType(
873 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});876 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
874 i += 1;877 i += 1;
875 sysroot = args[i];878 sysroot = args[i];
879 try clang_argv.append("-isysroot");
880 try clang_argv.append(args[i]);
876 } else if (mem.eql(u8, arg, "--libc")) {881 } else if (mem.eql(u8, arg, "--libc")) {
877 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});882 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
878 i += 1;883 i += 1;