|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import platform |
| 6 | +import settings |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | + |
| 11 | +from pathlib import Path, PurePath |
| 12 | + |
| 13 | +def default_toolchain(): |
| 14 | + system = platform.uname()[0] |
| 15 | + machine = platform.uname()[4] |
| 16 | + toolchain = Path(settings.THIS_DIR, |
| 17 | + 'cmake', |
| 18 | + f'toolchain_{system.lower()}_{machine.lower()}.cmake') |
| 19 | + return toolchain if toolchain.is_file else None |
| 20 | + |
| 21 | +def get_arguments(): |
| 22 | + parser = argparse.ArgumentParser( |
| 23 | + description="Help configure and build classic-flang-llvm-project") |
| 24 | + |
| 25 | + buildopt = parser.add_argument_group('general build options') |
| 26 | + buildopt.add_argument('-t', '--target', metavar='ARCH', choices=['X86', 'AArch64', 'PowerPC'], default='X86', |
| 27 | + help='Control which targets are enabled (%(choices)s) (default: %(default)s)') |
| 28 | + buildopt.add_argument('-p', '--install-prefix', metavar='PATH', nargs='?', default=Path(settings.LLVM_DIR,'..','install'), const=False, |
| 29 | + help='install directory (default: %(default)s) ') |
| 30 | + buildopt.add_argument('-j', '--jobs', metavar='N', type=int, default=os.cpu_count(), |
| 31 | + help='number of parallel build jobs (default: %(default)s)') |
| 32 | + buildopt.add_argument('--toolchain', metavar='FILE', default=default_toolchain().as_posix(), |
| 33 | + help='specify toolchain file (default: %(default)s)') |
| 34 | + buildopt.add_argument('-d', '--builddir', metavar='DIR', default='build', |
| 35 | + help=f'specify build directory (default: {settings.LLVM_DIR}/%(default)s)') |
| 36 | + buildopt.add_argument('--clean', action='store_true', default=False, |
| 37 | + help='clean build') |
| 38 | + buildopt.add_argument('-b', '--build-type', metavar='TYPE', default='Release', |
| 39 | + help='set build type (default: %(default)s)') |
| 40 | + buildopt.add_argument('-x', '--cmake-param', metavar='OPT', action='append', default=[], |
| 41 | + help='add custom argument to CMake') |
| 42 | + buildopt.add_argument('-e', '--llvm-enable-projects', metavar='OPT', action='append', default=['clang'] if sys.platform == 'win32' else ['clang', 'openmp'], |
| 43 | + help='enable llvm projects to build (in quotation marks separated by semicolons e.g.: "clang;openmp")') |
| 44 | + buildopt.add_argument('-c', '--use-ccache', action="store_true", default=False, |
| 45 | + help='Using ccache during the build (default: %(default)s)') |
| 46 | + buildopt.add_argument('--cc', metavar='OPT', default=None, |
| 47 | + help='use specific C compiler') |
| 48 | + buildopt.add_argument('--cxx', metavar='OPT', default=None, |
| 49 | + help='use specific C++ compiler') |
| 50 | + buildopt.add_argument('-v', '--verbose', action='store_true', default=False, |
| 51 | + help='Verbose build (default: %(default)s') |
| 52 | + arguments = parser.parse_args() |
| 53 | + return arguments |
| 54 | + |
| 55 | +def generate_buildoptions(arguments): |
| 56 | + install_root = Path(arguments.install_prefix) |
| 57 | + |
| 58 | + base_cmake_args = [ |
| 59 | + f'-DCMAKE_INSTALL_PREFIX={install_root.as_posix()}', |
| 60 | + f'-DCMAKE_BUILD_TYPE={arguments.build_type}', |
| 61 | + f'-DCMAKE_TOOLCHAIN_FILE={arguments.toolchain}' |
| 62 | + ] |
| 63 | + if sys.platform == 'win32' and platform.uname()[4].lower() == 'arm64': |
| 64 | + base_cmake_args.append('-GNMake Makefiles') |
| 65 | + else: |
| 66 | + generator = 'Ninja' if sys.platform == 'win32' else 'Unix Makefiles' |
| 67 | + base_cmake_args.append(f'-G{generator}') |
| 68 | + |
| 69 | + if arguments.use_ccache: |
| 70 | + base_cmake_args.append('-DCMAKE_C_COMPILER_LAUNCHER=ccache') |
| 71 | + base_cmake_args.append('-DCMAKE_CXX_COMPILER_LAUNCHER=ccache') |
| 72 | + |
| 73 | + if arguments.cmake_param: |
| 74 | + base_cmake_args.extend(arguments.cmake_param) |
| 75 | + |
| 76 | + if arguments.cc: |
| 77 | + base_cmake_args.append(f'-DCMAKE_C_COMPILER={arguments.cc}') |
| 78 | + |
| 79 | + if arguments.cxx: |
| 80 | + base_cmake_args.append(f'-DCMAKE_CXX_COMPILER={arguments.cxx}') |
| 81 | + |
| 82 | + if arguments.verbose: |
| 83 | + base_cmake_args.extend('-DCMAKE_VERBOSE_MAKEFILE=ON') |
| 84 | + |
| 85 | + return base_cmake_args |
| 86 | + |
| 87 | +def normalize_builddir(project_srcdir, builddir, clean): |
| 88 | + build_path = '' |
| 89 | + if PurePath(builddir).is_absolute(): |
| 90 | + build_path = Path(builddir, PurePath(project_srcdir).name) |
| 91 | + else: |
| 92 | + build_path = Path(project_srcdir, builddir) |
| 93 | + |
| 94 | + if clean and build_path.exists(): |
| 95 | + shutil.rmtree(build_path) |
| 96 | + |
| 97 | + return build_path.as_posix() |
| 98 | + |
| 99 | +def configure_llvm(arguments): |
| 100 | + build_path = normalize_builddir( |
| 101 | + settings.LLVM_DIR, arguments.builddir, arguments.clean) |
| 102 | + |
| 103 | + build_options = generate_buildoptions(arguments) |
| 104 | + additional_options = [ |
| 105 | + f'-DLLVM_TARGETS_TO_BUILD={arguments.target}', |
| 106 | + '-DLLVM_ENABLE_CLASSIC_FLANG=ON', |
| 107 | + ] |
| 108 | + if(arguments.llvm_enable_projects): |
| 109 | + additional_options.append(f'-DLLVM_ENABLE_PROJECTS={";".join(x for x in arguments.llvm_enable_projects)}') |
| 110 | + build_options.extend(additional_options) |
| 111 | + cmake_cmd = ['cmake', '-B', build_path, '-S', Path.joinpath(settings.LLVM_DIR,'llvm')] |
| 112 | + cmake_cmd.extend(build_options) |
| 113 | + subprocess.run(cmake_cmd, check=True) |
| 114 | + |
| 115 | + return build_path |
| 116 | + |
| 117 | +def build_project(build_path, arguments): |
| 118 | + build_cmd = ['cmake', '--build', build_path, '--config', |
| 119 | + arguments.build_type, '-j', str(arguments.jobs)] |
| 120 | + subprocess.run(build_cmd, check=True) |
| 121 | + |
| 122 | +def install_project(build_path, arguments): |
| 123 | + install_cmd = ['cmake', '--build', build_path, '--config', |
| 124 | + arguments.build_type, '--target', 'install'] |
| 125 | + subprocess.run(install_cmd, check=True) |
| 126 | + |
| 127 | +def print_success(): |
| 128 | + print() |
| 129 | + print('=' * 30) |
| 130 | + print('Build succeeded!') |
| 131 | + print('=' * 30) |
| 132 | + print() |
| 133 | + |
| 134 | +def print_header(title): |
| 135 | + print() |
| 136 | + print('*' * 30) |
| 137 | + print(f'{title}...') |
| 138 | + print('*' * 30) |
| 139 | + print() |
| 140 | + |
| 141 | +def main(): |
| 142 | + arguments = get_arguments() |
| 143 | + |
| 144 | + print_header('Building classic llvm') |
| 145 | + build_path = configure_llvm(arguments) |
| 146 | + build_project(build_path, arguments) |
| 147 | + install_project(build_path, arguments) |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + main() |
| 151 | + print_success() |
0 commit comments