-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
223 lines (179 loc) · 9.96 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
import os
import errno
import warnings
import argparse
import subprocess
from colored import stylize
from humanize import naturalsize
from multiprocessing import cpu_count
from packaging import version
import re
import glob
import shutil
def parse(project_name):
parser = argparse.ArgumentParser(description='CMake Project Builder for {}'.format(project_name))
parser.add_argument('-a', '--arch', type=str, help='Choose microarchitecture',
default='haswell', choices=['core2', 'nehalem', 'sandybridge', 'haswell', 'native'])
parser.add_argument('-b', '--build-type', type=str, help='Build type',
default='Release', choices=['Release', 'Debug', 'RelWithDebInfo'])
parser.add_argument('--clear-cmake', action='store_true', help='Delete CMake build directory before build')
parser.add_argument('-c', '--clear-cache', action='store_true', help='Delete CMakeCache.txt before build')
parser.add_argument('-d', '--dry-run', action='store_true', help='Dry run makes no changes')
parser.add_argument('-e', '--examples', action='store_true', help='Build examples')
parser.add_argument('-i', '--install-prefix', type=str, help='Install Prefix', default='install')
parser.add_argument('-j', '--make-threads', type=int, help='Make Threads', default=cpu_count())
parser.add_argument('-G', '--generator', type=str, help='CMake Generator', default=None,
choices=[None, 'Ninja', 'Unix Makefiles'])
parser.add_argument('-p', '--package-manager', type=str, help='Package Manager', default='conan',
choices=['find', 'cmake', 'find-or-cmake', 'conan'])
parser.add_argument('-s', '--shared', action='store_true', help='Shared library linkage')
parser.add_argument('-t', '--target', type=str, help='Build Target', default=None)
parser.add_argument('-v', '--verbose', action='store_true', help='Verbose build')
parser.add_argument('-X', '--cmake-args', action='append', type=str, help='Extra CMake arguments', default=[])
parser.add_argument('--mkl', action='store_true', help='Use Intel Math Kernel Library as instead of OpenBLAS')
parser.add_argument('--coverage', action='store_true', help='Enable test Coverage')
parser.add_argument('--lto', action='store_true', help='Enable Link Time Optimization')
parser.add_argument('--pch', action='store_true', help='Enable Precompiled Headers')
parser.add_argument('--ccache', action='store_true', help='Enable ccache to speed up repeated builds')
parser.add_argument('--asan', action='store_true', help='Enable Address Sanitizer')
parser.add_argument('--test', action='store_true', help='Enable CTest. Build and run tests')
parser.add_argument('--loglevel', type=str, help='CMake Log Level', default=None,
choices=['TRACE', 'DEBUG', 'VERBOSE', 'STATUS', 'NOTICE'])
parser.add_argument('--build-dir', type=str, help='CMake build directory', default='build')
parser.add_argument('--default-kraken', action='store_true', help='Set defaults for kraken cluster')
parser.add_argument('--default-tetralith', action='store_true', help='Set defaults for tetralith cluster')
parser.add_argument('--disable-color', action='store_true', help='Disable color output')
parser.add_argument('--debug', action='store_true', help='Debug this builder script')
args = parser.parse_args()
if args.default_kraken:
parser.set_defaults(mkl=True, lto=True, pch=True, ccache=True, make_threads=32, generator='Ninja', package_manager='conan', arch='haswell')
args = parser.parse_args()
if args.default_tetralith:
parser.set_defaults(mkl=True, lto=True, pch=True, ccache=True, make_threads=16, generator='Ninja', package_manager='conan', arch='native')
args = parser.parse_args()
return args
def assert_cmake_version(cmake_min_version):
cmake_which_cmd = subprocess.Popen(['which', 'cmake'], shell=False, stdout=subprocess.PIPE, encoding='utf-8')
which,_ = cmake_which_cmd.communicate()
cmake_which_cmd.stdout.close()
cmake_version_cmd = subprocess.Popen(['cmake', '--version'], shell=False, stdout=subprocess.PIPE, encoding='utf-8')
out, err = cmake_version_cmd.communicate()
cmake_version_cmd.stdout.close()
if not err:
cmake_version = version.parse(re.search(r'version \s*([\d.]+)', out).group(1))
if cmake_version < cmake_min_version:
raise ValueError(
'CMake version {} is lower than the minimum required: {}'.format(cmake_version, cmake_min_version))
print('Found CMake version {} at {}'.format(cmake_version, which))
def silentrm(filename):
try:
os.remove(filename)
except OSError as e:
if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
raise # re-raise exception if a different error occurred
def silentrmtree(path):
try:
shutil.rmtree(path)
except:
print("Directory could not be removed:", path)
pass
def generate_cmake_commands(project_name, args):
cmake_cfg = ['cmake']
cmake_bld = ['cmake']
cmake_tst = []
cmake_env = os.environ.copy() # Add environment variables here
cmake_c_flags_init = []
cmake_cxx_flags_init = []
cmake_cfg.extend(['-S', '.']) # Path to source
cmake_cfg.extend(['-B', '{}/{}'.format(args.build_dir, args.build_type)]) # Path to build
cmake_bld.extend(['--build', '{}/{}'.format(args.build_dir, args.build_type)])
if not args.disable_color:
cmake_env['CLICOLOR'] = str(1)
cmake_env['CLICOLOR_FORCE'] = str(1)
cmake_c_flags_init.extend(['-fdiagnostics-color=always'])
cmake_cxx_flags_init.extend(['-fdiagnostics-color=always'])
if args.arch:
cmake_cfg.extend(['-D{}_MICROARCH:STRING={}'.format(project_name.upper(), args.arch)])
if args.build_type:
cmake_cfg.extend(['-DCMAKE_BUILD_TYPE:STRING={}'.format(args.build_type)])
if args.clear_cache:
print('Deleting {}/{}/CMakeCache.txt'.format(args.build_dir, args.build_type))
silentrm('{}/{}/CMakeCache.txt'.format(args.build_dir, args.build_type))
conanFiles = glob.glob('{}/{}/conan*'.format(args.build_dir, args.build_type))
for file in conanFiles:
silentrm(file)
if args.clear_cmake:
print('Deleting directory {}/{}'.format(args.build_dir, args.build_type))
silentrmtree('{}/{}'.format(args.build_dir, args.build_type))
if args.install_prefix:
cmake_cfg.extend(['-DCMAKE_INSTALL_PREFIX:PATH={}'.format(args.install_prefix)])
if args.make_threads:
cmake_env['CMAKE_BUILD_PARALLEL_LEVEL'] = str(args.make_threads)
cmake_bld.extend(['--parallel', str(args.make_threads)])
if args.generator:
cmake_env['CMAKE_GENERATOR'] = args.generator
cmake_cfg.extend(['-G', args.generator])
if args.package_manager:
cmake_cfg.extend(['-D{}_PACKAGE_MANAGER:STRING={}'.format(project_name.upper(), args.package_manager)])
if args.shared:
cmake_cfg.extend(['-DBUILD_SHARED_LIBS:BOOL=ON'])
if args.target:
cmake_bld.extend(['--target', args.target])
if args.verbose:
cmake_cfg.extend(['-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON'])
if args.cmake_args:
cmake_cfg.extend(args.cmake_args)
if args.mkl:
cmake_cfg.extend(['-D{}_ENABLE_MKL:BOOL=ON'.format(project_name.upper())])
if args.coverage:
cmake_cfg.extend(['-D{}_ENABLE_COVERAGE:BOOL=ON'.format(project_name.upper())])
if args.lto:
cmake_cfg.extend(['-D{}_ENABLE_LTO:BOOL=ON'.format(project_name.upper())])
if args.pch:
cmake_cfg.extend(['-D{}_ENABLE_PCH:BOOL=ON'.format(project_name.upper())])
if args.ccache:
cmake_cfg.extend(['-D{}_ENABLE_CCACHE:BOOL=ON'.format(project_name.upper())])
if args.asan:
cmake_cfg.extend(['-D{}_ENABLE_ASAN:BOOL=ON'.format(project_name.upper())])
if cmake_c_flags_init:
cmake_cfg.extend(['-DCMAKE_C_FLAGS_INIT:STRING={}'.format(' '.join(cmake_c_flags_init))])
if cmake_cxx_flags_init:
cmake_cfg.extend(['-DCMAKE_CXX_FLAGS_INIT:STRING={}'.format(' '.join(cmake_cxx_flags_init))])
if args.test:
cmake_cfg.extend(['-D{}_ENABLE_TESTS:BOOL=ON'.format(project_name.upper())])
cmake_tst.extend(['ctest', '--output-on-failure', '--build-config', args.build_type,
'--test-dir', '{}/{}'.format(args.build_dir, args.build_type)])
if args.target and 'test' in args.target:
cmake_tst.extend(['--build-target', args.target, '-R', args.target]) # Pattern match only the built target
else:
cmake_tst.extend(['-R', project_name.lower()]) # Pattern match test targets containing the project name
if args.examples:
cmake_cfg.extend(['-D{}_BUILD_EXAMPLES:BOOL=ON'.format(project_name.upper())])
if args.loglevel:
cmake_cfg.extend(['--loglevel={}'.format(args.loglevel)])
if args.debug:
print(' '.join(cmake_cfg))
print(' '.join(cmake_bld))
print(' '.join(cmake_tst))
print(' '.join(cmake_env))
return cmake_cfg, cmake_bld, cmake_tst, cmake_env
def run(cmd,env):
if len(cmd) == 0:
return
print("Running command: ", ' '.join(cmd))
with subprocess.Popen(cmd,bufsize=1, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding='utf-8',env=env) as p:
for line in p.stdout:
print(line, end='') # process line here
if p.returncode != 0:
raise subprocess.CalledProcessError(p.returncode, ' '.join(p.args))
def main():
project_name = 'h5du'
cmake_min_version = version.parse("3.15")
assert_cmake_version(cmake_min_version=cmake_min_version)
args = parse(project_name=project_name)
cmake_cfg_cmd, cmake_bld_cmd, cmake_tst_cmd, cmake_env = generate_cmake_commands(project_name=project_name,
args=args)
run(cmake_cfg_cmd, cmake_env)
run(cmake_bld_cmd, cmake_env)
if __name__ == "__main__":
main()