diff --git a/builder/build.go b/builder/build.go index b9965c1e85..321400433d 100644 --- a/builder/build.go +++ b/builder/build.go @@ -168,6 +168,13 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe return BuildResult{}, errors.New("could not find wasi-libc, perhaps you need to run `make wasi-libc`?") } libcDependencies = append(libcDependencies, dummyCompileJob(path)) + case "wasmbuiltins": + libcJob, unlock, err := WasmBuiltins.load(config, tmpdir) + if err != nil { + return BuildResult{}, err + } + defer unlock() + libcDependencies = append(libcDependencies, libcJob) case "mingw-w64": _, unlock, err := MinGW.load(config, tmpdir) if err != nil { diff --git a/builder/wasmbuiltins.go b/builder/wasmbuiltins.go new file mode 100644 index 0000000000..a48123b146 --- /dev/null +++ b/builder/wasmbuiltins.go @@ -0,0 +1,66 @@ +package builder + +import ( + "os" + "path/filepath" + + "github.com/tinygo-org/tinygo/goenv" +) + +var WasmBuiltins = Library{ + name: "wasmbuiltins", + makeHeaders: func(target, includeDir string) error { + os.Mkdir(includeDir+"/bits", 0o777) + f, err := os.Create(includeDir + "/bits/alltypes.h") + if err != nil { + return err + } + if _, err := f.Write([]byte(wasmAllTypes)); err != nil { + return err + } + return f.Close() + }, + cflags: func(target, headerPath string) []string { + libcDir := filepath.Join(goenv.Get("TINYGOROOT"), "lib/wasi-libc") + return []string{ + "-Werror", + "-Wall", + "-std=gnu11", + "-nostdlibinc", + "-isystem", libcDir + "/libc-top-half/musl/arch/wasm32", + "-isystem", libcDir + "/libc-top-half/musl/arch/generic", + "-isystem", libcDir + "/libc-top-half/musl/src/internal", + "-isystem", libcDir + "/libc-top-half/musl/src/include", + "-isystem", libcDir + "/libc-top-half/musl/include", + "-I" + headerPath, + } + }, + sourceDir: func() string { return filepath.Join(goenv.Get("TINYGOROOT"), "lib/wasi-libc") }, + librarySources: func(target string) ([]string, error) { + return []string{ + "libc-top-half/musl/src/math/__math_divzero.c", + "libc-top-half/musl/src/math/__math_invalid.c", + "libc-top-half/musl/src/math/__math_oflow.c", + "libc-top-half/musl/src/math/__math_uflow.c", + "libc-top-half/musl/src/math/__math_xflow.c", + "libc-top-half/musl/src/math/exp.c", + "libc-top-half/musl/src/math/exp_data.c", + "libc-top-half/musl/src/math/exp2.c", + "libc-top-half/musl/src/math/log.c", + "libc-top-half/musl/src/math/log_data.c", + }, nil + }, +} + +const wasmAllTypes = ` +typedef __INT8_TYPE__ int8_t; +typedef __INT16_TYPE__ int16_t; +typedef __INT32_TYPE__ int32_t; +typedef __INT64_TYPE__ int64_t; +typedef __UINT8_TYPE__ uint8_t; +typedef __UINT16_TYPE__ uint16_t; +typedef __UINT32_TYPE__ uint32_t; +typedef __UINT64_TYPE__ uint64_t; + +typedef double double_t; +` diff --git a/compileopts/config.go b/compileopts/config.go index 9349738a4a..24da4e280d 100644 --- a/compileopts/config.go +++ b/compileopts/config.go @@ -311,6 +311,8 @@ func (c *Config) CFlags(libclang bool) []string { case "wasi-libc": root := goenv.Get("TINYGOROOT") cflags = append(cflags, "--sysroot="+root+"/lib/wasi-libc/sysroot") + case "wasmbuiltins": + // nothing to add (library is purely for builtins) case "mingw-w64": root := goenv.Get("TINYGOROOT") path, _ := c.LibcPath("mingw-w64") diff --git a/targets/wasm-unknown.json b/targets/wasm-unknown.json index 903afa49e8..a3f6313c19 100644 --- a/targets/wasm-unknown.json +++ b/targets/wasm-unknown.json @@ -7,6 +7,7 @@ "goarch": "arm", "linker": "wasm-ld", "rtlib": "compiler-rt", + "libc": "wasmbuiltins", "scheduler": "none", "gc": "leaking", "default-stack-size": 4096,