-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconanfile.py
286 lines (236 loc) · 10.7 KB
/
conanfile.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
import textwrap
from pathlib import Path
from jinja2 import Template
from conan import ConanFile
from conan.tools.microsoft import VCVars
from conan.tools.cmake.toolchain.blocks import Block
from conan.tools.cmake.toolchain.toolchain import ToolchainBlocks
from conan.tools.gnu.autotoolstoolchain import AutotoolsToolchain
from conan.tools.files import save
from conan.errors import ConanInvalidConfiguration
from conan.tools._check_build_profile import check_using_build_profile
from conans.tools import Version
class BuildSystemBlock(Block):
template = textwrap.dedent("""
[build-system]
requires = [{{ build_requires }}]
{{ build_backend }}
""")
def context(self):
build_requires = self._conanfile.options.get_safe("py_build_requires")
if build_requires is None:
build_requires = "setuptools>=40.8.0", "wheel"
build_backend = self._conanfile.options.get_safe("py_build_backend")
if build_backend is not None:
build_backend = f"build-backend = \"{build_backend}\""
return {"build_requires": build_requires, "build_backend": build_backend}
class ToolSipMetadataBlock(Block):
template = textwrap.dedent("""
[tool.sip.metadata]
name = "{{ name }}"
version = "{{ version }}"
summary = "{{ description }}"
home-page = "{{ url }}"
author = "{{ author }}"
license = "{{ license }}"
description-file = "{{ description_file }}"
requires-python = ">={{ python_version }}"
""")
def context(self):
python_version = self._conanfile.options.get_safe("py_version")
if python_version is None:
try:
python_version = self._conanfile.dependencies["cpython"].ref.version
except:
raise ConanInvalidConfiguration(
"No minimum required Python version specified, either add the options: 'py_version' of add cpython as a Conan dependency!")
mod_version = Version(self._conanfile.version)
pypi_version = f"{mod_version.major}.{mod_version.minor}.{mod_version.patch}"
if mod_version.prerelease != "":
split_prerelease = mod_version.prerelease.split(".")
if len(split_prerelease) > 1:
pypi_version += f"{split_prerelease[0][0]}{split_prerelease[1]}"
else:
pypi_version += split_prerelease[0][0]
return {
"name": self._conanfile.name,
"version": pypi_version,
"description": self._conanfile.description,
"url": self._conanfile.url,
"author": self._conanfile.author,
"license": self._conanfile.license,
"description_file": "README.md",
"python_version": python_version
}
class ToolSipProjectPyQtBuilder(Block):
template = textwrap.dedent("""
{% if link_full_dll %}link-full-dll = true
{% endif %}py-pylib-dir = "{{ py_pylib_dir }}"
py-pylib-lib = "{{ py_pylib_lib }}"
{% if py_pylib_shlib is not none %}py-pylib-shlib = "{{ py_pylib_shlib }}"
{% endif %}qml-debug = {{ qml_debug | lower }}
""")
def context(self):
py_lib = self._conanfile.options.get_safe("py_lib")
py_lib_dir = self._conanfile.options.get_safe("py_lib_dir")
if py_lib_dir is None:
try:
py_lib_dir = Path(self._conanfile.deps_cpp_info['cpython'].rootpath, self._conanfile.deps_cpp_info['cpython'].components["python"].bindirs[0], "libs").as_posix()
py_lib = self._conanfile.deps_cpp_info['cpython'].libs[0]
except:
self._conanfile.output.warn(
"No include directory set for Python.h, either add the options: 'py_include' of add cpython as a Conan dependency!")
else:
py_lib_dir = Path(py_lib_dir).as_posix()
return {
"link_full_dll": self._conanfile.settings.get_safe("os") == "Windows" and self._conanfile.options.get_safe("shared", True),
"py_pylib_dir": py_lib_dir,
"py_pylib_lib": py_lib,
"py_pylib_shlib": py_lib,
"qml_debug": self._conanfile.settings.get_safe("build_type", "Release") == "Debug"
}
class ToolSipProjectBlock(Block):
template = textwrap.dedent("""
[tool.sip.project]
compile = {{ compile | lower }}
{% if sip_files_dir is not none %}sip-files-dir = "{{ sip_files_dir }}"
{% endif %}
build-dir = "{{ build_folder }}"
target-dir = "{{ package_folder }}"
{{ py_include_dir }}
{{ py_major_version }}
{{ py_minor_version }}""")
def context(self):
python_version = self._conanfile.options.get_safe("py_version")
py_include_dir = self._conanfile.options.get_safe("py_include")
py_major_version = None
py_minor_version = None
if python_version is None:
try:
python_version = self._conanfile.dependencies["cpython"].ref.version
except:
self._conanfile.output.warn(
"No minimum required Python version specified, either add the options: 'py_version' of add cpython as a Conan dependency!")
if python_version is not None:
py_version = Version(python_version)
if py_include_dir is None:
try:
header_path = "" if self._conanfile.settings.os == "Windows" else f"python{py_version.major}.{py_version.minor}"
py_include_dir = Path(self._conanfile.deps_cpp_info['cpython'].rootpath,
self._conanfile.deps_cpp_info['cpython'].components["python"].includedirs[0],
header_path).as_posix()
py_include_dir = f"py-include-dir = \"{py_include_dir}\""
except:
self._conanfile.output.warn(
"No include directory set for Python.h, either add the options: 'py_include' of add cpython as a Conan dependency!")
else:
py_include_dir = f"py-include-dir = \"{Path(py_include_dir).as_posix()}\""
py_major_version = f"py-major-version = {py_version.major}"
py_minor_version = f"py-minor-version = {py_version.minor}"
if self._conanfile.package_folder:
package_folder = Path(self._conanfile.package_folder, "site-packages").as_posix()
else:
package_folder = Path(self._conanfile.build_folder, "site-packages").as_posix()
sip_files_dir = Path(self._conanfile.source_folder, self._conanfile.name).as_posix()
return {
"sip_files_dir": sip_files_dir,
"compile": False,
"build_folder": Path(self._conanfile.build_folder).as_posix(),
"package_folder": package_folder,
"py_include_dir": py_include_dir,
"py_major_version": py_major_version,
"py_minor_version": py_minor_version
}
class ToolSipBindingsExtraSourcesBlock(Block):
template = textwrap.dedent("""
headers = {{ headers }}
sources = {{ sources }}
""")
def context(self):
return {
"headers": [],
"sources": []
}
class ToolSipBindingBlockCompile(Block):
template = textwrap.dedent("""
extra-compile-args = {{ compileargs }}
extra-link-args = {{ linkargs }}
""")
def context(self):
return {
"compileargs": list(filter(lambda item: item is not None and item != '', self._toolchain.cxxflags)),
"linkargs": list(filter(lambda item: item is not None and item != '', self._toolchain.ldflags)),
}
class ToolSipBindingsBlock(Block):
template = textwrap.dedent("""
[tool.sip.bindings.{{ name }}]
exceptions = true
release-gil = true
libraries = {{ libs }}
library-dirs = {{ libdirs }}
include-dirs = {{ includedirs }}
pep484-pyi = true
static = {{ build_static | lower }}
debug = {{ build_debug | lower }}
""")
def context(self):
settings = self._conanfile.settings
deps_cpp_info = self._conanfile.deps_cpp_info
build_type = settings.get_safe("build_type", "Release")
shared = settings.get_safe("shared", True)
libs = deps_cpp_info.libs
libdirs = [Path(d).as_posix() for d in deps_cpp_info.libdirs]
includedirs = [Path(d).as_posix() for d in deps_cpp_info.includedirs]
if self._conanfile.cpp.source.includedirs:
includedirs.extend(self._conanfile.cpp.source.includedirs)
return {
"name": self._conanfile.name,
"libs": libs,
"libdirs": libdirs,
"includedirs": includedirs,
"build_static": str(not shared),
"build_debug": str(build_type == "Debug")
}
class PyProjectToolchain(AutotoolsToolchain):
_pyproject_filename = Path("pyproject.toml")
_pyproject_template = textwrap.dedent("""
# Conan automatically generated pyproject.toml file
# DO NOT EDIT MANUALLY, it will be overwritten
{% for conan_block in conan_blocks %}{{ conan_block }}
{% endfor %}
""")
def __init__(self, conanfile: ConanFile, namespace = None):
super().__init__(conanfile, namespace)
check_using_build_profile(self._conanfile)
blocks = [
("build_system", BuildSystemBlock),
("tool_sip_metadata", ToolSipMetadataBlock),
("tool_sip_project", ToolSipProjectBlock)]
if "PyQt-builder" in str(self._conanfile.options.get_safe("py_build_requires", "")):
blocks.append(("pyqt_builder", ToolSipProjectPyQtBuilder))
blocks.extend([("tool_sip_bindings", ToolSipBindingsBlock),
("extra_sources", ToolSipBindingsExtraSourcesBlock),
("compiling", ToolSipBindingBlockCompile),
])
self.blocks = ToolchainBlocks(self._conanfile, self, blocks)
@property
def _context(self):
blocks = self.blocks.process_blocks()
print(blocks)
return {"conan_blocks": blocks}
@property
def content(self):
content = Template(self._pyproject_template, trim_blocks = True, lstrip_blocks = True).render(**self._context)
return content
def generate(self, env = None, scope = "build"):
env = env or self.environment()
env = env.vars(self._conanfile, scope = scope)
env.save_script("conanpyprojecttoolchain")
VCVars(self._conanfile).generate(scope = scope)
py_project_filename = Path(self._conanfile.source_folder, self._pyproject_filename)
save(self._conanfile, py_project_filename, self.content)
class PyProjectToolchainPkg(ConanFile):
name = "pyprojecttoolchain"
version = "0.1.6"
default_user = "ultimaker"
default_channel = "stable"