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 05a1e9d
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 31 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
20 changes: 20 additions & 0 deletions test/xgo_integration/cross_build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package xgo_integration

import (
"os/exec"
"testing"

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

// go test ./test/xgo_integration/ -v -run TestCrossBuild
func TestCrossBuild(t *testing.T) {
_, 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 test/xgo_integration/testdata/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 test/xgo_integration/testdata/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")
}

0 comments on commit 05a1e9d

Please sign in to comment.