Skip to content

Commit e988674

Browse files
committed
Implement cross compilation
1 parent 372e827 commit e988674

File tree

3 files changed

+52
-31
lines changed

3 files changed

+52
-31
lines changed

build.go

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"os/exec"
67
"path/filepath"
78
"runtime"
89
"strings"
910

11+
"github.com/Bananenpro/cli"
1012
"github.com/code-game-project/go-utils/cgfile"
1113
cgExec "github.com/code-game-project/go-utils/exec"
1214
"github.com/code-game-project/go-utils/modules"
@@ -22,19 +24,26 @@ func Build() error {
2224
if err != nil {
2325
return err
2426
}
27+
data.OS = strings.ReplaceAll(data.OS, "current", "")
28+
data.OS = strings.ReplaceAll(data.OS, "macos", "darwin")
29+
data.Arch = strings.ReplaceAll(data.Arch, "current", "")
30+
data.Arch = strings.ReplaceAll(data.Arch, "x86", "386")
31+
data.Arch = strings.ReplaceAll(data.Arch, "x64", "amd64")
32+
data.Arch = strings.ReplaceAll(data.Arch, "arm32", "arm")
2533

2634
switch config.Type {
2735
case "client":
28-
return buildClient(config.Game, data.Output, config.URL)
36+
return buildClient(config.Game, data.Output, config.URL, data.OS, data.Arch)
2937
case "server":
30-
return buildServer(data.Output)
38+
return buildServer(data.Output, data.OS, data.Arch)
3139
default:
3240
return fmt.Errorf("Unknown project type: %s", config.Type)
3341
}
3442
}
3543

