Skip to content

Commit

Permalink
fix cross compile
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Jul 11, 2024
1 parent b2c1918 commit 644da2d
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 39 deletions.
41 changes: 41 additions & 0 deletions cmd/xgo/go_build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

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

func buildCompiler(goroot string, output string) error {
args := []string{"build"}
if isDevelopment {
args = append(args, "-gcflags=all=-N -l")
}
args = append(args, "-o", output, "./")
cmd := exec.Command(filepath.Join(goroot, "bin", "go"), args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
env, err := patchEnvWithGoroot(os.Environ(), goroot)
if err != nil {
return err
}
cmd.Env = env
// when building the compiler, we want native
// build, see https://github.com/xhd2015/xgo/issues/231
cmd.Env = appendNativeBuildEnv(cmd.Env)
cmd.Dir = filepath.Join(goroot, "src", "cmd", "compile")
return cmd.Run()
}
func buildExecTool(dir string, execToolBin string) error {
// build exec tool
cmd := exec.Command(getNakedGo(), "build", "-o", execToolBin, "./exec_tool")
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = appendNativeBuildEnv(os.Environ())
return cmd.Run()
}

func appendNativeBuildEnv(env []string) []string {
return append(env, "GOOS=", "GOARCH=")
}
24 changes: 1 addition & 23 deletions cmd/xgo/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,7 @@ func buildInstrumentTool(goroot string, xgoSrc string, compilerBin string, compi
if false {
actualExecToolBin := execToolBin
if isDevelopment {
// build exec tool
buildExecToolCmd := exec.Command(getNakedGo(), "build", "-o", execToolBin, "./exec_tool")
buildExecToolCmd.Dir = filepath.Join(xgoSrc, "cmd")
buildExecToolCmd.Stdout = os.Stdout
buildExecToolCmd.Stderr = os.Stderr
err = buildExecToolCmd.Run()
err := buildExecTool(filepath.Join(xgoSrc, "cmd"), execToolBin)
if err != nil {
return false, "", err
}
Expand Down Expand Up @@ -297,23 +292,6 @@ func findBuiltExecTool() (string, error) {
}
return "", fmt.Errorf("exec_tool not found in %s and ~/.xgo/bin", dirName)
}
func buildCompiler(goroot string, output string) error {
args := []string{"build"}
if isDevelopment {
args = append(args, "-gcflags=all=-N -l")
}
args = append(args, "-o", output, "./")
cmd := exec.Command(filepath.Join(goroot, "bin", "go"), args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
env, err := patchEnvWithGoroot(os.Environ(), goroot)
if err != nil {
return err
}
cmd.Env = env
cmd.Dir = filepath.Join(goroot, "src", "cmd", "compile")
return cmd.Run()
}

func compareAndUpdateCompilerID(compilerFile string, compilerIDFile string) (changed bool, err error) {
prevData, statErr := fileutil.ReadFile(compilerIDFile)
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/patch_compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,11 @@ func patchRuntimeDef(origGoroot string, goroot string, goVersion *goinfo.GoVersi
dirs = []string{goroot, "src", "cmd", "compile", "internal", "gc"}
}
cmd.Dir = filepath.Join(dirs...)
cmd.Env = os.Environ()
cmd.Env, err = patchEnvWithGoroot(cmd.Env, origGoroot)
cmd.Env, err = patchEnvWithGoroot(os.Environ(), origGoroot)
if err != nil {
return err
}
cmd.Env = appendNativeBuildEnv(cmd.Env)

err = cmd.Run()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cmd/xgo/shadow.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main
import (
"embed"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/xhd2015/xgo/support/cmd"
"github.com/xhd2015/xgo/support/fileutil"
"github.com/xhd2015/xgo/support/osinfo"
)

Expand Down Expand Up @@ -37,15 +37,15 @@ func handleShadow() error {
return err
}

err = ioutil.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(`module github.com/xhd2015/xgo/cmd/xgo/shadow
err = fileutil.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(`module github.com/xhd2015/xgo/cmd/xgo/shadow
go 1.14
`), 0755)
`))
if err != nil {
return err
}

err = cmd.Dir(tmpDir).Run(getNakedGo(), "build", "-o", filepath.Join(shadowDir, "go"+osinfo.EXE_SUFFIX), "./")
err = cmd.Dir(tmpDir).Env(appendNativeBuildEnv(nil)).Run(getNakedGo(), "build", "-o", filepath.Join(shadowDir, "go"+osinfo.EXE_SUFFIX), "./")
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "fmt"

// auto updated
const VERSION = "1.0.46"
const REVISION = "ab32ad121c57c321089d61906f2dd36898b7bcd1+1"
const NUMBER = 294
const REVISION = "b2c1918871f0a8bc3ea4bb9cf46e297d21c5f433+1"
const NUMBER = 295

// manually updated
const CORE_VERSION = "1.0.43"
Expand Down
19 changes: 19 additions & 0 deletions runtime/test/build/cross_build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package xgo_integration

import (
"testing"
)

// go test ./test/xgo_integration/ -v -run TestCrossBuild
func TestCrossBuild(t *testing.T) {
// xgo test -c ./simple

// _, err := exec.LookPath("xgo")
// if err != nil {
// t.Skipf("missing: %v", err)
// }
// err = cmd.Env([]string{"GOOS=windows", "GOARCH=amd64"}).Debug().Dir("./testdata/build_simple").Run("xgo", "test", "--log-debug", "-c", "-o", "/dev/null")
// if err != nil {
// t.Fatal(err)
// }
}
7 changes: 7 additions & 0 deletions runtime/test/build/simple/greet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Printf("hello xgo\n")
}
7 changes: 7 additions & 0 deletions runtime/test/build/simple/greet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "testing"

func TestHelloWorld(t *testing.T) {
t.Logf("hello world")
}
26 changes: 18 additions & 8 deletions script/run-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type TestCase struct {
dir string
flags []string
windowsFlags []string
env []string
}

var extraSubTests = []*TestCase{
Expand Down Expand Up @@ -154,6 +155,13 @@ var extraSubTests = []*TestCase{
dir: "runtime/test/mock/rule",
flags: []string{"-run", "TestClosureWithMockRuleNoMock", "--mock-rule", `{"closure":true,"action":"exclude"}`},
},
{
// see https://github.com/xhd2015/xgo/issues/231
name: "cross_build",
dir: "runtime/test/build/simple",
flags: []string{"-c", "-o", "/dev/null"},
env: []string{"GOOS=", "GOARCH="},
},
{
name: "xgo_integration",
usePlainGo: true,
Expand Down Expand Up @@ -473,15 +481,15 @@ func listGoroots(dir string) ([]string, error) {
}

func runDefaultTest(goroot string, args []string, tests []string) error {
return doRunTest(goroot, testKind_default, false, "", args, tests)
return doRunTest(goroot, testKind_default, false, "", args, tests, nil)
}

func runXgoTest(goroot string, args []string, tests []string) error {
return doRunTest(goroot, testKind_xgoTest, false, "", args, tests)
return doRunTest(goroot, testKind_xgoTest, false, "", args, tests, nil)
}

func runRuntimeTest(goroot string, args []string, tests []string) error {
err := doRunTest(goroot, testKind_runtimeTest, false, "", args, tests)
err := doRunTest(goroot, testKind_runtimeTest, false, "", args, tests, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -513,6 +521,7 @@ func runRuntimeSubTest(goroot string, args []string, tests []string, names []str
continue
}
dir := tt.dir
env := tt.env
var subDirs bool
if strings.HasSuffix(dir, "/...") {
subDirs = true
Expand All @@ -535,7 +544,7 @@ func runRuntimeSubTest(goroot string, args []string, tests []string, names []str
}

if hasHook {
err := doRunTest(goroot, testKind_xgoAny, tt.usePlainGo, runDir, amendArgs(append(extraArgs, []string{"-run", "TestPreCheck"}...), nil), []string{"./hook_test.go"})
err := doRunTest(goroot, testKind_xgoAny, tt.usePlainGo, runDir, amendArgs(append(extraArgs, []string{"-run", "TestPreCheck"}...), nil), []string{"./hook_test.go"}, env)
if err != nil {
return err
}
Expand All @@ -550,20 +559,20 @@ func runRuntimeSubTest(goroot string, args []string, tests []string, names []str
if runtime.GOOS == "windows" && tt.windowsFlags != nil {
testFlags = tt.windowsFlags
}
err := doRunTest(goroot, testKind_xgoAny, tt.usePlainGo, runDir, amendArgs(extraArgs, testFlags), []string{testArgDir})
err := doRunTest(goroot, testKind_xgoAny, tt.usePlainGo, runDir, amendArgs(extraArgs, testFlags), []string{testArgDir}, env)
if err != nil {
return err
}

if hasHook {
err := doRunTest(goroot, testKind_xgoAny, tt.usePlainGo, runDir, amendArgs(append(extraArgs, []string{"-run", "TestPostCheck"}...), nil), []string{"./hook_test.go"})
err := doRunTest(goroot, testKind_xgoAny, tt.usePlainGo, runDir, amendArgs(append(extraArgs, []string{"-run", "TestPostCheck"}...), nil), []string{"./hook_test.go"}, env)
if err != nil {
return err
}
}
}
if len(names) == 0 {
err := doRunTest(goroot, testKind_runtimeSubTest, false, "", args, tests)
err := doRunTest(goroot, testKind_runtimeSubTest, false, "", args, tests, nil)
if err != nil {
return err
}
Expand All @@ -581,7 +590,7 @@ const (
testKind_xgoAny testKind = "xgo-any"
)

func doRunTest(goroot string, kind testKind, usePlainGo bool, dir string, args []string, tests []string) error {
func doRunTest(goroot string, kind testKind, usePlainGo bool, dir string, args []string, tests []string, env []string) error {
goroot, err := filepath.Abs(goroot)
if err != nil {
return err
Expand Down Expand Up @@ -685,6 +694,7 @@ func doRunTest(goroot string, kind testKind, usePlainGo bool, dir string, args [
execCmd.Env = os.Environ()
execCmd.Env = append(execCmd.Env, "GOROOT="+goroot)
execCmd.Env = append(execCmd.Env, "PATH="+filepath.Join(goroot, "bin")+string(filepath.ListSeparator)+os.Getenv("PATH"))
execCmd.Env = append(execCmd.Env, env...)

return execCmd.Run()
}

0 comments on commit 644da2d

Please sign in to comment.