authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-08 22:43:11-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-08 22:45:49-04:00
loga7346ea49f4d9b1af2d0babf67354b234a87fe42
tree18fe76c5f8958d1cf2533fb5136311d855346f4a
parent4b9e12be50a8d075d06b127712573b531d068837

fix build on macOS

Sadly due to a workaround for LLD linker limitations on macOS we cannot put libuserland into an .a file; instead we have to use object files. Again due to linker limitations, bundling compiler_rt.o into another relocatable object also doesn't work. So we're left with disabling stack probing on macOS for the stage1 self-hosted code. These workarounds could all be removed if the macos support in the LLD linker improved, or if Zig project had its own linker that did not have these issues.

7 files changed, 31 insertions(+), 6 deletions(-)

build.zig+6-1
...@@ -384,12 +384,17 @@ const Context = struct {...@@ -384,12 +384,17 @@ const Context = struct {
384};384};
385385
386fn addLibUserlandStep(b: *Builder) void {386fn addLibUserlandStep(b: *Builder) void {
387 // Sadly macOS requires hacks to work around the buggy MACH-O linker code.
387 const artifact = if (builtin.os == .macosx)388 const artifact = if (builtin.os == .macosx)
388 b.addObject("userland", "src-self-hosted/stage1.zig")389 b.addObject("userland", "src-self-hosted/stage1.zig")
389 else390 else
390 b.addStaticLibrary("userland", "src-self-hosted/stage1.zig");391 b.addStaticLibrary("userland", "src-self-hosted/stage1.zig");
391 artifact.disable_gen_h = true;392 artifact.disable_gen_h = true;
392 artifact.bundle_compiler_rt = true;393 if (builtin.os == .macosx) {
394 artifact.disable_stack_probing = true;
395 } else {
396 artifact.bundle_compiler_rt = true;
397 }
393 artifact.setTarget(builtin.arch, builtin.os, builtin.abi);398 artifact.setTarget(builtin.arch, builtin.os, builtin.abi);
394 artifact.linkSystemLibrary("c");399 artifact.linkSystemLibrary("c");
395 const libuserland_step = b.step("libuserland", "Build the userland compiler library for use in stage1");400 const libuserland_step = b.step("libuserland", "Build the userland compiler library for use in stage1");
src/all_types.hpp+1
...@@ -1862,6 +1862,7 @@ struct CodeGen {...@@ -1862,6 +1862,7 @@ struct CodeGen {
1862 bool is_dummy_so;1862 bool is_dummy_so;
1863 bool disable_gen_h;1863 bool disable_gen_h;
1864 bool bundle_compiler_rt;1864 bool bundle_compiler_rt;
1865 bool disable_stack_probing;
18651866
1866 Buf *mmacosx_version_min;1867 Buf *mmacosx_version_min;
1867 Buf *mios_version_min;1868 Buf *mios_version_min;
src/codegen.cpp+7-1
...@@ -401,7 +401,7 @@ static void add_uwtable_attr(CodeGen *g, LLVMValueRef fn_val) {...@@ -401,7 +401,7 @@ static void add_uwtable_attr(CodeGen *g, LLVMValueRef fn_val) {
401401
402static void add_probe_stack_attr(CodeGen *g, LLVMValueRef fn_val) {402static void add_probe_stack_attr(CodeGen *g, LLVMValueRef fn_val) {
403 // Windows already emits its own stack probes403 // Windows already emits its own stack probes
404 if (g->zig_target->os != OsWindows &&404 if (!g->disable_stack_probing && g->zig_target->os != OsWindows &&
405 (g->zig_target->arch == ZigLLVM_x86 ||405 (g->zig_target->arch == ZigLLVM_x86 ||
406 g->zig_target->arch == ZigLLVM_x86_64)) {406 g->zig_target->arch == ZigLLVM_x86_64)) {
407 addLLVMFnAttrStr(fn_val, "probe-stack", "__zig_probe_stack");407 addLLVMFnAttrStr(fn_val, "probe-stack", "__zig_probe_stack");
...@@ -7043,6 +7043,11 @@ static void zig_llvm_emit_output(CodeGen *g) {...@@ -7043,6 +7043,11 @@ static void zig_llvm_emit_output(CodeGen *g) {
7043 }7043 }
7044 validate_inline_fns(g);7044 validate_inline_fns(g);
7045 g->link_objects.append(output_path);7045 g->link_objects.append(output_path);
7046 if (g->bundle_compiler_rt && (g->out_type == OutTypeObj ||
7047 (g->out_type == OutTypeLib && !g->is_dynamic)))
7048 {
7049 zig_link_add_compiler_rt(g);
7050 }
7046 break;7051 break;
70477052
7048 case EmitFileTypeAssembly:7053 case EmitFileTypeAssembly:
...@@ -9347,6 +9352,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {...@@ -9347,6 +9352,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
9347 cache_bool(ch, g->each_lib_rpath);9352 cache_bool(ch, g->each_lib_rpath);
9348 cache_bool(ch, g->disable_gen_h);9353 cache_bool(ch, g->disable_gen_h);
9349 cache_bool(ch, g->bundle_compiler_rt);9354 cache_bool(ch, g->bundle_compiler_rt);
9355 cache_bool(ch, g->disable_stack_probing);
9350 cache_bool(ch, want_valgrind_support(g));9356 cache_bool(ch, want_valgrind_support(g));
9351 cache_bool(ch, g->have_pic);9357 cache_bool(ch, g->have_pic);
9352 cache_bool(ch, g->have_dynamic_link);9358 cache_bool(ch, g->have_dynamic_link);
src/codegen.hpp+1
...@@ -44,6 +44,7 @@ void codegen_set_lib_version(CodeGen *g, size_t major, size_t minor, size_t patc...@@ -44,6 +44,7 @@ void codegen_set_lib_version(CodeGen *g, size_t major, size_t minor, size_t patc
44void codegen_add_time_event(CodeGen *g, const char *name);44void codegen_add_time_event(CodeGen *g, const char *name);
45void codegen_print_timing_report(CodeGen *g, FILE *f);45void codegen_print_timing_report(CodeGen *g, FILE *f);
46void codegen_link(CodeGen *g);46void codegen_link(CodeGen *g);
47void zig_link_add_compiler_rt(CodeGen *g);
47void codegen_build_and_link(CodeGen *g);48void codegen_build_and_link(CodeGen *g);
4849
49ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path,50ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path,
src/link.cpp+6-4
...@@ -25,6 +25,7 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou...@@ -25,6 +25,7 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou
25 CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type,25 CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type,
26 parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path());26 parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path());
27 child_gen->disable_gen_h = true;27 child_gen->disable_gen_h = true;
28 child_gen->disable_stack_probing = true;
28 child_gen->verbose_tokenize = parent_gen->verbose_tokenize;29 child_gen->verbose_tokenize = parent_gen->verbose_tokenize;
29 child_gen->verbose_ast = parent_gen->verbose_ast;30 child_gen->verbose_ast = parent_gen->verbose_ast;
30 child_gen->verbose_link = parent_gen->verbose_link;31 child_gen->verbose_link = parent_gen->verbose_link;
...@@ -1653,6 +1654,11 @@ static void construct_linker_job(LinkJob *lj) {...@@ -1653,6 +1654,11 @@ static void construct_linker_job(LinkJob *lj) {
1653 }1654 }
1654}1655}
16551656
1657void zig_link_add_compiler_rt(CodeGen *g) {
1658 Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeObj);
1659 g->link_objects.append(compiler_rt_o_path);
1660}
1661
1656void codegen_link(CodeGen *g) {1662void codegen_link(CodeGen *g) {
1657 codegen_add_time_event(g, "Build Dependencies");1663 codegen_add_time_event(g, "Build Dependencies");
16581664
...@@ -1681,10 +1687,6 @@ void codegen_link(CodeGen *g) {...@@ -1681,10 +1687,6 @@ void codegen_link(CodeGen *g) {
1681 for (size_t i = 0; i < g->link_objects.length; i += 1) {1687 for (size_t i = 0; i < g->link_objects.length; i += 1) {
1682 file_names.append(buf_ptr(g->link_objects.at(i)));1688 file_names.append(buf_ptr(g->link_objects.at(i)));
1683 }1689 }
1684 if (g->bundle_compiler_rt) {
1685 Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeObj);
1686 file_names.append(buf_ptr(compiler_rt_o_path));
1687 }
1688 ZigLLVM_OSType os_type = get_llvm_os_type(g->zig_target->os);1690 ZigLLVM_OSType os_type = get_llvm_os_type(g->zig_target->os);
1689 codegen_add_time_event(g, "LLVM Link");1691 codegen_add_time_event(g, "LLVM Link");
1690 if (g->verbose_link) {1692 if (g->verbose_link) {
src/main.cpp+5
...@@ -56,6 +56,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {...@@ -56,6 +56,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
56 " --disable-gen-h do not generate a C header file (.h)\n"56 " --disable-gen-h do not generate a C header file (.h)\n"
57 " --disable-valgrind omit valgrind client requests in debug builds\n"57 " --disable-valgrind omit valgrind client requests in debug builds\n"
58 " --enable-valgrind include valgrind client requests release builds\n"58 " --enable-valgrind include valgrind client requests release builds\n"
59 " --disable-stack-probing workaround for macosx\n"
59 " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n"60 " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n"
60 " -fPIC enable Position Independent Code\n"61 " -fPIC enable Position Independent Code\n"
61 " -fno-PIC disable Position Independent Code\n"62 " -fno-PIC disable Position Independent Code\n"
...@@ -444,6 +445,7 @@ int main(int argc, char **argv) {...@@ -444,6 +445,7 @@ int main(int argc, char **argv) {
444 bool want_single_threaded = false;445 bool want_single_threaded = false;
445 bool disable_gen_h = false;446 bool disable_gen_h = false;
446 bool bundle_compiler_rt = false;447 bool bundle_compiler_rt = false;
448 bool disable_stack_probing = false;
447 Buf *override_std_dir = nullptr;449 Buf *override_std_dir = nullptr;
448 Buf *override_lib_dir = nullptr;450 Buf *override_lib_dir = nullptr;
449 Buf *main_pkg_path = nullptr;451 Buf *main_pkg_path = nullptr;
...@@ -656,6 +658,8 @@ int main(int argc, char **argv) {...@@ -656,6 +658,8 @@ int main(int argc, char **argv) {
656 disable_gen_h = true;658 disable_gen_h = true;
657 } else if (strcmp(arg, "--bundle-compiler-rt") == 0) {659 } else if (strcmp(arg, "--bundle-compiler-rt") == 0) {
658 bundle_compiler_rt = true;660 bundle_compiler_rt = true;
661 } else if (strcmp(arg, "--disable-stack-probing") == 0) {
662 disable_stack_probing = true;
659 } else if (strcmp(arg, "--test-cmd-bin") == 0) {663 } else if (strcmp(arg, "--test-cmd-bin") == 0) {
660 test_exec_args.append(nullptr);664 test_exec_args.append(nullptr);
661 } else if (arg[1] == 'L' && arg[2] != 0) {665 } else if (arg[1] == 'L' && arg[2] != 0) {
...@@ -1075,6 +1079,7 @@ int main(int argc, char **argv) {...@@ -1075,6 +1079,7 @@ int main(int argc, char **argv) {
1075 g->output_dir = output_dir;1079 g->output_dir = output_dir;
1076 g->disable_gen_h = disable_gen_h;1080 g->disable_gen_h = disable_gen_h;
1077 g->bundle_compiler_rt = bundle_compiler_rt;1081 g->bundle_compiler_rt = bundle_compiler_rt;
1082 g->disable_stack_probing = disable_stack_probing;
1078 codegen_set_errmsg_color(g, color);1083 codegen_set_errmsg_color(g, color);
1079 g->system_linker_hack = system_linker_hack;1084 g->system_linker_hack = system_linker_hack;
10801085
std/build.zig+5
...@@ -942,6 +942,7 @@ pub const LibExeObjStep = struct {...@@ -942,6 +942,7 @@ pub const LibExeObjStep = struct {
942 verbose_cc: bool,942 verbose_cc: bool,
943 disable_gen_h: bool,943 disable_gen_h: bool,
944 bundle_compiler_rt: bool,944 bundle_compiler_rt: bool,
945 disable_stack_probing: bool,
945 c_std: Builder.CStd,946 c_std: Builder.CStd,
946 override_std_dir: ?[]const u8,947 override_std_dir: ?[]const u8,
947 override_lib_dir: ?[]const u8,948 override_lib_dir: ?[]const u8,
...@@ -1052,6 +1053,7 @@ pub const LibExeObjStep = struct {...@@ -1052,6 +1053,7 @@ pub const LibExeObjStep = struct {
1052 .filter = null,1053 .filter = null,
1053 .disable_gen_h = false,1054 .disable_gen_h = false,
1054 .bundle_compiler_rt = false,1055 .bundle_compiler_rt = false,
1056 .disable_stack_probing = false,
1055 .output_dir = null,1057 .output_dir = null,
1056 .need_system_paths = false,1058 .need_system_paths = false,
1057 .single_threaded = false,1059 .single_threaded = false,
...@@ -1457,6 +1459,9 @@ pub const LibExeObjStep = struct {...@@ -1457,6 +1459,9 @@ pub const LibExeObjStep = struct {
1457 if (self.bundle_compiler_rt) {1459 if (self.bundle_compiler_rt) {
1458 try zig_args.append("--bundle-compiler-rt");1460 try zig_args.append("--bundle-compiler-rt");
1459 }1461 }
1462 if (self.disable_stack_probing) {
1463 try zig_args.append("--disable-stack-probing");
1464 }
14601465
1461 switch (self.target) {1466 switch (self.target) {
1462 Target.Native => {},1467 Target.Native => {},