forked from codezri/buildzri
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbz.py
More file actions
executable file
·279 lines (216 loc) · 6.93 KB
/
Copy pathbz.py
File metadata and controls
executable file
·279 lines (216 loc) · 6.93 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python3
import sys
import os
import subprocess
import platform
import json
import glob
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', action='store_true')
parser.add_argument('--target_arch', choices=['x64', 'arm64', 'armhf'])
args = parser.parse_args()
C = {}
RSP_FILENAME = 'buildzri.rsp'
BZ_VERSION = '1.0.0'
BZ_CONFIG_FILE = 'buildzri.config.json'
BZ_OS = platform.system().lower()
BZ_ISLINUX = BZ_OS == 'linux'
BZ_ISDARWIN = BZ_OS == 'darwin'
BZ_ISWIN = BZ_OS == 'windows'
def get_arch(short_names = True):
arch = platform.machine().lower()
if short_names and (arch in ['x86_64', 'amd64']):
arch = 'x64'
if short_names and (arch in ['aarch64']):
arch = 'arm64'
if short_names and (arch in ['armv7l']):
arch = 'armhf'
return arch
def get_target_arch():
if args.target_arch != None:
return args.target_arch
return get_arch()
def get_os_shortname():
osnames = {'linux': 'linux', 'windows': 'win', 'darwin': 'mac'}
return osnames[BZ_OS]
def get_compiler():
compilers = {'linux': 'g++', 'windows': 'cl', 'darwin': 'c++'}
return compilers[BZ_OS] + ' '
def get_latest_commit():
return subprocess.getoutput('git rev-parse HEAD').strip()
def apply_template_vars(text):
return text\
.replace('${BZ_OS}', get_os_shortname()) \
.replace('${BZ_VERSION}', C['version']) \
.replace('${BZ_ARCH}', get_arch()) \
.replace('${BZ_ARCHL}', get_arch(short_names = False)) \
.replace('${BZ_TARGET_ARCH}', get_target_arch()) \
.replace('${BZ_COMMIT}', get_latest_commit())
def get_target_name():
out_file = C['output']
out_file = apply_template_vars(out_file)
if BZ_ISWIN:
out_file += '.exe'
return out_file
def configure_vs_tools():
vsw_path = ''
vsw_path_f = '%s\\Microsoft Visual Studio\\Installer\\vswhere.exe'
for prog_path in [os.getenv('ProgramFiles(x86)'), os.getenv('ProgramFiles')]:
path = vsw_path_f % prog_path
if os.path.exists(path):
vsw_path = path
break
if vsw_path == '':
print('ERR: Unable to find vswhere.exe')
sys.exit(1)
if args.verbose:
print('vswhere.exe path: %s' % vsw_path)
vs_paths = subprocess\
.getoutput('"%s" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath' % vsw_path) \
.strip().split('\r\n')
vs_path = vs_paths[-1]
vs_devcmd_file = '%s\\Common7\\Tools\\vsdevcmd.bat' % vs_path
if not os.path.exists(vs_devcmd_file):
print('ERR: Unable to find VS dev command-line tools')
sys.exit(1)
return '"%s" -host_arch=%s -arch=%s' % (vs_devcmd_file, get_arch(), get_target_arch())
def get_std():
if 'std' not in C:
return ''
std_prefix = '--std='
if BZ_ISWIN:
std_prefix = '/std:'
return '%s%s ' % (std_prefix, C['std'])
def get_source_files():
file_defs = ['*', BZ_OS]
files = ''
for file_def in file_defs:
if file_def not in C['source']:
continue
for entry in C['source'][file_def]:
glob_files = glob.glob(entry, recursive = True)
if len(glob_files) > 0:
files += ' '.join(glob_files) + ' '
return files
def get_includes():
if 'include' not in C:
return ''
inc_defs = ['*', BZ_OS]
incs = ''
inc_prefix = '-I'
if BZ_ISWIN:
inc_prefix = '/I'
for inc_def in inc_defs:
if inc_def not in C['include']:
continue
for entry in C['include'][inc_def]:
incs += '%s %s ' % (inc_prefix, entry)
return incs
def get_definitions():
if 'definitions' not in C:
return ''
defs = ''
def_defs = ['*', BZ_OS]
def_prefix = '-D'
if BZ_ISWIN:
def_prefix = '/D'
for def_def in def_defs:
if def_def not in C['definitions']:
continue
for entry in C['definitions'][def_def]:
defs += '%s%s ' % (def_prefix, apply_template_vars(entry))
return defs
def get_target():
if 'output' not in C:
return ''
out_prefix = '-o '
if BZ_ISWIN:
out_prefix = '/OUT:'
out_file = get_target_name()
out_path = os.path.dirname(out_file)
if out_path != '' and not os.path.isdir(out_path):
os.makedirs(out_path, exist_ok = True)
if os.path.exists(out_file):
os.remove(out_file)
return '%s%s ' % (out_prefix, out_file)
def get_options():
if 'options' not in C:
return ''
opt_defs = ['*', BZ_OS]
opts = ''
for opt_def in opt_defs:
if opt_def not in C['options']:
continue
for entry in C['options'][opt_def]:
opts += '%s ' % apply_template_vars(entry)
return opts
def get_rsp_file():
rsp_content = ''
rsp_content += get_includes()
rsp_content += get_definitions()
rsp_content += get_source_files()
with open(RSP_FILENAME, 'w') as rsp_file:
rsp_file.write(rsp_content)
return '@%s ' % RSP_FILENAME
def clean_rsp_file():
try:
os.remove(RSP_FILENAME)
except OSError:
pass
def build_compiler_cmd():
arch = get_arch()
target_arch = get_target_arch()
cmd = ''
if BZ_ISWIN:
cmd += '%s && ' % configure_vs_tools()
cmd += get_compiler()
cmd += get_std()
cmd += get_rsp_file()
cmd += get_options()
cmd += get_target()
if BZ_ISDARWIN:
if target_arch == 'x64':
cmd += '-arch x86_64 '
elif target_arch == 'arm64':
cmd += '-arch arm64 '
else:
print('ERR: Unsupported target architecture for macOS cross-compile: %s' % target_arch)
sys.exit(1)
else:
if target_arch != arch:
print('ERR: Unable to cross-compile: target %s host %s' % (target_arch, arch))
sys.exit(1)
return cmd
def compile(cmd):
print('Compiling %s...' % C['name'])
if args.verbose:
print('Running command: %s' % cmd)
print(os.linesep + '@%s contents:' % RSP_FILENAME)
with open(RSP_FILENAME) as rsp_file:
print(rsp_file.read())
exit_code = subprocess.call(cmd, shell = True)
clean_rsp_file()
msg = ''
if exit_code == 0:
msg = 'OK: %s compiled into %s' % (C['name'], get_target_name())
else:
msg = 'ERR: Unable to compile %s' % C['name']
print(msg)
sys.exit(exit_code)
def print_ascii_art():
print(r'''
____ _ _ _ ______ _
| _ \ (_) | | |___ / (_)
| |_) |_ _ _| | __| | / / _ __ _
| _ <| | | | | |/ _` | / / | '__| |
| |_) | |_| | | | (_| |/ /__| | | |
|____/ \__,_|_|_|\__,_/_____|_| |_|
BuildZri v%s - A minimal build automation tool for C++
''' % BZ_VERSION)
if __name__ == '__main__':
with open(BZ_CONFIG_FILE) as configFile:
print_ascii_art()
C = json.loads(configFile.read())
cmd = build_compiler_cmd()
compile(cmd)