-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcmake_min_version.py
executable file
·223 lines (185 loc) · 7.83 KB
/
cmake_min_version.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3
import argparse
import contextlib
import math
import platform
import re
import subprocess
import sys
import tempfile
from pathlib import Path
from time import time
from typing import List, NamedTuple, Optional
from packaging.version import parse as version_parse
from termcolor import colored
class CMakeBinary(NamedTuple):
version: str
binary: Path
class ConfigureResult:
def __init__(self, return_code: int, stderr: str):
self.success = return_code == 0 # type: bool
self.proposed_version = None # type: Optional[str]
self.reason = None # type: Optional[str]
self.stderr = stderr
# try to read proposed minimal version from stderr output
try:
self.proposed_version = re.findall(r"CMake ([^ ]+) or higher is required.", stderr)[0]
# support ranges
if ".." in self.proposed_version:
self.proposed_version = self.proposed_version.split("..")[0]
# make sure all versions are major.minor.patch
if self.proposed_version.count(".") == 1:
self.proposed_version += ".0"
except IndexError:
pass
try:
self.reason = re.findall(r"CMake Error at (.*):", stderr)[0]
except IndexError:
try:
self.reason = re.findall(r"CMake Error: ([^\n]+)", stderr)[0]
except IndexError:
pass
def get_cmake_binaries(tools_dir: Path) -> List[CMakeBinary]:
start_time = time()
binaries = [] # type: List[CMakeBinary]
if platform.system() == "Windows":
filenames = tools_dir.rglob("**/bin/cmake.exe")
else:
filenames = tools_dir.rglob("**/bin/cmake")
for filename in filenames:
with contextlib.suppress(IndexError):
version = re.findall(r"cmake-([^-]+)-", str(filename))[0]
binaries.append(CMakeBinary(version, Path(filename).resolve()))
print(f"Found {len(binaries)} CMake binaries from directory {tools_dir} in {time()-start_time:.2f} seconds\n")
return sorted(binaries, key=lambda x: version_parse(x.version))
def try_configure(binary: Path, cmake_parameters: List[str]) -> ConfigureResult:
tmpdir = tempfile.TemporaryDirectory()
proc = subprocess.Popen(
[binary, *cmake_parameters, "-Wno-dev"],
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
cwd=tmpdir.name,
)
proc.wait()
return ConfigureResult(
return_code=proc.returncode,
stderr=proc.stderr.read().decode("utf-8") if proc.stderr else "",
)
def binary_search(*, cmake_parameters: List[str], tools_dir: Path, error_output: bool) -> Optional[CMakeBinary]:
versions = get_cmake_binaries(tools_dir) # type: List[CMakeBinary]
cmake_versions = [len(cmake.version) for cmake in versions]
if len(cmake_versions) == 0:
print(
colored(
"Error: No CMake versions found in the tool dir. Make sure to run the cmake_downloader script first.",
"red",
),
)
sys.exit(1)
longest_version_string = max(cmake_versions) + 1 # type: int
lower_idx = 0 # type: int
upper_idx = len(versions) - 1 # type: int
last_success_idx = None # type: Optional[int]
steps = 0 # type: int
while lower_idx <= upper_idx:
mid_idx = int((lower_idx + upper_idx) / 2) # type: int
cmake_binary = versions[mid_idx] # type: CMakeBinary
steps += 1
remaining_versions = upper_idx - lower_idx + 1 # type: int
remaining_steps = int(math.ceil(math.log2(remaining_versions))) # type: int
print(
"[{progress:3.0f}%] CMake {cmake_version:{longest_version_string}}".format(
progress=100.0 * float(steps - 1) / (steps + remaining_steps),
cmake_version=cmake_binary.version,
longest_version_string=longest_version_string,
),
end="",
flush=True,
)
result = try_configure(binary=cmake_binary.binary, cmake_parameters=cmake_parameters) # type: ConfigureResult
if result.success:
print(colored("✔ works", "green"))
last_success_idx = mid_idx
upper_idx = mid_idx - 1
else:
print(colored("✘ error", "red"))
if error_output:
for line in result.stderr.splitlines():
print(colored(f" {line}", "yellow"))
elif result.reason:
print(colored(f" {result.reason}", "yellow"))
proposed_binary = [x for x in versions if x.version == result.proposed_version]
lower_idx = versions.index(proposed_binary[0]) if len(proposed_binary) else mid_idx + 1
return versions[last_success_idx] if last_success_idx is not None else None
def full_search(*, cmake_parameters: List[str], tools_dir: Path, error_output: bool) -> Optional[CMakeBinary]:
versions = get_cmake_binaries(tools_dir) # type: List[CMakeBinary]
longest_version_string = max([len(cmake.version) for cmake in versions]) + 1 # type: int
last_success_idx = None # type: Optional[int]
for steps, cmake_binary in enumerate(versions):
print(
"[{progress:3.0f}%] CMake {cmake_version:{longest_version_string}}".format(
progress=100.0 * float(steps) / len(versions),
cmake_version=cmake_binary.version,
longest_version_string=longest_version_string,
),
end="",
flush=True,
)
result = try_configure(binary=cmake_binary.binary, cmake_parameters=cmake_parameters) # type: ConfigureResult
if result.success:
print(colored("✔ works", "green"))
if not last_success_idx:
last_success_idx = steps
else:
last_success_idx = None
print(colored("✘ error", "red"))
if error_output:
for line in result.stderr.splitlines():
print(colored(f" {line}", "yellow"))
elif result.reason:
print(colored(f" {result.reason}", "yellow"))
return versions[last_success_idx] if last_success_idx is not None else None
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Find the minimal required CMake version for a project.")
parser.add_argument("params", type=str, nargs="+", help="parameters to pass to CMake")
parser.add_argument(
"--tools_directory",
metavar="DIR",
default="tools",
help='path to the CMake binaries (default: "tools")',
)
parser.add_argument(
"--full_search",
default=False,
action="store_true",
help="Searches using a top down approach instead of a binary search (default: False)",
)
parser.add_argument(
"--error_details",
default=False,
action="store_true",
help="Print the full stderr output in case of an error (default: False)",
)
args = parser.parse_args()
if args.full_search:
working_version = full_search(
cmake_parameters=args.params,
tools_dir=Path(args.tools_directory),
error_output=args.error_details,
)
else:
working_version = binary_search(
cmake_parameters=args.params,
tools_dir=Path(args.tools_directory),
error_output=args.error_details,
)
if working_version:
print(
"[100%] Minimal working version: {cmake} {version}".format(
cmake=colored("CMake", "blue"),
version=colored(working_version.version, "blue"),
),
)
print(f"\ncmake_minimum_required(VERSION {working_version.version})")
else:
print("[100%] {message}".format(message=colored("ERROR: Could not find working version.", "red")))