main: test other architectures by specifying a different GOARCH

... instead of setting a special -target= value. This is more robust and
makes sure that the test actually tests different arcitectures as they
would be compiled by TinyGo. As an example, the bug of the bugfix in the
previous commit ("arm: use armv7 instead of thumbv7") would have been
caught if this change was applied earlier.

I've decided to put GOOS/GOARCH in compileopts.Options, as it makes
sense to me to treat them the same way as command line parameters.
Этот коммит содержится в:
Ayke van Laethem 2021-09-22 02:37:10 +02:00 коммит произвёл Ron Evans
родитель 36f1517e8d
коммит 0a80da46b1
8 изменённых файлов: 72 добавлений и 47 удалений

Просмотреть файл

@ -13,7 +13,7 @@ import (
// uses the currently active GOPATH (from the goenv package) to determine the Go // uses the currently active GOPATH (from the goenv package) to determine the Go
// version to use. // version to use.
func NewConfig(options *compileopts.Options) (*compileopts.Config, error) { func NewConfig(options *compileopts.Options) (*compileopts.Config, error) {
spec, err := compileopts.LoadTarget(options.Target) spec, err := compileopts.LoadTarget(options)
if err != nil { if err != nil {
return nil, err return nil, err
} }

Просмотреть файл

@ -16,8 +16,11 @@ var (
) )
// Options contains extra options to give to the compiler. These options are // Options contains extra options to give to the compiler. These options are
// usually passed from the command line. // usually passed from the command line, but can also be passed in environment
// variables for example.
type Options struct { type Options struct {
GOOS string // environment variable
GOARCH string // environment variable
Target string Target string
Opt string Opt string
GC string GC string

Просмотреть файл

@ -161,36 +161,34 @@ func (spec *TargetSpec) resolveInherits() error {
} }
// Load a target specification. // Load a target specification.
func LoadTarget(target string) (*TargetSpec, error) { func LoadTarget(options *Options) (*TargetSpec, error) {
if target == "" { if options.Target == "" {
// Configure based on GOOS/GOARCH environment variables (falling back to // Configure based on GOOS/GOARCH environment variables (falling back to
// runtime.GOOS/runtime.GOARCH), and generate a LLVM target based on it. // runtime.GOOS/runtime.GOARCH), and generate a LLVM target based on it.
goos := goenv.Get("GOOS") llvmos := options.GOOS
goarch := goenv.Get("GOARCH")
llvmos := goos
llvmarch := map[string]string{ llvmarch := map[string]string{
"386": "i386", "386": "i386",
"amd64": "x86_64", "amd64": "x86_64",
"arm64": "aarch64", "arm64": "aarch64",
"arm": "armv7", "arm": "armv7",
}[goarch] }[options.GOARCH]
if llvmarch == "" { if llvmarch == "" {
llvmarch = goarch llvmarch = options.GOARCH
} }
// Target triples (which actually have four components, but are called // Target triples (which actually have four components, but are called
// triples for historical reasons) have the form: // triples for historical reasons) have the form:
// arch-vendor-os-environment // arch-vendor-os-environment
target = llvmarch + "-unknown-" + llvmos target := llvmarch + "-unknown-" + llvmos
if goarch == "arm" { if options.GOARCH == "arm" {
target += "-gnueabihf" target += "-gnueabihf"
} }
return defaultTarget(goos, goarch, target) return defaultTarget(options.GOOS, options.GOARCH, target)
} }
// See whether there is a target specification for this target (e.g. // See whether there is a target specification for this target (e.g.
// Arduino). // Arduino).
spec := &TargetSpec{} spec := &TargetSpec{}
err := spec.loadFromGivenStr(target) err := spec.loadFromGivenStr(options.Target)
if err == nil { if err == nil {
// Successfully loaded this target from a built-in .json file. Make sure // Successfully loaded this target from a built-in .json file. Make sure
// it includes all parents as specified in the "inherits" key. // it includes all parents as specified in the "inherits" key.
@ -206,7 +204,7 @@ func LoadTarget(target string) (*TargetSpec, error) {
} else { } else {
// Load target from given triple, ignore GOOS/GOARCH environment // Load target from given triple, ignore GOOS/GOARCH environment
// variables. // variables.
tripleSplit := strings.Split(target, "-") tripleSplit := strings.Split(options.Target, "-")
if len(tripleSplit) < 3 { if len(tripleSplit) < 3 {
return nil, errors.New("expected a full LLVM target or a custom target in -target flag") return nil, errors.New("expected a full LLVM target or a custom target in -target flag")
} }

Просмотреть файл

@ -6,12 +6,12 @@ import (
) )
func TestLoadTarget(t *testing.T) { func TestLoadTarget(t *testing.T) {
_, err := LoadTarget("arduino") _, err := LoadTarget(&Options{Target: "arduino"})
if err != nil { if err != nil {
t.Error("LoadTarget test failed:", err) t.Error("LoadTarget test failed:", err)
} }
_, err = LoadTarget("notexist") _, err = LoadTarget(&Options{Target: "notexist"})
if err == nil { if err == nil {
t.Error("LoadTarget should have failed with non existing target") t.Error("LoadTarget should have failed with non existing target")
} }

Просмотреть файл

@ -73,12 +73,15 @@ func TestCompiler(t *testing.T) {
} }
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
target, err := compileopts.LoadTarget(targetString) options := &compileopts.Options{
Target: targetString,
}
target, err := compileopts.LoadTarget(options)
if err != nil { if err != nil {
t.Fatal("failed to load target:", err) t.Fatal("failed to load target:", err)
} }
config := &compileopts.Config{ config := &compileopts.Config{
Options: &compileopts.Options{}, Options: options,
Target: target, Target: target,
} }
compilerConfig := &Config{ compilerConfig := &Config{

Просмотреть файл

@ -1147,6 +1147,8 @@ func main() {
} }
options := &compileopts.Options{ options := &compileopts.Options{
GOOS: goenv.Get("GOOS"),
GOARCH: goenv.Get("GOARCH"),
Target: *target, Target: *target,
Opt: *opt, Opt: *opt,
GC: *gc, GC: *gc,
@ -1309,7 +1311,7 @@ func main() {
continue continue
} }
path := filepath.Join(dir, entry.Name()) path := filepath.Join(dir, entry.Name())
spec, err := compileopts.LoadTarget(path) spec, err := compileopts.LoadTarget(&compileopts.Options{Target: path})
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "could not list target:", err) fmt.Fprintln(os.Stderr, "could not list target:", err)
os.Exit(1) os.Exit(1)

Просмотреть файл

@ -67,13 +67,13 @@ func TestCompiler(t *testing.T) {
// This makes it possible to run one specific test (instead of all), // This makes it possible to run one specific test (instead of all),
// which is especially useful to quickly check whether some changes // which is especially useful to quickly check whether some changes
// affect a particular target architecture. // affect a particular target architecture.
runPlatTests(*testTarget, tests, t) runPlatTests(optionsFromTarget(*testTarget), tests, t)
return return
} }
if runtime.GOOS != "windows" { if runtime.GOOS != "windows" {
t.Run("Host", func(t *testing.T) { t.Run("Host", func(t *testing.T) {
runPlatTests("", tests, t) runPlatTests(optionsFromTarget(""), tests, t)
}) })
} }
@ -82,32 +82,32 @@ func TestCompiler(t *testing.T) {
} }
t.Run("EmulatedCortexM3", func(t *testing.T) { t.Run("EmulatedCortexM3", func(t *testing.T) {
runPlatTests("cortex-m-qemu", tests, t) runPlatTests(optionsFromTarget("cortex-m-qemu"), tests, t)
}) })
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
// Note: running only on Windows and macOS because Linux (as of 2020) // Note: running only on Windows and macOS because Linux (as of 2020)
// usually has an outdated QEMU version that doesn't support RISC-V yet. // usually has an outdated QEMU version that doesn't support RISC-V yet.
t.Run("EmulatedRISCV", func(t *testing.T) { t.Run("EmulatedRISCV", func(t *testing.T) {
runPlatTests("riscv-qemu", tests, t) runPlatTests(optionsFromTarget("riscv-qemu"), tests, t)
}) })
} }
if runtime.GOOS == "linux" { if runtime.GOOS == "linux" {
t.Run("X86Linux", func(t *testing.T) { t.Run("X86Linux", func(t *testing.T) {
runPlatTests("i386-unknown-linux", tests, t) runPlatTests(optionsFromOSARCH("linux", "386"), tests, t)
}) })
t.Run("ARMLinux", func(t *testing.T) { t.Run("ARMLinux", func(t *testing.T) {
runPlatTests("armv7-unknown-linux-gnueabihf", tests, t) runPlatTests(optionsFromOSARCH("linux", "arm"), tests, t)
}) })
t.Run("ARM64Linux", func(t *testing.T) { t.Run("ARM64Linux", func(t *testing.T) {
runPlatTests("aarch64-unknown-linux", tests, t) runPlatTests(optionsFromOSARCH("linux", "arm64"), tests, t)
}) })
t.Run("WebAssembly", func(t *testing.T) { t.Run("WebAssembly", func(t *testing.T) {
runPlatTests("wasm", tests, t) runPlatTests(optionsFromTarget("wasm"), tests, t)
}) })
t.Run("WASI", func(t *testing.T) { t.Run("WASI", func(t *testing.T) {
runPlatTests("wasi", tests, t) runPlatTests(optionsFromTarget("wasi"), tests, t)
}) })
} }
@ -122,8 +122,10 @@ func TestCompiler(t *testing.T) {
// Test with few optimizations enabled (no inlining, etc). // Test with few optimizations enabled (no inlining, etc).
t.Run("opt=1", func(t *testing.T) { t.Run("opt=1", func(t *testing.T) {
t.Parallel() t.Parallel()
runTestWithConfig("stdlib.go", "", t, compileopts.Options{ runTestWithConfig("stdlib.go", t, compileopts.Options{
Opt: "1", GOOS: goenv.Get("GOOS"),
GOARCH: goenv.Get("GOARCH"),
Opt: "1",
}, nil, nil) }, nil, nil)
}) })
@ -131,14 +133,18 @@ func TestCompiler(t *testing.T) {
// TODO: fix this for stdlib.go, which currently fails. // TODO: fix this for stdlib.go, which currently fails.
t.Run("opt=0", func(t *testing.T) { t.Run("opt=0", func(t *testing.T) {
t.Parallel() t.Parallel()
runTestWithConfig("print.go", "", t, compileopts.Options{ runTestWithConfig("print.go", t, compileopts.Options{
Opt: "0", GOOS: goenv.Get("GOOS"),
GOARCH: goenv.Get("GOARCH"),
Opt: "0",
}, nil, nil) }, nil, nil)
}) })
t.Run("ldflags", func(t *testing.T) { t.Run("ldflags", func(t *testing.T) {
t.Parallel() t.Parallel()
runTestWithConfig("ldflags.go", "", t, compileopts.Options{ runTestWithConfig("ldflags.go", t, compileopts.Options{
GOOS: goenv.Get("GOOS"),
GOARCH: goenv.Get("GOARCH"),
GlobalValues: map[string]map[string]string{ GlobalValues: map[string]map[string]string{
"main": { "main": {
"someGlobal": "foobar", "someGlobal": "foobar",
@ -149,30 +155,30 @@ func TestCompiler(t *testing.T) {
}) })
} }
func runPlatTests(target string, tests []string, t *testing.T) { func runPlatTests(options compileopts.Options, tests []string, t *testing.T) {
t.Parallel() t.Parallel()
for _, name := range tests { for _, name := range tests {
name := name // redefine to avoid race condition name := name // redefine to avoid race condition
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() t.Parallel()
runTest(name, target, t, nil, nil) runTest(name, options, t, nil, nil)
}) })
} }
t.Run("env.go", func(t *testing.T) { t.Run("env.go", func(t *testing.T) {
t.Parallel() t.Parallel()
runTest("env.go", target, t, []string{"first", "second"}, []string{"ENV1=VALUE1", "ENV2=VALUE2"}) runTest("env.go", options, t, []string{"first", "second"}, []string{"ENV1=VALUE1", "ENV2=VALUE2"})
}) })
if target == "" || target == "wasi" { if options.Target == "" || options.Target == "wasi" {
t.Run("filesystem.go", func(t *testing.T) { t.Run("filesystem.go", func(t *testing.T) {
t.Parallel() t.Parallel()
runTest("filesystem.go", target, t, nil, nil) runTest("filesystem.go", options, t, nil, nil)
}) })
} }
if target == "" || target == "wasi" || target == "wasm" { if options.Target == "" || options.Target == "wasi" || options.Target == "wasm" {
t.Run("rand.go", func(t *testing.T) { t.Run("rand.go", func(t *testing.T) {
t.Parallel() t.Parallel()
runTest("rand.go", target, t, nil, nil) runTest("rand.go", options, t, nil, nil)
}) })
} }
} }
@ -189,14 +195,27 @@ func runBuild(src, out string, opts *compileopts.Options) error {
return Build(src, out, opts) return Build(src, out, opts)
} }
func runTest(name, target string, t *testing.T, cmdArgs, environmentVars []string) { func optionsFromTarget(target string) compileopts.Options {
options := compileopts.Options{ return compileopts.Options{
// GOOS/GOARCH are only used if target == ""
GOOS: goenv.Get("GOOS"),
GOARCH: goenv.Get("GOARCH"),
Target: target, Target: target,
} }
runTestWithConfig(name, target, t, options, cmdArgs, environmentVars)
} }
func runTestWithConfig(name, target string, t *testing.T, options compileopts.Options, cmdArgs, environmentVars []string) { func optionsFromOSARCH(goos, goarch string) compileopts.Options {
return compileopts.Options{
GOOS: goos,
GOARCH: goarch,
}
}
func runTest(name string, options compileopts.Options, t *testing.T, cmdArgs, environmentVars []string) {
runTestWithConfig(name, t, options, cmdArgs, environmentVars)
}
func runTestWithConfig(name string, t *testing.T, options compileopts.Options, cmdArgs, environmentVars []string) {
// Set default config. // Set default config.
options.Debug = true options.Debug = true
options.VerifyIR = true options.VerifyIR = true
@ -227,7 +246,7 @@ func runTestWithConfig(name, target string, t *testing.T, options compileopts.Op
// we need to pass command line arguments and environment variables through // we need to pass command line arguments and environment variables through
// global variables (built into the binary directly) instead of the // global variables (built into the binary directly) instead of the
// conventional way. // conventional way.
spec, err := compileopts.LoadTarget(target) spec, err := compileopts.LoadTarget(&options)
if err != nil { if err != nil {
t.Fatal("failed to load target spec:", err) t.Fatal("failed to load target spec:", err)
} }
@ -314,7 +333,7 @@ func runTestWithConfig(name, target string, t *testing.T, options compileopts.Op
} }
}() }()
err = cmd.Wait() err = cmd.Wait()
if _, ok := err.(*exec.ExitError); ok && target != "" { if _, ok := err.(*exec.ExitError); ok && options.Target != "" {
err = nil // workaround for QEMU err = nil // workaround for QEMU
} }
close(runComplete) close(runComplete)

Просмотреть файл

@ -140,7 +140,7 @@ func filterIrrelevantIRLines(lines []string) []string {
// run. // run.
// If there are any errors, they are reported via the *testing.T instance. // If there are any errors, they are reported via the *testing.T instance.
func compileGoFileForTesting(t *testing.T, filename string) llvm.Module { func compileGoFileForTesting(t *testing.T, filename string) llvm.Module {
target, err := compileopts.LoadTarget("i686--linux") target, err := compileopts.LoadTarget(&compileopts.Options{GOOS: "linux", GOARCH: "386"})
if err != nil { if err != nil {
t.Fatal("failed to load target:", err) t.Fatal("failed to load target:", err)
} }