build/init.py

96 lines
2.5 KiB
Python
Raw Normal View History

2019-02-22 12:46:26 +11:00
#!/usr/bin/env python3
import sys
import os
import itertools
import multiprocessing
2019-02-22 12:46:26 +11:00
OPTIONS = {
# compiler
'gcc': {'vars': {'CMAKE_CXX_COMPILER': 'g++', 'CMAKE_C_COMPILER': 'gcc'}},
2020-04-16 11:46:33 +10:00
'clang': {'vars': {'CMAKE_CXX_COMPILER': 'clang++-10', 'CMAKE_C_COMPILER': 'clang-10'}},
2019-02-22 12:46:26 +11:00
# profile
'debug': {
'vars': {'CMAKE_BUILD_TYPE': 'Debug', 'LTO': 'OFF'},
#'args': ['-G', '"Unix Makefiles"'],
#'build': [ "make", f"-j{multiprocessing.cpu_count()}" ],
},
2019-02-22 12:46:26 +11:00
'release': {'vars': {'CMAKE_BUILD_TYPE': 'Release', 'LTO': 'ON'}},
# sanitizer
'sanitizer': {'vars': {'SANITIZER': 'ON'}, 'require': {'PROFILE': 'release'}},
# wrappers
'iwyu': { 'vars': { 'CMAKE_CXX_INCLUDE_WHAT_YOU_USE': 'include-what-you-use' } },
'tidy': { 'vars': { 'CMAKE_CXX_CLANG_TIDY': ';'.join(['clang-tidy', '--quiet', '-checks=bugprone-*,clang-analyzer-*,performance-*,portability-*,readability-,', '-warnings-as-errors=' ]) } },
# build tools
'make': {
'args': [ '-G', '"Unix Makefiles"' ],
'build': [ "make", f"-j{multiprocessing.cpu_count()}" ],
},
'ninja': {
'args': [ '-G', 'Ninja' ],
'build': [ 'ninja' ]
},
# platforms
'mingw': { 'vars': { 'CMAKE_TOOLCHAIN_FILE': os.path.realpath(os.path.join(os.path.dirname(__file__), 'mingw.toolchain')) }, },
2019-02-22 12:46:26 +11:00
# Default parameters
'': {
'vars': {
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
"TESTS": "ON",
},
'args': ['-G', 'Ninja'],
'build': [ 'ninja' ],
2019-02-22 12:46:26 +11:00
},
}
def split_all_path(path):
parts = []
while len(path) > 1:
head,tail = os.path.split(path)
yield tail
path = head
if __name__ == '__main__':
self_path = os.path.realpath(__file__)
self_dir = os.path.dirname(self_path)
build_dir = os.path.realpath(os.getcwd())
source_dir = os.path.dirname(self_dir)
2019-02-22 12:46:26 +11:00
accumulated = []
components = ['']
components += sys.argv[1:]
components += itertools.takewhile(lambda x: x in OPTIONS, split_all_path(os.getcwd()))
build = [ 'true' ]
2019-02-22 12:46:26 +11:00
for key in components:
obj = OPTIONS[key]
vars = obj.get('vars', {})
args = obj.get('args', [])
build = obj.get('build', build)
2019-02-22 12:46:26 +11:00
2019-02-22 12:53:59 +11:00
accumulated += [f'"-D{key}={val}"' for key, val in vars.items()]
2019-02-22 12:46:26 +11:00
accumulated += args
cmds = [
f"cmake {source_dir} {' '.join(accumulated)}",
' '.join(build)
]
2019-02-22 12:46:26 +11:00
print(" && ".join(cmds))