forked from conan-io/conan
/
test_cmakedeps_components_names.py
736 lines (612 loc) · 28.7 KB
/
test_cmakedeps_components_names.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
import platform
import textwrap
import pytest
from conans.model.ref import ConanFileReference
from conans.test.assets.genconanfile import GenConanfile
from conans.test.assets.sources import gen_function_h, gen_function_cpp
from conans.test.utils.tools import TestClient
@pytest.mark.slow
@pytest.mark.tool_cmake
@pytest.fixture(scope="module")
def setup_client_with_greetings():
"""
creates a multi-component package with 2 components "hello" and "bye
"""
hello_h = gen_function_h(name="hello")
hello_cpp = gen_function_cpp(name="hello", includes=["hello"])
bye_h = gen_function_h(name="bye")
bye_cpp = gen_function_cpp(name="bye", includes=["bye"])
conanfile_greetings = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import CMake
class GreetingsConan(ConanFile):
name = "greetings"
version = "0.0.1"
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps", "CMakeToolchain"
exports_sources = "src/*"
options = {"components": ["standard", "custom", "none"]}
default_options = {"components": "standard"}
def build(self):
cmake = CMake(self)
cmake.configure(build_script_folder="src")
cmake.build()
def package(self):
self.copy("*.h", dst="include", src="src")
self.copy("*.lib", dst="lib", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
def package_info(self):
if self.options.components == "standard":
self.cpp_info.components["hello"].libs = ["hello"]
self.cpp_info.components["bye"].libs = ["bye"]
elif self.options.components == "custom":
self.cpp_info.set_property("cmake_file_name", "MYG")
self.cpp_info.set_property("cmake_target_name", "MyGreetings")
self.cpp_info.components["hello"].set_property("cmake_target_name", "MyHello")
self.cpp_info.components["bye"].set_property("cmake_target_name", "MyBye")
self.cpp_info.components["hello"].libs = ["hello"]
self.cpp_info.components["bye"].libs = ["bye"]
else:
self.cpp_info.libs = ["hello", "bye"]
def package_id(self):
del self.info.options.components
""")
cmakelists_greetings = textwrap.dedent("""
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
cmake_minimum_required(VERSION 3.0)
project(greetings CXX)
add_library(hello hello.cpp)
add_library(bye bye.cpp)
""")
test_package_greetings_conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.cmake import CMake
class GreetingsTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps", "CMakeToolchain"
requires = "greetings/0.0.1"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def test(self):
path = "{}".format(self.settings.build_type) if self.settings.os == "Windows" else "."
self.run("{}{}example".format(path, os.sep))
""")
test_package_greetings_cpp = gen_function_cpp(name="main", includes=["hello", "bye"],
calls=["hello", "bye"])
test_package_greetings_cmakelists = textwrap.dedent("""
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
cmake_minimum_required(VERSION 3.0)
project(PackageTest CXX)
find_package(greetings)
add_executable(example example.cpp)
target_link_libraries(example greetings::greetings)
""")
client = TestClient()
client.save({"conanfile.py": conanfile_greetings,
"src/CMakeLists.txt": cmakelists_greetings,
"src/hello.h": hello_h,
"src/hello.cpp": hello_cpp,
"src/bye.h": bye_h,
"src/bye.cpp": bye_cpp,
"test_package/conanfile.py": test_package_greetings_conanfile,
"test_package/example.cpp": test_package_greetings_cpp,
"test_package/CMakeLists.txt": test_package_greetings_cmakelists})
client.run("create . -s build_type=Release")
assert "hello: Release!" in client.out
assert "bye: Release!" in client.out
client.run("create . -s build_type=Debug")
assert "hello: Debug!" in client.out
assert "bye: Debug!" in client.out
return client
def create_chat(client, components, package_info, cmake_find, test_cmake_find):
conanfile = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import CMake
class Chat(ConanFile):
name = "chat"
version = "0.0.1"
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps", "CMakeToolchain"
exports_sources = "src/*"
requires = "greetings/0.0.1"
default_options = {{"greetings:components": "{}"}}
def build(self):
cmake = CMake(self)
cmake.configure(build_script_folder="src")
cmake.build()
def package(self):
self.copy("*.h", dst="include", src="src")
self.copy("*.lib", dst="lib", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
def package_info(self):
{}
""").format(components, "\n ".join(package_info.splitlines()))
sayhello_h = gen_function_h(name="sayhello")
sayhello_cpp = gen_function_cpp(name="sayhello", includes=["hello"], calls=["hello"])
sayhellobye_h = gen_function_h(name="sayhellobye")
sayhellobye_cpp = gen_function_cpp(name="sayhellobye", includes=["sayhello", "bye"],
calls=["sayhello", "bye"])
cmakelists = textwrap.dedent("""
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
cmake_minimum_required(VERSION 3.0)
project(world CXX)
%s
""" % cmake_find)
test_conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.cmake import CMake
class WorldTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps", "CMakeToolchain"
requires = "chat/0.0.1"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def test(self):
path = "{}".format(self.settings.build_type) if self.settings.os == "Windows" else "."
self.run("{}{}example".format(path, os.sep))
self.run("{}{}example2".format(path, os.sep))
""")
test_example_cpp = gen_function_cpp(name="main", includes=["sayhellobye"], calls=["sayhellobye"])
test_cmakelists = textwrap.dedent("""
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
cmake_minimum_required(VERSION 3.0)
project(PackageTest CXX)
%s
""" % test_cmake_find)
client.save({"conanfile.py": conanfile,
"src/CMakeLists.txt": cmakelists,
"src/sayhello.h": sayhello_h,
"src/sayhello.cpp": sayhello_cpp,
"src/sayhellobye.h": sayhellobye_h,
"src/sayhellobye.cpp": sayhellobye_cpp,
"test_package/conanfile.py": test_conanfile,
"test_package/CMakeLists.txt": test_cmakelists,
"test_package/example.cpp": test_example_cpp}, clean_first=True)
client.run("create . -s build_type=Release")
assert "sayhellobye: Release!" in client.out
assert "sayhello: Release!" in client.out
assert "hello: Release!" in client.out
assert "bye: Release!" in client.out
client.run("create . -s build_type=Debug")
assert "sayhellobye: Debug!" in client.out
assert "sayhello: Debug!" in client.out
assert "hello: Debug!" in client.out
assert "bye: Debug!" in client.out
def test_standard_names(setup_client_with_greetings):
client = setup_client_with_greetings
package_info = textwrap.dedent("""
self.cpp_info.components["sayhello"].requires = ["greetings::hello"]
self.cpp_info.components["sayhello"].libs = ["sayhello"]
self.cpp_info.components["sayhellobye"].requires = ["sayhello", "greetings::bye"]
self.cpp_info.components["sayhellobye"].libs = ["sayhellobye"]
""")
cmake_find = textwrap.dedent("""
find_package(greetings COMPONENTS hello bye)
add_library(sayhello sayhello.cpp)
target_link_libraries(sayhello greetings::hello)
add_library(sayhellobye sayhellobye.cpp)
target_link_libraries(sayhellobye sayhello greetings::bye)
""")
test_cmake_find = textwrap.dedent("""
find_package(chat)
add_executable(example example.cpp)
target_link_libraries(example chat::sayhellobye)
add_executable(example2 example.cpp)
target_link_libraries(example2 chat::chat)
""")
create_chat(client, "standard", package_info, cmake_find, test_cmake_find)
# Test consumer multi-config
if platform.system() == "Windows":
with client.chdir("test_package"):
client.run("install . -s build_type=Release")
client.run("install . -s build_type=Debug")
client.run_command('cmake . -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake')
client.run_command("cmake --build . --config Debug")
client.run_command(r".\Debug\example.exe")
assert "sayhellobye: Debug!" in client.out
assert "sayhello: Debug!" in client.out
assert "hello: Debug!" in client.out
assert "bye: Debug!" in client.out
client.run_command("cmake --build . --config Release")
client.run_command(r".\Release\example.exe")
assert "sayhellobye: Release!" in client.out
assert "sayhello: Release!" in client.out
assert "hello: Release!" in client.out
assert "bye: Release!" in client.out
def test_custom_names(setup_client_with_greetings):
client = setup_client_with_greetings
package_info = textwrap.dedent("""
# NOTE: For the new CMakeDeps only filenames mean filename, it is not using the "names" field
self.cpp_info.set_property("cmake_target_name", "MyChat")
self.cpp_info.set_property("cmake_file_name", "MyChat")
self.cpp_info.components["sayhello"].set_property("cmake_target_name", "MySay")
self.cpp_info.components["sayhello"].requires = ["greetings::hello"]
self.cpp_info.components["sayhello"].libs = ["sayhello"]
self.cpp_info.components["sayhellobye"].set_property("cmake_target_name", "MySayBye")
self.cpp_info.components["sayhellobye"].requires = ["sayhello", "greetings::bye"]
self.cpp_info.components["sayhellobye"].libs = ["sayhellobye"]
""")
cmake_find = textwrap.dedent("""
find_package(MYG COMPONENTS MyHello MyBye)
add_library(sayhello sayhello.cpp)
target_link_libraries(sayhello MyGreetings::MyHello)
add_library(sayhellobye sayhellobye.cpp)
target_link_libraries(sayhellobye sayhello MyGreetings::MyBye)
""")
test_cmake_find = textwrap.dedent("""
find_package(MyChat)
add_executable(example example.cpp)
target_link_libraries(example MyChat::MySayBye)
add_executable(example2 example.cpp)
target_link_libraries(example2 MyChat::MyChat)
""")
create_chat(client, "custom", package_info, cmake_find, test_cmake_find)
def test_no_components(setup_client_with_greetings):
client = setup_client_with_greetings
package_info = textwrap.dedent("""
self.cpp_info.components["sayhello"].requires = ["greetings::greetings"]
self.cpp_info.components["sayhello"].libs = ["sayhello"]
self.cpp_info.components["sayhellobye"].requires = ["sayhello", "greetings::greetings"]
self.cpp_info.components["sayhellobye"].libs = ["sayhellobye"]
""")
cmake_find = textwrap.dedent("""
find_package(greetings)
add_library(sayhello sayhello.cpp)
target_link_libraries(sayhello greetings::greetings)
add_library(sayhellobye sayhellobye.cpp)
target_link_libraries(sayhellobye sayhello greetings::greetings)
""")
test_cmake_find = textwrap.dedent("""
find_package(chat)
add_executable(example example.cpp)
target_link_libraries(example chat::sayhellobye)
add_executable(example2 example.cpp)
target_link_libraries(example2 chat::chat)
""")
create_chat(client, "none", package_info, cmake_find, test_cmake_find)
@pytest.mark.slow
@pytest.mark.tool_cmake
def test_same_names():
client = TestClient()
conanfile_greetings = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import CMake
class HelloConan(ConanFile):
name = "hello"
version = "0.0.1"
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps", "CMakeToolchain"
exports_sources = "src/*"
def build(self):
cmake = CMake(self)
cmake.configure(build_script_folder="src")
cmake.build()
def package(self):
self.copy("*.h", dst="include", src="src")
self.copy("*.lib", dst="lib", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
def package_info(self):
self.cpp_info.components["global"].name = "hello"
self.cpp_info.components["global"].libs = ["hello"]
""")
hello_h = gen_function_h(name="hello")
hello_cpp = gen_function_cpp(name="hello", includes=["hello"])
cmakelists_greetings = textwrap.dedent("""
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
cmake_minimum_required(VERSION 3.0)
project(greetings CXX)
add_library(hello hello.cpp)
""")
test_package_greetings_conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.cmake import CMake
class HelloTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps", "CMakeToolchain"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def test(self):
path = "{}".format(self.settings.build_type) if self.settings.os == "Windows" else "."
self.run("{}{}example".format(path, os.sep))
""")
test_package_greetings_cpp = gen_function_cpp(name="main", includes=["hello"], calls=["hello"])
test_package_greetings_cmakelists = textwrap.dedent("""
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
cmake_minimum_required(VERSION 3.0)
project(PackageTest CXX)
find_package(hello)
add_executable(example example.cpp)
target_link_libraries(example hello::hello)
""")
client.save({"conanfile.py": conanfile_greetings,
"src/CMakeLists.txt": cmakelists_greetings,
"src/hello.h": hello_h,
"src/hello.cpp": hello_cpp,
"test_package/conanfile.py": test_package_greetings_conanfile,
"test_package/example.cpp": test_package_greetings_cpp,
"test_package/CMakeLists.txt": test_package_greetings_cmakelists})
client.run("create .")
assert "hello: Release!" in client.out
@pytest.mark.tool_cmake
class TestComponentsCMakeGenerators:
def test_component_not_found(self):
conanfile = textwrap.dedent("""
from conans import ConanFile
class GreetingsConan(ConanFile):
def package_info(self):
self.cpp_info.components["hello"].libs = ["hello"]
""")
client = TestClient()
client.save({"conanfile.py": conanfile})
client.run("create . greetings/0.0.1@")
conanfile = textwrap.dedent("""
from conans import ConanFile
class WorldConan(ConanFile):
requires = "greetings/0.0.1"
def package_info(self):
self.cpp_info.components["helloworld"].requires = ["greetings::non-existent"]
""")
client.save({"conanfile.py": conanfile})
client.run("create . world/0.0.1@")
client.run("install world/0.0.1@ -g CMakeDeps", assert_error=True)
assert ("Component 'greetings::non-existent' not found in 'greetings' "
"package requirement" in client.out)
def test_component_not_found_same_name_as_pkg_require(self):
zlib = GenConanfile("zlib", "0.1").with_setting("build_type").with_generator("CMakeDeps")
mypkg = GenConanfile("mypkg", "0.1").with_setting("build_type").with_generator("CMakeDeps")
final = GenConanfile("final", "0.1").with_setting("build_type").with_generator("CMakeDeps")\
.with_require(ConanFileReference("zlib", "0.1", None, None))\
.with_require(ConanFileReference("mypkg", "0.1", None, None))\
.with_package_info(cpp_info={"components": {"cmp": {"requires": ["mypkg::zlib",
"zlib::zlib"]}}},
env_info={})
consumer = GenConanfile("consumer", "0.1").with_setting("build_type")\
.with_generator("CMakeDeps")\
.with_requirement(ConanFileReference("final", "0.1", None, None))
consumer = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import CMakeDeps
class HelloConan(ConanFile):
name = 'consumer'
version = '0.1'
def generate(self):
deps = CMakeDeps(self)
deps.check_components_exist = True
deps.generate()
def requirements(self):
self.requires("final/0.1")
settings = "build_type"
""")
def test_component_not_found(self):
conanfile = textwrap.dedent("""
from conans import ConanFile
class GreetingsConan(ConanFile):
def package_info(self):
self.cpp_info.components["hello"].libs = ["hello"]
""")
client = TestClient()
client.save({"conanfile.py": conanfile})
client.run("create . greetings/0.0.1@")
conanfile = textwrap.dedent("""
from conans import ConanFile
class WorldConan(ConanFile):
requires = "greetings/0.0.1"
def package_info(self):
self.cpp_info.components["helloworld"].requires = ["greetings::non-existent"]
""")
client.save({"conanfile.py": conanfile})
client.run("create . world/0.0.1@")
client.run("install world/0.0.1@ -g CMakeDeps", assert_error=True)
assert ("Component 'greetings::non-existent' not found in 'greetings' "
"package requirement" in client.out)
client = TestClient()
client.save({"zlib.py": zlib, "mypkg.py": mypkg, "final.py": final, "consumer.py": consumer})
client.run("create zlib.py")
client.run("create mypkg.py")
client.run("create final.py")
client.run("install consumer.py", assert_error=True)
assert "Component 'mypkg::zlib' not found in 'mypkg' package requirement" in client.out
@pytest.mark.slow
@pytest.mark.xfail(reason="This test is failing in Py27, waiting for CMakeDeps improvements")
def test_same_name_global_target_collision(self):
# https://github.com/conan-io/conan/issues/7889
conanfile_tpl = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import CMake
class Conan(ConanFile):
name = "{name}"
version = "1.0"
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps", "CMakeToolchain"
exports_sources = "src/*"
def build(self):
cmake = CMake(self)
cmake.configure(build_script_folder="src")
cmake.build()
def package(self):
self.copy("*.h", dst="include", src="src")
self.copy("*.lib", dst="lib", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
def package_info(self):
self.cpp_info.set_property("cmake_target_name", "nonstd" )
self.cpp_info.set_property("cmake_file_name", "{name}")
self.cpp_info.components["1"].set_property("cmake_target_name", "{name}")
self.cpp_info.components["1"].libs = ["{name}"]
""")
basic_cmake = textwrap.dedent("""
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
project(middle CXX)
cmake_minimum_required(VERSION 3.1)
add_library({name} {name}.cpp)
""")
client = TestClient()
for name in ["expected", "variant"]:
client.run("new {name}/1.0 -s".format(name=name))
client.save({"conanfile.py": conanfile_tpl.format(name=name),
"src/CMakeLists.txt": basic_cmake.format(name=name)})
client.run("create . {name}/1.0@".format(name=name))
middle_cmakelists = textwrap.dedent("""
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
project(middle CXX)
cmake_minimum_required(VERSION 3.1)
find_package(expected)
find_package(variant)
add_library(middle middle.cpp)
target_link_libraries(middle nonstd::nonstd)
""")
middle_h = gen_function_h(name="middle")
middle_cpp = gen_function_cpp(name="middle", includes=["middle", "expected", "variant"],
calls=["expected", "variant"])
middle_conanfile = textwrap.dedent("""
from conans import ConanFile, CMake
class Conan(ConanFile):
name = "middle"
version = "1.0"
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps", "CMakeToolchain"
exports_sources = "src/*"
requires = "expected/1.0", "variant/1.0"
def build(self):
cmake = CMake(self)
cmake.configure(build_script_folder="src")
cmake.build()
def package(self):
self.copy("*.h", dst="include", src="src")
self.copy("*.lib", dst="lib", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
def package_info(self):
self.cpp_info.libs = ["middle"]
""")
client.save({"conanfile.py": middle_conanfile, "src/CMakeLists.txt": middle_cmakelists,
"src/middle.h": middle_h, "src/middle.cpp": middle_cpp}, clean_first=True)
client.run("create . middle/1.0@")
conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.cmake import CMake
class Conan(ConanFile):
name = "consumer"
version = "1.0"
generators = "CMakeDeps", "CMakeToolchain"
settings = "os", "compiler", "build_type", "arch"
exports_sources = "src/*"
requires = "middle/1.0"
def build(self):
cmake = CMake(self)
cmake.configure(build_script_folder="src")
cmake.build()
self.run(".%smain" % os.sep)
""".format("CMakeDeps"))
cmakelists = textwrap.dedent("""
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
project(consumer CXX)
cmake_minimum_required(VERSION 3.1)
find_package(middle)
get_target_property(tmp middle::middle INTERFACE_LINK_LIBRARIES)
message("Middle link libraries: ${tmp}")
add_executable(main main.cpp)
target_link_libraries(main middle::middle)
""")
main_cpp = gen_function_cpp(name="main", includes=["middle"], calls=["middle"])
client.save({"conanfile.py": conanfile,
"src/CMakeLists.txt": cmakelists,
"src/main.cpp": main_cpp}, clean_first=True)
client.run("create . consumer/1.0@")
assert 'main: Release!' in client.out
assert 'middle: Release!' in client.out
assert 'expected/1.0: Hello World Release!' in client.out
assert 'variant/1.0: Hello World Release!' in client.out
@pytest.mark.parametrize("check_components_exist", [False, True, None])
def test_targets_declared_in_build_modules(check_components_exist):
"""If a require is declaring the component targets in a build_module, CMakeDeps is
fine with it, not needed to locate it as a conan declared component"""
client = TestClient()
conanfile_hello = str(GenConanfile().with_name("hello").with_version("1.0")
.with_exports_sources("*.cmake", "*.h"))
conanfile_hello += """
def package(self):
self.copy("*.h", dst="include")
self.copy("*.cmake", dst="cmake")
def package_info(self):
self.cpp_info.set_property("cmake_build_modules", ["cmake/my_modules.cmake"])
"""
my_modules = textwrap.dedent("""
add_library(cool_component INTERFACE)
target_include_directories(cool_component INTERFACE ${CMAKE_CURRENT_LIST_DIR}/../include/)
add_library(hello::invented ALIAS cool_component)
""")
hello_h = "int cool_header_only=1;"
client.save({"conanfile.py": conanfile_hello,
"my_modules.cmake": my_modules, "hello.h": hello_h})
client.run("create .")
conanfile = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import CMake, CMakeDeps
class HelloConan(ConanFile):
name = 'app'
version = '1.0'
exports_sources = "*.txt", "*.cpp"
generators = "CMakeToolchain"
requires = ("hello/1.0", )
settings = "os", "compiler", "arch", "build_type"
def generate(self):
deps = CMakeDeps(self)
{}
deps.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
""")
if check_components_exist is False:
conanfile = conanfile.format("deps.check_components_exist=False")
elif check_components_exist is True:
conanfile = conanfile.format("deps.check_components_exist=True")
else:
conanfile = conanfile.format("")
main_cpp = textwrap.dedent("""
#include <iostream>
#include "hello.h"
int main(){
std::cout << "cool header value: " << cool_header_only;
return 0;
}
""")
cmakelist = textwrap.dedent("""
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_C_ABI_COMPILED 1)
cmake_minimum_required(VERSION 3.15)
project(project CXX)
find_package(hello COMPONENTS invented missing)
add_executable(myapp main.cpp)
target_link_libraries(myapp hello::invented)
""")
client.save({"conanfile.py": conanfile,
"CMakeLists.txt": cmakelist, "main.cpp": main_cpp})
client.run("create .", assert_error=check_components_exist)
assert bool(check_components_exist) == ("Conan: Component 'missing' NOT found in package "
"'hello'" in client.out)
assert "Conan: Including build module" in client.out
assert "my_modules.cmake" in client.out
assert bool(check_components_exist) == ("Conan: Component 'invented' found in package 'hello'"
in client.out)