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
Latest commit
279 lines (222 loc) · 7.17 KB
/
Copy pathmake.py
File metadata and controls
279 lines (222 loc) · 7.17 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
importos
importplatform
importqueue
importshutil
importsubprocess
importsys
importthreading
importtime
importzipfile
defparse_argv():
options= {}
options['build'] =False
options['clean'] =False
options['unit_test'] =False
options['compiler'] =None
options['config'] ='Release'
options['cpu'] ='x64'
options['num_threads'] =4
foriinrange(1, len(sys.argv)):
value=sys.argv[i]
value_upper=value.upper()
ifvalue=='-build':
options['build'] =True
ifvalue=='-clean':
options['clean'] =True
ifvalue=='-unit_test':
options['unit_test'] =True
# TODO: Refactor to use the form: -compiler=vs2015
ifvalue=='-vs2015':
options['compiler'] ='vs2015'
ifvalue=='-vs2017':
options['compiler'] ='vs2017'
ifvalue=='-android':
ifnotplatform.system() =='Windows':
print('Android is only supported on Windows')
sys.exit(1)
options['compiler'] ='android'
ifvalue=='-clang4':
options['compiler'] ='clang4'
ifvalue=='-clang5':
options['compiler'] ='clang5'
ifvalue=='-clang6':
options['compiler'] ='clang6'
ifvalue=='-gcc5':
options['compiler'] ='gcc5'
ifvalue=='-gcc6':
options['compiler'] ='gcc6'
ifvalue=='-gcc7':
options['compiler'] ='gcc7'
ifvalue=='-gcc8':
options['compiler'] ='gcc8'
ifvalue=='-osx':
options['compiler'] ='osx'
ifvalue=='-ios':
options['compiler'] ='ios'
# TODO: Refactor to use the form: -config=Release
ifvalue_upper=='-DEBUG':
options['config'] ='Debug'
ifvalue_upper=='-RELEASE':
options['config'] ='Release'
# TODO: Refactor to use the form: -cpu=x86
ifvalue=='-x86':
options['cpu'] ='x86'
ifvalue=='-x64':
options['cpu'] ='x64'
# Sanitize and validate our options
ifoptions['compiler'] =='android':
options['cpu'] ='armv7-a'
ifnotplatform.system() =='Windows':
print('Android is only supported on Windows')
sys.exit(1)
ifoptions['compiler'] =='ios':
options['cpu'] ='arm64'
ifnotplatform.system() =='Darwin':
print('iOS is only supported on OS X')
sys.exit(1)
returnoptions
defget_cmake_exes():
ifplatform.system() =='Windows':
return ('cmake.exe', 'ctest.exe')
else:
return ('cmake', 'ctest')
defget_generator(compiler, cpu):
ifcompiler==None:
returnNone
ifplatform.system() =='Windows':
ifcompiler=='vs2015':
ifcpu=='x86':
return'Visual Studio 14'
else:
return'Visual Studio 14 Win64'
elifcompiler=='vs2017':
ifcpu=='x86':
return'Visual Studio 15'
else:
return'Visual Studio 15 Win64'
elifcompiler=='android':
return'Visual Studio 14'
elifplatform.system() =='Darwin':
ifcompiler=='osx'orcompiler=='ios':
return'Xcode'
else:
return'Unix Makefiles'
print('Unknown compiler: {}'.format(compiler))
sys.exit(1)
defget_toolchain(compiler):
ifplatform.system() =='Windows'andcompiler=='android':
return'Toolchain-Android.cmake'
elifplatform.system() =='Darwin'andcompiler=='ios':
return'Toolchain-iOS.cmake'
# No toolchain
returnNone
defset_compiler_env(compiler, options):
ifplatform.system() =='Linux':
os.environ['MAKEFLAGS'] ='-j{}'.format(options['num_threads'])
ifcompiler=='clang4':
os.environ['CC'] ='clang-4.0'
os.environ['CXX'] ='clang++-4.0'
elifcompiler=='clang5':
os.environ['CC'] ='clang-5.0'
os.environ['CXX'] ='clang++-5.0'
elifcompiler=='clang6':
os.environ['CC'] ='clang-6.0'
os.environ['CXX'] ='clang++-6.0'
elifcompiler=='gcc5':
os.environ['CC'] ='gcc-5'
os.environ['CXX'] ='g++-5'
elifcompiler=='gcc6':
os.environ['CC'] ='gcc-6'
os.environ['CXX'] ='g++-6'
elifcompiler=='gcc7':
os.environ['CC'] ='gcc-7'
os.environ['CXX'] ='g++-7'
elifcompiler=='gcc8':
os.environ['CC'] ='gcc-8'
os.environ['CXX'] ='g++-8'
else:
print('Unknown compiler: {}'.format(compiler))
sys.exit(1)
defdo_generate_solution(cmake_exe, build_dir, cmake_script_dir, options):
compiler=options['compiler']
cpu=options['cpu']
config=options['config']
ifnotcompiler==None:
set_compiler_env(compiler, options)
extra_switches= ['--no-warn-unused-cli']
ifnotplatform.system() =='Windows':
extra_switches.append('-DCPU_INSTRUCTION_SET:STRING={}'.format(cpu))
ifnotplatform.system() =='Windows'andnotplatform.system() =='Darwin':
extra_switches.append('-DCMAKE_BUILD_TYPE={}'.format(config.upper()))
toolchain=get_toolchain(compiler)
ifnottoolchain==None:
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)
ifcmake_generator==None:
print('Using default generator')
else:
print('Using generator: {}'.format(cmake_generator))
cmake_cmd+=' -G "{}"'.format(cmake_generator)
result=subprocess.call(cmake_cmd, shell=True)
ifresult!=0:
sys.exit(result)
defdo_build(cmake_exe, options):
config=options['config']
print('Building ...')
cmake_cmd='"{}" --build .'.format(cmake_exe)
ifplatform.system() =='Windows':
ifoptions['compiler'] =='android':
cmake_cmd+=' --config {}'.format(config)
else:
cmake_cmd+=' --config {} --target INSTALL'.format(config)
elifplatform.system() =='Darwin':
ifoptions['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)
ifresult!=0:
sys.exit(result)
defdo_tests(ctest_exe, options):
config=options['config']
print('Running unit tests ...')
ctest_cmd='"{}" --output-on-failure'.format(ctest_exe)
ifplatform.system() =='Windows'orplatform.system() =='Darwin':
ctest_cmd+=' -C {}'.format(config)
result=subprocess.call(ctest_cmd, shell=True)
ifresult!=0:
sys.exit(result)
if__name__=="__main__":
options=parse_argv()
cmake_exe, ctest_exe=get_cmake_exes()
compiler=options['compiler']
cpu=options['cpu']
config=options['config']
# 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'inos.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')
ifoptions['clean'] andos.path.exists(build_dir):
print('Cleaning previous build ...')
shutil.rmtree(build_dir)
ifnotos.path.exists(build_dir):
os.makedirs(build_dir)
os.chdir(build_dir)
print('Using config: {}'.format(config))
print('Using cpu: {}'.format(cpu))
ifnotcompiler==None:
print('Using compiler: {}'.format(compiler))
do_generate_solution(cmake_exe, build_dir, cmake_script_dir, options)
ifoptions['build']:
do_build(cmake_exe, options)
ifoptions['unit_test']:
do_tests(ctest_exe, options)