36-
func buildClient(gameName, output, url string) error {
37-
out, err := getOutputName(output, false)
44+
func buildClient(gameName, output, url, operatingSystem, architecture string) error {
45+
cli.BeginLoading("Building...")
46+
out, err := getOutputName(output, false, operatingSystem)
3847
if err != nil {
3948
return err
4049
}
@@ -46,21 +55,47 @@ func buildClient(gameName, output, url string) error {
4655

4756
cmdArgs := []string{"build", "-o", out, "-ldflags", fmt.Sprintf("-X %s/%s.URL=%s", packageName, gamePackageName, url)}
4857

49-
_, err = cgExec.Execute(false, "go", cmdArgs...)
50-
return err
58+
if _, err = exec.LookPath("go"); err != nil {
59+
return fmt.Errorf("'go' ist not installed!")
60+
}
61+
62+
cmd := exec.Command("go", cmdArgs...)
63+
cmd.Env = append(os.Environ(), "GOOS="+operatingSystem, "GOARCH="+architecture)
64+
65+
buildOutput, err := cmd.CombinedOutput()
66+
if err != nil {
67+
fmt.Println(string(buildOutput))
68+
return fmt.Errorf("Failed to run 'GOOS=%s GOARCH=%s go %s'", operatingSystem, architecture, strings.Join(cmdArgs, " "))
69+
}
70+
cli.FinishLoading()
71+
return nil
5172
}
5273

53-
func buildServer(output string) error {
54-
out, err := getOutputName(output, true)
74+
func buildServer(output, operatingSystem, architecture string) error {
75+
cli.BeginLoading("Building...")
76+
out, err := getOutputName(output, true, operatingSystem)
5577
if err != nil {
5678
return err
5779
}
5880
cmdArgs := []string{"build", "-o", out}
5981
_, err = cgExec.Execute(false, "go", cmdArgs...)
60-
return err
82+
83+
if _, err = exec.LookPath("go"); err != nil {
84+
return fmt.Errorf("'go' ist not installed!")
85+
}
86+
87+
cmd := exec.Command("go", cmdArgs...)
88+
cmd.Env = append(os.Environ(), "GOOS="+operatingSystem, "GOARCH="+architecture)
89+
90+
err = cmd.Run()
91+
if err != nil {
92+
return fmt.Errorf("Failed to run 'GOOS=%s GOARCH=%s go %s'", operatingSystem, architecture, strings.Join(cmdArgs, " "))
93+
}
94+
cli.FinishLoading()
95+
return nil
6196
}
6297

63-
func getOutputName(output string, isServer bool) (string, error) {
98+
func getOutputName(output string, isServer bool, operatingSystem string) (string, error) {
6499
absRoot, err := filepath.Abs(".")
65100
if err != nil {
66101
return "", err
@@ -72,7 +107,7 @@ func getOutputName(output string, isServer bool) (string, error) {
72107
}
73108
}
74109

75-
if runtime.GOOS == "windows" && !strings.HasSuffix(output, ".exe") {
110+
if ((operatingSystem == "" && runtime.GOOS == "windows") || operatingSystem == "windows") && !strings.HasSuffix(output, ".exe") {
76111
output += ".exe"
77112
}
78113

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

55
require (
66
github.com/Bananenpro/cli v0.3.0
7-
github.com/code-game-project/go-utils v0.2.10
7+
github.com/code-game-project/go-utils v0.2.13
88
)
99

1010
require (
@@ -14,7 +14,7 @@ require (
1414
github.com/mattn/go-colorable v0.1.12 // indirect
1515
github.com/mattn/go-isatty v0.0.14 // indirect
1616
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
17-
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 // indirect
17+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
1818
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
1919
golang.org/x/text v0.3.7 // indirect
2020
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect

go.sum

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
github.com/AlecAivazis/survey/v2 v2.3.5 h1:A8cYupsAZkjaUmhtTYv3sSqc7LO5mp1XDfqe5E/9wRQ=
22
github.com/AlecAivazis/survey/v2 v2.3.5/go.mod h1:4AuI9b7RjAR+G7v9+C4YSlX/YL3K3cWNXgWXOhllqvI=
3-
github.com/Bananenpro/cli v0.2.1 h1:gzW9QCGFo5QbevVdcVbNFBTbQaM4piLd57TPWBehPCU=
4-
github.com/Bananenpro/cli v0.2.1/go.mod h1:JBXpIAXo/D0rlsfgCViQBicjcJY6UWUldmxvKM+ijRc=
53
github.com/Bananenpro/cli v0.3.0 h1:gQOzc22yv+rePT0nRYva1ccdva3hTGyUwrGdcnXqchU=
64
github.com/Bananenpro/cli v0.3.0/go.mod h1:JBXpIAXo/D0rlsfgCViQBicjcJY6UWUldmxvKM+ijRc=
75
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
86
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
97
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
108
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
11-
github.com/code-game-project/go-utils v0.1.0 h1:mtxld853WsjVeDFP4l5hMGaq7thXDLslkxC3MEdyPMY=
12-
github.com/code-game-project/go-utils v0.1.0/go.mod h1:kQ6kH9XDzdM2pnJUI1lw61Gp8XOams/E2dKABa1mBI8=
13-
github.com/code-game-project/go-utils v0.2.4 h1:UhWy1vJeXCaV4qjbFcIo9b0tzv69DK4HPwRFbrrDKF4=
14-
github.com/code-game-project/go-utils v0.2.4/go.mod h1:kQ6kH9XDzdM2pnJUI1lw61Gp8XOams/E2dKABa1mBI8=
15-
github.com/code-game-project/go-utils v0.2.5 h1:f5IbAsUMu+e0eiDlbzbpJ5Hg2jJOCZLdcDvY3CpmGSg=
16-
github.com/code-game-project/go-utils v0.2.5/go.mod h1:kQ6kH9XDzdM2pnJUI1lw61Gp8XOams/E2dKABa1mBI8=
17-
github.com/code-game-project/go-utils v0.2.9 h1:ezaxxLBAQRwFnIXso+zKPL7AJnuAMIcMwrhLruoyOa8=
18-
github.com/code-game-project/go-utils v0.2.9/go.mod h1:kQ6kH9XDzdM2pnJUI1lw61Gp8XOams/E2dKABa1mBI8=
19-
github.com/code-game-project/go-utils v0.2.10 h1:+TDLdj7Y+zyyavPeuAMFlA8IegUu6GBq2o/NxxosriQ=
20-
github.com/code-game-project/go-utils v0.2.10/go.mod h1:/ws9iYkZCnZvS9g2aqdxKwSnU5AEeI7SE1mbB+x4ggg=
9+
github.com/code-game-project/go-utils v0.2.13 h1:Am5raDGXHPywKH6VtKFEvG7rx6GbaJ8G0z6eHGnufX4=
10+
github.com/code-game-project/go-utils v0.2.13/go.mod h1:/ws9iYkZCnZvS9g2aqdxKwSnU5AEeI7SE1mbB+x4ggg=
2111
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
2212
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
2313
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -48,12 +38,8 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
4838
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4939
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5040
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
51-
golang.org/x/sys v0.0.0-20220727055044-e65921a090b8 h1:dyU22nBWzrmTQxtNrr4dzVOvaw35nUYE279vF9UmsI8=
52-
golang.org/x/sys v0.0.0-20220727055044-e65921a090b8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
53-
golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704 h1:Y7NOhdqIOU8kYI7BxsgL38d0ot0raxvcW+EMQU2QrT4=
54-
golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
55-
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 h1:v1W7bwXHsnLLloWYTVEdvGvA7BHMeBYsPcF0GLDxIRs=
56-
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
41+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
42+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5743
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
5844
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
5945
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

0 commit comments

Comments
 (0)