Skip to content
Merged
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
53 changes: 53 additions & 0 deletions stdlib_arm64_test_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//go:build !llgo
// +build !llgo

package plan9asm

import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)

const arm64LinuxGNUTriple = "aarch64-unknown-linux-gnu"

func testGOROOT(t *testing.T) string {
t.Helper()
if goroot := os.Getenv("GOROOT"); goroot != "" {
return goroot
}
goroot, err := testGoEnv("GOROOT")
if err != nil || goroot == "" {
t.Skip("GOROOT not available")
}
return goroot
}

func testGoEnv(key string) (string, error) {
out, err := exec.Command("go", "env", key).CombinedOutput()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}

func compileLLVMToObject(t *testing.T, llc, triple, llName, objName, ll string) {
t.Helper()
tmp := t.TempDir()
llPath := filepath.Join(tmp, llName)
objPath := filepath.Join(tmp, objName)
if err := os.WriteFile(llPath, []byte(ll), 0644); err != nil {
t.Fatal(err)
}
cmd := exec.Command(llc, "-mtriple="+triple, "-filetype=obj", llPath, "-o", objPath)
out, err := cmd.CombinedOutput()
if err != nil {
s := string(out)
if llcUnsupportedTarget(s) {
t.Skipf("llc does not support triple %q: %s", triple, strings.TrimSpace(s))
}
t.Fatalf("llc failed: %v\n%s", err, s)
}
}
25 changes: 7 additions & 18 deletions stdlib_cpu_arm64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ package plan9asm

import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)

func TestStdlibInternalCPU_ARM64_Compile(t *testing.T) {
if runtime.GOARCH != "arm64" {
t.Skip("host is not arm64")
}
llc, _, ok := findLlcAndClang(t)
if !ok {
t.Skip("llc not found")
}

goroot := runtime.GOROOT()
goroot := testGOROOT(t)
src, err := os.ReadFile(filepath.Join(goroot, "src", "internal", "cpu", "cpu_arm64.s"))
if err != nil {
if os.IsNotExist(err) {
t.Skip("internal/cpu/cpu_arm64.s not present in this GOROOT")
}
t.Fatal(err)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both TestStdlibInternalRuntimeSys_ARM64_Compile and TestTranslateGoModule_StdlibInternalRuntimeSys_ARM64_Compile (added in this same PR) guard os.ReadFile errors with an os.IsNotExist check and skip gracefully when the file is absent from the GOROOT. This test does not — a stripped or non-standard GOROOT that lacks cpu_arm64.s will hard-fail here rather than skip.

Suggested change
t.Fatal(err)
src, err := os.ReadFile(filepath.Join(goroot, "src", "internal", "cpu", "cpu_arm64.s"))
if err != nil {
if os.IsNotExist(err) {
t.Skip("internal/cpu/cpu_arm64.s not present in this GOROOT")
}
t.Fatal(err)
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in a7e4d58. TestStdlibInternalCPU_ARM64_Compile now mirrors the runtime/sys tests and skips cleanly on os.IsNotExist(err) for internal/cpu/cpu_arm64.s.

}

Expand Down Expand Up @@ -55,23 +53,14 @@ func TestStdlibInternalCPU_ARM64_Compile(t *testing.T) {
},
}
ll, err := Translate(file, Options{
TargetTriple: testTargetTriple(runtime.GOOS, runtime.GOARCH),
TargetTriple: arm64LinuxGNUTriple,
ResolveSym: resolve,
Sigs: sigs,
Goarch: "arm64",
})
if err != nil {
t.Fatal(err)
}

tmp := t.TempDir()
llPath := filepath.Join(tmp, "cpu.ll")
objPath := filepath.Join(tmp, "cpu.o")
if err := os.WriteFile(llPath, []byte(ll), 0644); err != nil {
t.Fatal(err)
}
cmd := exec.Command(llc, "-filetype=obj", llPath, "-o", objPath)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("llc failed: %v\n%s", err, string(out))
}
compileLLVMToObject(t, llc, arm64LinuxGNUTriple, "cpu.ll", "cpu.o", ll)
}
50 changes: 35 additions & 15 deletions stdlib_runtime_sys_arm64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@ package plan9asm

import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)

func TestStdlibInternalRuntimeSys_ARM64_Compile(t *testing.T) {
if runtime.GOARCH != "arm64" {
t.Skip("host is not arm64")
}
llc, _, ok := findLlcAndClang(t)
if !ok {
t.Skip("llc not found")
}

goroot := runtime.GOROOT()
goroot := testGOROOT(t)
src, err := os.ReadFile(filepath.Join(goroot, "src", "internal", "runtime", "sys", "dit_arm64.s"))
if err != nil {
if os.IsNotExist(err) {
Expand Down Expand Up @@ -61,24 +56,49 @@ func TestStdlibInternalRuntimeSys_ARM64_Compile(t *testing.T) {
},
}
ll, err := Translate(file, Options{
TargetTriple: testTargetTriple(runtime.GOOS, runtime.GOARCH),
TargetTriple: arm64LinuxGNUTriple,
ResolveSym: resolve,
Sigs: sigs,
Goarch: runtime.GOARCH,
Goarch: "arm64",
})
if err != nil {
t.Fatal(err)
}

tmp := t.TempDir()
llPath := filepath.Join(tmp, "dit.ll")
objPath := filepath.Join(tmp, "dit.o")
if err := os.WriteFile(llPath, []byte(ll), 0644); err != nil {
compileLLVMToObject(t, llc, arm64LinuxGNUTriple, "dit.ll", "dit.o", ll)
}

func TestTranslateGoModule_StdlibInternalRuntimeSys_ARM64_Compile(t *testing.T) {
llc, _, ok := findLlcAndClang(t)
if !ok {
t.Skip("llc not found")
}

goroot := testGOROOT(t)
src, err := os.ReadFile(filepath.Join(goroot, "src", "internal", "runtime", "sys", "dit_arm64.s"))
if err != nil {
if os.IsNotExist(err) {
t.Skip("internal/runtime/sys/dit_arm64.s not present in this GOROOT")
}
t.Fatal(err)
}
cmd := exec.Command(llc, "-filetype=obj", llPath, "-o", objPath)
out, err := cmd.CombinedOutput()
pkg := mustGoPackage(t, "internal/runtime/sys", `package sys
func EnableDIT() bool
func DITEnabled() bool
func DisableDIT()
`)

tr, err := TranslateGoModule(pkg, src, GoModuleOptions{
FileName: "dit_arm64.s",
GOOS: "linux",
GOARCH: "arm64",
TargetTriple: arm64LinuxGNUTriple,
ResolveSym: testResolveSym("internal/runtime/sys"),
})
if err != nil {
t.Fatalf("llc failed: %v\n%s", err, string(out))
t.Fatal(err)
}
defer tr.Module.Dispose()

compileLLVMToObject(t, llc, arm64LinuxGNUTriple, "dit-gomod.ll", "dit-gomod.o", tr.Module.String())
}
Loading