Skip to content

Commit 657e1dc

Browse files
dgryskiclaude
andcommitted
lib/wasi-libc: upgrade to wasi-sdk-32
wasi-libc migrated its build system from a Makefile to CMake between wasi-sdk-16 and wasi-sdk-32, restructuring some source directories and adding new generated headers. Update builder/wasilibc.go to match: - Generate wasi/version.h (previously produced by CMake's configure_file), which defines __wasip1__ and is now widely included and used to gate code. - Follow thread/thrd_sleep.c moving to thread/common/thrd_sleep.c, and add newly-added top-half sources (realpath.c, gethostname.c, inet_addr.c, inet_legacy.c, inet_ntoa.c). - Exclude internal/emulate_wait4.c, a new Linux wait4 emulation file that needs sys/wait.h, which isn't relevant on WASI. - Exclude newly-added wasip2/wasip3-only socket sources that now live alongside the wasip1 sources in libc-bottom-half/sources, and pick up the new libc-bottom-half/sources/math subdirectory. Bump the wasi-libc cache version to invalidate stale build caches. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 7c3dc05 commit 657e1dc

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

builder/wasilibc.go

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,33 @@ import (
99
"github.com/tinygo-org/tinygo/goenv"
1010
)
1111

12+
// generateWasiVersionHeader creates wasi/version.h from its .in template,
13+
// defining __wasip<preview>__ (preview is "1", "2", or "3") and leaving the
14+
// other __wasipN__ macros undefined. Upstream generates this file with
15+
// CMake's configure_file as part of the CMake build.
16+
func generateWasiVersionHeader(includeDir, preview string) error {
17+
libcDir := filepath.Join(goenv.Get("TINYGOROOT"), "lib/wasi-libc")
18+
versionHeaderIn := filepath.Join(libcDir, "libc-bottom-half/headers/public/wasi/version.h.in")
19+
data, err := os.ReadFile(versionHeaderIn)
20+
if err != nil {
21+
return err
22+
}
23+
define := "__wasip" + preview + "__"
24+
lines := strings.Split(string(data), "\n")
25+
for i, line := range lines {
26+
if !strings.HasPrefix(line, "#cmakedefine ") {
27+
continue
28+
}
29+
name := strings.Fields(line)[1]
30+
if name == define {
31+
lines[i] = "#define " + name
32+
} else {
33+
lines[i] = "/* #undef " + name + " */"
34+
}
35+
}
36+
return os.WriteFile(filepath.Join(includeDir, "wasi", "version.h"), []byte(strings.Join(lines, "\n")), 0o666)
37+
}
38+
1239
var libWasiLibc = Library{
1340
name: "wasi-libc",
1441
makeHeaders: func(target, includeDir string) error {
@@ -84,6 +111,17 @@ var libWasiLibc = Library{
84111
}
85112
}
86113

114+
// wasi/version.h is normally generated by CMake's configure_file
115+
// from version.h.in, defining __wasip1__/__wasip2__/__wasip3__
116+
// depending on the WASI preview targeted. It's included (directly or
117+
// transitively) by most of wasi-libc, including wasi/api.h.
118+
// libWasiLibc currently only targets wasip1; when wasip2/wasip3
119+
// build against wasi-libc directly (instead of libWasmBuiltins),
120+
// this will need to be told which preview to define.
121+
if err := generateWasiVersionHeader(includeDir, "1"); err != nil {
122+
return err
123+
}
124+
87125
return nil
88126
},
89127
cflags: func(target, headerPath string) []string {
@@ -133,6 +171,7 @@ var libWasiLibc = Library{
133171
{glob: "libc-top-half/musl/src/conf/*.c"},
134172
{glob: "libc-top-half/musl/src/internal/*.c", exclude: []string{
135173
"procfdname.c", "syscall.c", "syscall_ret.c", "vdso.c", "version.c",
174+
"emulate_wait4.c", // wait4/waitid emulation, not relevant/available on WASI
136175
}},
137176
{glob: "libc-top-half/musl/src/locale/*.c", exclude: []string{
138177
"dcngettext.c", "textdomain.c", "bind_textdomain_codeset.c"}},
@@ -166,7 +205,20 @@ var libWasiLibc = Library{
166205
// Bottom half: connect top half to WASI equivalents.
167206
{glob: "libc-bottom-half/cloudlibc/src/libc/*/*.c"},
168207
{glob: "libc-bottom-half/cloudlibc/src/libc/sys/*/*.c"},
169-
{glob: "libc-bottom-half/sources/*.c"},
208+
{glob: "libc-bottom-half/sources/math/*.c"},
209+
{glob: "libc-bottom-half/sources/*.c", exclude: []string{
210+
// These implement sockets on top of a shared "descriptor
211+
// table" abstraction, used only for wasip2/wasip3. wasip1
212+
// uses the cloudlibc socket syscalls (and accept-wasip1.c)
213+
// instead.
214+
"accept.c", "bind.c", "connect.c", "descriptor_table.c",
215+
"file.c", "file_utils.c", "getsockpeername.c", "listen.c",
216+
"netdb.c", "recv.c", "send.c", "shutdown.c", "socket.c",
217+
"sockets_utils.c", "sockopt.c", "tcp.c", "udp.c",
218+
// wasip2/wasip3-only bindings.
219+
"wasip2.c", "wasip2_stdio.c",
220+
"wasip3.c", "wasip3_block_on.c", "wasip3_stdio.c",
221+
}},
170222
}
171223

172224
// We're using the Boehm GC, so we need a heap implementation in the libc.
@@ -243,9 +295,14 @@ var libWasiLibc = Library{
243295
"libc-top-half/musl/src/env/setenv.c",
244296
"libc-top-half/musl/src/env/unsetenv.c",
245297
"libc-top-half/musl/src/unistd/posix_close.c",
298+
"libc-top-half/musl/src/unistd/gethostname.c",
246299
"libc-top-half/musl/src/stat/futimesat.c",
247300
"libc-top-half/musl/src/legacy/getpagesize.c",
248-
"libc-top-half/musl/src/thread/thrd_sleep.c",
301+
"libc-top-half/musl/src/thread/common/thrd_sleep.c",
302+
"libc-top-half/musl/src/misc/realpath.c",
303+
"libc-top-half/musl/src/network/inet_addr.c",
304+
"libc-top-half/musl/src/network/inet_legacy.c",
305+
"libc-top-half/musl/src/network/inet_ntoa.c",
249306
}
250307

251308
basepath := goenv.Get("TINYGOROOT") + "/lib/wasi-libc/"

compileopts/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import (
2323
// builder.Library struct but that's hard to do since we want to know the
2424
// library path in advance in several places).
2525
var libVersions = map[string]int{
26-
"musl": 3,
27-
"bdwgc": 2,
26+
"musl": 3,
27+
"bdwgc": 2,
28+
"wasi-libc": 1,
2829
}
2930

3031
// Config keeps all configuration affecting the build in a single struct.

lib/wasi-libc

Submodule wasi-libc updated 854 files

0 commit comments

Comments
 (0)