github.com/wasilibs/nottinygc@v0.7.2-0.20240312114022-d59c9478ef51/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{}, os.Stdout, os.Stderr, "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", ".idea/**",
    88  		"-ignore", "**/*.yml",
    89  		"-ignore", "**/*.yaml",
    90  		"."); err != nil {
    91  		return fmt.Errorf("missing copyright headers, use go run mage.go format: %w", err)
    92  	}
    93  
    94  	return sh.RunV("go", "run", fmt.Sprintf("github.com/golangci/golangci-lint/cmd/golangci-lint@%s", verGolancCILint), "run")
    95  }
    96  
    97  // Check runs lint and tests.
    98  func Check() {
    99  	mg.SerialDeps(Lint, Test)
   100  }
   101  
   102  // UpdateLibs updates the precompiled wasm libraries.
   103  func UpdateLibs() error {
   104  	libs := []string{"bdwgc", "mimalloc"}
   105  	for _, lib := range libs {
   106  		if err := sh.RunV("docker", "build", "-t", "ghcr.io/wasilibs/nottinygc/buildtools-"+lib, filepath.Join("buildtools", lib)); err != nil {
   107  			return err
   108  		}
   109  		wd, err := os.Getwd()
   110  		if err != nil {
   111  			return err
   112  		}
   113  		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 {
   114  			return err
   115  		}
   116  	}
   117  	return nil
   118  }
   119  
   120  // Bench runs benchmarks.
   121  func Bench() error {
   122  	if err := buildBenchExecutable(); err != nil {
   123  		return err
   124  	}
   125  
   126  	if err := sh.RunV("tinygo", "build", "-scheduler=none", "-target=wasi", "-o", "build/benchref.wasm", "./bench"); err != nil {
   127  		return err
   128  	}
   129  
   130  	return sh.RunV("go", "test", "-bench=.", "-benchtime=10s", "./bench")
   131  }
   132  
   133  func buildBenchExecutable() error {
   134  	if err := os.MkdirAll("build", 0o755); err != nil {
   135  		return err
   136  	}
   137  
   138  	if err := sh.RunV("tinygo", "build", "-gc=custom", "-tags=custommalloc", "-scheduler=none", "-target=wasi", "-o", "build/bench.wasm", "./bench"); err != nil {
   139  		return err
   140  	}
   141  
   142  	if err := sh.RunV("tinygo", "build", "-gc=custom", "-tags='custommalloc nottinygc_envoy'", "-scheduler=none", "-target=wasi", "-o", "build/bench_envoy.wasm", "./bench"); err != nil {
   143  		return err
   144  	}
   145  
   146  	return nil
   147  }
   148  
   149  var Default = Test