authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-16 14:31:54-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-05-16 14:31:54-04:00
log8468ff0fe1466d8b38aff1d265d953b5cc8fefe6
tree584a749d0d79d662f12454b0e2179dde12ad3056
parent07d0aee11a5725dc22eeaa116fb59c40a1a7c99c
parent978fab817caacb7bb3ba96fe3ec08bab1c78c1da
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2507 from ziglang/wasm-libs

improvements to build-lib use case of WebAssembly

5 files changed, 35 insertions(+), 28 deletions(-)

src/codegen.cpp+1-1
...@@ -8548,7 +8548,7 @@ static void gen_root_source(CodeGen *g) {...@@ -8548,7 +8548,7 @@ static void gen_root_source(CodeGen *g) {
8548 }8548 }
8549 report_errors_and_maybe_exit(g);8549 report_errors_and_maybe_exit(g);
85508550
8551 if (!g->is_test_build && g->zig_target->os != OsFreestanding &&8551 if (!g->is_test_build && (g->zig_target->os != OsFreestanding || target_is_wasm(g->zig_target)) &&
8552 g->zig_target->os != OsUefi &&8552 g->zig_target->os != OsUefi &&
8553 !g->have_c_main && !g->have_winmain && !g->have_winmain_crt_startup &&8553 !g->have_c_main && !g->have_winmain && !g->have_winmain_crt_startup &&
8554 ((g->have_pub_main && g->out_type == OutTypeObj) || g->out_type == OutTypeExe))8554 ((g->have_pub_main && g->out_type == OutTypeObj) || g->out_type == OutTypeExe))
src/link.cpp+12-13
...@@ -800,19 +800,18 @@ static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path,...@@ -800,19 +800,18 @@ static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path,
800 return &child_gen->output_file_path;800 return &child_gen->output_file_path;
801}801}
802802
803static Buf *build_a(CodeGen *parent_gen, const char *aname) {803static Buf *build_compiler_rt(CodeGen *parent_gen, OutType child_out_type) {
804 Buf *source_basename = buf_sprintf("%s.zig", aname);
805 Buf *full_path = buf_alloc();804 Buf *full_path = buf_alloc();
806 os_path_join(parent_gen->zig_std_special_dir, source_basename, full_path);805 os_path_join(parent_gen->zig_std_special_dir, buf_create_from_str("compiler_rt.zig"), full_path);
807806
808 return build_a_raw(parent_gen, aname, full_path, OutTypeLib);807 return build_a_raw(parent_gen, "compiler_rt", full_path, child_out_type);
809}808}
810809
811static Buf *build_compiler_rt(CodeGen *parent_gen, OutType child_out_type) {810static Buf *build_c(CodeGen *parent_gen, OutType child_out_type) {
812 Buf *full_path = buf_alloc();811 Buf *full_path = buf_alloc();
813 os_path_join(parent_gen->zig_std_special_dir, buf_create_from_str("compiler_rt.zig"), full_path);812 os_path_join(parent_gen->zig_std_special_dir, buf_create_from_str("c.zig"), full_path);
814813
815 return build_a_raw(parent_gen, "compiler_rt", full_path, child_out_type);814 return build_a_raw(parent_gen, "c", full_path, child_out_type);
816}815}
817816
818static const char *get_darwin_arch_string(const ZigTarget *t) {817static const char *get_darwin_arch_string(const ZigTarget *t) {
...@@ -1003,7 +1002,7 @@ static void construct_linker_job_elf(LinkJob *lj) {...@@ -1003,7 +1002,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
10031002
1004 if (!g->is_dummy_so && (g->out_type == OutTypeExe || is_dyn_lib)) {1003 if (!g->is_dummy_so && (g->out_type == OutTypeExe || is_dyn_lib)) {
1005 if (g->libc_link_lib == nullptr) {1004 if (g->libc_link_lib == nullptr) {
1006 Buf *libc_a_path = build_a(g, "c");1005 Buf *libc_a_path = build_c(g, OutTypeLib);
1007 lj->args.append(buf_ptr(libc_a_path));1006 lj->args.append(buf_ptr(libc_a_path));
1008 }1007 }
10091008
...@@ -1118,10 +1117,10 @@ static void construct_linker_job_wasm(LinkJob *lj) {...@@ -1118,10 +1117,10 @@ static void construct_linker_job_wasm(LinkJob *lj) {
1118 }1117 }
11191118
1120 if (g->out_type != OutTypeObj) {1119 if (g->out_type != OutTypeObj) {
1121 Buf *libc_a_path = build_a(g, "c");1120 Buf *libc_o_path = build_c(g, OutTypeObj);
1122 lj->args.append(buf_ptr(libc_a_path));1121 lj->args.append(buf_ptr(libc_o_path));
11231122
1124 Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeLib);1123 Buf *compiler_rt_o_path = build_compiler_rt(g, OutTypeObj);
1125 lj->args.append(buf_ptr(compiler_rt_o_path));1124 lj->args.append(buf_ptr(compiler_rt_o_path));
1126 }1125 }
1127}1126}
...@@ -1360,7 +1359,7 @@ static void construct_linker_job_coff(LinkJob *lj) {...@@ -1360,7 +1359,7 @@ static void construct_linker_job_coff(LinkJob *lj) {
13601359
1361 if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) {1360 if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) {
1362 if (g->libc_link_lib == nullptr && !g->is_dummy_so) {1361 if (g->libc_link_lib == nullptr && !g->is_dummy_so) {
1363 Buf *libc_a_path = build_a(g, "c");1362 Buf *libc_a_path = build_c(g, OutTypeLib);
1364 lj->args.append(buf_ptr(libc_a_path));1363 lj->args.append(buf_ptr(libc_a_path));
1365 }1364 }
13661365
...@@ -1687,7 +1686,7 @@ void codegen_link(CodeGen *g) {...@@ -1687,7 +1686,7 @@ void codegen_link(CodeGen *g) {
1687 lj.args.append("-r");1686 lj.args.append("-r");
1688 }1687 }
16891688
1690 if (g->out_type == OutTypeLib && !g->is_dynamic) {1689 if (g->out_type == OutTypeLib && !g->is_dynamic && !target_is_wasm(g->zig_target)) {
1691 ZigList<const char *> file_names = {};1690 ZigList<const char *> file_names = {};
1692 for (size_t i = 0; i < g->link_objects.length; i += 1) {1691 for (size_t i = 0; i < g->link_objects.length; i += 1) {
1693 file_names.append(buf_ptr(g->link_objects.at(i)));1692 file_names.append(buf_ptr(g->link_objects.at(i)));
src/target.cpp+4-3
...@@ -947,8 +947,6 @@ bool target_allows_addr_zero(const ZigTarget *target) {...@@ -947,8 +947,6 @@ bool target_allows_addr_zero(const ZigTarget *target) {
947const char *target_o_file_ext(const ZigTarget *target) {947const char *target_o_file_ext(const ZigTarget *target) {
948 if (target->abi == ZigLLVM_MSVC || target->os == OsWindows || target->os == OsUefi) {948 if (target->abi == ZigLLVM_MSVC || target->os == OsWindows || target->os == OsUefi) {
949 return ".obj";949 return ".obj";
950 } else if (target_is_wasm(target)) {
951 return ".wasm";
952 } else {950 } else {
953 return ".o";951 return ".o";
954 }952 }
...@@ -975,7 +973,7 @@ const char *target_exe_file_ext(const ZigTarget *target) {...@@ -975,7 +973,7 @@ const char *target_exe_file_ext(const ZigTarget *target) {
975}973}
976974
977const char *target_lib_file_prefix(const ZigTarget *target) {975const char *target_lib_file_prefix(const ZigTarget *target) {
978 if (target->os == OsWindows || target->os == OsUefi) {976 if (target->os == OsWindows || target->os == OsUefi || target_is_wasm(target)) {
979 return "";977 return "";
980 } else {978 } else {
981 return "lib";979 return "lib";
...@@ -985,6 +983,9 @@ const char *target_lib_file_prefix(const ZigTarget *target) {...@@ -985,6 +983,9 @@ const char *target_lib_file_prefix(const ZigTarget *target) {
985const char *target_lib_file_ext(const ZigTarget *target, bool is_static,983const char *target_lib_file_ext(const ZigTarget *target, bool is_static,
986 size_t version_major, size_t version_minor, size_t version_patch)984 size_t version_major, size_t version_minor, size_t version_patch)
987{985{
986 if (target_is_wasm(target)) {
987 return ".wasm";
988 }
988 if (target->os == OsWindows || target->os == OsUefi) {989 if (target->os == OsWindows || target->os == OsUefi) {
989 if (is_static) {990 if (is_static) {
990 return ".lib";991 return ".lib";
std/special/bootstrap.zig+15-8
...@@ -8,34 +8,41 @@ const assert = std.debug.assert;...@@ -8,34 +8,41 @@ const assert = std.debug.assert;
88
9var argc_ptr: [*]usize = undefined;9var argc_ptr: [*]usize = undefined;
1010
11const is_wasm = switch (builtin.arch) { .wasm32, .wasm64 => true, else => false};
12
11comptime {13comptime {
12 const strong_linkage = builtin.GlobalLinkage.Strong;
13 if (builtin.link_libc) {14 if (builtin.link_libc) {
14 @export("main", main, strong_linkage);15 @export("main", main, .Strong);
15 } else if (builtin.os == builtin.Os.windows) {16 } else if (builtin.os == .windows) {
16 @export("WinMainCRTStartup", WinMainCRTStartup, strong_linkage);17 @export("WinMainCRTStartup", WinMainCRTStartup, .Strong);
18 } else if (is_wasm and builtin.os == .freestanding) {
19 @export("_start", wasm_freestanding_start, .Strong);
17 } else {20 } else {
18 @export("_start", _start, strong_linkage);21 @export("_start", _start, .Strong);
19 }22 }
20}23}
2124
25extern fn wasm_freestanding_start() void {
26 _ = callMain();
27}
28
22nakedcc fn _start() noreturn {29nakedcc fn _start() noreturn {
23 if (builtin.os == builtin.Os.wasi) {30 if (builtin.os == builtin.Os.wasi) {
24 std.os.wasi.proc_exit(callMain());31 std.os.wasi.proc_exit(callMain());
25 }32 }
2633
27 switch (builtin.arch) {34 switch (builtin.arch) {
28 builtin.Arch.x86_64 => {35 .x86_64 => {
29 argc_ptr = asm ("lea (%%rsp), %[argc]"36 argc_ptr = asm ("lea (%%rsp), %[argc]"
30 : [argc] "=r" (-> [*]usize)37 : [argc] "=r" (-> [*]usize)
31 );38 );
32 },39 },
33 builtin.Arch.i386 => {40 .i386 => {
34 argc_ptr = asm ("lea (%%esp), %[argc]"41 argc_ptr = asm ("lea (%%esp), %[argc]"
35 : [argc] "=r" (-> [*]usize)42 : [argc] "=r" (-> [*]usize)
36 );43 );
37 },44 },
38 builtin.Arch.aarch64, builtin.Arch.aarch64_be => {45 .aarch64, .aarch64_be => {
39 argc_ptr = asm ("mov %[argc], sp"46 argc_ptr = asm ("mov %[argc], sp"
40 : [argc] "=r" (-> [*]usize)47 : [argc] "=r" (-> [*]usize)
41 );48 );
std/special/c.zig+3-3
...@@ -11,14 +11,14 @@ const maxInt = std.math.maxInt;...@@ -11,14 +11,14 @@ const maxInt = std.math.maxInt;
11const is_wasm = switch (builtin.arch) { .wasm32, .wasm64 => true, else => false};11const is_wasm = switch (builtin.arch) { .wasm32, .wasm64 => true, else => false};
12const is_freestanding = switch (builtin.os) { .freestanding => true, else => false };12const is_freestanding = switch (builtin.os) { .freestanding => true, else => false };
13comptime {13comptime {
14 if (is_freestanding and is_wasm) {14 if (is_freestanding and is_wasm and builtin.link_libc) {
15 @export("_start", wasm_start, .Strong);15 @export("_start", wasm_start, .Strong);
16 }16 }
17}17}
1818
19extern fn main(argc: c_int, argv: [*][*]u8) c_int;19extern fn main(argc: c_int, argv: [*][*]u8) c_int;
20extern fn wasm_start() c_int {20extern fn wasm_start() void {
21 return main(0, undefined);21 _ = main(0, undefined);
22}22}
2323
24// Avoid dragging in the runtime safety mechanisms into this .o file,24// Avoid dragging in the runtime safety mechanisms into this .o file,