-
Notifications
You must be signed in to change notification settings - Fork 77
/
build.py
169 lines (110 loc) · 3.78 KB
/
build.py
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
#!/usr/bin/env python3
import os, time, subprocess, getopt, sys
mainFile = "ran.go"
def runCmd(cmd):
p = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
stdout = p.communicate()[0].decode('utf-8').strip()
return stdout
# Get last tag.
def lastTag():
return runCmd('git describe --abbrev=0 --tags')
# Get current branch name.
def branch():
return runCmd('git rev-parse --abbrev-ref HEAD')
# Get last git commit id.
def lastCommitId():
return runCmd('git log --pretty=format:"%h" -1')
# Get package name in the current directory.
# E.g. github.com/m3ng9i/ran
# This function is not used any more.
def packageName():
return runCmd("go list")
# Assemble build command.
def buildCmd():
buildFlag = []
version = lastTag()
if version != "":
buildFlag.append("-X 'main._version_={}'".format(version))
branchName = branch()
if branchName != "":
buildFlag.append("-X 'main._branch_={}'".format(branchName))
commitId = lastCommitId()
if commitId != "":
buildFlag.append("-X 'main._commitId_={}'".format(commitId))
# current time
buildFlag.append("-X 'main._buildTime_={}'".format(time.strftime("%Y-%m-%d %H:%M %z")))
return 'go build -ldflags "{}"'.format(" ".join(buildFlag))
validOSArch = {
"darwin": ["386", "amd64", "arm", "arm64"],
"dragonfly": ["amd64"],
"freebsd": ["386", "amd64", "arm"],
"linux": ["386", "amd64", "arm", "arm64", "ppc64", "ppc64le"],
"netbsd": ["386", "amd64", "arm"],
"openbsd": ["386", "amd64", "arm"],
"plan9": ["386", "amd64"],
"solaris": ["amd64"],
"windows": ["386", "amd64"],
}
# Check if GOOS and GOARCH is valid combinations.
# Learn more at https://golang.org/doc/install/source
def isValidOSArch(goos, goarch):
os = validOSArch.get(goos)
if os is None:
return False
if goarch in os:
return True
return False
# Build binary for current OS and architecture
def build():
cmd = "{} {}".format(buildCmd(), mainFile)
if subprocess.call(cmd, shell = True) == 0:
print("Build finished.")
# Build binaries for specify OS and architecture
# pairs: valid GOOS/GOARCH pairs
# filePrefix: filename prefix used in output binaries
def buildPlatform(pairs, filePrefix):
cmd = buildCmd()
for p in pairs:
filename = "{}_{}_{}".format(filePrefix, p[0], p[1])
if p[0] == "windows":
filename += ".exe"
c = "GOOS={} GOARCH={} {} -o {} {}".format(p[0], p[1], cmd, filename, mainFile)
if subprocess.call(c, shell = True) == 0:
print("Build finished: {}".format(filename))
else:
# build error
return
print("All build finished.")
usage = """Go binary builder
Usage:
./build.py [GOOS/GOARCH pairs...]
./build.py [-h, --help]
Examples:
1. Build binary for current OS and architecture:
./build.py
2. Build binary for windows/386:
./build.py windows/386
3. Build binaries for windows/386 and linux/386:
./build.py windows/386 linux/386
4. Build binaries for linux/386 and linux/amd64:
./build.py linux/386 linux/amd64"""
errmsg = "Arguments are not valid GOOS/GOARCH pairs, use -h for help"
def main():
if len(sys.argv) <= 1:
build()
return
validPairs = []
for arg in sys.argv[1:]:
arg = arg.lower()
if arg in ["-h", "--help"]:
print(usage)
return
pairs = arg.split("/")
if len(pairs) != 2:
sys.exit(errmsg)
if isValidOSArch(pairs[0], pairs[1]) is False:
sys.exit(errmsg)
validPairs.append(pairs)
buildPlatform(validPairs, "ran")
if __name__ == "__main__":
main()