-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
226 lines (199 loc) · 5.48 KB
/
cli.go
File metadata and controls
226 lines (199 loc) · 5.48 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package ku
import (
"flag"
"fmt"
"os"
"slices"
"github.com/mgenware/j9/v3"
)
type CLIArgs struct {
SDKs []SDKEnum
Arch ArchEnum
Target string
Action CLIAction
DebugBuild bool
NDK string
CleanBuild bool
SignArg string
PlatformArg PlatformEnum
LibType LibType
NoPull bool
Options *CLIOptions
}
type CLIAction string
const (
// Run ./configure
CLIActionConfigure CLIAction = "configure"
// Run make clean
CLIActionClean CLIAction = "clean"
// Run make && make install
CLIActionBuild CLIAction = "build"
// Run make
CLIActionMake CLIAction = "make"
)
var SupportedCLIActions = map[CLIAction]bool{
CLIActionConfigure: true,
CLIActionClean: true,
CLIActionBuild: true,
}
type CLIOptions struct {
AllowedTargets []string
DefaultPlatform PlatformEnum
DefaultTarget string
DefaultSDK SDKEnum
DefaultArch ArchEnum
DefaultAction CLIAction
CreateDistDir bool
BeforeParseFn func()
AfterParseFn func(cliArgs *CLIArgs)
}
func ParseCLIArgs(opt *CLIOptions) *CLIArgs {
if opt == nil {
fmt.Printf("CLIOptions is nil\n")
os.Exit(1)
}
var allowedTargets []string
if len(opt.AllowedTargets) > 0 {
allowedTargets = opt.AllowedTargets
} else if opt.DefaultTarget != "" {
allowedTargets = []string{opt.DefaultTarget}
} else {
fmt.Printf("AllowedTargets is empty and DefaultTarget is not set\n")
os.Exit(1)
}
var platformInput string
var resolvedPlatform PlatformEnum
flag.StringVar(&platformInput, "platform", string(opt.DefaultPlatform), "Platform. Supported platforms: macos(m), ios(i), android(a), darwin(d).")
flag.StringVar(&platformInput, "p", string(opt.DefaultPlatform), "-platform shorthand.")
var target string
flag.StringVar(&target, "target", opt.DefaultTarget, "Build target. "+"Allowed targets: "+fmt.Sprintf("%v", allowedTargets))
flag.StringVar(&target, "t", opt.DefaultTarget, "-target shorthand.")
sdkPtr := flag.String("sdk", string(opt.DefaultSDK), "SDK. If not specified, all supported SDKs for the platform will be used.")
archPtr := flag.String("arch", string(opt.DefaultArch), "Arch. If not specified, all supported SDK archs for the platform will be used.")
actionPtr := flag.String("action", string(opt.DefaultAction), "Action. Supported actions: configure, clean, build.")
ndkPtr := flag.String("ndk", "", "NDK name.")
debugPtr := flag.Bool("debug", false, "Debug build.")
cleanPtr := flag.Bool("clean", false, "Run a clean build.")
dylibPtr := flag.Bool("dylib", false, "Whether the output is a dynamic/shared library.")
signPtr := flag.String("sign", "", "Sign the output with the specified identity.")
noPullPtr := flag.Bool("no-pull", false, "Whether to skip git pull")
if opt.BeforeParseFn != nil {
opt.BeforeParseFn()
}
flag.Parse()
if target == "" {
fmt.Printf("Target is required\n")
os.Exit(1)
}
if !slices.Contains(allowedTargets, target) {
fmt.Printf("Target %v is not allowed. Allowed targets: %v\n", target, allowedTargets)
os.Exit(1)
}
if platformInput == "" && *ndkPtr != "" {
// Make platform default to android if NDK is specified.
platformInput = string(PlatformAndroid)
}
var sdks []SDKEnum
// Validate platform if specified.
if platformInput != "" {
resolvedPlatform = ParsePlatformString(platformInput, false)
sdks = PlatformSDKs[resolvedPlatform]
if sdks == nil {
fmt.Printf("No supported SDKs for platform: %v\n", string(resolvedPlatform))
os.Exit(1)
}
}
// Validate sdk.
if *sdkPtr != "" {
if !SupportedSDKs[SDKEnum(*sdkPtr)] {
fmt.Printf("Unsupported SDK: %v\n", *sdkPtr)
os.Exit(1)
}
if sdks != nil {
fmt.Printf("Both -sdk and -platform are specified\n")
}
sdks = []SDKEnum{SDKEnum(*sdkPtr)}
}
// There must be at least one sdk.
// Note: `sdks` could be set by `-platform` or `-sdk`.
if len(sdks) == 0 {
fmt.Printf("No SDKs found. Please specify SDKs via -platform or -sdk.\n")
os.Exit(1)
}
// Validate arch.
if *archPtr != "" {
if !SupportedArchs[ArchEnum(*archPtr)] {
fmt.Printf("Unsupported arch: %v\n", *archPtr)
os.Exit(1)
}
}
// Validate action.
if *actionPtr == "" {
if !SupportedCLIActions[CLIActionBuild] {
fmt.Printf("Unsupported action: %v\n", *actionPtr)
os.Exit(1)
}
}
// Validate Android settings.
if sdks[0] == SDKAndroid {
if *ndkPtr == "" {
fmt.Printf("NDK is not specified\n")
os.Exit(1)
}
}
libType := LibTypeStatic
if *dylibPtr {
libType = LibTypeDynamic
}
res := &CLIArgs{
SDKs: sdks,
Arch: ArchEnum(*archPtr),
Target: target,
Action: CLIAction(*actionPtr),
DebugBuild: *debugPtr,
CleanBuild: *cleanPtr,
NDK: *ndkPtr,
SignArg: *signPtr,
PlatformArg: resolvedPlatform,
LibType: libType,
Options: opt,
NoPull: *noPullPtr,
}
if opt.AfterParseFn != nil {
opt.AfterParseFn(res)
}
return res
}
func CreateDefaultTunnel() *j9.Tunnel {
return j9.NewTunnel(j9.NewLocalNode(), j9.NewConsoleLogger())
}
func ParsePlatformString(s string, required bool) PlatformEnum {
// Check shorthand first.
switch s {
case "m":
return PlatformMacos
case "i":
return PlatformIos
case "a":
return PlatformAndroid
case "d":
return PlatformDarwin
}
// Check full string.
switch s {
case string(PlatformMacos):
return PlatformMacos
case string(PlatformIos):
return PlatformIos
case string(PlatformAndroid):
return PlatformAndroid
case string(PlatformDarwin):
return PlatformDarwin
default:
if required {
fmt.Printf("Unsupported platform: %v\n", s)
os.Exit(1)
}
}
return ""
}