diff options
author | Randy Reddig <[email protected]> | 2024-09-17 09:24:23 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2024-09-17 09:24:23 +0200 |
commit | 5a014dd6a3b80bba7f8f8a2d5e82b979eed26827 (patch) | |
tree | 272ef17946735bd317103a59fb2c179ad2909a6b | |
parent | d948941d82261f26adf37b5a699ee2359f1eb42c (diff) | |
download | tinygo-5a014dd6a3b80bba7f8f8a2d5e82b979eed26827.tar.gz tinygo-5a014dd6a3b80bba7f8f8a2d5e82b979eed26827.zip |
tinygo: add relative and absolute --dir options to wasmtime args (#4431)
main: add relative and absolute --dir options to wasmtime args
-rw-r--r-- | GNUmakefile | 2 | ||||
-rw-r--r-- | main.go | 90 |
2 files changed, 33 insertions, 59 deletions
diff --git a/GNUmakefile b/GNUmakefile index 70a7c5f58..34be4045b 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -293,7 +293,7 @@ tinygo: ## Build the TinyGo compiler @if [ ! -f "$(LLVM_BUILDDIR)/bin/llvm-config" ]; then echo "Fetch and build LLVM first by running:"; echo " $(MAKE) llvm-source"; echo " $(MAKE) $(LLVM_BUILDDIR)"; exit 1; fi CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GOENVFLAGS) $(GO) build -buildmode exe -o build/tinygo$(EXE) -tags "byollvm osusergo" -ldflags="-X github.com/tinygo-org/tinygo/goenv.GitSha1=`git rev-parse --short HEAD`" . test: wasi-libc check-nodejs-version - CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test $(GOTESTFLAGS) -timeout=20m -buildmode exe -tags "byollvm osusergo" $(GOTESTPKGS) + CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test $(GOTESTFLAGS) -v -timeout=1h -buildmode exe -tags "byollvm osusergo" $(GOTESTPKGS) # Standard library packages that pass tests on darwin, linux, wasi, and windows, but take over a minute in wasi TEST_PACKAGES_SLOW = \ @@ -283,46 +283,6 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options // Tests are always run in the package directory. cmd.Dir = result.MainDir - // wasmtime is the default emulator used for `-target=wasip1`. wasmtime - // is a WebAssembly runtime CLI with WASI enabled by default. However, - // only stdio are allowed by default. For example, while STDOUT routes - // to the host, other files don't. It also does not inherit environment - // variables from the host. Some tests read testdata files, often from - // outside the package directory. Other tests require temporary - // writeable directories. We allow this by adding wasmtime flags below. - if config.EmulatorName() == "wasmtime" { - // At this point, The current working directory is at the package - // directory. Ex. $GOROOT/src/compress/flate for compress/flate. - // buildAndRun has already added arguments for wasmtime, that allow - // read-access to files such as "testdata/huffman-zero.in". - // - // Ex. main(.wasm) --dir=. -- -test.v - - // Below adds additional wasmtime flags in case a test reads files - // outside its directory, like "../testdata/e.txt". This allows any - // relative directory up to the module root, even if the test never - // reads any files. - // - // Ex. run --dir=.. --dir=../.. --dir=../../.. - var dirs []string - switch config.Target.GOOS { - case "wasip1": - dirs = dirsToModuleRootRel(result.MainDir, result.ModuleRoot) - default: - dirs = dirsToModuleRootAbs(result.MainDir, result.ModuleRoot) - } - - args := []string{"run"} - for _, d := range dirs { - args = append(args, "--dir="+d) - } - - args = append(args, "--env=PWD="+cmd.Dir) - - args = append(args, cmd.Args[1:]...) - cmd.Args = args - } - // Run the test. start := time.Now() err = cmd.Run() @@ -848,12 +808,11 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c for _, v := range environmentVars { emuArgs = append(emuArgs, "--env", v) } - if len(cmdArgs) != 0 { - // Use of '--' argument no longer necessary as of Wasmtime v14: - // https://github.com/bytecodealliance/wasmtime/pull/6946 - // args = append(args, "--") - args = append(args, cmdArgs...) - } + + // Use of '--' argument no longer necessary as of Wasmtime v14: + // https://github.com/bytecodealliance/wasmtime/pull/6946 + // args = append(args, "--") + args = append(args, cmdArgs...) // Set this for nicer backtraces during tests, but don't override the user. if _, ok := os.LookupEnv("WASMTIME_BACKTRACE_DETAILS"); !ok { @@ -903,21 +862,36 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c name = emulator[0] + // wasmtime is a WebAssembly runtime CLI with WASI enabled by default. + // By default, only stdio is allowed. For example, while STDOUT routes + // to the host, other files don't. It also does not inherit environment + // variables from the host. Some tests read testdata files, often from + // outside the package directory. Other tests require temporary + // writeable directories. We allow this by adding wasmtime flags below. if name == "wasmtime" { - // Wasmtime needs some special flags to pass environment variables - // and allow reading from the current directory. - switch config.Options.Target { - case "wasip1": - emuArgs = append(emuArgs, "--dir=.") - case "wasip2": - dir := result.MainDir - if isSingleFile { - cwd, _ := os.Getwd() - dir = cwd + // Below adds additional wasmtime flags in case a test reads files + // outside its directory, like "../testdata/e.txt". This allows any + // relative directory up to the module root, even if the test never + // reads any files. + if config.TestConfig.CompileTestBinary { + // Add relative dirs (../, ../..) up to module root (for wasip1) + dirs := dirsToModuleRootRel(result.MainDir, result.ModuleRoot) + + // Add absolute dirs up to module root (for wasip2) + dirs = append(dirs, dirsToModuleRootAbs(result.MainDir, result.ModuleRoot)...) + + for _, d := range dirs { + emuArgs = append(emuArgs, "--dir="+d) } - emuArgs = append(emuArgs, "--dir="+dir) - emuArgs = append(emuArgs, "--env=PWD="+dir) } + + dir := result.MainDir + if isSingleFile { + dir, _ = os.Getwd() + } + emuArgs = append(emuArgs, "--dir=.") + emuArgs = append(emuArgs, "--dir="+dir) + emuArgs = append(emuArgs, "--env=PWD="+dir) } emuArgs = append(emuArgs, emulator[1:]...) |