-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
313 lines (249 loc) · 9.3 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import argparse
import collections
import datetime
import inspect
import json
import logging
import os
import platform
import re
import shutil
import subprocess
vcpkg_conf = collections.namedtuple(
'vcpkg_conf',
(
'repository',
'reference',
'baseline'
)
)
cli_args = collections.namedtuple(
'cli_args',
(
'vcpkg_root_dir',
'vcpkg_bootstrap',
'vcpkg_triplet',
'cmake_build_dir',
'cmake_generate',
'cmake_build',
'cmake_config'
)
)
def setup_logger():
class ColoredFormatter(logging.Formatter):
class Color:
INFO = "\033[94m" # Light Blue
WARNING = "\033[93m" # Yellow
ERROR = "\033[91m" # Red
RESET = "\033[0m" # Reset to default
def format(self, record):
if record.levelno == logging.INFO:
record.msg = f"{self.Color.INFO}{record.msg}{self.Color.RESET}"
elif record.levelno == logging.WARNING:
record.msg = f"{self.Color.WARNING}{record.msg}{self.Color.RESET}"
elif record.levelno == logging.ERROR:
record.msg = f"{self.Color.ERROR}{record.msg}{self.Color.RESET}"
return super().format(record)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
formatter = ColoredFormatter('%(asctime)s - %(lineno)d - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
def default_triplet() -> str:
arch = {
'x86': 'x86',
'i386': 'x86',
'AMD64': 'x64',
'x86_64': 'x64',
'arm': 'arm',
'armv7l': 'arm',
'arm64': 'arm64',
'aarch64': 'arm64',
}[platform.machine()]
os_name = platform.system()
if os_name == 'Windows':
return f'{arch}-windows-static'
elif os_name == 'Darwin':
return f'{arch}-osx'
elif os_name == 'Linux':
return f'{arch}-linux'
else:
logging.error('unsupport os: %s', os_name)
return ''
def parse_cli_args() -> cli_args:
logging.info(inspect.currentframe().f_code.co_name)
invalid_result = cli_args('', '', '', '', '', '', '')
arg_parser = argparse.ArgumentParser(description="vcpkg bootstrap/cmake generate/cmake build")
default_vcpkg_root_dir = os.path.abspath('./build/vcpkg')
arg_parser.add_argument(
'--vcpkg-root-dir',
type=str,
default=default_vcpkg_root_dir,
help=f'set vcpkg root dir path (default {default_vcpkg_root_dir})'
)
default_triplet_ = default_triplet()
if default_triplet_ == '':
return invalid_result
arg_parser.add_argument(
'--vcpkg-triplet',
type=str,
default=default_triplet_,
help=f'set vcpkg triplet (default {default_triplet_})'
)
arg_parser.add_argument(
'--vcpkg-bootstrap',
type=bool,
default=False,
help=f'fetch vcpkg and bootstrap it (default False)'
)
default_build_dir = 'build'
arg_parser.add_argument(
'--cmake-build-dir',
type=str,
default=default_build_dir,
help=f'set cmake build dir (default {default_build_dir})'
)
arg_parser.add_argument(
'--cmake-generate',
type=bool,
default=False,
help=f'cmake generate build system (default False)'
)
arg_parser.add_argument(
'--cmake-build',
type=bool,
default=False,
help=f'cmake build (default False)'
)
arg_parser.add_argument(
'--cmake-config',
type=str,
default='Debug',
help=f'cmake build config (default Debug)'
)
args = arg_parser.parse_args()
if not args.vcpkg_bootstrap and not args.cmake_generate and not args.cmake_build:
arg_parser.print_help()
return invalid_result
return cli_args(
vcpkg_root_dir=args.vcpkg_root_dir,
vcpkg_bootstrap=args.vcpkg_bootstrap,
vcpkg_triplet=args.vcpkg_triplet,
cmake_build_dir=args.cmake_build_dir,
cmake_generate=args.cmake_generate,
cmake_build=args.cmake_build,
cmake_config=args.cmake_config
)
def parse_vcpkg_conf() -> vcpkg_conf:
logging.info(inspect.currentframe().f_code.co_name)
invalid_conf = vcpkg_conf('', '', '')
conf_file = 'vcpkg-configuration.json'
if not os.path.exists(conf_file) or not os.path.isfile(conf_file):
return invalid_conf
with open(conf_file) as f:
try:
data = json.load(f)
except Exception as e:
logging.error('read vcpkg-configuration.json error, exception: %s', str(e))
return invalid_conf
registry = data.get('default-registry', {})
return vcpkg_conf(
repository=registry.get('repository', ''),
reference=registry.get('reference', 'master'),
baseline=registry.get('baseline', ''),
)
def shell(args, **kwargs):
logging.info('run: %s', ' '.join(args))
return subprocess.run(args=args, **kwargs)
def bootstrap_vcpkg(root_dir: str, conf: vcpkg_conf, triplet: str):
logging.info(inspect.currentframe().f_code.co_name)
cwd = os.getcwd()
if not os.path.exists(root_dir):
os.makedirs(os.path.dirname(root_dir), exist_ok=True)
# clone
shell(args=['git', 'clone', conf.repository, root_dir])
os.chdir(root_dir)
else:
# fetch
os.chdir(root_dir)
shell(args=['git', 'fetch'])
# switch branch
current_branch = shell(args=['git', 'rev-parse', '--abbrev-ref', 'HEAD'], stdout=subprocess.PIPE, text=True).stdout.strip()
if conf.reference != current_branch:
shell(args=['git', 'checkout', conf.reference])
# reset
git_status = shell(args=['git', 'status', '--porcelain'], stdout=subprocess.PIPE, text=True).stdout.strip()
if git_status != '':
shell(args=['git', 'stash'])
shell(args=['git', 'reset', '--hard', conf.baseline])
if git_status != '':
shell(args=['git', 'stash', 'pop'])
# bootstrap
if triplet == 'arm64-osx':
triplet = 'x64-arm64-osx'
current_tool = 'vcpkg'
prebuilt_tool = f'prebuilt/vcpkg.{triplet}'
if platform.system() == 'Windows':
current_tool += '.exe'
prebuilt_tool += '.exe'
prebuilt_tool = prebuilt_tool.replace('-static', '')
prebuilt_tool = prebuilt_tool.replace('-dynamic', '')
if not os.path.exists(current_tool):
if os.path.exists(prebuilt_tool):
shutil.copy(prebuilt_tool, current_tool)
else:
shell(args=['bootstrap-vcpkg.bat' if current_tool.endswith('.exe') else 'bootstrap-vcpkg.sh'])
os.chdir(cwd)
def record_git_info(build_dir: str, triplet: str):
commit_hash = shell(args=['git', 'log', '-1', '--pretty=format:%H'], stdout=subprocess.PIPE, text=True).stdout.strip()
commit_datetime = shell(args=['git', 'log', '-1', '--date=format:%Y-%m-%d %H:%M:%S', '--pretty=%ad'], stdout=subprocess.PIPE, text=True).stdout.strip()
commit_message = shell(args=['git', 'log', '-1', '--pretty=%s'], stdout=subprocess.PIPE, text=True).stdout.strip()
build_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
version_h_path = f'{build_dir}/{triplet}/version.h'
if os.path.exists(version_h_path) and os.path.isfile(version_h_path):
with open(version_h_path) as f:
text = f.read()
text = re.sub('GIT_COMMIT_HASH.*', f'GIT_COMMIT_HASH "{commit_hash}"', text, 1)
text = re.sub('GIT_COMMIT_DATE_TIME.*', f'GIT_COMMIT_DATE_TIME "{commit_datetime}"', text, 1)
text = re.sub('GIT_COMMIT_MESSAGE.*', f'GIT_COMMIT_MESSAGE "{commit_message}"', text, 1)
text = re.sub('BUILD_DATETIME.*', f'BUILD_DATETIME "{build_time}"', text, 1)
with open(version_h_path, mode='w') as f:
f.write(text)
def cmake_generate(build_dir: str, vcpkg_root_dir: str, triplet: str):
logging.info(inspect.currentframe().f_code.co_name)
shell(args=[
'cmake',
'-B', f'{build_dir}/{triplet}',
f'-DCMAKE_TOOLCHAIN_FILE={vcpkg_root_dir}/scripts/buildsystems/vcpkg.cmake',
f'-DVCPKG_TARGET_TRIPLET={triplet}',
f'-DVCPKG_HOST_TRIPLET={triplet}',
])
def cmake_build(build_dir: str, triplet: str, config: str):
logging.info(inspect.currentframe().f_code.co_name)
shell(args=[
'cmake',
'--build', f'{build_dir}/{triplet}',
'--config', config
])
def main():
setup_logger()
args = parse_cli_args()
if args.vcpkg_root_dir == '':
return
logging.info(args)
conf = parse_vcpkg_conf()
if conf.repository == '':
logging.error('invalid vcpkg-configuration.json')
return
logging.info(conf)
if args.vcpkg_bootstrap:
bootstrap_vcpkg(root_dir=args.vcpkg_root_dir, conf=conf, triplet=args.vcpkg_triplet)
if args.cmake_generate:
cmake_generate(build_dir=args.cmake_build_dir, vcpkg_root_dir=args.vcpkg_root_dir, triplet=args.vcpkg_triplet)
record_git_info(build_dir=args.cmake_build_dir, triplet=args.vcpkg_triplet)
if args.cmake_build:
cmake_build(build_dir=args.cmake_build_dir, triplet=args.vcpkg_triplet, config=args.cmake_config)
if __name__ == "__main__":
main()