forked from conan-io/conan
/
test_cmaketoolchain.py
583 lines (484 loc) · 21.8 KB
/
test_cmaketoolchain.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
import json
import os
import platform
import textwrap
import pytest
from mock import mock
from conan.tools.cmake.presets import load_cmake_presets
from conans.test.assets.genconanfile import GenConanfile
from conans.test.utils.tools import TestClient
def test_cross_build():
windows_profile = textwrap.dedent("""
[settings]
os=Windows
arch=x86_64
""")
rpi_profile = textwrap.dedent("""
[settings]
os=Linux
compiler=gcc
compiler.version=6
compiler.libcxx=libstdc++11
arch=armv8
build_type=Release
""")
client = TestClient(path_with_spaces=False)
conanfile = GenConanfile().with_settings("os", "arch", "compiler", "build_type")\
.with_generator("CMakeToolchain")
client.save({"conanfile.py": conanfile,
"rpi": rpi_profile,
"windows": windows_profile})
client.run("install . --profile:build=windows --profile:host=rpi")
toolchain = client.load("conan_toolchain.cmake")
assert "set(CMAKE_SYSTEM_NAME Linux)" in toolchain
assert "set(CMAKE_SYSTEM_PROCESSOR armv8)" in toolchain
def test_cross_build_user_toolchain():
# When a user_toolchain is defined in [conf], CMakeToolchain will not generate anything
# for cross-build
windows_profile = textwrap.dedent("""
[settings]
os=Windows
arch=x86_64
""")
rpi_profile = textwrap.dedent("""
[settings]
os=Linux
compiler=gcc
compiler.version=6
compiler.libcxx=libstdc++11
arch=armv8
build_type=Release
[conf]
tools.cmake.cmaketoolchain:user_toolchain+=rpi_toolchain.cmake
""")
client = TestClient(path_with_spaces=False)
conanfile = GenConanfile().with_settings("os", "arch", "compiler", "build_type")\
.with_generator("CMakeToolchain")
client.save({"conanfile.py": conanfile,
"rpi": rpi_profile,
"windows": windows_profile})
client.run("install . --profile:build=windows --profile:host=rpi")
toolchain = client.load("conan_toolchain.cmake")
assert "CMAKE_SYSTEM_NAME " not in toolchain
assert "CMAKE_SYSTEM_PROCESSOR" not in toolchain
def test_no_cross_build():
windows_profile = textwrap.dedent("""
[settings]
os=Windows
arch=x86_64
compiler=gcc
compiler.version=6
compiler.libcxx=libstdc++11
build_type=Release
""")
client = TestClient(path_with_spaces=False)
conanfile = GenConanfile().with_settings("os", "arch", "compiler", "build_type")\
.with_generator("CMakeToolchain")
client.save({"conanfile.py": conanfile,
"windows": windows_profile})
client.run("install . --profile:build=windows --profile:host=windows")
toolchain = client.load("conan_toolchain.cmake")
assert "CMAKE_SYSTEM_NAME " not in toolchain
assert "CMAKE_SYSTEM_PROCESSOR" not in toolchain
def test_cross_arch():
# Compiling to 32bits in an architecture that runs is not full cross-compiling
build_profile = textwrap.dedent("""
[settings]
os=Linux
arch=x86_64
""")
profile_arm = textwrap.dedent("""
[settings]
os=Linux
arch=armv8
compiler=gcc
compiler.version=6
compiler.libcxx=libstdc++11
build_type=Release
""")
client = TestClient(path_with_spaces=False)
conanfile = GenConanfile().with_settings("os", "arch", "compiler", "build_type")\
.with_generator("CMakeToolchain")
client.save({"conanfile.py": conanfile,
"linux64": build_profile,
"linuxarm": profile_arm})
client.run("install . --profile:build=linux64 --profile:host=linuxarm")
toolchain = client.load("conan_toolchain.cmake")
assert "set(CMAKE_SYSTEM_NAME Linux)" in toolchain
assert "set(CMAKE_SYSTEM_PROCESSOR armv8)" in toolchain
def test_no_cross_build_arch():
# Compiling to 32bits in an architecture that runs is not full cross-compiling
build_profile = textwrap.dedent("""
[settings]
os=Linux
arch=x86_64
""")
profile_32 = textwrap.dedent("""
[settings]
os=Linux
arch=x86
compiler=gcc
compiler.version=6
compiler.libcxx=libstdc++11
build_type=Release
""")
client = TestClient(path_with_spaces=False)
conanfile = GenConanfile().with_settings("os", "arch", "compiler", "build_type")\
.with_generator("CMakeToolchain")
client.save({"conanfile.py": conanfile,
"linux64": build_profile,
"linux32": profile_32})
client.run("install . --profile:build=linux64 --profile:host=linux32")
toolchain = client.load("conan_toolchain.cmake")
assert "CMAKE_SYSTEM_NAME" not in toolchain
assert "CMAKE_SYSTEM_PROCESSOR " not in toolchain
def test_cross_build_conf():
windows_profile = textwrap.dedent("""
[settings]
os=Windows
arch=x86_64
""")
rpi_profile = textwrap.dedent("""
[settings]
os=Linux
compiler=gcc
compiler.version=6
compiler.libcxx=libstdc++11
arch=armv8
build_type=Release
[conf]
tools.cmake.cmaketoolchain:system_name = Custom
tools.cmake.cmaketoolchain:system_version= 42
tools.cmake.cmaketoolchain:system_processor = myarm
""")
client = TestClient(path_with_spaces=False)
conanfile = GenConanfile().with_settings("os", "arch", "compiler", "build_type")\
.with_generator("CMakeToolchain")
client.save({"conanfile.py": conanfile,
"rpi": rpi_profile,
"windows": windows_profile})
client.run("install . --profile:build=windows --profile:host=rpi")
toolchain = client.load("conan_toolchain.cmake")
assert "set(CMAKE_SYSTEM_NAME Custom)" in toolchain
assert "set(CMAKE_SYSTEM_VERSION 42)" in toolchain
assert "set(CMAKE_SYSTEM_PROCESSOR myarm)" in toolchain
def test_find_builddirs():
client = TestClient()
conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.cmake import CMakeToolchain
class Conan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
def package_info(self):
self.cpp_info.builddirs = ["/path/to/builddir"]
""")
client.save({"conanfile.py": conanfile})
client.run("create . dep/1.0@")
conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.cmake import CMakeToolchain
class Conan(ConanFile):
name = "mydep"
version = "1.0"
settings = "os", "arch", "compiler", "build_type"
requires = "dep/1.0@"
def generate(self):
cmake = CMakeToolchain(self)
{}
cmake.generate()
""")
client.save({"conanfile.py": conanfile})
client.run("install . ")
with open(os.path.join(client.current_folder, "conan_toolchain.cmake")) as f:
contents = f.read()
assert "/path/to/builddir" in contents
@pytest.mark.skipif(platform.system() != "Darwin", reason="Only OSX")
def test_cmaketoolchain_cmake_system_processor_cross_apple():
"""
https://github.com/conan-io/conan/pull/10434
CMAKE_SYSTEM_PROCESSOR was not set when cross-building in Mac
"""
client = TestClient()
client.save({"hello.py": GenConanfile().with_name("hello")
.with_version("1.0")
.with_settings("os", "arch", "compiler", "build_type")})
profile_ios = textwrap.dedent("""
include(default)
[settings]
os=iOS
os.version=15.4
os.sdk=iphoneos
os.sdk_version=15.0
arch=armv8
""")
client.save({"profile_ios": profile_ios})
client.run("install hello.py -pr:h=./profile_ios -pr:b=default -g CMakeToolchain")
toolchain = client.load("conan_toolchain.cmake")
assert "set(CMAKE_SYSTEM_NAME iOS)" in toolchain
assert "set(CMAKE_SYSTEM_VERSION 15.0)" in toolchain
assert "set(CMAKE_SYSTEM_PROCESSOR arm64)" in toolchain
@pytest.mark.skipif(platform.system() != "Darwin", reason="Only OSX")
def test_apple_vars_overwrite_user_conf():
"""
tools.cmake.cmaketoolchain:system_name and tools.cmake.cmaketoolchain:system_version
will be overwritten by the apple block
"""
client = TestClient()
client.save({"hello.py": GenConanfile().with_name("hello")
.with_version("1.0")
.with_settings("os", "arch", "compiler", "build_type")})
profile_ios = textwrap.dedent("""
include(default)
[settings]
os=iOS
os.version=15.4
os.sdk=iphoneos
os.sdk_version=15.0
arch=armv8
""")
client.save({"profile_ios": profile_ios})
client.run("install hello.py -pr:h=./profile_ios -pr:b=default -g CMakeToolchain "
"-c tools.cmake.cmaketoolchain:system_name=tvOS "
"-c tools.cmake.cmaketoolchain:system_version=15.1 "
"-c tools.cmake.cmaketoolchain:system_processor=x86_64 ")
toolchain = client.load("conan_toolchain.cmake")
# should set the conf values but system/version are overwritten by the apple block
assert "CMAKE_SYSTEM_NAME tvOS" in toolchain
assert "CMAKE_SYSTEM_NAME iOS" not in toolchain
assert "CMAKE_SYSTEM_VERSION 15.1" in toolchain
assert "CMAKE_SYSTEM_VERSION 15.0" not in toolchain
assert "CMAKE_SYSTEM_PROCESSOR x86_64" in toolchain
assert "CMAKE_SYSTEM_PROCESSOR armv8" not in toolchain
def test_extra_flags_via_conf():
profile = textwrap.dedent("""
[settings]
os=Linux
compiler=gcc
compiler.version=6
compiler.libcxx=libstdc++11
arch=armv8
build_type=Release
[conf]
tools.build:cxxflags=["--flag1", "--flag2"]
tools.build:cflags+=["--flag3", "--flag4"]
tools.build:sharedlinkflags=+["--flag5", "--flag6"]
tools.build:exelinkflags=["--flag7", "--flag8"]
tools.build:defines=["D1", "D2"]
""")
client = TestClient(path_with_spaces=False)
conanfile = GenConanfile().with_settings("os", "arch", "compiler", "build_type")\
.with_generator("CMakeToolchain")
client.save({"conanfile.py": conanfile,
"profile": profile})
client.run("install . --profile:build=profile --profile:host=profile")
toolchain = client.load("conan_toolchain.cmake")
assert 'string(APPEND CONAN_CXX_FLAGS " --flag1 --flag2")' in toolchain
assert 'string(APPEND CONAN_C_FLAGS " --flag3 --flag4")' in toolchain
assert 'string(APPEND CONAN_SHARED_LINKER_FLAGS " --flag5 --flag6")' in toolchain
assert 'string(APPEND CONAN_EXE_LINKER_FLAGS " --flag7 --flag8")' in toolchain
assert 'add_compile_definitions( "D1" "D2")' in toolchain
def test_cmake_presets_binary_dir_available():
client = TestClient(path_with_spaces=False)
conanfile = textwrap.dedent("""
from conan import ConanFile
from conan.tools.cmake import cmake_layout
class HelloConan(ConanFile):
generators = "CMakeToolchain"
settings = "os", "compiler", "build_type", "arch"
def layout(self):
cmake_layout(self)
""")
client.save({"conanfile.py": conanfile})
client.run("install .")
if platform.system() != "Windows":
build_dir = os.path.join(client.current_folder, "build", "Release")
else:
build_dir = os.path.join(client.current_folder, "build")
presets = load_cmake_presets(os.path.join(client.current_folder, "build", "generators"))
assert presets["configurePresets"][0]["binaryDir"] == build_dir
def test_cmake_presets_multiconfig():
client = TestClient()
profile = textwrap.dedent("""
[settings]
os = Windows
arch = x86_64
compiler=msvc
compiler.version=193
compiler.runtime=static
compiler.runtime_type=Release
""")
client.save({"conanfile.py": GenConanfile(), "profile": profile})
client.run("create . mylib/1.0@ -s build_type=Release --profile:h=profile")
client.run("create . mylib/1.0@ -s build_type=Debug --profile:h=profile")
client.run("install mylib/1.0@ -g CMakeToolchain -s build_type=Release --profile:h=profile")
presets = json.loads(client.load("CMakePresets.json"))
assert len(presets["buildPresets"]) == 1
assert presets["buildPresets"][0]["configuration"] == "Release"
client.run("install mylib/1.0@ -g CMakeToolchain -s build_type=Debug --profile:h=profile")
presets = json.loads(client.load("CMakePresets.json"))
assert len(presets["buildPresets"]) == 2
assert presets["buildPresets"][0]["configuration"] == "Release"
assert presets["buildPresets"][1]["configuration"] == "Debug"
client.run("install mylib/1.0@ -g CMakeToolchain -s build_type=RelWithDebInfo --profile:h=profile")
client.run("install mylib/1.0@ -g CMakeToolchain -s build_type=MinSizeRel --profile:h=profile")
presets = json.loads(client.load("CMakePresets.json"))
assert len(presets["buildPresets"]) == 4
assert presets["buildPresets"][0]["configuration"] == "Release"
assert presets["buildPresets"][1]["configuration"] == "Debug"
assert presets["buildPresets"][2]["configuration"] == "RelWithDebInfo"
assert presets["buildPresets"][3]["configuration"] == "MinSizeRel"
# Repeat one
client.run("install mylib/1.0@ -g CMakeToolchain -s build_type=Debug --profile:h=profile")
client.run("install mylib/1.0@ -g CMakeToolchain -s build_type=Debug --profile:h=profile")
assert len(presets["buildPresets"]) == 4
assert presets["buildPresets"][0]["configuration"] == "Release"
assert presets["buildPresets"][1]["configuration"] == "Debug"
assert presets["buildPresets"][2]["configuration"] == "RelWithDebInfo"
assert presets["buildPresets"][3]["configuration"] == "MinSizeRel"
assert len(presets["configurePresets"]) == 1
assert presets["configurePresets"][0]["name"] == "default"
def test_cmake_presets_singleconfig():
client = TestClient()
profile = textwrap.dedent("""
[settings]
os = Linux
arch = x86_64
compiler=gcc
compiler.version=8
""")
client.save({"conanfile.py": GenConanfile(), "profile": profile})
client.run("create . mylib/1.0@ -s build_type=Release --profile:h=profile")
client.run("create . mylib/1.0@ -s build_type=Debug --profile:h=profile")
client.run("install mylib/1.0@ -g CMakeToolchain -s build_type=Release --profile:h=profile")
presets = json.loads(client.load("CMakePresets.json"))
assert len(presets["configurePresets"]) == 1
assert presets["configurePresets"][0]["name"] == "release"
assert len(presets["buildPresets"]) == 1
assert presets["buildPresets"][0]["configurePreset"] == "release"
# Now two configurePreset, but named correctly
client.run("install mylib/1.0@ -g CMakeToolchain -s build_type=Debug --profile:h=profile")
presets = json.loads(client.load("CMakePresets.json"))
assert len(presets["configurePresets"]) == 2
assert presets["configurePresets"][1]["name"] == "debug"
assert len(presets["buildPresets"]) == 2
assert presets["buildPresets"][1]["configurePreset"] == "debug"
# Repeat configuration, it shouldn't add a new one
client.run("install mylib/1.0@ -g CMakeToolchain -s build_type=Debug --profile:h=profile")
presets = json.loads(client.load("CMakePresets.json"))
assert len(presets["configurePresets"]) == 2
def test_toolchain_cache_variables():
client = TestClient()
conanfile = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake
class Conan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
def generate(self):
toolchain = CMakeToolchain(self)
toolchain.cache_variables["foo"] = True
toolchain.cache_variables["foo2"] = False
toolchain.cache_variables["var"] = "23"
toolchain.cache_variables["CMAKE_SH"] = "THIS VALUE HAS PRIORITY"
toolchain.cache_variables["CMAKE_POLICY_DEFAULT_CMP0091"] = "THIS VALUE HAS PRIORITY"
toolchain.cache_variables["CMAKE_MAKE_PROGRAM"] = "THIS VALUE HAS NO PRIORITY"
toolchain.generate()
""")
client.save({"conanfile.py": conanfile})
with mock.patch("platform.system", mock.MagicMock(return_value="Windows")):
client.run("install . mylib/1.0@ -c tools.cmake.cmaketoolchain:generator='MinGW Makefiles' "
"-c tools.gnu:make_program='MyMake'")
presets = json.loads(client.load("CMakePresets.json"))
cache_variables = presets["configurePresets"][0]["cacheVariables"]
assert cache_variables["foo"] == 'ON'
assert cache_variables["foo2"] == 'OFF'
assert cache_variables["var"] == '23'
assert cache_variables["CMAKE_SH"] == "THIS VALUE HAS PRIORITY"
assert cache_variables["CMAKE_POLICY_DEFAULT_CMP0091"] == "THIS VALUE HAS PRIORITY"
assert cache_variables["CMAKE_MAKE_PROGRAM"] == "MyMake"
def test_android_c_library():
client = TestClient()
conanfile = textwrap.dedent("""
from conans import ConanFile
class Conan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeToolchain"
def configure(self):
del self.settings.compiler.libcxx
""")
client.save({"conanfile.py": conanfile})
client.run("create . foo/1.0@ -s os=Android -s os.api_level=23 -c tools.android:ndk_path=/foo")
def test_user_presets_version2():
client = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
from conan.tools.cmake import cmake_layout
class Conan(ConanFile):
name = "foo"
version = "1.0"
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeToolchain"
def layout(self):
cmake_layout(self)
""")
client.save({"conanfile.py": conanfile, "CMakeLists.txt": "foo"})
configs = ["-c tools.cmake.cmaketoolchain.presets:max_schema_version=2 ",
"-c tools.cmake.cmake_layout:build_folder_vars='[\"settings.compiler.cppstd\"]'"]
client.run("install . {} -s compiler.cppstd=14".format(" ".join(configs)))
client.run("install . {} -s compiler.cppstd=17".format(" ".join(configs)))
presets = json.loads(client.load("CMakeUserPresets.json"))
assert len(presets["configurePresets"]) == 2
assert presets["version"] == 2
assert "build/14/generators/conan_toolchain.cmake" \
in presets["configurePresets"][0]["cacheVariables"]["CMAKE_TOOLCHAIN_FILE"].replace("\\",
"/")
assert "build/17/generators/conan_toolchain.cmake" \
in presets["configurePresets"][1]["cacheVariables"]["CMAKE_TOOLCHAIN_FILE"].replace("\\",
"/")
def test_user_presets_version2_no_overwrite_user():
client = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
from conan.tools.cmake import cmake_layout
class Conan(ConanFile):
name = "foo"
version = "1.0"
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeToolchain"
def layout(self):
cmake_layout(self)
""")
client.save({"conanfile.py": conanfile, "CMakeLists.txt": "foo",
"CMakeUserPresets.json": '{"from_user": 1}'})
configs = ["-c tools.cmake.cmaketoolchain.presets:max_schema_version=2 ",
"-c tools.cmake.cmake_layout:build_folder_vars='[\"settings.compiler.cppstd\"]'"]
client.run("install . {} -s compiler.cppstd=14".format(" ".join(configs)))
presets = json.loads(client.load("CMakeUserPresets.json"))
assert presets == {"from_user": 1}
@pytest.mark.skipif(platform.system() != "Windows", reason="Only Windows")
def test_presets_paths_correct():
client = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
from conan.tools.cmake import cmake_layout
class Conan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeToolchain"
def layout(self):
cmake_layout(self)
""")
client.save({"conanfile.py": conanfile, "CMakeLists.txt": "foo"})
configs = ["-c tools.cmake.cmaketoolchain.presets:max_schema_version=2 ",
"-c tools.cmake.cmake_layout:build_folder_vars='[\"settings.compiler.cppstd\"]'"]
client.run("install . {} -s compiler.cppstd=14".format(" ".join(configs)))
client.run("install . {} -s compiler.cppstd=17".format(" ".join(configs)))
presets = json.loads(client.load("CMakeUserPresets.json"))
assert len(presets["configurePresets"]) == 2
assert presets["version"] == 2
assert "build/14/generators/conan_toolchain.cmake" \
in presets["configurePresets"][0]["cacheVariables"]["CMAKE_TOOLCHAIN_FILE"].replace("\\",
"/")
assert "build/17/generators/conan_toolchain.cmake" \
in presets["configurePresets"][1]["cacheVariables"]["CMAKE_TOOLCHAIN_FILE"].replace("\\",
"/")