From 8f8232aada70f3f939f1d18cedad0d6cc3b4db31 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Tue, 14 Jan 2020 13:46:54 +0100 Subject: compileopts: fix CGo when cross compiling Use the cross compiling toolchains for compiling/linking. This fixes CGo support, and therefore allows CGo to be used when cross compiling to Linux on a different architecture. This commit also removes some redundant testing code. --- .circleci/config.yml | 2 -- compileopts/target.go | 14 +++++++++++++- main_test.go | 9 +-------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index dcd33dd5c..8b3915d07 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,9 +23,7 @@ commands: libclang<>-dev \ lld<> \ gcc-arm-linux-gnueabihf \ - libc6-dev-armel-cross \ gcc-aarch64-linux-gnu \ - libc6-dev-arm64-cross \ qemu-system-arm \ qemu-user \ gcc-avr \ diff --git a/compileopts/target.go b/compileopts/target.go index 949f00e47..637712c83 100644 --- a/compileopts/target.go +++ b/compileopts/target.go @@ -229,6 +229,14 @@ func LoadTarget(target string) (*TargetSpec, error) { if len(tripleSplit) < 3 { return nil, errors.New("expected a full LLVM target or a custom target in -target flag") } + if tripleSplit[0] == "arm" { + // LLVM and Clang have a different idea of what "arm" means, so + // upgrade to a slightly more modern ARM. In fact, when you pass + // --target=arm--linux-gnueabihf to Clang, it will convert that + // internally to armv7-unknown-linux-gnueabihf. Changing the + // architecture to armv7 will keep things consistent. + tripleSplit[0] = "armv7" + } goos := tripleSplit[2] if strings.HasPrefix(goos, "darwin") { goos = "darwin" @@ -237,11 +245,12 @@ func LoadTarget(target string) (*TargetSpec, error) { "i386": "386", "x86_64": "amd64", "aarch64": "arm64", + "armv7": "arm", }[tripleSplit[0]] if goarch == "" { goarch = tripleSplit[0] } - return defaultTarget(goos, goarch, target) + return defaultTarget(goos, goarch, strings.Join(tripleSplit, "-")) } } @@ -255,6 +264,7 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) { BuildTags: []string{goos, goarch}, Compiler: "clang", Linker: "cc", + CFlags: []string{"--target=" + triple}, GDB: "gdb", PortReset: "false", FlashMethod: "native", @@ -267,11 +277,13 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) { if goarch != runtime.GOARCH { // Some educated guesses as to how to invoke helper programs. if goarch == "arm" && goos == "linux" { + spec.CFlags = append(spec.CFlags, "--sysroot=/usr/arm-linux-gnueabihf") spec.Linker = "arm-linux-gnueabihf-gcc" spec.GDB = "arm-linux-gnueabihf-gdb" spec.Emulator = []string{"qemu-arm", "-L", "/usr/arm-linux-gnueabihf"} } if goarch == "arm64" && goos == "linux" { + spec.CFlags = append(spec.CFlags, "--sysroot=/usr/aarch64-linux-gnu") spec.Linker = "aarch64-linux-gnu-gcc" spec.GDB = "aarch64-linux-gnu-gdb" spec.Emulator = []string{"qemu-aarch64", "-L", "/usr/aarch64-linux-gnu"} diff --git a/main_test.go b/main_test.go index 188f318e8..3287025fc 100644 --- a/main_test.go +++ b/main_test.go @@ -80,15 +80,8 @@ func runPlatTests(target string, matches []string, t *testing.T) { if path == filepath.Join("testdata", "gc.go") { continue } - case target == "": - // run all tests on host - case target == "cortex-m-qemu": - // all tests are supported default: - // cross-compilation of cgo is not yet supported - if path == filepath.Join("testdata", "cgo")+string(filepath.Separator) { - continue - } + // all tests are supported } t.Run(filepath.Base(path), func(t *testing.T) { -- cgit v1.2.3