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
This commit is contained in:
Jakub Konka
2021-08-05 10:33:42 +02:00
parent f2bf1390a2
commit 049ff45430
3 changed files with 34 additions and 14 deletions

View File

@@ -2672,7 +2672,11 @@ pub const LibExeObjStep = struct {
try zig_args.append(self.builder.pathFromRoot(include_path));
},
.raw_path_system => |include_path| {
try zig_args.append("-isystem");
if (builder.sysroot != null) {
try zig_args.append("-iwithsysroot");
} else {
try zig_args.append("-isystem");
}
try zig_args.append(self.builder.pathFromRoot(include_path));
},
.other_step => |other| if (other.emit_h) {
@@ -2700,6 +2704,12 @@ pub const LibExeObjStep = struct {
if (self.target.isDarwin()) {
for (self.framework_dirs.items) |dir| {
if (builder.sysroot != null) {
try zig_args.append("-iframeworkwithsysroot");
} else {
try zig_args.append("-iframework");
}
try zig_args.append(dir);
try zig_args.append("-F");
try zig_args.append(dir);
}

View File

@@ -794,15 +794,14 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
}
}
// If we're compiling native and we can find libSystem.B.{dylib, tbd},
// we link against that instead of embedded libSystem.B.tbd file.
var native_libsystem_available = false;
if (self.base.options.is_native_os) blk: {
// If we were given the sysroot, try to look there first for libSystem.B.{dylib, tbd}.
var libsystem_available = false;
if (self.base.options.sysroot != null) blk: {
// Try stub file first. If we hit it, then we're done as the stub file
// re-exports every single symbol definition.
if (try resolveLib(arena, lib_dirs.items, "System", ".tbd")) |full_path| {
try libs.append(full_path);
native_libsystem_available = true;
libsystem_available = true;
break :blk;
}
// 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 {
if (try resolveLib(arena, lib_dirs.items, "c", ".dylib")) |libc_path| {
try libs.append(libsystem_path);
try libs.append(libc_path);
native_libsystem_available = true;
libsystem_available = true;
break :blk;
}
}
}
if (!native_libsystem_available) {
if (!libsystem_available) {
const full_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
"libc", "darwin", "libSystem.B.tbd",
});
@@ -841,7 +840,7 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
break;
}
} else {
log.warn("framework not found for '-f{s}'", .{framework});
log.warn("framework not found for '-framework {s}'", .{framework});
framework_not_found = true;
}
}
@@ -901,10 +900,8 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
try argv.append("-o");
try argv.append(full_out_path);
if (native_libsystem_available) {
try argv.append("-lSystem");
try argv.append("-lc");
}
try argv.append("-lSystem");
try argv.append("-lc");
for (search_lib_names.items) |l_name| {
try argv.append(try std.fmt.allocPrint(arena, "-l{s}", .{l_name}));
@@ -914,6 +911,14 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
try argv.append(try std.fmt.allocPrint(arena, "-L{s}", .{lib_dir}));
}
for (self.base.options.frameworks) |framework| {
try argv.append(try std.fmt.allocPrint(arena, "-framework {s}", .{framework}));
}
for (self.base.options.framework_dirs) |framework_dir| {
try argv.append(try std.fmt.allocPrint(arena, "-F{s}", .{framework_dir}));
}
Compilation.dump_argv(argv.items);
}

View File

@@ -830,7 +830,10 @@ fn buildOutputType(
} else if (mem.eql(u8, arg, "-D") or
mem.eql(u8, arg, "-isystem") or
mem.eql(u8, arg, "-I") or
mem.eql(u8, arg, "-dirafter"))
mem.eql(u8, arg, "-dirafter") or
mem.eql(u8, arg, "-iwithsysroot") or
mem.eql(u8, arg, "-iframework") or
mem.eql(u8, arg, "-iframeworkwithsysroot"))
{
if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
i += 1;
@@ -873,6 +876,8 @@ fn buildOutputType(
if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
i += 1;
sysroot = args[i];
try clang_argv.append("-isysroot");
try clang_argv.append(args[i]);
} else if (mem.eql(u8, arg, "--libc")) {
if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
i += 1;