-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathtest_cmake_config.py
275 lines (232 loc) · 7.58 KB
/
test_cmake_config.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from __future__ import annotations
import os
import shutil
import sysconfig
from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING
import pytest
from packaging.specifiers import SpecifierSet
from packaging.version import Version
from scikit_build_core.builder.builder import Builder
from scikit_build_core.cmake import CMake, CMaker
from scikit_build_core.errors import CMakeNotFoundError
from scikit_build_core.program_search import best_program, get_cmake_programs
from scikit_build_core.settings.skbuild_read_settings import SettingsReader
if TYPE_CHECKING:
from collections.abc import Generator
DIR = Path(__file__).parent.resolve()
cmake_preset_info = best_program(get_cmake_programs(), version=SpecifierSet(">=3.19"))
def single_config(param: None | str) -> bool:
if param is None:
return not sysconfig.get_platform().startswith("win")
return param in {"Ninja", "Makefiles"}
@pytest.fixture(
params=[
pytest.param(None, id="default"),
pytest.param("Ninja", id="ninja"),
pytest.param(
"Makefiles",
id="makefiles",
marks=pytest.mark.skipif(
sysconfig.get_platform().startswith("win"), reason="run on Windows only"
),
),
pytest.param(
"Others",
id="others",
marks=pytest.mark.skipif(
sysconfig.get_platform().startswith("win"), reason="run on Windows only"
),
),
]
)
def generator(
request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch
) -> str | None:
if request.param is None:
monkeypatch.delenv("CMAKE_GENERATOR", raising=False)
else:
monkeypatch.setenv("CMAKE_GENERATOR", request.param)
return request.param # type: ignore[no-any-return]
def configure_args(
config: CMaker, *, init: bool = False, single_config: bool = False
) -> Generator[str, None, None]:
yield f"-S{config.source_dir}"
yield f"-B{config.build_dir}"
if single_config:
yield f"-DCMAKE_BUILD_TYPE:STRING={config.build_type}"
if init:
cmake_init = config.build_dir / "CMakeInit.txt"
yield f"-C{cmake_init}"
@pytest.mark.configure
def test_init_cache(
generator: str,
tmp_path: Path,
fp,
):
fp.register(
[fp.program("cmake"), "-E", "capabilities"],
stdout='{"version":{"string":"3.14.0"}}',
)
fp.register(
[fp.program("cmake3"), "-E", "capabilities"],
stdout='{"version":{"string":"3.14.0"}}',
)
config = CMaker(
CMake.default_search(),
source_dir=DIR / "packages/simple_pure",
build_dir=tmp_path / "build",
build_type="Release",
)
config.init_cache(
{"SKBUILD": True, "SKBUILD_VERSION": "1.0.0", "SKBUILD_PATH": config.source_dir}
)
cmd = list(
configure_args(config, init=True, single_config=single_config(generator))
)
print("Registering: cmake", *cmd)
fp.register([fp.program("cmake"), *cmd])
fp.register([fp.program("cmake3"), *cmd])
config.configure()
cmake_init = config.build_dir / "CMakeInit.txt"
source_dir_str = str(config.source_dir).replace("\\", "/")
assert (
cmake_init.read_text(encoding="utf-8")
== f"""\
set(SKBUILD ON CACHE BOOL "" FORCE)
set(SKBUILD_VERSION [===[1.0.0]===] CACHE STRING "" FORCE)
set(SKBUILD_PATH [===[{source_dir_str}]===] CACHE PATH "" FORCE)
"""
)
@pytest.mark.configure
def test_too_old(fp, monkeypatch):
monkeypatch.setattr(shutil, "which", lambda _: None)
fp.register(
[fp.program("cmake"), "-E", "capabilities"],
stdout='{"version":{"string":"3.14.0"}}',
)
fp.register(
[fp.program("cmake3"), "-E", "capabilities"],
stdout='{"version":{"string":"3.14.0"}}',
)
with pytest.raises(CMakeNotFoundError) as excinfo:
CMake.default_search(version=SpecifierSet(">=3.15"))
assert "Could not find CMake with version >=3.15" in excinfo.value.args[0]
@pytest.mark.configure
def test_cmake_args(
generator: str,
tmp_path: Path,
fp,
):
fp.register(
[fp.program("cmake"), "-E", "capabilities"],
stdout='{"version":{"string":"3.15.0"}}',
)
fp.register(
[fp.program("cmake3"), "-E", "capabilities"],
stdout='{"version":{"string":"3.15.0"}}',
)
config = CMaker(
CMake.default_search(),
source_dir=DIR / "packages" / "simple_pure",
build_dir=tmp_path / "build",
build_type="Release",
)
cmd = list(configure_args(config, single_config=single_config(generator)))
cmd.append("-DSOMETHING=one")
print("Registering: cmake", *cmd)
fp.register([fp.program("cmake"), *cmd])
fp.register([fp.program("cmake3"), *cmd])
config.configure(cmake_args=["-DSOMETHING=one"])
# config.configure might mutate config.single_config
assert config.single_config == single_config(generator)
assert len(fp.calls) == 2
@pytest.mark.configure
def test_cmake_paths(
generator: str,
tmp_path: Path,
fp,
):
fp.register(
[fp.program("cmake"), "-E", "capabilities"],
stdout='{"version":{"string":"3.15.0"}}',
)
fp.register(
[fp.program("cmake3"), "-E", "capabilities"],
stdout='{"version":{"string":"3.15.0"}}',
)
config = CMaker(
CMake.default_search(),
source_dir=DIR / "packages/simple_pure",
build_dir=tmp_path / "build",
build_type="Release",
prefix_dirs=[tmp_path / "prefix"],
module_dirs=[tmp_path / "module"],
)
cmd = list(configure_args(config, single_config=single_config(generator)))
print("Registering: cmake", *cmd)
fp.register([fp.program("cmake"), *cmd])
fp.register([fp.program("cmake3"), *cmd])
config.configure()
assert len(fp.calls) == 2
@pytest.mark.parametrize(
"with_preset",
[
pytest.param(
True,
marks=pytest.mark.skipif(
cmake_preset_info is None,
reason="CMake version does not support presets.",
),
),
False,
],
)
@pytest.mark.configure
def test_cmake_defines(
monkeypatch,
tmp_path: Path,
with_preset: bool,
):
monkeypatch.setenv("WITH_PRESET", f"{with_preset}")
source_dir = DIR / "packages" / "cmake_defines"
binary_dir = tmp_path / "build"
config = CMaker(
CMake.default_search(),
source_dir=source_dir,
build_dir=binary_dir,
build_type="Release",
)
reader = SettingsReader.from_file(source_dir / "pyproject.toml")
builder = Builder(reader.settings, config)
builder.configure(defines={})
configure_log = Path.read_text(binary_dir / "log.txt")
# This var is always overwritten
overwritten_var = "overwritten"
preset_only_var = "defined" if with_preset else ""
assert configure_log == dedent(
f"""\
PRESET_ONLY_VAR={preset_only_var}
OVERWRITTEN_VAR={overwritten_var}
ONE_LEVEL_LIST.LENGTH = 4
Foo
Bar
ExceptionallyLargeListEntryThatWouldOverflowTheLine
Baz
NESTED_LIST.LENGTH = 3
Apple
Lemon;Lime
Banana
"""
)
def test_get_cmake_via_envvar(monkeypatch: pytest.MonkeyPatch, fp):
monkeypatch.setattr("shutil.which", lambda x: x)
cmake_path = Path("some-prog")
fp.register(
[cmake_path, "-E", "capabilities"], stdout='{"version":{"string":"3.20.0"}}'
)
monkeypatch.setenv("CMAKE_EXECUTABLE", str(cmake_path))
result = CMake.default_search(env=os.environ)
assert result.cmake_path == cmake_path
assert result.version == Version("3.20.0")