-
Notifications
You must be signed in to change notification settings - Fork 991
/
Copy pathtest_cmakedeps_build_modules.py
206 lines (171 loc) · 6.88 KB
/
test_cmakedeps_build_modules.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
import textwrap
import pytest
from conan.test.utils.tools import TestClient
@pytest.mark.tool("cmake")
def test_build_modules_alias_target():
client = TestClient()
conanfile = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import copy
class Conan(ConanFile):
name = "hello"
version = "1.0"
settings = "os", "arch", "compiler", "build_type"
exports_sources = ["target-alias.cmake"]
def package(self):
copy(self, "target-alias.cmake", self.source_folder,
os.path.join(self.package_folder, "share/cmake"))
def package_info(self):
module = os.path.join("share", "cmake", "target-alias.cmake")
self.cpp_info.set_property("cmake_build_modules", [module])
""")
target_alias = textwrap.dedent("""
add_library(otherhello INTERFACE IMPORTED)
target_link_libraries(otherhello INTERFACE hello::hello)
""")
client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias})
client.run("create .")
consumer = textwrap.dedent("""
from conan import ConanFile
from conan.tools.cmake import CMake
class Conan(ConanFile):
name = "consumer"
version = "1.0"
settings = "os", "compiler", "build_type", "arch"
exports_sources = ["CMakeLists.txt"]
generators = "CMakeDeps", "CMakeToolchain"
requires = "hello/1.0"
def build(self):
cmake = CMake(self)
cmake.configure()
""")
cmakelists = textwrap.dedent("""
cmake_minimum_required(VERSION 3.0)
project(test)
find_package(hello CONFIG)
get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES)
message("otherhello link libraries: ${tmp}")
""")
client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists})
client.run("create .")
assert "otherhello link libraries: hello::hello" in client.out
@pytest.mark.tool("cmake")
def test_build_modules_custom_script():
client = TestClient()
conanfile = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import copy
class Conan(ConanFile):
name = "myfunctions"
version = "1.0"
exports_sources = ["*.cmake"]
def package(self):
copy(self, "*.cmake", self.source_folder, self.package_folder)
def package_info(self):
self.cpp_info.set_property("cmake_build_modules", ["myfunction.cmake"])
""")
myfunction = textwrap.dedent("""
function(myfunction)
message("Hello myfunction!!!!")
endfunction()
""")
client.save({"conanfile.py": conanfile, "myfunction.cmake": myfunction})
client.run("create .")
consumer = textwrap.dedent("""
from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps
class Conan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeToolchain"
tool_requires = "myfunctions/1.0"
def generate(self):
deps = CMakeDeps(self)
deps.build_context_activated = ["myfunctions"]
deps.build_context_build_modules = ["myfunctions"]
deps.set_property("myfunctions", "cmake_find_mode", "module", build_context=True)
deps.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
""")
cmakelists = textwrap.dedent("""
cmake_minimum_required(VERSION 3.0)
project(test)
find_package(myfunctions MODULE REQUIRED)
myfunction()
""")
client.save({"conanfile.py": consumer,
"CMakeLists.txt": cmakelists},
clean_first=True)
client.run("build .")
assert "Hello myfunction!!!!" in client.out
@pytest.mark.tool("cmake")
def test_build_modules_components_is_not_possible():
"""
The "cmake_build_module" property declared in the components is useless
"""
client = TestClient()
conanfile = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import copy
class Conan(ConanFile):
name = "openssl"
version = "1.0"
settings = "os", "arch", "compiler", "build_type"
exports_sources = ["crypto.cmake", "root.cmake"]
def package(self):
copy(self, "*.cmake", self.source_folder, os.path.join(self.package_folder, "share/cmake"))
def package_info(self):
crypto_module = os.path.join("share", "cmake", "crypto.cmake")
self.cpp_info.components["crypto"].set_property("cmake_build_modules", [crypto_module])
root_module = os.path.join("share", "cmake", "root.cmake")
self.cpp_info.set_property("cmake_build_modules", [root_module])
""")
crypto_cmake = textwrap.dedent("""
function(crypto_message MESSAGE_OUTPUT)
message("CRYPTO MESSAGE:${ARGV${0}}")
endfunction()
""")
root_cmake = textwrap.dedent("""
function(root_message MESSAGE_OUTPUT)
message("ROOT MESSAGE:${ARGV${0}}")
endfunction()
""")
client.save({"conanfile.py": conanfile,
"crypto.cmake": crypto_cmake,
"root.cmake": root_cmake})
client.run("create .")
consumer = textwrap.dedent("""
from conan import ConanFile
from conan.tools.cmake import CMake
class Conan(ConanFile):
name = "consumer"
version = "1.0"
settings = "os", "compiler", "build_type", "arch"
exports_sources = ["CMakeLists.txt"]
generators = "CMakeDeps", "CMakeToolchain"
requires = "openssl/1.0"
def build(self):
cmake = CMake(self)
cmake.configure()
def package_info(self):
self.cpp_info.requires = ["openssl::crypto"]
""")
cmakelists = textwrap.dedent("""
cmake_minimum_required(VERSION 3.0)
project(test)
find_package(openssl CONFIG)
crypto_message("hello!")
root_message("hello!")
""")
client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists})
# As we are requiring only "crypto" but it doesn't matter, it is not possible to include
# only crypto build_modules
client.run("create .", assert_error=True)
assert 'Unknown CMake command "crypto_message"' in client.out
# Comment the function call
client.save({"CMakeLists.txt": cmakelists.replace("crypto", "#crypto")})
assert "ROOT MESSAGE:hello!" not in client.out