Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasip1: deduplicate target selection #4437

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions compileopts/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,14 @@ func (spec *TargetSpec) resolveInherits() error {

// Load a target specification.
func LoadTarget(options *Options) (*TargetSpec, error) {
if options.Target == "" {
return defaultTarget(options)
switch options.Target {
case "":
// No target given, use GOOS/GOARCH env variables.
return defaultTarget(options, options.GOOS, options.GOARCH)
case "wasip1", "wasi":
// Special case: support both -target=wasip1 and the GOOS/GOARCH pair.
// They should both have the same effect.
return defaultTarget(options, "wasip1", "wasm")
}

// See whether there is a target specification for this target (e.g.
Expand Down Expand Up @@ -240,11 +246,11 @@ func GetTargetSpecs() (map[string]*TargetSpec, error) {

// Load a target from environment variables (which default to
// runtime.GOOS/runtime.GOARCH).
func defaultTarget(options *Options) (*TargetSpec, error) {
func defaultTarget(options *Options, goos, goarch string) (*TargetSpec, error) {
spec := TargetSpec{
GOOS: options.GOOS,
GOARCH: options.GOARCH,
BuildTags: []string{options.GOOS, options.GOARCH},
GOOS: goos,
GOARCH: goarch,
BuildTags: []string{goos, goarch},
GC: "precise",
Scheduler: "tasks",
Linker: "cc",
Expand All @@ -255,7 +261,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {

// Configure target based on GOARCH.
var llvmarch string
switch options.GOARCH {
switch goarch {
case "386":
llvmarch = "i386"
spec.CPU = "pentium4"
Expand Down Expand Up @@ -325,20 +331,20 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
case "arm64":
spec.CPU = "generic"
llvmarch = "aarch64"
if options.GOOS == "darwin" {
if goos == "darwin" {
spec.Features = "+fp-armv8,+neon"
// Looks like Apple prefers to call this architecture ARM64
// instead of AArch64.
llvmarch = "arm64"
} else if options.GOOS == "windows" {
} else if goos == "windows" {
spec.Features = "+fp-armv8,+neon,-fmv"
} else { // linux
spec.Features = "+fp-armv8,+neon,-fmv,-outline-atomics"
}
case "mips", "mipsle":
spec.CPU = "mips32r2"
spec.CFlags = append(spec.CFlags, "-fno-pic")
if options.GOARCH == "mips" {
if goarch == "mips" {
llvmarch = "mips" // big endian
} else {
llvmarch = "mipsel" // little endian
Expand All @@ -364,16 +370,16 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
"-msign-ext",
)
default:
return nil, fmt.Errorf("unknown GOARCH=%s", options.GOARCH)
return nil, fmt.Errorf("unknown GOARCH=%s", goarch)
}

// Configure target based on GOOS.
llvmos := options.GOOS
llvmos := goos
llvmvendor := "unknown"
switch options.GOOS {
switch goos {
case "darwin":
platformVersion := "10.12.0"
if options.GOARCH == "arm64" {
if goarch == "arm64" {
platformVersion = "11.0.0" // first macosx platform with arm64 support
}
llvmvendor = "apple"
Expand All @@ -395,7 +401,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
spec.RTLib = "compiler-rt"
spec.Libc = "musl"
spec.LDFlags = append(spec.LDFlags, "--gc-sections")
if options.GOARCH == "arm64" {
if goarch == "arm64" {
// Disable outline atomics. For details, see:
// https://cpufun.substack.com/p/atomics-in-aarch64
// A better way would be to fully support outline atomics, which
Expand All @@ -420,7 +426,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
// normally present in Go (without explicitly opting in).
// For more discussion:
// https://groups.google.com/g/Golang-nuts/c/Jd9tlNc6jUE/m/Zo-7zIP_m3MJ?pli=1
switch options.GOARCH {
switch goarch {
case "amd64":
spec.LDFlags = append(spec.LDFlags,
"-m", "i386pep",
Expand Down Expand Up @@ -455,16 +461,16 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
)
llvmos = "wasi"
default:
return nil, fmt.Errorf("unknown GOOS=%s", options.GOOS)
return nil, fmt.Errorf("unknown GOOS=%s", goos)
}

// Target triples (which actually have four components, but are called
// triples for historical reasons) have the form:
// arch-vendor-os-environment
spec.Triple = llvmarch + "-" + llvmvendor + "-" + llvmos
if options.GOOS == "windows" {
if goos == "windows" {
spec.Triple += "-gnu"
} else if options.GOOS == "linux" {
} else if goos == "linux" {
// We use musl on Linux (not glibc) so we should use -musleabi* instead
// of -gnueabi*.
// The *hf suffix selects between soft/hard floating point ABI.
Expand All @@ -476,27 +482,27 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
}

// Add extra assembly files (needed for the scheduler etc).
if options.GOARCH != "wasm" {
if goarch != "wasm" {
suffix := ""
if options.GOOS == "windows" && options.GOARCH == "amd64" {
if goos == "windows" && goarch == "amd64" {
// Windows uses a different calling convention on amd64 from other
// operating systems so we need separate assembly files.
suffix = "_windows"
}
asmGoarch := options.GOARCH
if options.GOARCH == "mips" || options.GOARCH == "mipsle" {
asmGoarch := goarch
if goarch == "mips" || goarch == "mipsle" {
asmGoarch = "mipsx"
}
spec.ExtraFiles = append(spec.ExtraFiles, "src/runtime/asm_"+asmGoarch+suffix+".S")
spec.ExtraFiles = append(spec.ExtraFiles, "src/internal/task/task_stack_"+asmGoarch+suffix+".S")
}

// Configure the emulator.
if options.GOARCH != runtime.GOARCH {
if goarch != runtime.GOARCH {
// Some educated guesses as to how to invoke helper programs.
spec.GDB = []string{"gdb-multiarch"}
if options.GOOS == "linux" {
switch options.GOARCH {
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" {
Expand All @@ -515,8 +521,8 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
}
}
}
if options.GOOS != runtime.GOOS {
if options.GOOS == "windows" {
if goos != runtime.GOOS {
if goos == "windows" {
spec.Emulator = "wine {}"
}
}
Expand Down
3 changes: 0 additions & 3 deletions targets/wasi.json

This file was deleted.

26 changes: 0 additions & 26 deletions targets/wasip1.json

This file was deleted.

Loading