-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
172 lines (139 loc) · 3.59 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/otiai10/copy"
)
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 mingw
_, err := exec.LookPath("x86_64-w64-mingw32-gcc")
if err != nil {
panic("missing x86_64-w64-mingw32-gcc command (you may need to install mingw-w64)")
}
}
// 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)
// 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
switch runtime.GOOS {
case "darwin":
check(copy.Copy(filepath.Join(outDir, *name+".mxo"), filepath.Join(dir, *name+".mxo")))
case "windows":
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...")
// build
run("go",
[]string{"build", "-v", "-buildmode=c-shared", "-o", filepath.Join("out", *name)},
[]string{"CGO_ENABLED=1"},
)
// ensure directory
check(os.MkdirAll(filepath.Join(outDir, *name+".mxo", "MacOS"), os.ModePerm))
// copy binary
check(os.Rename(filepath.Join(outDir, *name), 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=x86_64-w64-mingw32-gcc", "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)
}
}