github.com/higress-group/nottinygc@v0.0.0-20231101025119-e93c4c2f8520/magefiles/magefile.go (about) 1 // Copyright wasilibs authors 2 // SPDX-License-Identifier: MIT 3 4 package main 5 6 import ( 7 "bytes" 8 "errors" 9 "fmt" 10 "io" 11 "os" 12 "path/filepath" 13 "strings" 14 15 "github.com/magefile/mage/mg" 16 "github.com/magefile/mage/sh" 17 ) 18 19 // Test runs unit tests 20 func Test() error { 21 v, err := sh.Output("tinygo", "version") 22 if err != nil { 23 return fmt.Errorf("invoking tinygo: %w", err) 24 } 25 26 tags := []string{"custommalloc"} 27 if strings.HasSuffix(v, "tinygo version 0.28.") { 28 tags = append(tags, "nottinygc_finalizer") 29 } 30 31 if err := sh.RunV("tinygo", "test", "-gc=custom", fmt.Sprintf("-tags='%s'", strings.Join(tags, " ")), "-target=wasi", "-v", "-scheduler=none", "./..."); err != nil { 32 return err 33 } 34 35 var stdout bytes.Buffer 36 if _, err := sh.Exec(map[string]string{}, &stdout, io.Discard, "tinygo", "test", "-target=wasi", "-v", "-scheduler=none", "./..."); err == nil { 37 return errors.New("expected tinygo test to fail without -gc=custom") 38 } 39 if s := stdout.String(); !strings.Contains(s, "nottinygc requires passing -gc=custom and -tags=custommalloc to TinyGo when compiling") { 40 return fmt.Errorf("unexpected error message: %s", s) 41 } 42 43 if err := buildBenchExecutable(); err != nil { 44 return err 45 } 46 47 if err := sh.RunV("go", "test", "./bench"); err != nil { 48 return err 49 } 50 51 return nil 52 } 53 54 func Format() error { 55 if err := sh.RunV("go", "run", fmt.Sprintf("mvdan.cc/gofumpt@%s", verGoFumpt), "-l", "-w", "."); err != nil { 56 return err 57 } 58 59 // addlicense strangely logs skipped files to stderr despite not being erroneous, so use the long sh.Exec form to 60 // discard stderr too. 61 if _, err := sh.Exec(map[string]string{}, io.Discard, io.Discard, "go", "run", fmt.Sprintf("github.com/google/addlicense@%s", verAddLicense), 62 "-c", "wasilibs authors", 63 "-l", "mit", 64 "-s=only", 65 "-y=", 66 "-ignore", "**/*.yml", 67 "-ignore", "**/*.yaml", 68 "."); err != nil { 69 return err 70 } 71 72 if err := sh.RunV("go", "run", fmt.Sprintf("github.com/rinchsan/gosimports/cmd/gosimports@%s", verGosImports), "-w", 73 "-local", "github.com/wasilibs/nottinygc", 74 "."); err != nil { 75 return nil 76 } 77 return nil 78 } 79 80 func Lint() error { 81 if _, err := sh.Exec(map[string]string{}, io.Discard, io.Discard, "go", "run", fmt.Sprintf("github.com/google/addlicense@%s", verAddLicense), 82 "-check", 83 "-c", "wasilibs authors", 84 "-s=only", 85 "-l=mit", 86 "-y=", 87 "-ignore", "**/*.yml", 88 "-ignore", "**/*.yaml", 89 "."); err != nil { 90 return fmt.Errorf("missing copyright headers, use go run mage.go format: %w", err) 91 } 92 93 return sh.RunV("go", "run", fmt.Sprintf("github.com/golangci/golangci-lint/cmd/golangci-lint@%s", verGolancCILint), "run") 94 } 95 96 // Check runs lint and tests. 97 func Check() { 98 mg.SerialDeps(Lint, Test) 99 } 100 101 // UpdateLibs updates the precompiled wasm libraries. 102 func UpdateLibs() error { 103 libs := []string{"bdwgc", "mimalloc"} 104 for _, lib := range libs { 105 if err := sh.RunV("docker", "build", "-t", "ghcr.io/wasilibs/nottinygc/buildtools-"+lib, filepath.Join("buildtools", lib)); err != nil { 106 return err 107 } 108 wd, err := os.Getwd() 109 if err != nil { 110 return err 111 } 112 if err := sh.RunV("docker", "run", "-it", "--rm", "-v", fmt.Sprintf("%s:/out", filepath.Join(wd, "wasm")), "ghcr.io/wasilibs/nottinygc/buildtools-"+lib); err != nil { 113 return err 114 } 115 } 116 return nil 117 } 118 119 // Bench runs benchmarks. 120 func Bench() error { 121 if err := buildBenchExecutable(); err != nil { 122 return err 123 } 124 125 if err := sh.RunV("tinygo", "build", "-scheduler=none", "-target=wasi", "-o", "build/benchref.wasm", "./bench"); err != nil { 126 return err 127 } 128 129 return sh.RunV("go", "test", "-bench=.", "-benchtime=10s", "./bench") 130 } 131 132 func buildBenchExecutable() error { 133 if err := os.MkdirAll("build", 0o755); err != nil { 134 return err 135 } 136 137 return sh.RunV("tinygo", "build", "-gc=custom", "-tags=custommalloc", "-scheduler=none", "-target=wasi", "-o", "build/bench.wasm", "./bench") 138 } 139 140 var Default = Test