-
Notifications
You must be signed in to change notification settings - Fork 17
/
magefile.go
201 lines (168 loc) · 4.43 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
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
//go:build mage
// +build mage
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
)
var (
soName = "libopenimsdk" //
outPath = "../shared/"
goSrc = "go" //
)
var Default = Build
// BuildAll compiles the project for all platforms.
func Build() {
if err := BuildAndroid(); err != nil {
fmt.Println("Error building for Android:", err)
}
if err := BuildIOS(); err != nil {
fmt.Println("Error building for iOS:", err)
}
if err := BuildLinux(); err != nil {
fmt.Println("Error building for Linux:", err)
}
if err := BuildWindows(); err != nil {
fmt.Println("Error building for Windows:", err)
}
}
// BuildAndroid compiles the project for Android.
func BuildAndroid() error {
architectures := []struct {
Arch, API string
}{
{"arm", "16"},
{"arm64", "21"},
{"386", "16"},
{"amd64", "21"},
}
for _, arch := range architectures {
if err := buildAndroid(outPath+"android", arch.Arch, arch.API); err != nil {
fmt.Printf("Failed to build for %s: %v\n", arch.Arch, err)
}
}
return nil
}
// BuildIOS compiles the project for iOS.
func BuildIOS() error {
fmt.Println("Building for iOS...")
outPath += "ios"
arch := os.Getenv("GOARCH")
if len(arch) == 0 {
arch = runtime.GOARCH
}
os.Setenv("GOOS", "darwin")
os.Setenv("GOARCH", arch)
os.Setenv("CGO_ENABLED", "1")
os.Setenv("CC", "clang")
cmd := exec.Command("go", "build", "-buildmode=c-shared", "-o", outPath+"/"+soName+".dylib", ".")
cmd.Dir = goSrc
cmd.Env = os.Environ()
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
fmt.Printf("Failed to build for iOS: %v\n", err)
return err
}
fmt.Println("Build for iOS completed successfully.")
return nil
}
// BuildLinux compiles the project for Linux.
func BuildLinux() error {
fmt.Println("Building for Linux...")
outPath += "linux"
arch := os.Getenv("GOARCH")
cc := os.Getenv("CC")
cxx := os.Getenv("CXX")
if len(arch) == 0 {
arch = runtime.GOARCH
}
if len(cc) == 0 {
cc = "gcc"
}
if len(cxx) != 0 {
os.Setenv("CXX", cxx)
}
os.Setenv("GOOS", "linux")
os.Setenv("GOARCH", arch)
os.Setenv("CGO_ENABLED", "1")
os.Setenv("CC", cc) //
cmd := exec.Command("go", "build", "-buildmode=c-shared", "-trimpath", "-ldflags=-s -w", "-o", outPath+"/"+soName+".so", ".")
cmd.Dir = goSrc
cmd.Env = os.Environ()
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
fmt.Printf("Failed to build for Linux: %v\n", err)
return err
}
fmt.Println("Build for Linux completed successfully.")
return nil
}
// BuildWindows compiles the project for Windows.
func BuildWindows() error {
fmt.Println("Building for Windows...")
outPath += "windows"
arch := os.Getenv("GOARCH")
cc := os.Getenv("CC")
cxx := os.Getenv("CXX")
if len(arch) == 0 {
arch = runtime.GOARCH
}
if len(cc) == 0 {
cc = "gcc"
}
if len(cxx) != 0 {
os.Setenv("CXX", cxx)
}
os.Setenv("GOOS", "windows")
os.Setenv("GOARCH", arch)
os.Setenv("CGO_ENABLED", "1")
os.Setenv("CC", cc)
cmd := exec.Command("go", "build", "-buildmode=c-shared", "-trimpath", "-ldflags=-s -w", "-o", outPath+"/"+soName+".dll", ".")
cmd.Dir = goSrc
cmd.Env = os.Environ()
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
fmt.Printf("Failed to build for Windows: %v\n", err)
return err
}
fmt.Println("Build for Windows completed successfully.")
return nil
}
// buildAndroid builds the Android library for the specified architecture.
func buildAndroid(aOutPath, arch, apiLevel string) error {
fmt.Printf("Building for %s...\n", arch)
ndkPath := os.Getenv("ANDROID_NDK_HOME")
osSuffix := ""
if runtime.GOOS == "windows" {
osSuffix = ".cmd" //
}
ccBasePath := ndkPath + "/toolchains/llvm/prebuilt/" + runtime.GOOS + "-x86_64/bin/"
var cc string
switch arch {
case "arm":
cc = ccBasePath + "armv7a-linux-androideabi" + apiLevel + "-clang" + osSuffix
case "arm64":
cc = ccBasePath + "aarch64-linux-android" + apiLevel + "-clang" + osSuffix
case "386":
cc = ccBasePath + "i686-linux-android" + apiLevel + "-clang" + osSuffix
case "amd64":
cc = ccBasePath + "x86_64-linux-android" + apiLevel + "-clang" + osSuffix
}
env := []string{
"CGO_ENABLED=1",
"GOOS=android",
"GOARCH=" + arch,
"CC=" + cc,
}
cmd := exec.Command("go", "build", "-buildmode=c-shared", "-trimpath", "-ldflags=-s -w", "-o", aOutPath+"/"+arch+"/"+soName+".so", ".")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Dir = goSrc
cmd.Env = append(os.Environ(), env...)
return cmd.Run()
}