2020-01-28 16:39:53 +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.
|
|
|
|
|
2021-10-04 16:07:06 +02:00
|
|
|
"""Compute the LLVM_ENABLE_PROJECTS for cmake from diff.
|
2020-01-28 16:39:53 +01:00
|
|
|
|
2021-10-04 16:07:06 +02:00
|
|
|
This script will compute which projects are affected by the diff provided via STDIN.
|
|
|
|
It gets the modified files in the patch, assigns them to projects and based on a
|
2020-01-28 16:39:53 +01:00
|
|
|
project dependency graph it will get the transitively affected projects.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import os
|
2020-03-23 09:03:24 +01:00
|
|
|
import platform
|
2020-01-28 16:39:53 +01:00
|
|
|
import sys
|
2021-10-04 16:07:06 +02:00
|
|
|
from typing import Any, Dict, List, Set, TextIO, Tuple, Optional, Union
|
|
|
|
from unidiff import PatchSet # type: ignore
|
2020-01-28 16:39:53 +01:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
class ChooseProjects:
|
|
|
|
# file where dependencies are defined
|
|
|
|
SCRIPT_DIR = os.path.dirname(__file__)
|
|
|
|
DEPENDENCIES_FILE = os.path.join(SCRIPT_DIR, 'llvm-dependencies.yaml')
|
|
|
|
|
2020-04-23 11:22:24 +02:00
|
|
|
def __init__(self, llvm_dir: Optional[str]):
|
2021-10-04 16:07:06 +02:00
|
|
|
self.llvm_dir = llvm_dir
|
|
|
|
self.defaultProjects: Dict[str, Dict[str, str]] = {}
|
|
|
|
# List of projects this project depends on, transitive closure.
|
|
|
|
# E.g. compiler-rt -> [llvm, clang].
|
|
|
|
self.dependencies: Dict[str,Set[str]] = {}
|
|
|
|
# List of projects that depends on this project. It's a full closure.
|
|
|
|
# E.g. llvm -> [clang, libcxx, ...]
|
|
|
|
self.usages: Dict[str, Set[str]] = dict()
|
|
|
|
self.all_projects: List[str] = ['all']
|
|
|
|
self.config: Dict[str, Any] = {}
|
2020-01-28 16:39:53 +01:00
|
|
|
self._load_config()
|
|
|
|
|
|
|
|
def _load_config(self):
|
|
|
|
logging.info('loading project config from {}'.format(self.DEPENDENCIES_FILE))
|
|
|
|
with open(self.DEPENDENCIES_FILE) as dependencies_file:
|
2020-10-08 11:52:57 +02:00
|
|
|
self.config = yaml.load(dependencies_file, Loader=yaml.SafeLoader)
|
2021-10-04 16:07:06 +02:00
|
|
|
for k, v in self.config['dependencies'].items():
|
|
|
|
self.dependencies[k] = set(v)
|
|
|
|
# Closure of dependencies.
|
|
|
|
while True:
|
|
|
|
updated = False
|
|
|
|
for s in self.dependencies.values():
|
|
|
|
n = len(s)
|
|
|
|
extend = set()
|
|
|
|
for d in s:
|
|
|
|
extend.update(self.dependencies.get(d, set()))
|
|
|
|
s.update(extend)
|
|
|
|
if len(s) > n:
|
|
|
|
updated = True
|
|
|
|
if not updated:
|
|
|
|
break
|
|
|
|
# Usages don't need to be closed as dependencies already are.
|
|
|
|
for project, deps in self.dependencies.items():
|
|
|
|
for d in deps:
|
|
|
|
self.usages.setdefault(d, set()).add(project)
|
|
|
|
logging.info(f'computed dependencies: {self.dependencies}')
|
|
|
|
logging.info(f'computed usages: {self.usages}')
|
2021-09-17 22:31:15 +02:00
|
|
|
self.all_projects = self.config['allprojects'].keys()
|
2020-10-08 11:52:57 +02:00
|
|
|
|
2021-10-04 16:07:06 +02:00
|
|
|
def get_excluded(self, os: str) -> Set[str]:
|
|
|
|
"""Returns transitive closure for excluded projects"""
|
|
|
|
return self.get_affected_projects(set(self.config['excludedProjects'].get(os, [])))
|
2020-01-28 16:39:53 +01:00
|
|
|
|
2021-10-04 16:07:06 +02:00
|
|
|
def get_check_targets(self, projects: Set[str]) -> Set[str]:
|
2021-09-17 22:31:15 +02:00
|
|
|
"""Return the `check-xxx` targets to pass to ninja for the given list of projects"""
|
2021-10-04 16:07:06 +02:00
|
|
|
if 'all' in projects:
|
|
|
|
return set(["check-all"])
|
2021-09-17 22:31:15 +02:00
|
|
|
targets = set()
|
|
|
|
all_projects = self.config['allprojects']
|
2021-10-04 16:07:06 +02:00
|
|
|
for project in projects:
|
|
|
|
targets.update(set(all_projects.get(project, [])))
|
2021-09-17 22:31:15 +02:00
|
|
|
return targets
|
|
|
|
|
2020-03-23 09:03:24 +01:00
|
|
|
@staticmethod
|
|
|
|
def _detect_os() -> str:
|
|
|
|
"""Detect the current operating system."""
|
|
|
|
if platform.system() == 'Windows':
|
|
|
|
return 'windows'
|
|
|
|
return 'linux'
|
|
|
|
|
2021-10-04 16:07:06 +02:00
|
|
|
def choose_projects(self, patch: str = None, os_name: Optional[str] = None) -> List[str]:
|
2020-10-02 16:18:33 +02:00
|
|
|
"""List all touched project with all projects that they depend on and also
|
|
|
|
all projects that depend on them"""
|
2020-04-23 11:22:24 +02:00
|
|
|
if self.llvm_dir is None:
|
|
|
|
raise ValueError('path to llvm folder must be set in ChooseProject.')
|
2020-03-23 09:03:24 +01:00
|
|
|
llvm_dir = os.path.abspath(os.path.expanduser(self.llvm_dir))
|
2020-01-28 16:39:53 +01:00
|
|
|
logging.info('Scanning LLVM in {}'.format(llvm_dir))
|
|
|
|
if not self.match_projects_dirs():
|
2021-10-04 16:07:06 +02:00
|
|
|
logging.warning(f'{llvm_dir} does not look like a llvm-project directory')
|
|
|
|
return self.get_all_enabled_projects(os_name)
|
2020-03-23 09:03:24 +01:00
|
|
|
changed_files = self.get_changed_files(patch)
|
2020-01-28 16:39:53 +01:00
|
|
|
changed_projects, unmapped_changes = self.get_changed_projects(changed_files)
|
|
|
|
if unmapped_changes:
|
|
|
|
logging.warning('There were changes that could not be mapped to a project.'
|
|
|
|
'Building all projects instead!')
|
2021-10-04 16:07:06 +02:00
|
|
|
return self.get_all_enabled_projects(os_name)
|
|
|
|
return self.extend_projects(changed_projects, os_name)
|
2020-01-28 16:39:53 +01:00
|
|
|
|
2021-10-04 16:07:06 +02:00
|
|
|
def extend_projects(self, projects: Set[str], os_name : Optional[str] = None) -> List[str]:
|
|
|
|
"""Given a set of projects returns a set of projects to be tested taking
|
|
|
|
in account exclusions from llvm-dependencies.yaml.
|
|
|
|
"""
|
2020-07-14 18:26:53 +02:00
|
|
|
logging.info(f'projects: {projects}')
|
2021-10-04 16:07:06 +02:00
|
|
|
if not os_name:
|
|
|
|
os_name = self._detect_os()
|
|
|
|
# Find all affected by current set.
|
2020-07-14 18:26:53 +02:00
|
|
|
affected_projects = self.get_affected_projects(projects)
|
2021-10-04 16:07:06 +02:00
|
|
|
logging.info(f'all affected projects(*) {affected_projects}')
|
|
|
|
# Exclude everything that is affected by excluded.
|
|
|
|
excluded_projects = self.get_excluded(os_name)
|
|
|
|
logging.info(f'all excluded projects(*) {excluded_projects}')
|
|
|
|
affected_projects = affected_projects - excluded_projects
|
|
|
|
logging.info(f'effective projects list {affected_projects}')
|
|
|
|
return sorted(affected_projects)
|
2020-01-28 16:39:53 +01:00
|
|
|
|
2020-03-23 09:03:24 +01:00
|
|
|
def run(self):
|
2021-10-04 16:07:06 +02:00
|
|
|
affected_projects = self.choose_projects()
|
2021-09-17 22:31:15 +02:00
|
|
|
print("Affected:", ';'.join(affected_projects))
|
2021-10-04 16:07:06 +02:00
|
|
|
print("Dependencies:", ';'.join(self.get_dependencies(affected_projects)))
|
2021-09-17 22:31:15 +02:00
|
|
|
print("Check targets:", ';'.join(self.get_check_targets(affected_projects)))
|
2020-03-23 09:03:24 +01:00
|
|
|
return 0
|
2020-01-28 16:39:53 +01:00
|
|
|
|
|
|
|
def match_projects_dirs(self) -> bool:
|
|
|
|
"""Make sure that all projects are folders in the LLVM dir.
|
|
|
|
"""
|
|
|
|
subdirs = os.listdir(self.llvm_dir)
|
|
|
|
for project in self.all_projects:
|
|
|
|
if project not in subdirs:
|
2021-09-17 22:31:15 +02:00
|
|
|
logging.error('Project not found in LLVM root folder: {}'.format(project))
|
2020-01-28 16:39:53 +01:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
2021-10-04 16:07:06 +02:00
|
|
|
def get_changed_files(patch_str: Union[str, TextIO, None] = None) -> Set[str]:
|
2021-07-28 15:50:57 +02:00
|
|
|
"""get list of changed files from the patch or from STDIN.
|
|
|
|
e.g. ['compiler-rt/lib/tsan/CMakeLists.txt']"""
|
2020-03-23 09:03:24 +01:00
|
|
|
if patch_str is None:
|
2020-03-24 09:27:43 +01:00
|
|
|
patch_str = sys.stdin
|
2020-03-23 09:03:24 +01:00
|
|
|
patch = PatchSet(patch_str)
|
|
|
|
|
2020-01-28 16:39:53 +01:00
|
|
|
changed_files = set({f.path for f in patch.modified_files + patch.added_files + patch.removed_files})
|
|
|
|
|
|
|
|
logging.info('Files modified by this patch:\n ' + '\n '.join(sorted(changed_files)))
|
|
|
|
return changed_files
|
|
|
|
|
2020-03-23 09:03:24 +01:00
|
|
|
def get_changed_projects(self, changed_files: Set[str]) -> Tuple[Set[str], bool]:
|
2020-01-28 16:39:53 +01:00
|
|
|
"""Get list of projects affected by the change."""
|
2021-09-17 22:31:15 +02:00
|
|
|
logging.info("Get list of projects affected by the change.")
|
2020-01-28 16:39:53 +01:00
|
|
|
changed_projects = set()
|
|
|
|
unmapped_changes = False
|
|
|
|
for changed_file in changed_files:
|
2020-03-23 09:03:24 +01:00
|
|
|
project = changed_file.split('/', maxsplit=1)
|
2021-07-29 00:06:01 +02:00
|
|
|
# There is no utils project.
|
|
|
|
if project[0] == 'utils':
|
2021-07-23 09:35:22 +02:00
|
|
|
continue
|
|
|
|
if (project is None) or (project[0] not in self.all_projects):
|
2020-01-28 16:39:53 +01:00
|
|
|
unmapped_changes = True
|
|
|
|
logging.warning('Could not map file to project: {}'.format(changed_file))
|
|
|
|
else:
|
|
|
|
changed_projects.add(project[0])
|
|
|
|
|
|
|
|
logging.info('Projects directly modified by this patch:\n ' + '\n '.join(sorted(changed_projects)))
|
|
|
|
return changed_projects, unmapped_changes
|
|
|
|
|
2020-03-23 09:03:24 +01:00
|
|
|
def get_affected_projects(self, changed_projects: Set[str]) -> Set[str]:
|
|
|
|
"""Compute transitive closure of affected projects based on the
|
2021-10-04 16:07:06 +02:00
|
|
|
dependencies between the projects (including initially passed)."""
|
|
|
|
affected: Set[str] = set(changed_projects)
|
|
|
|
for p in changed_projects:
|
|
|
|
affected.update(self.usages.get(p, set()))
|
|
|
|
logging.info(f'added {affected - changed_projects} projects as they are affected')
|
|
|
|
return affected
|
2020-01-28 16:39:53 +01:00
|
|
|
|
2021-09-17 22:31:15 +02:00
|
|
|
def get_dependencies(self, projects: Set[str]) -> Set[str]:
|
2021-10-04 16:07:06 +02:00
|
|
|
"""Return transitive dependencies for a given projects (including the projects themself).
|
2020-02-03 13:42:19 +01:00
|
|
|
|
2021-09-17 22:31:15 +02:00
|
|
|
These are the required dependencies for given `projects` so that they can be built.
|
2020-02-03 13:42:19 +01:00
|
|
|
"""
|
2021-10-04 16:07:06 +02:00
|
|
|
affected: Set[str] = set(projects)
|
|
|
|
for p in projects:
|
|
|
|
affected.update(self.dependencies.get(p, set()))
|
|
|
|
return affected
|
2020-02-03 13:42:19 +01:00
|
|
|
|
2021-10-04 16:07:06 +02:00
|
|
|
def get_all_enabled_projects(self, os_name: Optional[str] = None) -> List[str]:
|
2020-04-23 11:22:24 +02:00
|
|
|
"""Get list of all not-excluded projects for current platform."""
|
2021-10-04 16:07:06 +02:00
|
|
|
return self.extend_projects(set(self.all_projects), os_name)
|
2020-04-23 11:22:24 +02:00
|
|
|
|
2020-03-23 09:03:24 +01:00
|
|
|
|
2020-01-28 16:39:53 +01:00
|
|
|
if __name__ == "__main__":
|
2020-03-23 09:03:24 +01:00
|
|
|
parser = argparse.ArgumentParser(
|
2021-09-17 22:31:15 +02:00
|
|
|
description='Compute the projects affected by a change. A patch file is expected on stdin.')
|
2021-10-04 16:07:06 +02:00
|
|
|
parser.add_argument('--llvmdir', type=str, default='.')
|
|
|
|
parser.add_argument('--log-level', type=str, default='INFO')
|
2020-01-28 16:39:53 +01:00
|
|
|
args = parser.parse_args()
|
2021-10-04 16:07:06 +02:00
|
|
|
logging.basicConfig(level=args.log_level, format='%(levelname)-7s %(message)s')
|
|
|
|
logging.info(f'checking changes in {args.llvmdir}')
|
2020-01-28 16:39:53 +01:00
|
|
|
chooser = ChooseProjects(args.llvmdir)
|
2020-03-23 09:03:24 +01:00
|
|
|
sys.exit(chooser.run())
|