-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
196 lines (159 loc) · 4.45 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/otiai10/copy"
)
// On macOS, we need to make sure that we always write external binaries to a
// new file. Otherwise, kernel-side code-signing caches become outdated and Max
// crashes with a SIGKILL when loading the modified external.
// https://developer.apple.com/documentation/security/updating_mac_software
var name = flag.String("name", "", "the name of the external")
var out = flag.String("out", "out", "the output directory")
var cross = flag.Bool("cross", false, "cross compile for Windows on macOS")
var install = flag.String("install", "", "install into specified package")
func main() {
// parse flags
flag.Parse()
// log
fmt.Println("==> checking system...")
// check go
_, err := exec.LookPath("go")
if err != nil {
panic("missing go command (you may need to install Go)")
}
// check cross compile
if *cross {
// check OS
if runtime.GOOS != "darwin" {
panic("cannot cross compile for macOS from Windows")
}
// check zig
_, err := exec.LookPath("zig")
if err != nil {
panic("missing zig command (you may need to install zig)")
}
}
// log
fmt.Println("==> preparing build...")
// check name
if *name == "" {
panic("missing external name")
}
// get out dir
var outDir = filepath.Join(".", "out")
if *out != "" {
outDir, err = filepath.Abs(*out)
check(err)
}
// print
fmt.Printf("name: %s\n", *name)
fmt.Printf("out: %s\n", outDir)
// clear directory (see top notes)
check(os.RemoveAll(outDir))
check(os.MkdirAll(outDir, os.ModePerm))
// build
switch runtime.GOOS {
case "darwin":
buildDarwin(outDir)
if *cross {
crossBuildWindows(outDir)
}
case "windows":
buildWindows(outDir)
}
// install
if *install != "" {
// log
fmt.Println("==> installing external...")
// get home dir
user, err := os.UserHomeDir()
check(err)
// prepare path
dir, err := filepath.Abs(filepath.Join(user, "Documents", "Max 8", "Packages", *install, "externals"))
check(err)
// log
fmt.Printf("target: %s\n", dir)
// create path
check(os.MkdirAll(dir, os.ModePerm))
// copy external (see top notes)
switch runtime.GOOS {
case "darwin":
check(os.RemoveAll(filepath.Join(dir, *name+".mxo")))
check(copy.Copy(filepath.Join(outDir, *name+".mxo"), filepath.Join(dir, *name+".mxo")))
case "windows":
check(os.RemoveAll(filepath.Join(dir, *name+".mxe64")))
check(copy.Copy(filepath.Join(outDir, *name+".mxe64"), filepath.Join(dir, *name+".mxe64")))
}
}
// log
fmt.Println("==> done!")
}
func buildDarwin(outDir string) {
// log
fmt.Println("==> building...")
// prepare bin file
bin := filepath.Join("out", *name)
// build arm64 and amd64
run("go",
[]string{"build", "-v", "-buildmode=c-shared", "-o", bin + "-arm64"},
[]string{"CGO_ENABLED=1", "GOARCH=arm64", "CGO_LDFLAGS=-Wl,-no_fixup_chains"},
)
run("go",
[]string{"build", "-v", "-buildmode=c-shared", "-o", bin + "-amd64"},
[]string{"CGO_ENABLED=1", "GOARCH=amd64", "CGO_LDFLAGS=-Wl,-no_fixup_chains"},
)
// assemble universal binary
run("lipo",
[]string{"-create", "-output", bin, bin + "-amd64", bin + "-arm64"},
nil,
)
// ensure directory
check(os.MkdirAll(filepath.Join(outDir, *name+".mxo", "MacOS"), os.ModePerm))
// copy binary
check(os.Rename(bin, filepath.Join(outDir, *name+".mxo", "MacOS", *name)))
// write info plist
check(ioutil.WriteFile(filepath.Join(outDir, *name+".mxo", "Info.plist"), []byte(infoPlist(*name)), os.ModePerm))
// write package info
check(ioutil.WriteFile(filepath.Join(outDir, *name+".mxo", "PkgInfo"), []byte(pkgInfo), os.ModePerm))
}
func buildWindows(outDir string) {
// log
fmt.Println("==> building...")
// build
run("go",
[]string{"build", "-v", "-buildmode=c-shared", "-o", filepath.Join(outDir, *name+".mxe64")},
[]string{"CGO_ENABLED=1"},
)
}
func crossBuildWindows(outDir string) {
// log
fmt.Println("==> cross building...")
// build
run("go",
[]string{"build", "-v", "-buildmode=c-shared", "-o", filepath.Join(outDir, *name+".mxe64")},
[]string{`CC=zig cc -target x86_64-windows-gnu`, "GOOS=windows", "GOARCH=amd64", "CGO_ENABLED=1"},
)
}
func run(bin string, args []string, env []string) {
// construct
cmd := exec.Command(bin, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
cmd.Env = append(env, os.Environ()...)
// run
err := cmd.Run()
if err != nil {
panic("command failed")
}
}
func check(err error) {
if err != nil {
panic(err)
}
}