forked from nfrechette/sjson-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake.py
More file actions
249 lines (202 loc) · 7.58 KB
/
make.py
File metadata and controls
249 lines (202 loc) · 7.58 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
import argparse
import os
import platform
import queue
import shutil
import subprocess
import sys
import threading
import time
import zipfile
def parse_argv():
parser = argparse.ArgumentParser(add_help=False)
actions = parser.add_argument_group(title='Actions', description='If no action is specified, on Windows, OS X, and Linux the solution/make files are generated. Multiple actions can be used simultaneously.')
actions.add_argument('-build', action='store_true')
actions.add_argument('-clean', action='store_true')
actions.add_argument('-unit_test', action='store_true')
target = parser.add_argument_group(title='Target')
target.add_argument('-compiler', choices=['vs2015', 'vs2017', 'android', 'clang4', 'clang5', 'clang6', 'gcc5', 'gcc6', 'gcc7', 'gcc8', 'osx', 'ios'], help='Defaults to the host system\'s default compiler')
target.add_argument('-config', choices=['Debug', 'Release'], type=str.capitalize)
target.add_argument('-cpu', choices=['x86', 'x64', 'arm64'], help='Only supported for Windows, OS X, and Linux; defaults to the host system\'s architecture')
misc = parser.add_argument_group(title='Miscellaneous')
misc.add_argument('-num_threads', help='No. to use while compiling')
misc.add_argument('-tests_matching', help='Only run tests whose names match this regex')
misc.add_argument('-help', action='help', help='Display this usage information')
parser.set_defaults(build=False, clean=False, unit_test=False, compiler=None, config='Release', cpu='x64', num_threads=4, tests_matching='')
args = parser.parse_args()
# Sanitize and validate our options
if args.compiler == 'android':
args.cpu = 'armv7-a'
if not platform.system() == 'Windows':
print('Android is only supported on Windows')
sys.exit(1)
if args.compiler == 'ios':
args.cpu = 'arm64'
if not platform.system() == 'Darwin':
print('iOS is only supported on OS X')
sys.exit(1)
if args.cpu == 'arm64':
if not args.compiler in ['vs2017', 'ios']:
print('ARM64 is only supported with VS2017 and iOS')
sys.exit(1)
return args
def get_cmake_exes():
if platform.system() == 'Windows':
return ('cmake.exe', 'ctest.exe')
else:
return ('cmake', 'ctest')
def get_generator(compiler, cpu):
if compiler == None:
return None
if platform.system() == 'Windows':
if compiler == 'vs2015':
if cpu == 'x86':
return 'Visual Studio 14'
elif cpu == 'x64':
return 'Visual Studio 14 Win64'
elif compiler == 'vs2017':
if cpu == 'x86':
return 'Visual Studio 15'
elif cpu == 'x64':
return 'Visual Studio 15 Win64'
elif cpu == 'arm64':
# VS2017 ARM/ARM64 support only works with cmake 3.13 and up and the architecture must be specified with
# the -A cmake switch
return 'Visual Studio 15 2017'
elif compiler == 'android':
return 'Visual Studio 14'
elif platform.system() == 'Darwin':
if compiler == 'osx' or compiler == 'ios':
return 'Xcode'
else:
return 'Unix Makefiles'
print('Unknown compiler: {}'.format(compiler))
print('See help with: python make.py -help')
sys.exit(1)
def get_architecture(compiler, cpu):
if compiler == None:
return None
if platform.system() == 'Windows':
if compiler == 'vs2017':
if cpu == 'arm64':
return 'ARM64'
# This compiler/cpu pair does not need the architecture switch
return None
def get_toolchain(compiler):
if platform.system() == 'Windows' and compiler == 'android':
return 'Toolchain-Android.cmake'
elif platform.system() == 'Darwin' and compiler == 'ios':
return 'Toolchain-iOS.cmake'
# No toolchain
return None
def set_compiler_env(compiler, args):
if platform.system() == 'Linux':
os.environ['MAKEFLAGS'] = '-j{}'.format(args.num_threads)
if compiler == 'clang4':
os.environ['CC'] = 'clang-4.0'
os.environ['CXX'] = 'clang++-4.0'
elif compiler == 'clang5':
os.environ['CC'] = 'clang-5.0'
os.environ['CXX'] = 'clang++-5.0'
elif compiler == 'clang6':
os.environ['CC'] = 'clang-6.0'
os.environ['CXX'] = 'clang++-6.0'
elif compiler == 'gcc5':
os.environ['CC'] = 'gcc-5'
os.environ['CXX'] = 'g++-5'
elif compiler == 'gcc6':
os.environ['CC'] = 'gcc-6'
os.environ['CXX'] = 'g++-6'
elif compiler == 'gcc7':
os.environ['CC'] = 'gcc-7'
os.environ['CXX'] = 'g++-7'
elif compiler == 'gcc8':
os.environ['CC'] = 'gcc-8'
os.environ['CXX'] = 'g++-8'
else:
print('Unknown compiler: {}'.format(compiler))
sys.exit(1)
def do_generate_solution(cmake_exe, build_dir, cmake_script_dir, args):
compiler = args.compiler
cpu = args.cpu
config = args.config
if compiler:
set_compiler_env(compiler, args)
extra_switches = ['--no-warn-unused-cli']
extra_switches.append('-DCPU_INSTRUCTION_SET:STRING={}'.format(cpu))
if not platform.system() == 'Windows' and not platform.system() == 'Darwin':
extra_switches.append('-DCMAKE_BUILD_TYPE={}'.format(config.upper()))
toolchain = get_toolchain(compiler)
if toolchain:
extra_switches.append('-DCMAKE_TOOLCHAIN_FILE={}'.format(os.path.join(cmake_script_dir, toolchain)))
# Generate IDE solution
print('Generating build files ...')
cmake_cmd = '"{}" .. -DCMAKE_INSTALL_PREFIX="{}" {}'.format(cmake_exe, build_dir, ' '.join(extra_switches))
cmake_generator = get_generator(compiler, cpu)
if not cmake_generator:
print('Using default generator')
else:
print('Using generator: {}'.format(cmake_generator))
cmake_cmd += ' -G "{}"'.format(cmake_generator)
cmake_arch = get_architecture(compiler, cpu)
if cmake_arch:
print('Using architecture: {}'.format(cmake_arch))
cmake_cmd += ' -A {}'.format(cmake_arch)
result = subprocess.call(cmake_cmd, shell=True)
if result != 0:
sys.exit(result)
def do_build(cmake_exe, args):
config = args.config
print('Building ...')
cmake_cmd = '"{}" --build .'.format(cmake_exe)
if platform.system() == 'Windows':
if args.compiler == 'android':
cmake_cmd += ' --config {}'.format(config)
else:
cmake_cmd += ' --config {} --target INSTALL'.format(config)
elif platform.system() == 'Darwin':
if args.compiler == 'ios':
cmake_cmd += ' --config {}'.format(config)
else:
cmake_cmd += ' --config {} --target install'.format(config)
else:
cmake_cmd += ' --target install'
result = subprocess.call(cmake_cmd, shell=True)
if result != 0:
sys.exit(result)
def do_tests(ctest_exe, args):
print('Running unit tests ...')
ctest_cmd = '"{}" --output-on-failure'.format(ctest_exe)
if platform.system() == 'Windows' or platform.system() == 'Darwin':
ctest_cmd += ' -C {}'.format(args.config)
if args.tests_matching:
ctest_cmd += ' --tests-regex {}'.format(args.tests_matching)
result = subprocess.call(ctest_cmd, shell=True)
if result != 0:
sys.exit(result)
if __name__ == "__main__":
args = parse_argv()
cmake_exe, ctest_exe = get_cmake_exes()
# Set the SJSON_CPP_CMAKE_HOME environment variable to point to CMake
# otherwise we assume it is already in the user PATH
if 'SJSON_CPP_CMAKE_HOME' in os.environ:
cmake_home = os.environ['SJSON_CPP_CMAKE_HOME']
cmake_exe = os.path.join(cmake_home, 'bin', cmake_exe)
ctest_exe = os.path.join(cmake_home, 'bin', ctest_exe)
build_dir = os.path.join(os.getcwd(), 'build')
cmake_script_dir = os.path.join(os.getcwd(), 'cmake')
if args.clean and os.path.exists(build_dir):
print('Cleaning previous build ...')
shutil.rmtree(build_dir)
if not os.path.exists(build_dir):
os.makedirs(build_dir)
os.chdir(build_dir)
print('Using config: {}'.format(args.config))
print('Using cpu: {}'.format(args.cpu))
if not args.compiler == None:
print('Using compiler: {}'.format(args.compiler))
do_generate_solution(cmake_exe, build_dir, cmake_script_dir, args)
if args.build:
do_build(cmake_exe, args)
if args.unit_test:
do_tests(ctest_exe, args)