File: test_cmaker.py

package info (click to toggle)
scikit-build 0.18.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,792 kB
  • sloc: python: 5,258; cpp: 284; makefile: 171; f90: 12; sh: 7
file content (227 lines) | stat: -rw-r--r-- 8,943 bytes parent folder | download
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
"""test_cmaker
----------------------------------

Tests for CMaker functionality.
"""

from __future__ import annotations

import os
import re
import textwrap

import pytest

from skbuild.cmaker import CMaker, has_cmake_cache_arg
from skbuild.constants import (
    CMAKE_BUILD_DIR,
    CMAKE_DEFAULT_EXECUTABLE,
    CMAKE_INSTALL_DIR,
)
from skbuild.exceptions import SKBuildError
from skbuild.utils import push_dir, to_unix_path

from . import _tmpdir, get_cmakecache_variables


def test_get_python_version():
    assert re.match(r"^[23](\.?)\d+$", CMaker.get_python_version())


def test_get_python_include_dir():
    python_include_dir = CMaker.get_python_include_dir(CMaker.get_python_version())
    assert python_include_dir
    assert os.path.exists(python_include_dir)


def test_get_python_library():
    python_library = CMaker.get_python_library(CMaker.get_python_version())
    assert python_library
    assert os.path.exists(python_library)


def test_cmake_executable():
    assert CMaker().cmake_executable == CMAKE_DEFAULT_EXECUTABLE


def test_has_cmake_cache_arg():
    cmake_args = ["-DFOO:STRING=42", "-DBAR", "-DCLIMBING:BOOL=ON"]
    assert has_cmake_cache_arg(cmake_args, "FOO", "42")
    assert not has_cmake_cache_arg(cmake_args, "foo", "42")
    assert not has_cmake_cache_arg(cmake_args, "FOO", "43")
    assert not has_cmake_cache_arg(cmake_args, "BAR")
    assert not has_cmake_cache_arg(cmake_args, "BA")
    assert not has_cmake_cache_arg(cmake_args, "BAR", None)
    assert not has_cmake_cache_arg(cmake_args, "BAR", "42")
    assert has_cmake_cache_arg(cmake_args, "CLIMBING")
    assert has_cmake_cache_arg(cmake_args, "CLIMBING", None)
    assert has_cmake_cache_arg(cmake_args, "CLIMBING", "ON")

    override = ["-DOTHER:STRING=C", "-DOVERRIDE:STRING=A", "-DOVERRIDE:STRING=B"]
    assert has_cmake_cache_arg(override, "OVERRIDE")
    assert has_cmake_cache_arg(override, "OVERRIDE", "B")
    assert not has_cmake_cache_arg(override, "OVERRIDE", "A")
    # ensure overriding doesn't magically have side effects.
    assert has_cmake_cache_arg(override, "OTHER")
    assert has_cmake_cache_arg(override, "OTHER", "C")
    assert not has_cmake_cache_arg(override, "OTHER", "A")
    assert not has_cmake_cache_arg(override, "OTHER", "B")


def test_make_without_build_dir_fails():
    src_dir = _tmpdir("test_make_without_build_dir_fails")
    with push_dir(str(src_dir)), pytest.raises(SKBuildError) as excinfo:
        CMaker().make()
    assert "Did you forget to run configure before make" in str(excinfo.value)


def test_make_without_configure_fails(capfd):
    src_dir = _tmpdir("test_make_without_configure_fails")
    src_dir.ensure(CMAKE_BUILD_DIR(), dir=1)
    with push_dir(str(src_dir)), pytest.raises(SKBuildError) as excinfo:
        CMaker().make()
    _, err = capfd.readouterr()
    assert "An error occurred while building with CMake." in str(excinfo.value)
    assert "Error: could not load cache" in err or "Error: not a CMake build directory" in err


@pytest.mark.parametrize("configure_with_cmake_source_dir", [True, False])
def test_make(configure_with_cmake_source_dir, capfd):
    tmp_dir = _tmpdir("test_make")
    with push_dir(str(tmp_dir)):
        src_dir = tmp_dir.ensure("SRC", dir=1)
        src_dir.join("CMakeLists.txt").write(
            textwrap.dedent(
                """
            cmake_minimum_required(VERSION 3.5.0)
            project(foobar NONE)
            file(WRITE "${CMAKE_BINARY_DIR}/foo.txt" "# foo")
            install(FILES "${CMAKE_BINARY_DIR}/foo.txt" DESTINATION ".")
            install(CODE "message(STATUS \\"Project has been installed\\")")
            message(STATUS "CMAKE_SOURCE_DIR:${CMAKE_SOURCE_DIR}")
            message(STATUS "CMAKE_BINARY_DIR:${CMAKE_BINARY_DIR}")
            """
            )
        )
        src_dir.ensure(CMAKE_BUILD_DIR(), dir=1)

        with push_dir(str(src_dir) if not configure_with_cmake_source_dir else str(tmp_dir.ensure("BUILD", dir=1))):
            cmkr = CMaker()
            config_kwargs = {}
            if configure_with_cmake_source_dir:
                config_kwargs["cmake_source_dir"] = str(src_dir)
            env = cmkr.configure(**config_kwargs)  # type: ignore[arg-type]
            cmkr.make(env=env)

        messages = ["Project has been installed"]

        if configure_with_cmake_source_dir:
            messages += [
                "/SRC",
                f"/BUILD/{to_unix_path(CMAKE_BUILD_DIR())}",
                f"/BUILD/{to_unix_path(CMAKE_INSTALL_DIR())}/./foo.txt",
            ]
        else:
            messages += [
                "/SRC",
                f"/SRC/{to_unix_path(CMAKE_BUILD_DIR())}",
                f"/SRC/{to_unix_path(CMAKE_INSTALL_DIR())}/./foo.txt",
            ]

        out, _ = capfd.readouterr()
        for message in messages:
            assert message in out


