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 {
384384};
385385
386386fn addLibUserlandStep(b: *Builder) void {
387 // Sadly macOS requires hacks to work around the buggy MACH-O linker code.
387388 const artifact = if (builtin.os == .macosx)
388389 b.addObject("userland", "src-self-hosted/stage1.zig")
389390 else
390391 b.addStaticLibrary("userland", "src-self-hosted/stage1.zig");
391392 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 }
393398 artifact.setTarget(builtin.arch, builtin.os, builtin.abi);
394399 artifact.linkSystemLibrary("c");
395400 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 {
18621862 bool is_dummy_so;
18631863 bool disable_gen_h;
18641864 bool bundle_compiler_rt;
1865 bool disable_stack_probing;
18651866
18661867 Buf *mmacosx_version_min;
18671868 Buf *mios_version_min;
src/codegen.cpp+7-1
......@@ -401,7 +401,7 @@ static void add_uwtable_attr(CodeGen *g, LLVMValueRef fn_val) {
401401
402402static void add_probe_stack_attr(CodeGen *g, LLVMValueRef fn_val) {
403403 // 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 &&
405405 (g->zig_target->arch == ZigLLVM_x86 ||
406406 g->zig_target->arch == ZigLLVM_x86_64)) {
407407 addLLVMFnAttrStr(fn_val, "probe-stack", "__zig_probe_stack");
......@@ -7043,6 +7043,11 @@ static void zig_llvm_emit_output(CodeGen *g) {
70437043 }
70447044 validate_inline_fns(g);
70457045 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 }
70467051 break;
70477052
70487053 case EmitFileTypeAssembly:
......@@ -9347,6 +9352,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
93479352 cache_bool(ch, g->each_lib_rpath);
93489353 cache_bool(ch, g->disable_gen_h);
93499354 cache_bool(ch, g->bundle_compiler_rt);
9355 cache_bool(ch, g->disable_stack_probing);
93509356 cache_bool(ch, want_valgrind_support(g));
93519357 cache_bool(ch, g->have_pic);
93529358 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
4444void codegen_add_time_event(CodeGen *g, const char *name);
4545void codegen_print_timing_report(CodeGen *g, FILE *f);
4646void codegen_link(CodeGen *g);
47void zig_link_add_compiler_rt(CodeGen *g);
4748void codegen_build_and_link(CodeGen *g);
4849
4950ZigPackage *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
2525 CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type,
2626 parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path());
2727 child_gen->disable_gen_h = true;
28 child_gen->disable_stack_probing = true;
2829 child_gen->verbose_tokenize = parent_gen->verbose_tokenize;
2930 child_gen->verbose_ast = parent_gen->verbose_ast;
3031 child_gen->verbose_link = parent_gen->verbose_link;
......@@ -1653,6 +1654,11 @@ static void construct_linker_job(LinkJob *lj) {
16531654 }
16541655}
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
16561662void codegen_link(CodeGen *g) {
16571663 codegen_add_time_event(g, "Build Dependencies");
16581664
......@@ -1681,10 +1687,6 @@ void codegen_link(CodeGen *g) {
16811687 for (size_t i = 0; i < g->link_objects.length; i += 1) {
16821688 file_names.append(buf_ptr(g->link_objects.at(i)));
16831689 }
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 }
16881690 ZigLLVM_OSType os_type = get_llvm_os_type(g->zig_target->os);
16891691 codegen_add_time_event(g, "LLVM Link");
16901692 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) {
5656 " --disable-gen-h do not generate a C header file (.h)\n"
5757 " --disable-valgrind omit valgrind client requests in debug builds\n"
5858 " --enable-valgrind include valgrind client requests release builds\n"
59 " --disable-stack-probing workaround for macosx\n"
5960 " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n"
6061 " -fPIC enable Position Independent Code\n"
6162 " -fno-PIC disable Position Independent Code\n"
......@@ -444,6 +445,7 @@ int main(int argc, char **argv) {
444445 bool want_single_threaded = false;
445446 bool disable_gen_h = false;
446447 bool bundle_compiler_rt = false;
448 bool disable_stack_probing = false;
447449 Buf *override_std_dir = nullptr;
448450 Buf *override_lib_dir = nullptr;
449451 Buf *main_pkg_path = nullptr;
......@@ -656,6 +658,8 @@ int main(int argc, char **argv) {
656658 disable_gen_h = true;
657659 } else if (strcmp(arg, "--bundle-compiler-rt") == 0) {
658660 bundle_compiler_rt = true;
661 } else if (strcmp(arg, "--disable-stack-probing") == 0) {
662 disable_stack_probing = true;
659663 } else if (strcmp(arg, "--test-cmd-bin") == 0) {
660664 test_exec_args.append(nullptr);
661665 } else if (arg[1] == 'L' && arg[2] != 0) {
......@@ -1075,6 +1079,7 @@ int main(int argc, char **argv) {
10751079 g->output_dir = output_dir;
10761080 g->disable_gen_h = disable_gen_h;
10771081 g->bundle_compiler_rt = bundle_compiler_rt;
1082 g->disable_stack_probing = disable_stack_probing;
10781083 codegen_set_errmsg_color(g, color);
10791084 g->system_linker_hack = system_linker_hack;
10801085
std/build.zig+5
......@@ -942,6 +942,7 @@ pub const LibExeObjStep = struct {
942942 verbose_cc: bool,
943943 disable_gen_h: bool,
944944 bundle_compiler_rt: bool,
945 disable_stack_probing: bool,
945946 c_std: Builder.CStd,
946947 override_std_dir: ?[]const u8,
947948 override_lib_dir: ?[]const u8,
......@@ -1052,6 +1053,7 @@ pub const LibExeObjStep = struct {
10521053 .filter = null,
10531054 .disable_gen_h = false,
10541055 .bundle_compiler_rt = false,
1056 .disable_stack_probing = false,
10551057 .output_dir = null,
10561058 .need_system_paths = false,
10571059 .single_threaded = false,
......@@ -1457,6 +1459,9 @@ pub const LibExeObjStep = struct {
14571459 if (self.bundle_compiler_rt) {
14581460 try zig_args.append("--bundle-compiler-rt");
14591461 }
1462 if (self.disable_stack_probing) {
1463 try zig_args.append("--disable-stack-probing");
1464 }
14601465
14611466 switch (self.target) {
14621467 Target.Native => {},