-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconanfile.py
More file actions
437 lines (369 loc) · 16.9 KB
/
conanfile.py
File metadata and controls
437 lines (369 loc) · 16.9 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
"""
*** BEGIN LICENSE INFORMATION ***
The contents of this file are subject to the Common Public Attribution License
Version 1.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
https://lass.cocamware.com/cpal-license. The License is based on the
Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
use of software over a computer network and provide for limited attribution for
the Original Developer. In addition, Exhibit A has been modified to be consistent
with Exhibit B.
Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
WARRANTY OF ANY KIND, either express or implied. See the License for the specific
language governing rights and limitations under the License.
The Original Code is LASS - Library of Assembled Shared Sources.
The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
The Original Developer is the Initial Developer.
All portions of the code written by the Initial Developer are:
Copyright (C) 2018-2025 the Initial Developer.
All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the GPL), in which case the
provisions of GPL are applicable instead of those above. If you wish to allow use
of your version of this file only under the terms of the GPL and not to allow
others to use your version of this file under the CPAL, indicate your decision by
deleting the provisions above and replace them with the notice and other
provisions required by the GPL License. If you do not delete the provisions above,
a recipient may use your version of this file under either the CPAL or the GPL.
*** END LICENSE INFORMATION ***
"""
import json
import os
import re
import shutil
import subprocess
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path
from types import ModuleType
from typing import Any, List, Optional, Protocol
from conan import ConanFile
from conan.errors import ConanException, ConanInvalidConfiguration
from conan.internal.model.options import _PackageOption
from conan.internal.model.settings import SettingsItem as _SettingsItem
from conan.tools import CppInfo
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.microsoft import check_min_vs
from conan.tools.scm import Git
required_conan_version = ">=2.12.0"
class _Settings(Protocol):
os: _SettingsItem
compiler: _SettingsItem
build_type: _SettingsItem
arch: _SettingsItem
def get_safe(self, name: str, default: Any = None) -> Optional[_SettingsItem]: ...
class _Options(Protocol):
shared: _PackageOption
fPIC: _PackageOption
simd_aligned: _PackageOption
without_iterator_debugging: _PackageOption
have_avx: _PackageOption
with_stubgen: _PackageOption
def get_safe(self, name: str, default: Any = None) -> Optional[_PackageOption]: ...
def rm_safe(self, name: str) -> None: ...
def _get_version(conanfile: ConanFile) -> Optional[str]:
try:
content = (Path(conanfile.recipe_folder) / "CMakeLists.txt").read_text()
except FileNotFoundError:
return None # Assume conan metadata already knows
match = re.search(r"project\(Lass VERSION (\d+\.\d+\.\d+)\)", content)
assert match
version = match.group(1)
git = Git(conanfile)
# if this version has been tagged in Git, it's either this very commit (and a
# release version), or a post-release (which is not allowed).
version_tag = f"lass-{version}"
try:
describe = git.run(f"describe --abbrev=8 --dirty --tags --match {version_tag}")
except (ConanException, subprocess.CalledProcessError):
pass # release tag doesn't exist yet, it's a pre-release.
else:
assert describe == version_tag, (
f"Post-release builds are not allowed. Set base_tag to {version_tag!r} and "
+ "increment project(Lass VERSION X.Y.Z) in CMakeLists.txt."
)
return version
try:
describe = git.run("describe --abbrev=8 --dirty --tags --match lass-*")
except ConanException:
return None # Assume conan metadata already knows
match = re.match(r"lass-(\d+\.\d+\.\d+)-(\d+)-(g\w+(-dirty)?)$", describe)
assert match, "unexpected describe format: {!r}".format(describe)
prev_version, pre_release, rev = match.group(1), match.group(2), match.group(3)
assert _int_version(prev_version) < _int_version(version)
return f"{version}-{pre_release}+{rev}"
def _int_version(str_version: str) -> List[int]:
return [int(x) for x in str_version.split(".")]
class LassConan(ConanFile): # type: ignore[misc]
name = "lass"
package_type = "library"
def set_version(self) -> None:
self.version: Optional[str] = self.version or _get_version(self)
license = "CPAL-1.0", "GPL-2.0-or-later"
author = "Cocamware <info@cocamware.com>"
url = "https://github.com/cocamware/lass.git"
description = "Library of Assembled Shared Source."
topics = "C++", "Python"
settings: _Settings = "os", "compiler", "build_type", "arch" # type: ignore[assignment]
options: _Options = { # type: ignore[assignment]
"shared": [True, False],
"fPIC": [True, False],
"simd_aligned": [True, False],
"without_iterator_debugging": [True, False],
"have_avx": [True, False],
"with_stubgen": [True, False, "auto"],
}
default_options = {
"shared": True,
"fPIC": True,
"simd_aligned": False,
"without_iterator_debugging": False,
"have_avx": True,
"with_stubgen": "auto",
}
requires = "syspython/1.0.5@cocamware/stable"
def export_sources(self) -> None:
git = Git(self)
regex = re.compile(r"i/(?P<eol_index>\S+)\s+w/(?P<eol_worktree>\S+)")
for line in git.run("ls-files --eol").splitlines():
match = regex.match(line)
assert match, f"Failed to parse line {line}"
if match["eol_worktree"] != match["eol_index"]:
raise AssertionError(
"Some files are checked out in the worktree with "
f"{match['eol_worktree'].upper()} line endings while they have "
f"{match['eol_index'].upper()} line endings in the index. This "
"would result in the wrong Conan recipe revision ID.\n"
"Fix this with following commands (backup local changes first!):\n"
"\n"
" git rm --cached -r .\n"
" git reset --hard\n"
"\n"
"You can verify that all files have the expected line endings with "
"the following command:\n"
"\n"
" git ls-files --eol\n"
)
for src in git.run("ls-files").splitlines():
dest = os.path.join(self.export_sources_folder, src)
os.makedirs(os.path.dirname(dest), exist_ok=True)
shutil.copy2(src, dest)
def config_options(self) -> None:
if self.settings.os == "Windows":
del self.options.fPIC
if self.settings.arch not in ["x86", "x86_64"]:
self.options.rm_safe("have_avx")
def configure(self) -> None:
if self.options.shared:
self.options.rm_safe("fPIC")
def validate(self) -> None:
check_min_cppstd(self, 17)
compiler = self.settings.get_safe("compiler")
if compiler in ["gcc", "clang"]:
if self.settings.get_safe("compiler.libcxx") in ["libstdc++"]:
raise ConanInvalidConfiguration(
"gcc and clang require C++11 compatible libcxx"
)
elif compiler in ["msvc"]:
check_min_vs(self, "193") # require VS 2019 as minimum
def layout(self) -> None:
cmake_layout(self)
subdir = self.cpp.build.libdirs[0]
build_libdir = os.path.join("lib", subdir)
build_bindir = os.path.join("bin", subdir)
share_dir = os.path.join("share", "Lass")
# include dirs for local build
self.cpp.source.includedirs = ["."]
self.cpp.build.includedirs = ["local"]
# binary dirs for local build
self.cpp.build.libdirs = [build_libdir]
self.cpp.build.bindirs = [build_bindir]
# We generate our own LassConfig.cmake, but we have to tell conan
# where it can be found, relative to the package or build dir ...
self.cpp.package.builddirs = [share_dir]
self.cpp.build.builddirs = ["."]
# LassStubgen.cmake and stubgen folder are installed in share/Lass,
# but in editable mode it is found in the source folder tools
self.cpp.source.builddirs = ["tools"]
# Repeat for components
for comp in ["lass", "lass_python"]:
self.cpp.source.components[comp].includedirs = ["."]
self.cpp.build.components[comp].includedirs = ["local"]
self.cpp.build.components[comp].libdirs = [build_libdir]
self.cpp.build.components[comp].bindirs = [build_bindir]
self.cpp.package.components[comp].builddirs = [share_dir]
self.cpp.build.components[comp].builddirs = ["."]
def generate(self) -> None:
python = self.dependencies["syspython"]
tc = CMakeToolchain(self)
tc.cache_variables["BUILD_SIMD_ALIGNED"] = bool(self.options.simd_aligned)
tc.cache_variables["BUILD_WITHOUT_ITERATOR_DEBUGGING"] = bool(
self.options.without_iterator_debugging
)
tc.cache_variables["Lass_PYTHON_VERSION"] = python.options.python_version
tc.cache_variables["BUILD_TESTING"] = True
tc.cache_variables["Lass_HAVE_AVX"] = self.options.get_safe("have_avx", False)
if self.options.with_stubgen != "auto":
tc.cache_variables["Lass_WITH_STUBGEN"] = self.options.with_stubgen
if libclang := self.conf.get("user.lass:libclang_library"):
tc.cache_variables["Lass_LIBCLANG_LIBRARY"] = libclang
if (
build_folder := self.conf.get("tools.cmake.cmake_layout:build_folder")
) and build_folder != "build":
tc.presets_prefix = build_folder
tc.generate()
vs = VSCodeCCppProperties(self)
vs.generate()
def build(self) -> None:
cmake = CMake(self)
cmake.configure()
cmake.build()
cmake.test()
def package(self) -> None:
cmake = CMake(self)
cmake.install()
def package_info(self) -> None:
if self.folders.build_folder:
# we're in editable mode, find LassConfig.py in the build folder
lass_config = self._load_module_from_file(
os.path.join(self.folders.build_folder, "LassConfig.py")
)
else:
# we're in normal package mode, find LassConfig.py in the package folder
lass_config = self._load_module_from_file(
os.path.join(self.package_folder, "share/Lass/LassConfig.py")
)
if self.settings.build_type == "Debug":
components = {
"lass": lass_config.LASS_OUTPUT_NAME_DEBUG,
"lass_python": lass_config.LASS_PYTHON_OUTPUT_NAME_DEBUG,
}
else:
components = {
"lass": lass_config.LASS_OUTPUT_NAME,
"lass_python": lass_config.LASS_PYTHON_OUTPUT_NAME,
}
# Define the components that this package provides
for comp, lib in components.items():
self.cpp_info.components[comp].libs = [lib]
self.cpp_info.components[comp].cxxflags = lass_config.LASS_EXTRA_CXX_FLAGS
self.cpp_info.components[comp].set_property(
"cmake_target_name", f"Lass::{comp}"
)
self.cpp_info.components["lass_python"].requires = ["lass"]
# We generate our own LassConfig.cmake, so CMakeDeps should generate none
self.cpp_info.set_property("cmake_find_mode", "none")
self.cpp_info.set_property("cmake_file_name", "Lass") # for LassConfig.cmake
# The default CMake target that a consumer's CMakeDeps's generated config will
# link against. This is the main lass library, as it's the most common use case.
#
# If Conan package pkg requires Lass and has CMakeDeps generate its
# pkg-config.cmake (i.e. doesn't provides its own, like Lass does), by default
# its imported target pkg::pkg will *only* link against the main lass library.
#
# If pkg also requires the Python bindings, it should specify that in
# package_info of its conanfile.py:
#
# def package_info(self):
# self.cpp_info.requires = ["lass::lass_python"]
#
# Note that it should be spelled as lowercase lass:: for Conan 2, as this is the
# name of the Conan package, and not the CMake namepace Lass::
#
self.cpp_info.set_property("cmake_target_name", "Lass::lass")
def _load_module_from_file(self, filename: str) -> ModuleType:
file_path = Path(filename)
module_name = file_path.stem
spec = spec_from_file_location(module_name, file_path)
if not spec or not spec.loader:
raise ConanException(f"Failed to load module from {filename}")
module = module_from_spec(spec)
spec.loader.exec_module(module)
return module
class VSCodeCCppProperties:
name: str
include_path: List[str]
defines: List[str]
compiler_args: List[str]
cpp_standard: str
intellisense_mode: str
configuration_provider: Optional[str]
compile_commands: Optional[str]
def __init__(self, conanfile: ConanFile, *, cmake: bool = True):
self._conanfile = conanfile
self.name = f"conan-{str(conanfile.settings.build_type).lower()}"
cpp_info = CppInfo(conanfile)
cpp_info.merge(conanfile.cpp.source)
cpp_info.set_relative_base_folder(conanfile.source_folder)
cpp_info.merge(conanfile.cpp.build)
cpp_info.set_relative_base_folder(conanfile.build_folder)
deps = conanfile.dependencies.host.topological_sort
for dep in reversed(deps.values()):
cpp_info.merge(dep.cpp_info.aggregated_components())
self.include_path = cpp_info.includedirs
self.defines = cpp_info.defines
self.compiler_args = cpp_info.cxxflags
cppstd = conanfile.settings.get_safe("compiler.cppstd")
self.cpp_standard = f"c++{cppstd.replace('gnu', '')}"
os_ = str(conanfile.settings.os).lower()
compiler = str(conanfile.settings.compiler).lower()
if compiler == "visual studio": # old compiler setting in Conan 1
compiler = "msvc"
arch = str(conanfile.settings.arch)
if arch == "x86":
pass
elif arch == "x86_64":
arch = "x64"
elif arch.startswith("arm64"):
arch = "arm64"
elif match := re.match(r"armv(\d+)", arch):
if int(match.group(1)) < 8:
arch = "arm"
else:
arch = "arm64"
else:
raise ConanInvalidConfiguration(f"Unsupported architecture {arch}")
self.intellisense_mode = f"{os_}-{compiler}-{arch}"
if cmake:
self.configuration_provider = "ms-vscode.cmake-tools"
self.compile_commands = (
os.path.join(conanfile.build_folder, "compile_commands.json")
if compiler != "msvc"
else None
)
else:
self.configuration_provider = None
self.compile_commands = None
def generate(self) -> None:
vscode_folder = os.path.join(self._conanfile.source_folder, ".vscode")
c_cpp_properties = os.path.join(vscode_folder, "c_cpp_properties.json")
try:
with open(c_cpp_properties) as fp:
data = json.load(fp)
except FileNotFoundError:
data = {
"configurations": [],
"version": 4,
}
configuration = {
"name": self.name,
"includePath": self.include_path,
"defines": self.defines,
"compilerArgs": self.compiler_args,
"cppStandard": self.cpp_standard,
"intelliSenseMode": self.intellisense_mode,
}
if self.configuration_provider:
configuration["configurationProvider"] = self.configuration_provider
if self.compile_commands:
configuration["compileCommands"] = self.compile_commands
for index, config in enumerate(data["configurations"]):
if config["name"] == configuration["name"]:
data["configurations"][index] = configuration
break
else:
data["configurations"].append(configuration)
os.makedirs(vscode_folder, exist_ok=True)
with open(c_cpp_properties, "w") as fp:
json.dump(data, fp, indent=2)