2020-03-23 09:03:24 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright 2020 Google LLC
|
|
|
|
#
|
|
|
|
# Licensed under the the Apache License v2.0 with LLVM Exceptions (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# https://llvm.org/LICENSE.txt
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import argparse
|
2020-06-03 13:40:22 +02:00
|
|
|
import logging
|
2020-03-23 09:03:24 +01:00
|
|
|
from enum import Enum
|
|
|
|
from git import Repo
|
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
2020-04-16 10:30:39 +02:00
|
|
|
import stat
|
2020-05-25 16:42:40 +02:00
|
|
|
import sys
|
2020-03-23 09:03:24 +01:00
|
|
|
from typing import List, Dict
|
|
|
|
import yaml
|
2020-04-16 10:30:39 +02:00
|
|
|
|
2020-03-23 09:03:24 +01:00
|
|
|
from choose_projects import ChooseProjects
|
|
|
|
|
|
|
|
|
|
|
|
class OperatingSystem(Enum):
|
|
|
|
Linux = 'linux'
|
|
|
|
Windows = 'windows'
|
|
|
|
|
|
|
|
|
|
|
|
class Configuration:
|
2020-04-23 11:22:24 +02:00
|
|
|
"""Configuration for running cmake.
|
|
|
|
|
|
|
|
The data is mostly read from the file `run_cmake_config.yaml`
|
|
|
|
residing in the same folder as this script.
|
|
|
|
"""
|
2020-03-23 09:03:24 +01:00
|
|
|
|
|
|
|
def __init__(self, config_file_path: str):
|
|
|
|
with open(config_file_path) as config_file:
|
|
|
|
config = yaml.load(config_file, Loader=yaml.SafeLoader)
|
|
|
|
self._environment = config['environment'] # type: Dict[OperatingSystem, Dict[str, str]]
|
|
|
|
self.general_cmake_arguments = config['arguments']['general'] # type: List[str]
|
2020-05-25 16:42:40 +02:00
|
|
|
self._specific_cmake_arguments = config[
|
|
|
|
'arguments'] # type: Dict[OperatingSystem, List[str]]
|
2020-03-23 09:03:24 +01:00
|
|
|
self.operating_system = self._detect_os() # type: OperatingSystem
|
|
|
|
|
|
|
|
@property
|
|
|
|
def environment(self) -> Dict[str, str]:
|
|
|
|
return self._environment[self.operating_system.value]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def specific_cmake_arguments(self) -> List[str]:
|
|
|
|
return self._specific_cmake_arguments[self.operating_system.value]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def default_projects(self) -> str:
|
2020-04-23 11:22:24 +02:00
|
|
|
"""Get string of projects enabled by default.
|
|
|
|
|
|
|
|
This returns all projects in the mono repo minus the project that were
|
|
|
|
excluded for the current platform.
|
|
|
|
"""
|
|
|
|
cp = ChooseProjects(None)
|
|
|
|
return ';'.join(cp.get_all_enabled_projects())
|
2020-03-23 09:03:24 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _detect_os() -> OperatingSystem:
|
|
|
|
"""Detect the current operating system."""
|
|
|
|
if platform.system() == 'Windows':
|
|
|
|
return OperatingSystem.Windows
|
|
|
|
return OperatingSystem.Linux
|
|
|
|
|
|
|
|
|
|
|
|
def _select_projects(config: Configuration, projects: str, repo_path: str) -> str:
|
|
|
|
"""select which projects to build.
|
|
|
|
|
|
|
|
if projects == "default", a default configuraiton will be used.
|
|
|
|
|
|
|
|
if project == "detect", ChooseProjects is used to magically detect the projects
|
|
|
|
based on the files modified in HEAD
|
|
|
|
"""
|
|
|
|
if projects == "default" or projects is None or len(projects) == 0:
|
|
|
|
return config.default_projects
|
|
|
|
if projects == "detect":
|
|
|
|
cp = ChooseProjects(repo_path)
|
|
|
|
repo = Repo('.')
|
|
|
|
patch = repo.git.diff("HEAD~1")
|
2020-07-14 18:26:53 +02:00
|
|
|
logging.debug(f'diff {patch}')
|
2020-03-23 09:03:24 +01:00
|
|
|
enabled_projects = ';'.join(cp.choose_projects(patch))
|
|
|
|
if enabled_projects is None or len(enabled_projects) == 0:
|
2020-07-14 18:26:53 +02:00
|
|
|
logging.warning('Cannot detect affected projects. Enable all projects')
|
2020-05-11 16:09:27 +02:00
|
|
|
enabled_projects = cp.get_all_enabled_projects()
|
2020-03-23 09:03:24 +01:00
|
|
|
return enabled_projects
|
|
|
|
return projects
|
|
|
|
|
|
|
|
|
|
|
|
def _create_env(config: Configuration) -> Dict[str, str]:
|
|
|
|
"""Generate the environment variables for cmake."""
|
|
|
|
env = os.environ.copy()
|
|
|
|
env.update(config.environment)
|
|
|
|
return env
|
|
|
|
|
|
|
|
|
2020-07-09 15:24:27 +02:00
|
|
|
def _create_args(config: Configuration, llvm_enable_projects: str, use_cache: bool) -> List[str]:
|
2020-03-23 09:03:24 +01:00
|
|
|
"""Generate the command line arguments for cmake."""
|
|
|
|
arguments = [
|
|
|
|
os.path.join('..', 'llvm'),
|
|
|
|
'-D LLVM_ENABLE_PROJECTS="{}"'.format(llvm_enable_projects),
|
|
|
|
]
|
|
|
|
arguments.extend(config.general_cmake_arguments)
|
|
|
|
arguments.extend(config.specific_cmake_arguments)
|
|
|
|
|
2020-07-09 15:24:27 +02:00
|
|
|
if use_cache:
|
|
|
|
if 'SCCACHE_DIR' in os.environ:
|
|
|
|
logging.info("using sccache")
|
|
|
|
arguments.extend([
|
|
|
|
'-DCMAKE_C_COMPILER_LAUNCHER=sccache',
|
|
|
|
'-DCMAKE_CXX_COMPILER_LAUNCHER=sccache',
|
|
|
|
])
|
|
|
|
# enable ccache if the path is set in the environment
|
|
|
|
elif 'CCACHE_PATH' in os.environ:
|
|
|
|
logging.info("using ccache")
|
|
|
|
arguments.extend([
|
|
|
|
'-D LLVM_CCACHE_BUILD=ON',
|
|
|
|
'-D LLVM_CCACHE_DIR={}'.format(os.environ['CCACHE_PATH']),
|
|
|
|
'-D LLVM_CCACHE_MAXSIZE=20G',
|
|
|
|
])
|
2020-03-23 09:03:24 +01:00
|
|
|
return arguments
|
|
|
|
|
|
|
|
|
2020-05-25 16:42:40 +02:00
|
|
|
def run(projects: str, repo_path: str, config_file_path: str = None, *, dry_run: bool = False):
|
|
|
|
"""Use cmake to configure the project and create build directory.
|
|
|
|
|
|
|
|
Returns build directory and path to created artifacts.
|
2020-03-23 09:03:24 +01:00
|
|
|
|
2020-07-09 15:24:27 +02:00
|
|
|
This version works on Linux and Windows.
|
|
|
|
|
|
|
|
Returns: exit code of cmake command, build directory, path to CMakeCache.txt, commands.
|
2020-03-23 09:03:24 +01:00
|
|
|
"""
|
2020-07-09 15:24:27 +02:00
|
|
|
commands = []
|
2020-03-23 09:03:24 +01:00
|
|
|
if config_file_path is None:
|
|
|
|
script_dir = os.path.dirname(__file__)
|
|
|
|
config_file_path = os.path.join(script_dir, 'run_cmake_config.yaml')
|
|
|
|
config = Configuration(config_file_path)
|
|
|
|
|
|
|
|
build_dir = os.path.abspath(os.path.join(repo_path, 'build'))
|
2020-05-25 16:42:40 +02:00
|
|
|
if not dry_run:
|
2020-04-15 18:16:41 +02:00
|
|
|
secure_delete(build_dir)
|
2020-03-30 16:01:30 +02:00
|
|
|
os.makedirs(build_dir)
|
2020-07-09 15:24:27 +02:00
|
|
|
commands.append("rm -rf build")
|
|
|
|
commands.append("mkdir build")
|
|
|
|
commands.append("cd build")
|
|
|
|
for k, v in config.environment.items():
|
|
|
|
commands.append(f'export {k}="{v}"')
|
2020-03-23 09:03:24 +01:00
|
|
|
env = _create_env(config)
|
|
|
|
llvm_enable_projects = _select_projects(config, projects, repo_path)
|
2020-06-17 15:07:19 +02:00
|
|
|
print('Enabled projects: {}'.format(llvm_enable_projects), flush=True)
|
2020-07-09 15:24:27 +02:00
|
|
|
arguments = _create_args(config, llvm_enable_projects, True)
|
2020-03-23 09:03:24 +01:00
|
|
|
cmd = 'cmake ' + ' '.join(arguments)
|
2020-05-25 16:42:40 +02:00
|
|
|
|
2020-04-23 15:06:23 +02:00
|
|
|
print('Running cmake with these arguments:\n{}'.format(cmd), flush=True)
|
2020-05-25 16:42:40 +02:00
|
|
|
if dry_run:
|
|
|
|
print('Dry run, not invoking CMake!')
|
2020-07-14 18:26:53 +02:00
|
|
|
return 0, build_dir, [], []
|
2020-05-25 16:42:40 +02:00
|
|
|
result = subprocess.call(cmd, env=env, shell=True, cwd=build_dir)
|
2020-07-09 15:24:27 +02:00
|
|
|
commands.append('cmake ' + ' '.join(_create_args(config, llvm_enable_projects, False)))
|
|
|
|
commands.append('# ^note that compiler cache arguments are omitted')
|
2020-05-25 16:42:40 +02:00
|
|
|
_link_compile_commands(config, repo_path, build_dir)
|
2020-07-09 15:24:27 +02:00
|
|
|
return result, build_dir, [os.path.join(build_dir, 'CMakeCache.txt')], commands
|
2020-03-23 17:46:21 +01:00
|
|
|
|
|
|
|
|
2020-04-15 18:16:41 +02:00
|
|
|
def secure_delete(path: str):
|
|
|
|
"""Try do delete a local folder.
|
|
|
|
|
2020-04-16 10:30:39 +02:00
|
|
|
Handle read-only files.
|
2020-04-15 18:16:41 +02:00
|
|
|
"""
|
2020-04-16 10:30:39 +02:00
|
|
|
if not os.path.exists(path):
|
|
|
|
return
|
|
|
|
|
|
|
|
def del_rw(action, name, exc):
|
2020-04-16 13:54:50 +02:00
|
|
|
os.chmod(name, stat.S_IWRITE)
|
|
|
|
os.unlink(name)
|
2020-04-16 10:30:39 +02:00
|
|
|
|
|
|
|
shutil.rmtree(path, onerror=del_rw)
|
2020-04-15 18:16:41 +02:00
|
|
|
|
|
|
|
|
2020-03-23 17:46:21 +01:00
|
|
|
def _link_compile_commands(config: Configuration, repo_path: str, build_dir: str):
|
|
|
|
"""Link compile_commands.json from build to root dir"""
|
|
|
|
if config.operating_system != OperatingSystem.Linux:
|
|
|
|
return
|
|
|
|
source_path = os.path.join(build_dir, 'compile_commands.json')
|
|
|
|
target_path = os.path.join(repo_path, 'compile_commands.json')
|
|
|
|
if os.path.exists(target_path):
|
|
|
|
os.remove(target_path)
|
|
|
|
os.symlink(source_path, target_path)
|
|
|
|
|
2020-03-23 09:03:24 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(description='Run CMake for LLVM.')
|
|
|
|
parser.add_argument('projects', type=str, nargs='?', default='default')
|
|
|
|
parser.add_argument('repo_path', type=str, nargs='?', default=os.getcwd())
|
2020-03-30 16:01:30 +02:00
|
|
|
parser.add_argument('--dryrun', action='store_true')
|
2020-07-14 18:26:53 +02:00
|
|
|
parser.add_argument('--log-level', type=str, default='WARNING')
|
2020-03-23 09:03:24 +01:00
|
|
|
args = parser.parse_args()
|
2020-07-14 18:26:53 +02:00
|
|
|
logging.basicConfig(level=args.log_level, format='%(levelname)-7s %(message)s')
|
|
|
|
result, _, _, _ = run(args.projects, args.repo_path, dry_run=args.dryrun)
|
2020-05-25 16:42:40 +02:00
|
|
|
sys.exit(result)
|