From 010cc13e9e5861bd9292a66f22be867f6629f943 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sun, 6 Feb 2022 23:26:25 -0500 Subject: [PATCH] Fix cross-Linux setup on non-amd64 arches In that case, an emulator is needed for amd64, and tests should be run for amd64 as a _cross_ test. --- compileopts/target.go | 19 ++++++++++++++----- main_test.go | 39 +++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/compileopts/target.go b/compileopts/target.go index ffdf7365..9e341537 100644 --- a/compileopts/target.go +++ b/compileopts/target.go @@ -317,11 +317,20 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) { if goarch != runtime.GOARCH { // Some educated guesses as to how to invoke helper programs. spec.GDB = []string{"gdb-multiarch"} - if goarch == "arm" && goos == "linux" { - spec.Emulator = []string{"qemu-arm"} - } - if goarch == "arm64" && goos == "linux" { - spec.Emulator = []string{"qemu-aarch64"} + if goos == "linux" { + switch goarch { + case "386": + // amd64 can _usually_ run 32-bit programs, so skip the emulator in that case. + if runtime.GOARCH != "amd64" { + spec.Emulator = []string{"qemu-i386"} + } + case "amd64": + spec.Emulator = []string{"qemu-x86_64"} + case "arm": + spec.Emulator = []string{"qemu-arm"} + case "arm64": + spec.Emulator = []string{"qemu-aarch64"} + } } } if goos != runtime.GOOS { diff --git a/main_test.go b/main_test.go index cf3fc989..82c0b611 100644 --- a/main_test.go +++ b/main_test.go @@ -31,6 +31,13 @@ const TESTDATA = "testdata" var testTarget = flag.String("target", "", "override test target") +var supportedLinuxArches = map[string]string{ + "AMD64Linux": "linux/amd64", + "X86Linux": "linux/386", + "ARMLinux": "linux/arm/6", + "ARM64Linux": "linux/arm64", +} + var sema = make(chan struct{}, runtime.NumCPU()) func TestBuild(t *testing.T) { @@ -180,18 +187,14 @@ func TestBuild(t *testing.T) { }) if runtime.GOOS == "linux" { - t.Run("X86Linux", func(t *testing.T) { - t.Parallel() - runPlatTests(optionsFromOSARCH("linux/386", sema), tests, t) - }) - t.Run("ARMLinux", func(t *testing.T) { - t.Parallel() - runPlatTests(optionsFromOSARCH("linux/arm/6", sema), tests, t) - }) - t.Run("ARM64Linux", func(t *testing.T) { - t.Parallel() - runPlatTests(optionsFromOSARCH("linux/arm64", sema), tests, t) - }) + for name, osArch := range supportedLinuxArches { + options := optionsFromOSARCH(osArch, sema) + if options.GOARCH != runtime.GOARCH { // Native architecture already run above. + t.Run(name, func(t *testing.T) { + runPlatTests(options, tests, t) + }) + } + } t.Run("WebAssembly", func(t *testing.T) { t.Parallel() runPlatTests(optionsFromTarget("wasm", sema), tests, t) @@ -475,12 +478,12 @@ func TestTest(t *testing.T) { } if !testing.Short() { if runtime.GOOS == "linux" { - targs = append(targs, - // Linux - targ{"X86Linux", optionsFromOSARCH("linux/386", sema)}, - targ{"ARMLinux", optionsFromOSARCH("linux/arm/6", sema)}, - targ{"ARM64Linux", optionsFromOSARCH("linux/arm64", sema)}, - ) + for name, osArch := range supportedLinuxArches { + options := optionsFromOSARCH(osArch, sema) + if options.GOARCH != runtime.GOARCH { // Native architecture already run above. + targs = append(targs, targ{name, options}) + } + } } targs = append(targs,