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

     1  package builder
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  
     7  	"github.com/tinygo-org/tinygo/goenv"
     8  )
     9  
    10  // runCCompiler invokes a C compiler with the given arguments.
    11  func runCCompiler(flags ...string) error {
    12  	if hasBuiltinTools {
    13  		// Compile this with the internal Clang compiler.
    14  		cmd := exec.Command(os.Args[0], append([]string{"clang"}, flags...)...)
    15  		cmd.Stdout = os.Stdout
    16  		cmd.Stderr = os.Stderr
    17  		return cmd.Run()
    18  	}
    19  
    20  	// Compile this with an external invocation of the Clang compiler.
    21  	return execCommand("clang", flags...)
    22  }
    23  
    24  // link invokes a linker with the given name and flags.
    25  func link(linker string, flags ...string) error {
    26  	if hasBuiltinTools && (linker == "ld.lld" || linker == "wasm-ld") {
    27  		// Run command with internal linker.
    28  		cmd := exec.Command(os.Args[0], append([]string{linker}, flags...)...)
    29  		cmd.Stdout = os.Stdout
    30  		cmd.Stderr = os.Stderr
    31  		return cmd.Run()
    32  	}
    33  
    34  	// Fall back to external command.
    35  	if _, ok := commands[linker]; ok {
    36  		return execCommand(linker, flags...)
    37  	}
    38  
    39  	cmd := exec.Command(linker, flags...)
    40  	cmd.Stdout = os.Stdout
    41  	cmd.Stderr = os.Stderr
    42  	cmd.Dir = goenv.Get("TINYGOROOT")
    43  	return cmd.Run()
    44  }