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

     1  //go:build byollvm
     2  
     3  package builder
     4  
     5  import (
     6  	"errors"
     7  	"unsafe"
     8  )
     9  
    10  /*
    11  #cgo CXXFLAGS: -fno-rtti
    12  #include <stdbool.h>
    13  #include <stdlib.h>
    14  bool tinygo_clang_driver(int argc, char **argv);
    15  bool tinygo_link(int argc, char **argv);
    16  */
    17  import "C"
    18  
    19  const hasBuiltinTools = true
    20  
    21  // RunTool runs the given tool (such as clang).
    22  //
    23  // This version actually runs the tools because TinyGo was compiled while
    24  // linking statically with LLVM (with the byollvm build tag).
    25  func RunTool(tool string, args ...string) error {
    26  	args = append([]string{tool}, args...)
    27  
    28  	var cflag *C.char
    29  	buf := C.calloc(C.size_t(len(args)), C.size_t(unsafe.Sizeof(cflag)))
    30  	defer C.free(buf)
    31  	cflags := (*[1 << 10]*C.char)(unsafe.Pointer(buf))[:len(args):len(args)]
    32  	for i, flag := range args {
    33  		cflag := C.CString(flag)
    34  		cflags[i] = cflag
    35  		defer C.free(unsafe.Pointer(cflag))
    36  	}
    37  
    38  	var ok C.bool
    39  	switch tool {
    40  	case "clang":
    41  		ok = C.tinygo_clang_driver(C.int(len(args)), (**C.char)(buf))
    42  	case "ld.lld", "wasm-ld":
    43  		ok = C.tinygo_link(C.int(len(args)), (**C.char)(buf))
    44  	default:
    45  		return errors.New("unknown tool: " + tool)
    46  	}
    47  	if !ok {
    48  		return errors.New("failed to run tool: " + tool)
    49  	}
    50  	return nil
    51  }