From 869baca117224103266ae90608198cce681c2ab3 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 4 Mar 2021 14:31:34 +0100 Subject: [PATCH] wasi: specify wasi-libc in a different way This way is more consistent with how picolibc is specified and allows generating a helpful error message. This error message should never be generated for TinyGo binary releases, only when doing local development. --- builder/build.go | 15 +++++++++++++-- targets/wasi.json | 4 ++-- targets/wasm.json | 4 ++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/builder/build.go b/builder/build.go index 4d186271..bdeccad5 100644 --- a/builder/build.go +++ b/builder/build.go @@ -186,7 +186,9 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil } // Add libc dependency if needed. - if config.Target.Libc == "picolibc" { + root := goenv.Get("TINYGOROOT") + switch config.Target.Libc { + case "picolibc": path, job, err := Picolibc.load(config.Triple(), config.CPU(), dir) if err != nil { return err @@ -198,12 +200,21 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil linkerDependencies = append(linkerDependencies, job) } ldflags = append(ldflags, path) + case "wasi-libc": + path := filepath.Join(root, "lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a") + if _, err := os.Stat(path); os.IsNotExist(err) { + return errors.New("could not find wasi-libc, perhaps you need to run `make wasi-libc`?") + } + ldflags = append(ldflags, path) + case "": + // no library specified, so nothing to do + default: + return fmt.Errorf("unknown libc: %s", config.Target.Libc) } // Add jobs to compile extra files. These files are in C or assembly and // contain things like the interrupt vector table and low level operations // such as stack switching. - root := goenv.Get("TINYGOROOT") for i, path := range config.ExtraFiles() { abspath := filepath.Join(root, path) outpath := filepath.Join(dir, "extra-"+strconv.Itoa(i)+"-"+filepath.Base(path)+".o") diff --git a/targets/wasi.json b/targets/wasi.json index 091e3c5a..45ce68d8 100644 --- a/targets/wasi.json +++ b/targets/wasi.json @@ -5,6 +5,7 @@ "goarch": "arm", "compiler": "clang", "linker": "wasm-ld", + "libc": "wasi-libc", "cflags": [ "--target=wasm32--wasi", "--sysroot={root}/lib/wasi-libc/sysroot", @@ -14,8 +15,7 @@ "--allow-undefined", "--stack-first", "--export-dynamic", - "--no-demangle", - "{root}/lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a" + "--no-demangle" ], "emulator": ["wasmtime"], "wasm-abi": "generic" diff --git a/targets/wasm.json b/targets/wasm.json index 7767cd96..5b0aad04 100644 --- a/targets/wasm.json +++ b/targets/wasm.json @@ -5,6 +5,7 @@ "goarch": "wasm", "compiler": "clang", "linker": "wasm-ld", + "libc": "wasi-libc", "cflags": [ "--target=wasm32--wasi", "--sysroot={root}/lib/wasi-libc/sysroot", @@ -14,8 +15,7 @@ "--allow-undefined", "--stack-first", "--export-all", - "--no-demangle", - "{root}/lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a" + "--no-demangle" ], "emulator": ["node", "targets/wasm_exec.js"], "wasm-abi": "js"