From f7062199969e2d4760f8a199ecf1639ff286581c Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Wed, 14 Apr 2021 22:12:25 +0200 Subject: [PATCH] builder: hard code Clang compiler At the moment, all targets use the Clang compiler to compile C and assembly files. There is no good reason to make this configurable anymore and in fact it will make future changes more complicated (and thus more likely to have bugs). Therefore, I've removed support for setting the compiler. Note that the same is not true for the linker. While it makes sense to standardize on the Clang compiler (because if Clang doesn't support a target, TinyGo is unlikely to support it either), linkers will remain configurable for the foreseeable future. One example is Xtensa, which is supported by the Xtensa LLVM fork but doesn't have support in ld.lld yet. I've also fixed a bug in compileAndCacheCFile: it wasn't using the right CFlags for caching purposes. This could lead to using stale caches. This commit fixes that too. --- builder/build.go | 4 ++-- builder/cc.go | 13 +++++-------- builder/library.go | 2 +- builder/tools.go | 17 ++++------------- compileopts/target.go | 2 -- targets/avr.json | 1 - targets/cortex-m.json | 1 - targets/gameboy-advance.json | 1 - targets/nintendoswitch.json | 1 - targets/riscv.json | 1 - targets/wasi.json | 1 - targets/wasm.json | 1 - targets/xtensa.json | 1 - 13 files changed, 12 insertions(+), 34 deletions(-) diff --git a/builder/build.go b/builder/build.go index f16916ceb..cd292e0bc 100644 --- a/builder/build.go +++ b/builder/build.go @@ -494,7 +494,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil job := &compileJob{ description: "compile extra file " + path, run: func(job *compileJob) error { - result, err := compileAndCacheCFile(abspath, dir, config.CFlags(), config) + result, err := compileAndCacheCFile(abspath, dir, config.CFlags(), config.Options.PrintCommands) job.result = result return err }, @@ -513,7 +513,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil job := &compileJob{ description: "compile CGo file " + abspath, run: func(job *compileJob) error { - result, err := compileAndCacheCFile(abspath, dir, pkg.CFlags, config) + result, err := compileAndCacheCFile(abspath, dir, pkg.CFlags, config.Options.PrintCommands) job.result = result return err }, diff --git a/builder/cc.go b/builder/cc.go index f9bdbaf19..a450cc6e8 100644 --- a/builder/cc.go +++ b/builder/cc.go @@ -17,7 +17,6 @@ import ( "strings" "unicode" - "github.com/tinygo-org/tinygo/compileopts" "github.com/tinygo-org/tinygo/goenv" "tinygo.org/x/go-llvm" ) @@ -57,7 +56,7 @@ import ( // depfile but without invalidating its name. For this reason, the depfile is // written on each new compilation (even when it seems unnecessary). However, it // could in rare cases lead to a stale file fetched from the cache. -func compileAndCacheCFile(abspath, tmpdir string, cflags []string, config *compileopts.Config) (string, error) { +func compileAndCacheCFile(abspath, tmpdir string, cflags []string, printCommands bool) (string, error) { // Hash input file. fileHash, err := hashFile(abspath) if err != nil { @@ -68,14 +67,12 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, config *compi buf, err := json.Marshal(struct { Path string Hash string - Compiler string Flags []string LLVMVersion string }{ Path: abspath, Hash: fileHash, - Compiler: config.Target.Compiler, - Flags: config.CFlags(), + Flags: cflags, LLVMVersion: llvm.Version, }) if err != nil { @@ -131,10 +128,10 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, config *compi // flags (for the assembler) is a compiler error. flags = append(flags, "-Qunused-arguments") } - if config.Options.PrintCommands { - fmt.Printf("%s %s\n", config.Target.Compiler, strings.Join(flags, " ")) + if printCommands { + fmt.Printf("clang %s\n", strings.Join(flags, " ")) } - err = runCCompiler(config.Target.Compiler, flags...) + err = runCCompiler(flags...) if err != nil { return "", &commandError{"failed to build", abspath, err} } diff --git a/builder/library.go b/builder/library.go index 0ce4a6fe0..707eef1ac 100644 --- a/builder/library.go +++ b/builder/library.go @@ -136,7 +136,7 @@ func (l *Library) load(target, cpu, tmpdir string) (job *compileJob, err error) var compileArgs []string compileArgs = append(compileArgs, args...) compileArgs = append(compileArgs, "-o", objpath, srcpath) - err := runCCompiler("clang", compileArgs...) + err := runCCompiler(compileArgs...) if err != nil { return &commandError{"failed to build", srcpath, err} } diff --git a/builder/tools.go b/builder/tools.go index bc0f97824..0b6cd37f9 100644 --- a/builder/tools.go +++ b/builder/tools.go @@ -9,8 +9,8 @@ import ( ) // runCCompiler invokes a C compiler with the given arguments. -func runCCompiler(command string, flags ...string) error { - if hasBuiltinTools && command == "clang" { +func runCCompiler(flags ...string) error { + if hasBuiltinTools { // Compile this with the internal Clang compiler. headerPath := getClangHeaderPath(goenv.Get("TINYGOROOT")) if headerPath == "" { @@ -23,17 +23,8 @@ func runCCompiler(command string, flags ...string) error { return cmd.Run() } - // Running some other compiler. Maybe it has been defined in the - // commands map (unlikely). - if cmdNames, ok := commands[command]; ok { - return execCommand(cmdNames, flags...) - } - - // Alternatively, run the compiler directly. - cmd := exec.Command(command, flags...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return cmd.Run() + // Compile this with an external invocation of the Clang compiler. + return execCommand(commands["clang"], flags...) } // link invokes a linker with the given name and flags. diff --git a/compileopts/target.go b/compileopts/target.go index fc0b70184..0b44c127f 100644 --- a/compileopts/target.go +++ b/compileopts/target.go @@ -31,7 +31,6 @@ type TargetSpec struct { BuildTags []string `json:"build-tags"` GC string `json:"gc"` Scheduler string `json:"scheduler"` - Compiler string `json:"compiler"` Linker string `json:"linker"` RTLib string `json:"rtlib"` // compiler runtime library (libgcc, compiler-rt) Libc string `json:"libc"` @@ -244,7 +243,6 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) { GOOS: goos, GOARCH: goarch, BuildTags: []string{goos, goarch}, - Compiler: "clang", Linker: "cc", CFlags: []string{"--target=" + triple}, GDB: []string{"gdb"}, diff --git a/targets/avr.json b/targets/avr.json index ae1385ce5..2ec83a7aa 100644 --- a/targets/avr.json +++ b/targets/avr.json @@ -3,7 +3,6 @@ "build-tags": ["avr", "baremetal", "linux", "arm"], "goos": "linux", "goarch": "arm", - "compiler": "clang", "gc": "conservative", "linker": "avr-gcc", "scheduler": "none", diff --git a/targets/cortex-m.json b/targets/cortex-m.json index bdff605fb..eed685c78 100644 --- a/targets/cortex-m.json +++ b/targets/cortex-m.json @@ -2,7 +2,6 @@ "build-tags": ["cortexm", "baremetal", "linux", "arm"], "goos": "linux", "goarch": "arm", - "compiler": "clang", "gc": "conservative", "scheduler": "tasks", "linker": "ld.lld", diff --git a/targets/gameboy-advance.json b/targets/gameboy-advance.json index 1e1eb1e02..2eb8aaa83 100644 --- a/targets/gameboy-advance.json +++ b/targets/gameboy-advance.json @@ -4,7 +4,6 @@ "build-tags": ["gameboyadvance", "arm7tdmi", "baremetal", "linux", "arm"], "goos": "linux", "goarch": "arm", - "compiler": "clang", "linker": "ld.lld", "rtlib": "compiler-rt", "libc": "picolibc", diff --git a/targets/nintendoswitch.json b/targets/nintendoswitch.json index 847859ea7..87d6696e7 100644 --- a/targets/nintendoswitch.json +++ b/targets/nintendoswitch.json @@ -3,7 +3,6 @@ "build-tags": ["nintendoswitch", "arm64"], "goos": "linux", "goarch": "arm64", - "compiler": "clang", "linker": "ld.lld", "rtlib": "compiler-rt", "libc": "picolibc", diff --git a/targets/riscv.json b/targets/riscv.json index c2313e57d..1ccf123a4 100644 --- a/targets/riscv.json +++ b/targets/riscv.json @@ -3,7 +3,6 @@ "goarch": "arm", "build-tags": ["tinygo.riscv", "baremetal", "linux", "arm"], "gc": "conservative", - "compiler": "clang", "linker": "ld.lld", "rtlib": "compiler-rt", "libc": "picolibc", diff --git a/targets/wasi.json b/targets/wasi.json index 45ce68d8b..c24ed8f10 100644 --- a/targets/wasi.json +++ b/targets/wasi.json @@ -3,7 +3,6 @@ "build-tags": ["wasm", "wasi"], "goos": "linux", "goarch": "arm", - "compiler": "clang", "linker": "wasm-ld", "libc": "wasi-libc", "cflags": [ diff --git a/targets/wasm.json b/targets/wasm.json index ea78e58de..6208eb27f 100644 --- a/targets/wasm.json +++ b/targets/wasm.json @@ -3,7 +3,6 @@ "build-tags": ["js", "wasm"], "goos": "js", "goarch": "wasm", - "compiler": "clang", "linker": "wasm-ld", "libc": "wasi-libc", "cflags": [ diff --git a/targets/xtensa.json b/targets/xtensa.json index f4b57b0eb..6ebfc4e98 100644 --- a/targets/xtensa.json +++ b/targets/xtensa.json @@ -5,7 +5,6 @@ "build-tags": ["xtensa", "baremetal", "linux", "arm"], "gc": "conservative", "scheduler": "none", - "compiler": "clang", "cflags": [ "--target=xtensa", "-Oz",