-
Notifications
You must be signed in to change notification settings - Fork 1
/
detect_system_deps.py
executable file
·146 lines (128 loc) · 5.03 KB
/
detect_system_deps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
import sys
import yaml
import tempfile
import textwrap
from pathlib import Path
from subprocess import run, DEVNULL
script_dir = Path(__file__).parent
def debug(_msg):
pass
def parse_args():
import argparse
ap = argparse.ArgumentParser()
ap.add_argument('option', type=str, nargs='*',
help="Project option from conanfile.py that are set to True.")
ap.add_argument("-v", "--verbose", action="store_true",
help="Print generated CMakeLists.txt, cmake command-line and full output.")
args = ap.parse_args()
if args.verbose:
global debug
debug = lambda msg: print(msg)
return args.option
def add_required_components(options):
result = set(options)
all_tools = None
for name in options:
if name == 'widgets':
result.add('text')
result.add('graphics')
if name == 'text':
result.add('graphics')
if name == 'script':
result.add('data')
if name == 'tools' and all_tools is None:
all_tools = True
if name.endswith('_tool'):
all_tools = False
if all_tools:
if 'widgets' in result:
result.add('shed_tool')
if 'script' in result:
result.add('fire_tool')
if 'data' in result:
result.add('dati_tool')
result |= {'ff_tool', 'tc_tool'}
return result
def requirements():
with open(script_dir.joinpath('conandata.yml'), newline='') as conandata:
return yaml.safe_load(conandata)['requirements']
def filtered_requirements(options: set):
for name, info in requirements().items():
if 'prereq' not in info or set(info['prereq']).intersection(options):
yield name, info
def detect_deps(reqs, built_deps):
cmake_name_to_sysopt = {}
items = []
for name, info in reqs:
cmake_find = info['cmake']
sysopt = f"system_{name}" if 'conan' in info else f"with_{name}"
if isinstance(cmake_find, list):
for cf in cmake_find:
cmake_name_to_sysopt[cf.split('/')[0]] = sysopt
items.append(cf)
else:
cmake_name_to_sysopt[cmake_find.split('/')[0]] = sysopt
items.append(cmake_find)
items = ';'.join(items)
with tempfile.TemporaryDirectory() as tmp_dir:
# Convert the path to posix (forward slashes) even on Windows.
# Paths with backslashes are not supported by CMake.
custom_modules = '"' + str(script_dir.joinpath('cmake').as_posix()) + '"'
cml = textwrap.dedent("""
cmake_minimum_required(VERSION 3.16)
project(SystemPackageFinder CXX)
list(APPEND CMAKE_MODULE_PATH """ + custom_modules + """)
foreach (ITEM IN LISTS DEPS)
string(REPLACE "/" ";" ITEM ${ITEM})
list(GET ITEM 0 NAME)
list(GET ITEM 1 VERSION)
message(STATUS "Find ${NAME} ${VERSION}")
find_package(${NAME} ${VERSION})
if (${NAME}_FOUND)
message(NOTICE "FOUND ${NAME} ${${NAME}_VERSION}")
endif()
endforeach()
""")
debug(cml)
with open(tmp_dir + "/CMakeLists.txt", 'w') as f:
f.write(cml)
# Prefer ninja if available. Needed to allow choice, otherwise `make` installation
# would be required on unixes, as it's the default in cmake.
ninja = ""
if run("command -v ninja", shell=True, stdout=DEVNULL, stderr=DEVNULL).returncode == 0:
ninja = "-G Ninja"
cmd = f"cmake . {ninja} -DDEPS='{items}' -DCMAKE_PREFIX_PATH='{';'.join(built_deps)}'"
debug(cmd)
p = run(cmd, shell=True,
capture_output=True, encoding='UTF-8', cwd=tmp_dir)
debug(p.stdout)
debug(p.stderr)
if p.returncode != 0:
print(f'Failed: {p.returncode}', file=sys.stderr)
print(p.stdout)
print(p.stderr)
sys.exit(1)
found = set()
for line in p.stderr.splitlines():
if line.startswith('FOUND '):
_, name, version = line.split(' ')
print(f"Found: {name} {version}")
found.add(cmake_name_to_sysopt[name])
return tuple(found)
def main():
options = parse_args()
options = add_required_components(options)
reqs = tuple(filtered_requirements(options))
built_deps_dir = script_dir / '.deps'
if built_deps_dir.is_dir():
built_deps = [str(d) for d in built_deps_dir.glob("[!.]*") if d.is_symlink()]
else:
built_deps = []
deps = detect_deps(reqs, built_deps)
cmake_args = [f'-DXCI_{o.upper()}=ON' for o in deps if o.startswith('with_')] + \
[f"-DCMAKE_PREFIX_PATH={';'.join(built_deps)}"]
print(' '.join(cmake_args))
print(' '.join(f'-o xcikit/*:{o}=True' for o in deps))
if __name__ == '__main__':
main()