forked from conan-io/conan
/
test_cmakedeps.py
443 lines (381 loc) · 18.4 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import os
import platform
import textwrap
import pytest
from conans.client.tools import replace_in_file
from conans.test.assets.cmake import gen_cmakelists
from conans.test.assets.genconanfile import GenConanfile
from conans.test.assets.pkg_cmake import pkg_cmake
from conans.test.assets.sources import gen_function_cpp, gen_function_h
from conans.test.utils.tools import TestClient
@pytest.fixture
def client():
c = TestClient()
conanfile = textwrap.dedent("""
from conans import ConanFile
from conans.tools import save
import os
class Pkg(ConanFile):
settings = "build_type", "os", "arch", "compiler"
{}
def package(self):
save(os.path.join(self.package_folder, "include", "%s.h" % self.name),
'#define MYVAR%s "%s"' % (self.name, self.settings.build_type))
""")
c.save({"conanfile.py": conanfile.format("")})
c.run("create . liba/0.1@ -s build_type=Release")
c.run("create . liba/0.1@ -s build_type=Debug")
c.save({"conanfile.py": conanfile.format("requires = 'liba/0.1'")})
c.run("create . libb/0.1@ -s build_type=Release")
c.run("create . libb/0.1@ -s build_type=Debug")
return c
@pytest.mark.tool_cmake
def test_transitive_multi(client):
# TODO: Make a full linking example, with correct header transitivity
# Save conanfile and example
conanfile = textwrap.dedent("""
[requires]
libb/0.1
[generators]
CMakeDeps
CMakeToolchain
""")
example_cpp = gen_function_cpp(name="main", includes=["libb", "liba"],
preprocessor=["MYVARliba", "MYVARlibb"])
client.save({"conanfile.txt": conanfile,
"CMakeLists.txt": gen_cmakelists(appname="example",
appsources=["example.cpp"], find_package=["libb"]),
"example.cpp": example_cpp}, clean_first=True)
with client.chdir("build"):
for bt in ("Debug", "Release"):
client.run("install .. user/channel -s build_type={}".format(bt))
# Test that we are using find_dependency with the NO_MODULE option
# to skip finding first possible FindBye somewhere
assert "find_dependency(${_DEPENDENCY} REQUIRED ${${_DEPENDENCY}_FIND_MODE})" \
in client.load("libb-config.cmake")
assert 'set(liba_FIND_MODE "NO_MODULE")' in client.load("libb-release-x86_64-data.cmake")
if platform.system() == "Windows":
client.run_command('cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake')
client.run_command('cmake --build . --config Debug')
client.run_command('cmake --build . --config Release')
client.run_command('Debug\\example.exe')
assert "main: Debug!" in client.out
assert "MYVARliba: Debug" in client.out
assert "MYVARlibb: Debug" in client.out
client.run_command('Release\\example.exe')
assert "main: Release!" in client.out
assert "MYVARliba: Release" in client.out
assert "MYVARlibb: Release" in client.out
else:
# The CMakePresets IS MESSING WITH THE BUILD TYPE and then ignores the -D so I remove it
replace_in_file(os.path.join(client.current_folder, "CMakePresets.json"),
"CMAKE_BUILD_TYPE", "DONT_MESS_WITH_BUILD_TYPE")
for bt in ("Debug", "Release"):
client.run_command('cmake .. -DCMAKE_BUILD_TYPE={} '
'-DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake'.format(bt))
client.run_command('cmake --build . --clean-first')
client.run_command('./example')
assert "main: {}!".format(bt) in client.out
assert "MYVARliba: {}".format(bt) in client.out
assert "MYVARlibb: {}".format(bt) in client.out
@pytest.mark.tool_cmake
def test_system_libs():
conanfile = textwrap.dedent("""
from conans import ConanFile
from conans.tools import save
import os
class Test(ConanFile):
name = "Test"
version = "0.1"
settings = "build_type"
def package(self):
save(os.path.join(self.package_folder, "lib/lib1.lib"), "")
save(os.path.join(self.package_folder, "lib/liblib1.a"), "")
def package_info(self):
self.cpp_info.libs = ["lib1"]
if self.settings.build_type == "Debug":
self.cpp_info.system_libs.append("sys1d")
else:
self.cpp_info.system_libs.append("sys1")
""")
client = TestClient()
client.save({"conanfile.py": conanfile})
client.run("create . -s build_type=Release")
client.run("create . -s build_type=Debug")
conanfile = textwrap.dedent("""
[requires]
Test/0.1
[generators]
CMakeDeps
""")
cmakelists = textwrap.dedent("""
cmake_minimum_required(VERSION 3.15)
project(consumer NONE)
set(CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
find_package(Test)
message("System libs release: ${Test_SYSTEM_LIBS_RELEASE}")
message("Libraries to Link release: ${Test_LIBS_RELEASE}")
message("System libs debug: ${Test_SYSTEM_LIBS_DEBUG}")
message("Libraries to Link debug: ${Test_LIBS_DEBUG}")
get_target_property(tmp Test::Test INTERFACE_LINK_LIBRARIES)
message("Target libs: ${tmp}")
""")
for build_type in ["Release", "Debug"]:
client.save({"conanfile.txt": conanfile, "CMakeLists.txt": cmakelists}, clean_first=True)
client.run("install conanfile.txt -s build_type=%s" % build_type)
client.run_command('cmake . -DCMAKE_BUILD_TYPE={0}'.format(build_type))
library_name = "sys1d" if build_type == "Debug" else "sys1"
# FIXME: Note it is CONAN_LIB::Test_lib1_RELEASE, not "lib1" as cmake_find_package
if build_type == "Release":
assert "System libs release: %s" % library_name in client.out
assert "Libraries to Link release: lib1" in client.out
else:
assert "System libs debug: %s" % library_name in client.out
assert "Libraries to Link debug: lib1" in client.out
# FIXME: This assert is ugly, empty configs comes from empty _OBJECTS, FRAMEWORKS etc
target_libs = f"$<$<CONFIG:{build_type}>:>;CONAN_LIB::Test_lib1;$<$<CONFIG:{build_type}>" \
f":>;$<$<CONFIG:{build_type}>:>;$<$<CONFIG:{build_type}>:{library_name}>"
assert target_libs in client.out
@pytest.mark.tool_cmake
def test_do_not_mix_cflags_cxxflags():
# TODO: Verify with components too
client = TestClient()
cpp_info = {"cflags": ["one", "two"], "cxxflags": ["three", "four"]}
client.save({"conanfile.py": GenConanfile("upstream", "1.0").with_package_info(cpp_info=cpp_info,
env_info={})})
client.run("create .")
consumer_conanfile = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import CMake
class Consumer(ConanFile):
name = "consumer"
version = "1.0"
settings = "os", "compiler", "arch", "build_type"
exports_sources = "CMakeLists.txt"
requires = "upstream/1.0"
generators = "CMakeDeps", "CMakeToolchain"
def build(self):
cmake = CMake(self)
cmake.configure()
""")
cmakelists = textwrap.dedent("""
cmake_minimum_required(VERSION 3.15)
project(consumer NONE)
find_package(upstream CONFIG REQUIRED)
get_target_property(tmp upstream::upstream INTERFACE_COMPILE_OPTIONS)
message("compile options: ${tmp}")
message("cflags: ${upstream_COMPILE_OPTIONS_C_RELEASE}")
message("cxxflags: ${upstream_COMPILE_OPTIONS_CXX_RELEASE}")
""")
client.save({"conanfile.py": consumer_conanfile,
"CMakeLists.txt": cmakelists}, clean_first=True)
client.run("create .")
assert "compile options: $<$<CONFIG:Release>:" \
"$<$<COMPILE_LANGUAGE:CXX>:three;four>;$<$<COMPILE_LANGUAGE:C>:one;two>>" in client.out
assert "cflags: one;two" in client.out
assert "cxxflags: three;four" in client.out
def test_custom_configuration(client):
""" The configuration may differ from the build context and the host context"""
conanfile = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import CMakeDeps
class Consumer(ConanFile):
name = "consumer"
version = "1.0"
settings = "os", "compiler", "arch", "build_type"
requires = "liba/0.1"
build_requires = "liba/0.1"
generators = "CMakeToolchain"
def generate(self):
cmake = CMakeDeps(self)
cmake.configuration = "Debug"
cmake.build_context_activated = ["liba"]
cmake.build_context_suffix["liba"] = "_build"
cmake.generate()
""")
client.save({"conanfile.py": conanfile})
client.run("install . -pr:h default -s:b build_type=RelWithDebInfo"
" -pr:b default -s:b arch=x86 --build missing")
curdir = client.current_folder
data_name_context_build = "liba_build-relwithdebinfo-x86-data.cmake"
data_name_context_host = "liba-debug-x86_64-data.cmake"
assert os.path.exists(os.path.join(curdir, data_name_context_build))
assert os.path.exists(os.path.join(curdir, data_name_context_host))
assert "set(liba_build_INCLUDE_DIRS_RELWITHDEBINFO" in \
open(os.path.join(curdir, data_name_context_build)).read()
assert "set(liba_INCLUDE_DIRS_DEBUG" in \
open(os.path.join(curdir, data_name_context_host)).read()
def test_buildirs_working():
""" If a recipe declares cppinfo.buildirs those dirs will be exposed to be consumer
to allow a cmake "include" function call after a find_package"""
c = TestClient()
conanfile = str(GenConanfile().with_name("my_lib").with_version("1.0")
.with_import("import os").with_import("from conans import tools"))
conanfile += """
def package(self):
tools.save(os.path.join(self.package_folder, "my_build_dir", "my_cmake_script.cmake"),
'set(MYVAR "Like a Rolling Stone")')
def package_info(self):
self.cpp_info.builddirs=["my_build_dir"]
"""
c.save({"conanfile.py": conanfile})
c.run("create .")
consumer_conanfile = GenConanfile().with_name("consumer").with_version("1.0")\
.with_cmake_build().with_require("my_lib/1.0") \
.with_settings("os", "arch", "build_type", "compiler") \
.with_exports_sources("*.txt")
cmake = gen_cmakelists(find_package=["my_lib"])
cmake += """
message("CMAKE_MODULE_PATH: ${CMAKE_MODULE_PATH}")
include("my_cmake_script")
message("MYVAR=>${MYVAR}")
"""
c.save({"conanfile.py": consumer_conanfile, "CMakeLists.txt": cmake})
c.run("create .")
assert "MYVAR=>Like a Rolling Stone" in c.out
@pytest.mark.tool_cmake
def test_cpp_info_link_objects():
client = TestClient()
obj_ext = "obj" if platform.system() == "Windows" else "o"
cpp_info = {"objects": [os.path.join("lib", "myobject.{}".format(obj_ext))]}
object_cpp = gen_function_cpp(name="myobject")
object_h = gen_function_h(name="myobject")
cmakelists = textwrap.dedent("""
cmake_minimum_required(VERSION 3.15)
project(MyObject)
file(GLOB HEADERS *.h)
add_library(myobject OBJECT myobject.cpp)
if( WIN32 )
set(OBJ_PATH "myobject.dir/Release/myobject${CMAKE_C_OUTPUT_EXTENSION}")
else()
set(OBJ_PATH "CMakeFiles/myobject.dir/myobject.cpp${CMAKE_C_OUTPUT_EXTENSION}")
endif()
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${OBJ_PATH}
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
RENAME myobject${CMAKE_C_OUTPUT_EXTENSION})
install(FILES ${HEADERS}
DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
""")
test_package_cpp = gen_function_cpp(name="main", includes=["myobject"], calls=["myobject"])
test_package_cmakelists = textwrap.dedent("""
cmake_minimum_required(VERSION 3.15)
project(example)
find_package(myobject REQUIRED)
add_executable(example example.cpp)
target_link_libraries(example myobject::myobject)
""")
client.save({"CMakeLists.txt": cmakelists,
"conanfile.py": GenConanfile("myobject", "1.0").with_package_info(cpp_info=cpp_info,
env_info={})
.with_exports_sources("*")
.with_cmake_build()
.with_package("cmake = CMake(self)",
"cmake.install()"),
"myobject.cpp": object_cpp,
"myobject.h": object_h,
"test_package/conanfile.py": GenConanfile().with_cmake_build()
.with_import("import os")
.with_test('path = "{}".format(self.settings.build_type) '
'if self.settings.os == "Windows" else "."')
.with_test('self.run("{}{}example".format(path, os.sep))'),
"test_package/example.cpp": test_package_cpp,
"test_package/CMakeLists.txt": test_package_cmakelists})
client.run("create . -s build_type=Release")
assert "myobject: Release!" in client.out
def test_private_transitive():
# https://github.com/conan-io/conan/issues/9514
client = TestClient()
client.save({"dep/conanfile.py": GenConanfile(),
"pkg/conanfile.py": GenConanfile().with_require("dep/0.1", private=True),
"consumer/conanfile.py": GenConanfile().with_requires("pkg/0.1")
.with_settings("os", "build_type", "arch")})
client.run("create dep dep/0.1@")
client.run("create pkg pkg/0.1@")
client.run("install consumer -g CMakeDeps -s arch=x86_64 -s build_type=Release")
assert "dep/0.1:5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9 - Skip" in client.out
data_cmake = client.load("pkg-release-x86_64-data.cmake")
assert 'set(pkg_FIND_DEPENDENCY_NAMES "")' in data_cmake
@pytest.mark.tool_cmake
def test_system_dep():
"""This test creates a zlib package and use the installation CMake FindZLIB.cmake to locate
the library of the package. That happens because:
- The package declares: self.cpp_info.set_property("cmake_find_mode", "none") so CMakeDeps does nothing
- The toolchain set the CMAKE_LIBRARY_PATH to the "lib" of the package, so the library file is found
"""
client = TestClient()
files = pkg_cmake("zlib", "0.1")
files["conanfile.py"] += """
def package_info(self):
# This will use the FindZLIB from CMake but will find this library package
self.cpp_info.set_property("cmake_file_name", "ZLIB")
self.cpp_info.set_property("cmake_target_name", "ZLIB::ZLIB")
self.cpp_info.set_property("cmake_find_mode", "none")
"""
client.save({os.path.join("zlib", name): content for name, content in files.items()})
files = pkg_cmake("mylib", "0.1", requires=["zlib/0.1"])
files["CMakeLists.txt"] = files["CMakeLists.txt"].replace("find_package(zlib)",
"find_package(ZLIB)")
files["CMakeLists.txt"] = files["CMakeLists.txt"].replace("zlib::zlib","ZLIB::ZLIB")
client.save({os.path.join("mylib", name): content for name, content in files.items()})
files = pkg_cmake("consumer", "0.1", requires=["mylib/0.1"])
client.save({os.path.join("consumer", name): content for name, content in files.items()})
client.run("create zlib")
client.run("create mylib")
client.run("create consumer")
assert "Found ZLIB:" in client.out
client.run("install consumer")
if platform.system() != "Windows":
data = os.path.join("consumer/build/generators/mylib-release-x86_64-data.cmake")
contents = client.load(data)
assert 'set(ZLIB_FIND_MODE "")' in contents
@pytest.mark.tool_cmake(version="3.19")
def test_error_missing_build_type():
# https://github.com/conan-io/conan/issues/11168
client = TestClient()
client.run("new hello/1.0 -m=cmake_lib")
client.run("create . -tf=None")
conanfile = textwrap.dedent("""
[requires]
hello/1.0
[generators]
CMakeDeps
CMakeToolchain
""")
main = textwrap.dedent("""
#include <hello.h>
int main() {hello();return 0;}
""")
cmakelists = textwrap.dedent("""
cmake_minimum_required(VERSION 3.15)
project(app)
find_package(hello REQUIRED)
add_executable(app)
target_link_libraries(app hello::hello)
target_sources(app PRIVATE main.cpp)
""")
if platform.system() != "Windows":
client.save({
"conanfile.txt": conanfile,
"main.cpp": main,
"CMakeLists.txt": cmakelists
}, clean_first=True)
client.run("install .")
client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -G 'Unix Makefiles'", assert_error=True)
assert "Please, set the CMAKE_BUILD_TYPE variable when calling to CMake" in client.out
client.save({
"conanfile.txt": conanfile,
"main.cpp": main,
"CMakeLists.txt": cmakelists
}, clean_first=True)
client.run("install .")
generator = {
"Windows": '-G "Visual Studio 15 2017"',
"Darwin": '-G "Xcode"',
"Linux": '-G "Ninja Multi-Config"'
}.get(platform.system())
client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake {}".format(generator))
client.run_command("cmake --build . --config Release")
run_app = r".\Release\app.exe" if platform.system() == "Windows" else "./Release/app"
client.run_command(run_app)
assert "Hello World Release!" in client.out