@pytest.mark.parametrize("install_target", ["", "install", "install-runtime", "nonexistant-install-target"])
def test_make_with_install_target(install_target, capfd):
    tmp_dir = _tmpdir("test_make_with_install_target")
    with push_dir(str(tmp_dir)):
        tmp_dir.join("CMakeLists.txt").write(
            textwrap.dedent(
                """
            cmake_minimum_required(VERSION 3.5.0)
            project(foobar NONE)
            file(WRITE "${CMAKE_BINARY_DIR}/foo.txt" "# foo")
            file(WRITE "${CMAKE_BINARY_DIR}/runtime.txt" "# runtime")
            install(FILES "${CMAKE_BINARY_DIR}/foo.txt" DESTINATION ".")
            install(CODE "message(STATUS \\"Project has been installed\\")")
            install(FILES "${CMAKE_BINARY_DIR}/runtime.txt" DESTINATION "." COMPONENT runtime)
            install(CODE "message(STATUS \\"Runtime component has been installed\\")" COMPONENT runtime)

            # Add custom target to only install component: runtime (libraries)
            add_custom_target(install-runtime
              ${CMAKE_COMMAND}
              -DCMAKE_INSTALL_COMPONENT=runtime
              -P "${PROJECT_BINARY_DIR}/cmake_install.cmake"
              )
            """
            )
        )

        with push_dir(str(tmp_dir)):
            cmkr = CMaker()
            env = cmkr.configure()
            if install_target in ["", "install", "install-runtime"]:
                cmkr.make(install_target=install_target, env=env)
            else:
                with pytest.raises(SKBuildError) as excinfo:
                    cmkr.make(install_target=install_target, env=env)
                assert "check the install target is valid" in str(excinfo.value)

        out, err = capfd.readouterr()
        # This message appears with both install_targets: default 'install' and
        # 'install-runtime'
        message = "Runtime component has been installed"
        if install_target in ["install", "install-runtime"]:
            assert message in out

        # One of these error appears with install_target: nonexistant-install-target
        err_message1 = "No rule to make target"
        err_message2 = "unknown target"
        err_message3 = "CMAKE_MAKE_PROGRAM is not set"  # Showing up on windows-2016
        if install_target == "nonexistant-install-target":
            assert err_message1 in err or err_message2 in err or err_message3 in err


def test_configure_with_cmake_args(capfd):
    tmp_dir = _tmpdir("test_configure_with_cmake_args")
    with push_dir(str(tmp_dir)):
        tmp_dir.join("CMakeLists.txt").write(
            textwrap.dedent(
                """
            cmake_minimum_required(VERSION 3.5.0)
            project(foobar NONE)
            # Do not complain about missing arguments passed to the main
            # project
            """
            )
        )

        with push_dir(str(tmp_dir)):
            cmkr = CMaker()
            cmkr.configure(clargs=["-DCMAKE_EXPECTED_FOO:STRING=foo", "-DCMAKE_EXPECTED_BAR:STRING=bar"], cleanup=False)

        cmakecache = tmp_dir.join("_cmake_test_compile", "build", "CMakeCache.txt")
        assert cmakecache.exists()
        variables = get_cmakecache_variables(str(cmakecache))
        assert variables.get("CMAKE_EXPECTED_FOO", (None, None))[1] == "foo"
        assert variables.get("CMAKE_EXPECTED_BAR", (None, None))[1] == "bar"

        unexpected = "Manually-specified variables were not used by the project"
        _, err = capfd.readouterr()
        assert unexpected not in err


def test_check_for_bad_installs(tmpdir):
    with push_dir(str(tmpdir)):
        tmpdir.ensure(CMAKE_BUILD_DIR(), "cmake_install.cmake").write(
            textwrap.dedent(
                """
            file(INSTALL DESTINATION "${CMAKE_INSTALL_PREFIX}/../hello" TYPE FILE FILES "/path/to/hello/world.py")
            """
            )
        )
        with pytest.raises(SKBuildError) as excinfo:
            CMaker.check_for_bad_installs()
        assert "CMake-installed files must be within the project root" in str(excinfo.value)