commit 6f6182a5f3a4b027bbff6405600fd21b3ef8b86c (tree)
parent 2d43db1d767ec56183cd7cf3ee8c5fd929548beb
Author: Jakub Konka <kubkon@jakubkonka.com>
Date: Sat, 5 Jun 2021 01:08:47 +0200
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.
Diffstat:
| M | src/wasi_libc.zig | | | 269 | ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
1 file changed, 164 insertions(+), 105 deletions(-)
diff --git 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,37 +67,43 @@ 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",
- }),
+ try addLibcBottomHalfIncludes(comp, arena, &args);
- "-I",
- try comp.zig_lib_directory.join(arena, &[_][]const u8{
- "libc",
- "wasi",
- "libc-bottom-half",
- "cloudlibc",
- "src",
- }),
+ for (libc_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,
+ });
+ }
+ }
- "-I",
- try comp.zig_lib_directory.join(arena, &[_][]const u8{
- "libc",
- "wasi",
- "libc-bottom-half",
- "cloudlibc",
- "src",
- "include",
- }),
- });
+ {
+ // 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 (libc_bottom_half_src_files) |file_path| {
+ 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),
@@ -136,59 +114,42 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void {
}
{
- // Compile libc-top-half.
+ // Compile emulated signals (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-top-half",
- "musl",
- "src",
- "include",
- }),
- "-I",
- try comp.zig_lib_directory.join(arena, &[_][]const u8{
- "libc",
- "wasi",
- "libc-top-half",
- "musl",
- "src",
- "internal",
- }),
+ 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,
+ });
+ }
+ }
- "-I",
- try comp.zig_lib_directory.join(arena, &[_][]const u8{
- "libc",
- "wasi",
- "libc-top-half",
- "musl",
- "arch",
- "wasm32",
- }),
+ {
+ // 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");
- "-I",
- try comp.zig_lib_directory.join(arena, &[_][]const u8{
- "libc",
- "wasi",
- "libc-top-half",
- "musl",
- "arch",
- "generic",
- }),
+ 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,
+ });
+ }
+ }
- "-I",
- try comp.zig_lib_directory.join(arena, &[_][]const u8{
- "libc",
- "wasi",
- "libc-top-half",
- "headers",
- "private",
- }),
- });
+ {
+ // Compile libc-top-half.
+ var args = std.ArrayList([]const u8).init(arena);
+ try addCCArgs(comp, arena, &args, true);
+ 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",
+};