github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/builder/wasmbuiltins.go (about)

     1  package builder
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/tinygo-org/tinygo/goenv"
     8  )
     9  
    10  var WasmBuiltins = Library{
    11  	name: "wasmbuiltins",
    12  	makeHeaders: func(target, includeDir string) error {
    13  		if err := os.Mkdir(includeDir+"/bits", 0o777); err != nil {
    14  			return err
    15  		}
    16  		f, err := os.Create(includeDir + "/bits/alltypes.h")
    17  		if err != nil {
    18  			return err
    19  		}
    20  		if _, err := f.Write([]byte(wasmAllTypes)); err != nil {
    21  			return err
    22  		}
    23  		return f.Close()
    24  	},
    25  	cflags: func(target, headerPath string) []string {
    26  		libcDir := filepath.Join(goenv.Get("TINYGOROOT"), "lib/wasi-libc")
    27  		return []string{
    28  			"-Werror",
    29  			"-Wall",
    30  			"-std=gnu11",
    31  			"-nostdlibinc",
    32  			"-isystem", libcDir + "/libc-top-half/musl/arch/wasm32",
    33  			"-isystem", libcDir + "/libc-top-half/musl/arch/generic",
    34  			"-isystem", libcDir + "/libc-top-half/musl/src/internal",
    35  			"-isystem", libcDir + "/libc-top-half/musl/src/include",
    36  			"-isystem", libcDir + "/libc-top-half/musl/include",
    37  			"-isystem", libcDir + "/libc-bottom-half/headers/public",
    38  			"-I" + headerPath,
    39  		}
    40  	},
    41  	sourceDir: func() string { return filepath.Join(goenv.Get("TINYGOROOT"), "lib/wasi-libc") },
    42  	librarySources: func(target string) ([]string, error) {
    43  		return []string{
    44  			// memory builtins needed for llvm.memcpy.*, llvm.memmove.*, and
    45  			// llvm.memset.* LLVM intrinsics.
    46  			"libc-top-half/musl/src/string/memcpy.c",
    47  			"libc-top-half/musl/src/string/memmove.c",
    48  			"libc-top-half/musl/src/string/memset.c",
    49  
    50  			// exp, exp2, and log are needed for LLVM math builtin functions
    51  			// like llvm.exp.*.
    52  			"libc-top-half/musl/src/math/__math_divzero.c",
    53  			"libc-top-half/musl/src/math/__math_invalid.c",
    54  			"libc-top-half/musl/src/math/__math_oflow.c",
    55  			"libc-top-half/musl/src/math/__math_uflow.c",
    56  			"libc-top-half/musl/src/math/__math_xflow.c",
    57  			"libc-top-half/musl/src/math/exp.c",
    58  			"libc-top-half/musl/src/math/exp_data.c",
    59  			"libc-top-half/musl/src/math/exp2.c",
    60  			"libc-top-half/musl/src/math/log.c",
    61  			"libc-top-half/musl/src/math/log_data.c",
    62  		}, nil
    63  	},
    64  }
    65  
    66  // alltypes.h for wasm-libc, using the types as defined inside Clang.
    67  const wasmAllTypes = `
    68  typedef __SIZE_TYPE__    size_t;
    69  typedef __INT8_TYPE__    int8_t;
    70  typedef __INT16_TYPE__   int16_t;
    71  typedef __INT32_TYPE__   int32_t;
    72  typedef __INT64_TYPE__   int64_t;
    73  typedef __UINT8_TYPE__   uint8_t;
    74  typedef __UINT16_TYPE__  uint16_t;
    75  typedef __UINT32_TYPE__  uint32_t;
    76  typedef __UINT64_TYPE__  uint64_t;
    77  typedef __UINTPTR_TYPE__ uintptr_t;
    78  
    79  // This type is used internally in wasi-libc.
    80  typedef double double_t;
    81  `