From 81e960eb748fdb37d1d61da262ec343e2fdb2511 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 16 May 2019 13:56:56 -0400 Subject: improvements to build-lib use case of WebAssembly * build-exe does include the startup code that supplies _start for the wasm32-freestanding target. Previously this did not occur because of logic excluding "freestanding". * build-lib for wasm32-freestanding target gets linked by LLD. To avoid infinite recursion, compiler_rt and zig libc are built as objects rather than libraries. - no "lib" prefix and ".wasm" extension instead of ".a". Rather than build-lib foo.zig producing "libfoo.a", now it produces "foo.wasm". * go back to using `.o` extension for webassembly objects * zig libc only provides _start symbol for wasm when linking libc. --- src/link.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'src/link.cpp') diff --git a/src/link.cpp b/src/link.cpp index 538d663b03..3585f675a5 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -800,19 +800,18 @@ static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path, return &child_gen->output_file_path; } -static Buf *build_a(CodeGen *parent_gen, const char *aname) { - Buf *source_basename = buf_sprintf("%s.zig", aname); +static Buf *build_compiler_rt(CodeGen *parent_gen, OutType child_out_type) { Buf *full_path = buf_alloc(); - os_path_join(parent_gen->zig_std_special_dir, source_basename, full_path); + os_path_join(parent_gen->zig_std_special_dir, buf_create_from_str("compiler_rt.zig"), full_path); - return build_a_raw(parent_gen, aname, full_path, OutTypeLib); + return build_a_raw(parent_gen, "compiler_rt", full_path, child_out_type); } -static Buf *build_compiler_rt(CodeGen *parent_gen, OutType child_out_type) { +static Buf *build_c(CodeGen *parent_gen, OutType child_out_type) { Buf *full_path = buf_alloc(); - os_path_join(parent_gen->zig_std_special_dir, buf_create_from_str("compiler_rt.zig"), full_path); + os_path_join(parent_gen->zig_std_special_dir, buf_create_from_str("c.zig"), full_path); - return build_a_raw(parent_gen, "compiler_rt", full_path, child_out_type); + return build_a_raw(parent_gen, "c", full_path, child_out_type); } static const char *get_darwin_arch_string(const ZigTarget *t) { @@ -1003,7 +1002,7 @@ static void construct_linker_job_elf(LinkJob *lj) { if (!g->is_dummy_so && (g->out_type == OutTypeExe || is_dyn_lib)) { if (g->libc_link_lib == nullptr) { - Buf *libc_a_path = build_a(g, "c"); + Buf *libc_a_path = build_c(g, OutTypeLib); lj->args.append(buf_ptr(libc_a_path)); } @@ -1118,10 +1117,10 @@ static void construct_linker_job_wasm(LinkJob *lj) { } if (g->out_type != OutTypeObj) { - Buf *libc_a_path = build_a(g, "c"); - lj->args.append(buf_ptr(libc_a_path)); + Buf *libc_o_path = build_c(g, OutTypeObj); + lj->args.append(buf_ptr(libc_o_path)); - Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeLib); + Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeObj); lj->args.append(buf_ptr(compiler_rt_o_path)); } } @@ -1360,7 +1359,7 @@ static void construct_linker_job_coff(LinkJob *lj) { if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) { if (g->libc_link_lib == nullptr && !g->is_dummy_so) { - Buf *libc_a_path = build_a(g, "c"); + Buf *libc_a_path = build_c(g, OutTypeLib); lj->args.append(buf_ptr(libc_a_path)); } @@ -1687,7 +1686,7 @@ void codegen_link(CodeGen *g) { lj.args.append("-r"); } - if (g->out_type == OutTypeLib && !g->is_dynamic) { + if (g->out_type == OutTypeLib && !g->is_dynamic && !target_is_wasm(g->zig_target)) { ZigList file_names = {}; for (size_t i = 0; i < g->link_objects.length; i += 1) { file_names.append(buf_ptr(g->link_objects.at(i))); -- cgit v1.2.3