2018-05-18 18:52:17 +02:00
|
|
|
#!/usr/bin/env python3
|
2023-08-16 19:27:31 +02:00
|
|
|
# Copyright (c) 2018-2019 The Bitcoin Core developers
|
2020-01-16 21:57:24 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2018-05-18 18:52:17 +02:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import re
|
2022-04-27 20:14:40 +02:00
|
|
|
from multiprocess import Pool
|
2018-05-18 18:52:17 +02:00
|
|
|
|
|
|
|
MAPPING = {
|
|
|
|
'core_read.cpp': 'core_io.cpp',
|
|
|
|
'core_write.cpp': 'core_io.cpp',
|
|
|
|
}
|
|
|
|
|
2021-11-14 11:02:37 +01:00
|
|
|
# Directories with header-based modules, where the assumption that .cpp files
|
|
|
|
# define functions and variables declared in corresponding .h files is
|
|
|
|
# incorrect.
|
|
|
|
HEADER_MODULE_PATHS = [
|
|
|
|
'interfaces/'
|
|
|
|
]
|
|
|
|
|
2018-05-18 18:52:17 +02:00
|
|
|
def module_name(path):
|
|
|
|
if path in MAPPING:
|
|
|
|
path = MAPPING[path]
|
2021-11-14 11:02:37 +01:00
|
|
|
if any(path.startswith(dirpath) for dirpath in HEADER_MODULE_PATHS):
|
|
|
|
return path
|
2018-05-18 18:52:17 +02:00
|
|
|
if path.endswith(".h"):
|
|
|
|
return path[:-2]
|
|
|
|
if path.endswith(".c"):
|
|
|
|
return path[:-2]
|
|
|
|
if path.endswith(".cpp"):
|
|
|
|
return path[:-4]
|
|
|
|
return None
|
|
|
|
|
2021-12-30 17:34:36 +01:00
|
|
|
if __name__=="__main__":
|
|
|
|
files = dict()
|
|
|
|
deps = dict()
|
|
|
|
|
|
|
|
RE = re.compile("^#include <(.*)>")
|
|
|
|
|
2022-04-27 20:14:40 +02:00
|
|
|
def handle_module(arg):
|
2021-12-30 17:34:36 +01:00
|
|
|
module = module_name(arg)
|
|
|
|
if module is None:
|
|
|
|
print("Ignoring file %s (does not constitute module)\n" % arg)
|
|
|
|
else:
|
|
|
|
files[arg] = module
|
|
|
|
deps[module] = set()
|
|
|
|
|
2022-04-27 20:14:40 +02:00
|
|
|
def handle_module2(module):
|
|
|
|
# Build the transitive closure of dependencies of module
|
|
|
|
closure = dict()
|
|
|
|
for dep in deps[module]:
|
|
|
|
closure[dep] = []
|
|
|
|
while True:
|
|
|
|
old_size = len(closure)
|
|
|
|
old_closure_keys = sorted(closure.keys())
|
|
|
|
for src in old_closure_keys:
|
|
|
|
for dep in deps[src]:
|
|
|
|
if dep not in closure:
|
|
|
|
closure[dep] = closure[src] + [src]
|
|
|
|
if len(closure) == old_size:
|
|
|
|
break
|
|
|
|
# If module is in its own transitive closure, it's a circular dependency; check if it is the shortest
|
|
|
|
if module in closure:
|
|
|
|
return [module] + closure[module]
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2021-12-30 17:34:36 +01:00
|
|
|
|
|
|
|
# Iterate over files, and create list of modules
|
|
|
|
for arg in sys.argv[1:]:
|
|
|
|
handle_module(arg)
|
|
|
|
|
|
|
|
def build_list_direct(arg):
|
|
|
|
module = files[arg]
|
|
|
|
with open(arg, 'r', encoding="utf8") as f:
|
|
|
|
for line in f:
|
|
|
|
match = RE.match(line)
|
|
|
|
if match:
|
|
|
|
include = match.group(1)
|
|
|
|
included_module = module_name(include)
|
|
|
|
if included_module is not None and included_module in deps and included_module != module:
|
|
|
|
deps[module].add(included_module)
|
|
|
|
|
|
|
|
|
|
|
|
# Iterate again, and build list of direct dependencies for each module
|
|
|
|
# TODO: implement support for multiple include directories
|
|
|
|
for arg in sorted(files.keys()):
|
|
|
|
build_list_direct(arg)
|
|
|
|
# Loop to find the shortest (remaining) circular dependency
|
|
|
|
|
|
|
|
def shortest_c_dep():
|
|
|
|
have_cycle = False
|
|
|
|
|
2022-04-27 20:14:40 +02:00
|
|
|
sorted_keys = None
|
2021-12-30 17:34:36 +01:00
|
|
|
|
2018-05-18 18:52:17 +02:00
|
|
|
while True:
|
2021-12-30 17:34:36 +01:00
|
|
|
|
|
|
|
shortest_cycles = None
|
2022-04-27 20:14:40 +02:00
|
|
|
if sorted_keys is None:
|
|
|
|
sorted_keys = sorted(deps.keys())
|
|
|
|
|
|
|
|
with Pool(8) as pool:
|
|
|
|
cycles = pool.map(handle_module2, sorted_keys)
|
|
|
|
|
|
|
|
for cycle in cycles:
|
|
|
|
if cycle is not None and (shortest_cycles is None or len(cycle) < len(shortest_cycles)):
|
|
|
|
shortest_cycles = cycle
|
2021-12-30 17:34:36 +01:00
|
|
|
|
|
|
|
if shortest_cycles is None:
|
2018-05-18 18:52:17 +02:00
|
|
|
break
|
2021-12-30 17:34:36 +01:00
|
|
|
# We have the shortest circular dependency; report it
|
|
|
|
module = shortest_cycles[0]
|
|
|
|
print("Circular dependency: %s" % (" -> ".join(shortest_cycles + [module])))
|
|
|
|
# And then break the dependency to avoid repeating in other cycles
|
2022-04-27 20:14:40 +02:00
|
|
|
deps[shortest_cycles[-1]] -= {module}
|
|
|
|
sorted_keys = None
|
2021-12-30 17:34:36 +01:00
|
|
|
have_cycle = True
|
|
|
|
|
|
|
|
if have_cycle:
|
|
|
|
return True
|
|
|
|
|
|
|
|
sys.exit(1 if shortest_c_dep() else 0)
|