From 6f6182a5f3a4b027bbff6405600fd21b3ef8b86c Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Sat, 5 Jun 2021 01:08:47 +0200 Subject: [PATCH] zig,cc,wasi: include emulated libs in WASI libc This commit includes emulated libc sublibs that were not included in the compilation and caching of WASI libc that ships with Zig. The libs include (emulated): process clocks, getpid, mman, and signal. With this change, it is now possible to successfully cross-compile `wasm3` engine to WASI with `zig cc`. For the future though, it might be worth considering splitting WASI libc into libc-proper and modularised emulated libs as it is done in upstream, and then have them included only if the user specifically requests emulation/parts of it. --- src/wasi_libc.zig | 277 ++++++++++++++++++++++++++++------------------ 1 file changed, 168 insertions(+), 109 deletions(-) diff --git a/src/wasi_libc.zig b/src/wasi_libc.zig index 3c04203082..b7be6af82f 100644 --- a/src/wasi_libc.zig +++ b/src/wasi_libc.zig @@ -21,35 +21,7 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void { // Compile crt sources. var args = std.ArrayList([]const u8).init(arena); try addCCArgs(comp, arena, &args, false); - try args.appendSlice(&[_][]const u8{ - "-I", - try comp.zig_lib_directory.join(arena, &[_][]const u8{ - "libc", - "wasi", - "libc-bottom-half", - "headers", - "private", - }), - - "-I", - try comp.zig_lib_directory.join(arena, &[_][]const u8{ - "libc", - "wasi", - "libc-bottom-half", - "cloudlibc", - "src", - "include", - }), - - "-I", - try comp.zig_lib_directory.join(arena, &[_][]const u8{ - "libc", - "wasi", - "libc-bottom-half", - "cloudlibc", - "src", - }), - }); + try addLibcBottomHalfIncludes(comp, arena, &args); var comp_sources = std.ArrayList(Compilation.CSourceFile).init(arena); for (crt_src_files) |file_path| { @@ -95,35 +67,7 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void { // Compile libc-bottom-half. var args = std.ArrayList([]const u8).init(arena); try addCCArgs(comp, arena, &args, true); - try args.appendSlice(&[_][]const u8{ - "-I", - try comp.zig_lib_directory.join(arena, &[_][]const u8{ - "libc", - "wasi", - "libc-bottom-half", - "headers", - "private", - }), - - "-I", - try comp.zig_lib_directory.join(arena, &[_][]const u8{ - "libc", - "wasi", - "libc-bottom-half", - "cloudlibc", - "src", - }), - - "-I", - try comp.zig_lib_directory.join(arena, &[_][]const u8{ - "libc", - "wasi", - "libc-bottom-half", - "cloudlibc", - "src", - "include", - }), - }); + try addLibcBottomHalfIncludes(comp, arena, &args); for (libc_bottom_half_src_files) |file_path| { try comp_sources.append(.{ @@ -135,60 +79,77 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void { } } + { + // Compile emulated sources depending only on libc-bottom-half. + var args = std.ArrayList([]const u8).init(arena); + try addCCArgs(comp, arena, &args, true); + try addLibcBottomHalfIncludes(comp, arena, &args); + + for (emulated_process_clocks_src_files) |file_path| { + try comp_sources.append(.{ + .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", try sanitize(arena, file_path), + }), + .extra_flags = args.items, + }); + } + + for (emulated_getpid_src_files) |file_path| { + try comp_sources.append(.{ + .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", try sanitize(arena, file_path), + }), + .extra_flags = args.items, + }); + } + + for (emulated_mman_src_files) |file_path| { + try comp_sources.append(.{ + .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", try sanitize(arena, file_path), + }), + .extra_flags = args.items, + }); + } + } + + { + // Compile emulated signals (bottom-half). + var args = std.ArrayList([]const u8).init(arena); + try addCCArgs(comp, arena, &args, true); + + for (emulated_signal_bottom_half_src_files) |file_path| { + try comp_sources.append(.{ + .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", try sanitize(arena, file_path), + }), + .extra_flags = args.items, + }); + } + } + + { + // Compile emulated signals (top-half). + var args = std.ArrayList([]const u8).init(arena); + try addCCArgs(comp, arena, &args, true); + try addLibcTopHalfIncludes(comp, arena, &args); + try args.append("-D_WASI_EMULATED_SIGNAL"); + + for (emulated_signal_top_half_src_files) |file_path| { + try comp_sources.append(.{ + .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", try sanitize(arena, file_path), + }), + .extra_flags = args.items, + }); + } + } + { // Compile libc-top-half. var args = std.ArrayList([]const u8).init(arena); try addCCArgs(comp, arena, &args, true); - try args.appendSlice(&[_][]const u8{ - "-I", - try comp.zig_lib_directory.join(arena, &[_][]const u8{ - "libc", - "wasi", - "libc-top-half", - "musl", - "src", - "include", - }), - - "-I", - try comp.zig_lib_directory.join(arena, &[_][]const u8{ - "libc", - "wasi", - "libc-top-half", - "musl", - "src", - "internal", - }), - - "-I", - try comp.zig_lib_directory.join(arena, &[_][]const u8{ - "libc", - "wasi", - "libc-top-half", - "musl", - "arch", - "wasm32", - }), - - "-I", - try comp.zig_lib_directory.join(arena, &[_][]const u8{ - "libc", - "wasi", - "libc-top-half", - "musl", - "arch", - "generic", - }), - - "-I", - try comp.zig_lib_directory.join(arena, &[_][]const u8{ - "libc", - "wasi", - "libc-top-half", - "headers", - "private", - }), - }); + try addLibcTopHalfIncludes(comp, arena, &args); for (libc_top_half_src_files) |file_path| { try comp_sources.append(.{ @@ -251,6 +212,99 @@ fn addCCArgs( }); } +fn addLibcBottomHalfIncludes( + comp: *Compilation, + arena: *Allocator, + args: *std.ArrayList([]const u8), +) error{OutOfMemory}!void { + try args.appendSlice(&[_][]const u8{ + "-I", + try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", + "wasi", + "libc-bottom-half", + "headers", + "private", + }), + + "-I", + try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", + "wasi", + "libc-bottom-half", + "cloudlibc", + "src", + "include", + }), + + "-I", + try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", + "wasi", + "libc-bottom-half", + "cloudlibc", + "src", + }), + }); +} + +fn addLibcTopHalfIncludes( + comp: *Compilation, + arena: *Allocator, + args: *std.ArrayList([]const u8), +) error{OutOfMemory}!void { + try args.appendSlice(&[_][]const u8{ + "-I", + try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", + "wasi", + "libc-top-half", + "musl", + "src", + "include", + }), + + "-I", + try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", + "wasi", + "libc-top-half", + "musl", + "src", + "internal", + }), + + "-I", + try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", + "wasi", + "libc-top-half", + "musl", + "arch", + "wasm32", + }), + + "-I", + try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", + "wasi", + "libc-top-half", + "musl", + "arch", + "generic", + }), + + "-I", + try comp.zig_lib_directory.join(arena, &[_][]const u8{ + "libc", + "wasi", + "libc-top-half", + "headers", + "private", + }), + }); +} + const dlmalloc_src_files = [_][]const u8{ "wasi/dlmalloc/src/dlmalloc.c", }; @@ -1025,6 +1079,11 @@ const emulated_mman_src_files = &[_][]const u8{ "wasi/libc-bottom-half/mman/mman.c", }; -const emulated_signal_src_files = &[_][]const u8{ +const emulated_signal_bottom_half_src_files = &[_][]const u8{ "wasi/libc-bottom-half/signal/signal.c", }; + +const emulated_signal_top_half_src_files = &[_][]const u8{ + "wasi/libc-top-half/musl/src/signal/psignal.c", + "wasi/libc-top-half/musl/src/string/strsignal.c", +};