-
-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathconanfile.py
More file actions
481 lines (446 loc) · 21.1 KB
/
Copy pathconanfile.py
File metadata and controls
481 lines (446 loc) · 21.1 KB
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
# The MIT License (MIT)
#
# Copyright (c) 2018 Mateusz Pusz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import re
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.build import can_run, default_cppstd, valid_min_cppstd
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import copy, load, rm
from conan.tools.scm import Version
required_conan_version = ">=2.0.15"
class MPUnitsConan(ConanFile):
name = "mp-units"
homepage = "https://github.com/mpusz/mp-units"
description = "The quantities and units library for C++"
topics = (
"units",
"dimensions",
"quantities",
"dimensional-analysis",
"physical-quantities",
"physical-units",
"system-of-units",
"system-of-quantities",
"isq",
"si",
"library",
"quantity-manipulation",
)
license = "MIT"
url = "https://github.com/mpusz/mp-units"
settings = "os", "arch", "compiler", "build_type"
options = {
"cxx_modules": [True, False],
"import_std": [True, False],
"std_format": [True, False],
"no_crtp": [True, False],
"contracts": ["none", "std", "gsl-lite", "ms-gsl"],
"freestanding": [True, False],
}
default_options = {
# "cxx_modules" default set in config_options()
# "import_std" default set in config_options()
# "std_format" default set in config_options()
# "no_crtp" default set in config_options()
"import_std": False, # still experimental in CMake
"contracts": "gsl-lite",
"freestanding": False,
}
# third-party libraries exercised by the linear algebra integration example and tests. These are
# never runtime requirements of mp-units: the integration headers are dependency-free (guarded
# by `__has_include`) and always ship; only the optional C++ modules are compiled against a
# library, and only when it happens to be available (e.g. in the `build_all` developer build).
_linear_algebra_libs = ["eigen/5.0.1", "glm/1.0.1", "blaze/3.8.2"]
implements = ["auto_header_only"]
exports = "LICENSE.md"
exports_sources = (
"docs/*",
"src/*",
"test/*",
"cmake/*",
"example/*",
"CMakeLists.txt",
)
no_copy_source = True
@property
def _feature_compatibility(self):
return {
"minimum_support": {
"min_cppstd": "20",
"compiler": {
"gcc": "12",
"clang": "16",
"apple-clang": "15",
"msvc": "195",
},
},
"std_format": {
"min_cppstd": "20",
"compiler": {
"gcc": "13",
"clang": "17",
"apple-clang": "16",
"msvc": "195",
},
},
"cxx_modules": {
"min_cppstd": "20",
"compiler": {"gcc": "", "clang": "17", "apple-clang": "", "msvc": ""},
},
"import_std": {
"min_cppstd": "23",
"compiler": {
"gcc": "15",
"clang": "18",
"apple-clang": "",
"msvc": "195",
},
},
"explicit_this": {
"min_cppstd": "23",
"compiler": {
"gcc": "14",
"clang": "18",
"apple-clang": "17",
"msvc": "195",
},
},
"std_contracts": {
"min_cppstd": "26",
"compiler": {
"gcc": "16",
"clang": "",
"apple-clang": "",
"msvc": "",
},
},
}
@property
def _option_feature_map(self):
return {
"std_format": "std_format",
"cxx_modules": "cxx_modules",
# "import_std": "import_std", # still experimental in CMake
"no_crtp": "explicit_this",
}
def _set_default_option(self, name):
compiler = self.settings.compiler
feature_name = self._option_feature_map[name]
feature = self._feature_compatibility[feature_name]
min_version = feature["compiler"].get(str(compiler))
setattr(
self.options,
name,
bool(
min_version
and Version(compiler.version) >= min_version
and valid_min_cppstd(self, feature["min_cppstd"])
),
)
def _check_feature_supported(self, name, feature_name=name):
compiler = self.settings.compiler
cppstd = compiler.get_safe("cppstd", default_cppstd(self))
feature = self._feature_compatibility[feature_name]
# check C++ version
if not valid_min_cppstd(self, feature["min_cppstd"]):
raise ConanInvalidConfiguration(
f"'{name}' requires at least cppstd={feature['min_cppstd']} ({cppstd} in use)",
)
# check compiler version
min_version = feature["compiler"].get(str(compiler))
if min_version == None:
# not tested compiler being used - use at your own risk
return
if min_version == "":
raise ConanInvalidConfiguration(
f"'{name}' is not yet supported by any known {compiler} version"
)
if Version(compiler.version) < min_version:
raise ConanInvalidConfiguration(
f"'{name}' requires at least {compiler}-{min_version} ({compiler}-{compiler.version} in use)"
)
@property
def _build_all(self):
return bool(self.conf.get("user.mp-units.build:all", default=False))
@property
def _run_clang_tidy(self):
return bool(self.conf.get("user.mp-units.analyze:clang-tidy", default=False))
def set_version(self):
content = load(self, os.path.join(self.recipe_folder, "src/CMakeLists.txt"))
version = re.search(
r"project\([^\)]+VERSION (\d+\.\d+\.\d+)[^\)]*\)", content
).group(1)
self.version = version.strip()
def config_options(self):
for key in self._option_feature_map.keys():
self._set_default_option(key)
# TODO mixing of `import std;` and regular header files includes does not work for now
if self.options.import_std:
self.options.contracts = "none"
def configure(self):
if self.options.cxx_modules:
self.package_type = "static-library"
else:
self.package_type = "header-library"
if self.options.freestanding:
self.options.rm_safe("std_format")
def requirements(self):
if not self.options.freestanding:
if self.options.contracts == "gsl-lite":
self.requires("gsl-lite/1.0.1", transitive_headers=True)
elif self.options.contracts == "ms-gsl":
self.requires("ms-gsl/4.2.0", transitive_headers=True)
if not self.options.std_format:
self.requires("fmt/12.1.0", transitive_headers=True)
def build_requirements(self):
self.tool_requires("cmake/[>=4.4.0 <5]")
if self.options.freestanding:
return
if self._build_all:
self.test_requires("catch2/3.11.0")
# Make the linear algebra libraries available in the BUILD context
# A consumer that later uses an integration pulls its library in via
# `find_package(mp-units-integrations-<lib>)`, not via mp-units itself.
if self._build_all or self.options.get_safe("cxx_modules"):
for ref in self._linear_algebra_libs:
self.test_requires(ref)
def validate(self):
compiler = self.settings.compiler
if compiler == "clang" and Version(compiler.version).major == 19:
raise ConanInvalidConfiguration(
"clang-19 does not build mp-units because of an unfixable bug in the compiler."
)
if compiler == "apple-clang" and Version(compiler.version).major == 17:
raise ConanInvalidConfiguration(
"apple-clang-17 (Xcode 16.3+) does not build mp-units "
+ "because it has the same unfixable bug as clang-19."
)
self._check_feature_supported("mp-units", "minimum_support")
for key, value in self._option_feature_map.items():
if self.options.get_safe(key) == True:
self._check_feature_supported(key, value)
if self.options.contracts == "std":
self._check_feature_supported("contracts", "std_contracts")
# third-party contract libraries require a hosted implementation; C++26 contracts
# are a language feature (the application has to provide a contract-violation
# handler or compile with an evaluation semantic that does not need one)
if self.options.freestanding and self.options.contracts not in ["none", "std"]:
raise ConanInvalidConfiguration(
"'contracts' should be set to 'none' or 'std' for a freestanding build"
)
# TODO mixing of `import std;` and regular header files includes does not work for now
if self.options.import_std:
# C++26 contracts are a language feature and do not bring any third-party
# library headers that would conflict with `import std;`
if self.options.contracts not in ["none", "std"]:
raise ConanInvalidConfiguration(
"'contracts' should be set to 'none' or 'std' to use `import std;`"
)
if not self.options.get_safe("std_format", default=True):
raise ConanInvalidConfiguration(
"'std_format' should be enabled to use `import std;`"
)
# MSVC Debug + std::format triggers error C7595 ("call to immediate function is not
# a constant expression") on heavily templated quantity types — the
# std::basic_format_string consteval ctor can't be evaluated under Debug's stricter
# constant evaluator. Release works; {fmt} works. Mirror the CI matrix's
# exclusion of this combination here so a local repro fails fast at install time.
if (
compiler == "msvc"
and self.settings.build_type == "Debug"
and self.options.get_safe("std_format", default=True)
):
raise ConanInvalidConfiguration(
"'std_format' is not supported on MSVC Debug builds (C7595: consteval "
"format-string ctor fails on deeply templated quantity arguments). "
"Use 'std_format=False' to build with {fmt} instead, or build Release."
)
def layout(self):
cmake_layout(self)
def generate(self):
opt = self.options
tc = CMakeToolchain(self)
tc.absolute_paths = True # only needed for CMake CI
if self._build_all:
tc.cache_variables["CMAKE_EXPORT_COMPILE_COMMANDS"] = True
tc.cache_variables["CMAKE_COMPILE_WARNING_AS_ERROR"] = True
tc.cache_variables["CMAKE_VERIFY_INTERFACE_HEADER_SETS"] = (
not opt.import_std
)
if self._run_clang_tidy:
tc.cache_variables["MP_UNITS_DEV_CLANG_TIDY"] = True
tc.cache_variables["MP_UNITS_BUILD_CXX_MODULES"] = opt.cxx_modules
if opt.cxx_modules or opt.import_std:
tc.cache_variables["CMAKE_CXX_SCAN_FOR_MODULES"] = True
if opt.import_std:
tc.cache_variables["CMAKE_CXX_MODULE_STD"] = True
# Current experimental support according to `Help/dev/experimental.rst`
tc.cache_variables["CMAKE_EXPERIMENTAL_CXX_IMPORT_STD"] = (
"f35a9ac6-8463-4d38-8eec-5d6008153e7d"
)
# TODO remove the below when Conan will learn to handle C++ modules
if opt.freestanding:
tc.cache_variables["MP_UNITS_API_FREESTANDING"] = True
# Fix for freestanding builds: CMake compiler tests fail when linking with -ffreestanding
# Set CMAKE_TRY_COMPILE_TARGET_TYPE to STATIC_LIBRARY to avoid linking during compiler tests
tc.cache_variables["CMAKE_TRY_COMPILE_TARGET_TYPE"] = "STATIC_LIBRARY"
else:
tc.cache_variables["MP_UNITS_API_STD_FORMAT"] = opt.std_format
tc.cache_variables["MP_UNITS_API_NO_CRTP"] = opt.no_crtp
tc.cache_variables["MP_UNITS_API_CONTRACTS"] = str(opt.contracts).upper()
tc.generate()
deps = CMakeDeps(self)
deps.generate()
def build(self):
cmake = CMake(self)
cmake.configure(build_script_folder=None if self._build_all else "src")
if self._build_all or self.options.cxx_modules:
cmake.build()
if self._build_all:
if not self.options.import_std:
cmake.build(target="all_verify_interface_header_sets")
if can_run(self):
cmake.ctest(cli_args=["--output-on-failure"])
def package(self):
copy(
self,
"LICENSE.md",
self.source_folder,
os.path.join(self.package_folder, "licenses"),
)
cmake = CMake(self)
cmake.install()
# TODO remove the below when Conan will learn to handle C++ modules
if not self.options.cxx_modules:
# CMakeDeps regenerates the package config from cpp_info when
# cxx_modules is off, so the bundled `mp-unitsConfig.cmake` /
# `mp-unitsTargets*.cmake` exports would conflict. Remove only
# those — keep helper scripts like `import-std-setup.cmake`,
# which `cmake_build_modules` points at for import_std consumers.
cmake_dir = os.path.join(self.package_folder, "lib", "cmake", "mp-units")
rm(self, "mp-unitsConfig.cmake", cmake_dir)
rm(self, "mp-unitsConfigVersion.cmake", cmake_dir)
rm(self, "mp-unitsTargets*.cmake", cmake_dir)
def package_info(self):
compiler = self.settings.compiler
# TODO remove the branch when Conan will learn to handle C++ modules
if self.options.cxx_modules:
# CMakeDeps does not generate C++ modules definitions for now
# Skip the Conan-generated files and use the mp-unitsConfig.cmake bundled with mp-units
self.cpp_info.set_property("cmake_find_mode", "none")
self.cpp_info.builddirs = ["."]
else:
# handle contracts
if self.options.contracts == "none":
self.cpp_info.components["core"].defines.append(
"MP_UNITS_API_CONTRACTS=0"
)
elif self.options.contracts == "std":
self.cpp_info.components["core"].defines.append(
"MP_UNITS_API_CONTRACTS=1"
)
if self.settings.compiler == "gcc":
self.cpp_info.components["core"].cxxflags.append("-fcontracts")
if not self.options.freestanding:
# the default contract-violation handler lives in the experimental
# library; a freestanding application has to provide its own handler
# (or compile with `-fcontract-evaluation-semantic=quick_enforce`)
self.cpp_info.components["core"].system_libs.append("stdc++exp")
elif self.options.contracts == "gsl-lite":
self.cpp_info.components["core"].requires.append("gsl-lite::gsl-lite")
self.cpp_info.components["core"].defines.append(
"MP_UNITS_API_CONTRACTS=2"
)
elif self.options.contracts == "ms-gsl":
self.cpp_info.components["core"].requires.append("ms-gsl::ms-gsl")
self.cpp_info.components["core"].defines.append(
"MP_UNITS_API_CONTRACTS=3"
)
# handle API options
self.cpp_info.components["core"].defines.append(
"MP_UNITS_API_NO_CRTP=" + str(int(self.options.no_crtp == True))
)
# handle hosted configuration
if self.options.freestanding:
self.cpp_info.components["core"].defines.append("MP_UNITS_HOSTED=0")
if compiler == "msvc":
self.cpp_info.components["core"].cxxflags.append("/kernel")
else:
self.cpp_info.components["core"].cxxflags.append("-ffreestanding")
else:
self.cpp_info.components["core"].defines.append("MP_UNITS_HOSTED=1")
if not self.options.std_format:
self.cpp_info.components["core"].requires.append("fmt::fmt")
self.cpp_info.components["core"].defines.append(
"MP_UNITS_API_STD_FORMAT="
+ str(int(self.options.std_format == True))
)
# handle import std
if self.options.import_std:
self.cpp_info.components["core"].defines.append("MP_UNITS_IMPORT_STD")
if compiler == "clang" and Version(compiler.version) < 19:
self.cpp_info.components["core"].cxxflags.append(
"-Wno-deprecated-declarations"
)
# Ship the import-std-setup.cmake snippet that escalates the
# compiler-specific flags needed for `import std;` to project-wide
# compile options, so CMake's implicit std module BMI is built
# with matching preprocessor / warning state (MSVC C5050, Clang 21
# reserved-module-identifier). `cmake_find_mode = "none"` consumers
# (cxx_modules=True) get the same snippet through the bundled
# mp-unitsConfig.cmake instead.
self.cpp_info.components["core"].set_property(
"cmake_build_modules",
[
os.path.join(
"lib", "cmake", "mp-units", "import-std-setup.cmake"
)
],
)
if compiler == "msvc":
self.cpp_info.components["core"].cxxflags.append("/utf-8")
self.cpp_info.components["systems"].requires = ["core"]
# Add-on representation types and helpers (cartesian/polar vectors, tensors, constrained,
# safe_int, random) layered above systems; everything beyond the standardizable core.
self.cpp_info.components["utility"].requires = ["core", "systems"]
# The dependency-free integration headers (`mp-units::integrations`). Mirrors the CMake
# target exported in `mp-unitsTargets`. The third-party backends stay out of the dependency
# graph: they are pulled in only by the separate `find_package(mp-units-integrations-<lib>)`
# packages, never by mp-units itself.
#
# The per-backend `mp-units::integrations-<lib>` targets are intentionally NOT surfaced as
# Conan components. They are a CMake-level feature for source consumers (`add_subdirectory`/
# FetchContent) and raw `cmake --install` users, where the backend is found at configure time
# and the module is built from source. Conan consumers instead link `mp-units::integrations`
# (always-shipped, dependency-free headers) plus their own copy of the linear algebra library
# (see `test_package`). Making them Conan components would force opt-in host dependencies on
# mp-units for a benefit that only fully lands once module consumption from an installed
# package is supported - revisit then.
self.cpp_info.components["integrations"].requires = ["core"]
# https://github.com/llvm/llvm-project/issues/131410
if (
compiler == "clang"
and Version(compiler.version).major == 20
and Version(compiler.version).minor == 1
):
self.cpp_info.components["core"].cxxflags.append("-Wno-unused-result")