/
test_cmakedeps.py
187 lines (158 loc) · 7.5 KB
/
test_cmakedeps.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
import os
import textwrap
from conans.test.assets.genconanfile import GenConanfile
from conans.test.utils.tools import TestClient
def test_package_from_system():
"""
If a node declares "system_package" property, the CMakeDeps generator will skip generating
the -config.cmake and the other files for that node but will keep the "find_dependency" for
the nodes depending on it. That will cause that cmake looks for the config files elsewhere
https://github.com/conan-io/conan/issues/8919"""
client = TestClient()
dep2 = str(GenConanfile().with_name("dep2").with_version("1.0")
.with_settings("os", "arch", "build_type", "compiler"))
dep2 += """
def package_info(self):
self.cpp_info.set_property("cmake_find_mode", "None")
self.cpp_info.set_property("cmake_file_name", "custom_dep2")
"""
client.save({"conanfile.py": dep2})
client.run("create .")
dep1 = GenConanfile().with_name("dep1").with_version("1.0").with_require("dep2/1.0")\
.with_settings("os", "arch", "build_type", "compiler")
client.save({"conanfile.py": dep1})
client.run("create .")
consumer = GenConanfile().with_name("consumer").with_version("1.0").\
with_require("dep1/1.0").with_generator("CMakeDeps").\
with_settings("os", "arch", "build_type", "compiler")
client.save({"conanfile.py": consumer})
client.run("install .")
assert os.path.exists(os.path.join(client.current_folder, "dep1-config.cmake"))
assert not os.path.exists(os.path.join(client.current_folder, "dep2-config.cmake"))
assert not os.path.exists(os.path.join(client.current_folder, "custom_dep2-config.cmake"))
dep1_contents = client.load("dep1-release-x86_64-data.cmake")
assert 'list(APPEND dep1_FIND_DEPENDENCY_NAMES custom_dep2)' in dep1_contents
assert 'set(custom_dep2_FIND_MODE "")' in dep1_contents
def test_test_package():
client = TestClient()
client.save({"conanfile.py": GenConanfile()})
client.run("create . gtest/1.0@")
client.run("create . cmake/1.0@")
client.save({"conanfile.py": GenConanfile().with_build_requires("cmake/1.0").
with_build_requirement("gtest/1.0", force_host_context=True)})
client.run("export . pkg/1.0@")
consumer = textwrap.dedent(r"""
from conans import ConanFile
class Pkg(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps"
requires = "pkg/1.0"
""")
client.save({"conanfile.py": consumer})
client.run("install . -s:b os=Windows -s:h os=Linux --build=missing")
cmake_data = client.load("pkg-release-x86_64-data.cmake")
assert "gtest" not in cmake_data
def test_components_error():
# https://github.com/conan-io/conan/issues/9331
client = TestClient()
conan_hello = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.files import save
class Pkg(ConanFile):
settings = "os", "arch", "compiler", "build_type"
def layout(self):
pass
def package_info(self):
self.cpp_info.components["say"].includedirs = ["include"]
""")
client.save({"conanfile.py": conan_hello})
client.run("create . hello/1.0@")
def test_cpp_info_component_objects():
client = TestClient()
conan_hello = textwrap.dedent("""
from conans import ConanFile
class Pkg(ConanFile):
settings = "os", "arch", "build_type"
def package_info(self):
self.cpp_info.components["say"].objects = ["mycomponent.o"]
""")
client.save({"conanfile.py": conan_hello})
client.run("create . hello/1.0@ -s arch=x86_64 -s build_type=Release")
client.run("install hello/1.0@ -g CMakeDeps -s arch=x86_64 -s build_type=Release")
with open(os.path.join(client.current_folder, "hello-Target-release.cmake")) as f:
content = f.read()
assert """set_property(TARGET hello::say
PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:Release>:${hello_hello_say_OBJECTS_RELEASE}>
${hello_hello_say_LIBRARIES_TARGETS}""" in content
# If there are componets, there is not a global cpp so this is not generated
assert """set_property(TARGET hello::hello
PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:Release>:${hello_OBJECTS_RELEASE}>
${hello_LIBRARIES_TARGETS}""" not in content
# But the global target is linked with the targets from the components
assert "target_link_libraries(hello::hello INTERFACE hello::say)" in content
with open(os.path.join(client.current_folder, "hello-release-x86_64-data.cmake")) as f:
content = f.read()
# Not global variables
assert 'set(hello_OBJECTS_RELEASE "${hello_PACKAGE_FOLDER_RELEASE}/mycomponent.o")' \
not in content
# But component variables
assert 'set(hello_hello_say_OBJECTS_RELEASE "${hello_PACKAGE_FOLDER_RELEASE}/' \
'mycomponent.o")' in content
def test_cpp_info_component_error_aggregate():
# https://github.com/conan-io/conan/issues/10176
# This test was consistently failing because "VirtualRunEnv" was not doing a "copy()"
# of cpp_info before calling "aggregate_components()", and it was destructive, removing
# components data
client = TestClient()
conan_hello = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
def package_info(self):
self.cpp_info.components["say"].includedirs = ["include"]
""")
consumer = textwrap.dedent("""
from conans import ConanFile
class Pkg(ConanFile):
settings = "os", "compiler", "arch", "build_type"
requires = "hello/1.0"
generators = "VirtualRunEnv", "CMakeDeps"
def package_info(self):
self.cpp_info.components["chat"].requires = ["hello::say"]
""")
test_package = textwrap.dedent("""
from conans import ConanFile
class Pkg(ConanFile):
settings = "os", "compiler", "arch", "build_type"
generators = "VirtualRunEnv", "CMakeDeps"
def test(self):
pass
""")
client.save({"hello/conanfile.py": conan_hello,
"consumer/conanfile.py": consumer,
"consumer/test_package/conanfile.py": test_package})
client.run("create hello hello/1.0@")
client.run("create consumer consumer/1.0@")
assert "consumer/1.0 (test package): Running test()" in client.out
def test_cmakedeps_cppinfo_complex_strings():
client = TestClient(path_with_spaces=False)
conanfile = textwrap.dedent(r'''
from conans import ConanFile
class HelloLib(ConanFile):
def package_info(self):
self.cpp_info.defines.append("escape=partially \"escaped\"")
self.cpp_info.defines.append("spaces=me you")
self.cpp_info.defines.append("foobar=bazbuz")
self.cpp_info.defines.append("answer=42")
''')
client.save({"conanfile.py": conanfile})
client.run("export . hello/1.0@")
client.save({"conanfile.txt": "[requires]\nhello/1.0\n"}, clean_first=True)
client.run("install . --build=missing -g CMakeDeps")
deps = client.load("hello-release-x86_64-data.cmake")
assert r"escape=partially \"escaped\"" in deps
assert r"spaces=me you" in deps
assert r"foobar=bazbuz" in deps
assert r"answer=42" in deps