-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmagefile.go
158 lines (128 loc) · 3.18 KB
/
magefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//+build mage
// This is the build script for Mage. The install target is all you really need.
// The release target is for generating official releases and is really only
// useful to project admins.
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
type BuildPlatform struct {
OS string
Arch string
}
var (
godotBin string
ci bool
targetOS string
targetArch string
)
func init() {
var (
ok bool
)
if targetOS, ok = os.LookupEnv("TARGET_OS"); !ok {
targetOS = runtime.GOOS
}
if targetArch, ok = os.LookupEnv("TARGET_ARCH"); !ok {
targetArch = runtime.GOARCH
}
envCI, _ := os.LookupEnv("CI")
ci = envCI == "true"
}
func initGodotBin() {
var (
err error
)
godotBin, _ = os.LookupEnv("GODOT_BIN")
if godotBin, err = which(godotBin); err == nil {
fmt.Printf("GODOT_BIN = %s\n", godotBin)
return
}
if !ci {
if godotBin, err = which("godot3"); err == nil {
fmt.Printf("GODOT_BIN = %s\n", godotBin)
return
}
if godotBin, err = which("godot"); err == nil {
fmt.Printf("GODOT_BIN = %s\n", godotBin)
return
}
if godotBin, err = which("/Applications/Godot.app/Contents/MacOS/Godot"); err == nil {
fmt.Printf("GODOT_BIN = %s\n", godotBin)
return
}
}
panic(err)
}
func envWithPlatform(platform BuildPlatform) map[string]string {
envs := map[string]string{
"GOOS": targetOS,
"GOARCH": targetArch,
"CGO_ENABLED": "1",
}
return envs
}
func Clean() error {
return nil
}
func Build() error {
mg.Deps(initGodotBin)
appPath := filepath.Join(".")
outputPath := filepath.Join("libs")
return buildGodotPlugin(
appPath,
outputPath,
BuildPlatform{
OS: targetOS,
Arch: targetArch,
},
)
}
func Run() error {
mg.Deps(Build)
appPath := filepath.Join(".")
return runPlugin(appPath)
}
func runPlugin(appPath string) error {
return sh.RunWith(
map[string]string{
"GOTRACEBACK": "crash",
"GODEBUG": "asyncpreemptoff=1,cgocheck=1,invalidptr=1,clobberfree=1,tracebackancestors=3",
"LOG_LEVEL": "debug",
"TEST_USE_GINKGO_WRITER": "1",
},
godotBin, "--verbose", "--path", appPath)
}
func buildGodotPlugin(appPath string, outputPath string, platform BuildPlatform) error {
return sh.RunWith(envWithPlatform(platform), mg.GoCmd(), "build",
"-tags", "tools", "-buildmode=c-shared", "-x", "-trimpath",
"-o", filepath.Join(outputPath, platform.godotPluginCSharedName(appPath)),
filepath.Join(appPath, "main.go"),
)
}
func (p BuildPlatform) godotPluginCSharedName(appPath string) string {
// NOTE: these files needs to line up with CI as well as the naming convention
// expected by the test godot project
switch p.OS {
case "windows":
return fmt.Sprintf("game-windows-4.0-%s.dll", p.Arch)
case "darwin":
return fmt.Sprintf("game-darwin-10.6-%s.dylib", p.Arch)
case "linux":
return fmt.Sprintf("game-linux-%s.so", p.Arch)
default:
panic(fmt.Errorf("unsupported build platform: %s", p.OS))
}
}
func which(filename string) (string, error) {
if len(filename) == 0 {
return "", fmt.Errorf("no filename specified")
}
return sh.Output("which", filename)
}
var Default = Build