-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_extension.py
More file actions
239 lines (207 loc) · 8.66 KB
/
build_extension.py
File metadata and controls
239 lines (207 loc) · 8.66 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
# SPDX-FileCopyrightText: 2023 Gabriele Pongelli
#
# SPDX-License-Identifier: MIT
"""Module to build C/C++ extension with Poetry."""
import configparser
import os
import platform
from glob import glob
from os.path import join, relpath, splitext
from pathlib import Path
from setuptools.command.develop import develop
from setuptools.extension import Extension
# arch information comes from
# gcc -v
# gcc -dumpmachine
# info
# https://stackoverflow.com/questions/6928110/how-may-i-override-the-compiler-gcc-flags-that-setup-py-uses-by-default
print(f"SYSTEM: {platform.system()}")
print(f"MACHINE: {platform.machine()}")
print(f"UNAME: {platform.uname()}")
print(f"RELEASE: {platform.release()}")
print(f"VERSION: {platform.version()}")
print(f"PROCESSOR: {platform.processor()}")
_platform = platform.system().lower()
_is_windows = 'windows' in _platform # pylint: disable=C0103
_arch = 'x86-64' # pylint: disable=C0103
_c_compiler = "" # pylint: disable=C0103
if _is_windows:
_lflags = f"-Wl,--subsystem,windows,--out-implib,libcode128_{_arch}.a"
_c_compiler = "mingw32" # pylint: disable=C0103
else:
# if "linux" in _platform:
_c_compiler = "unix" # pylint: disable=C0103
_lflags = "-Wl,-soname,libcode128.so" # pylint: disable=C0103
if 'arm' in platform.machine():
_arch = "armv7" # pylint: disable=C0103
elif 'aarch64' in platform.machine():
_arch = "armv8.8-a" # pylint: disable=C0103
_march = f"-march={_arch}" # pylint: disable=C0103
if 'darwin' in _platform:
_march = "" # pylint: disable=C0103 # clang compiler does not support -march=x86_64
# list of tuples (python_module, dict of C/C++ library build data ),
# python_module is the extension that uses those C/C++ library info.
# the dict can be extended with all the kwargs needed by Extension to correctly build C/C++ sourcecode
_LIBS = [
(
'pycode128',
{
"libs": ['libs/code128'],
# Building library part
"lib_cflags": ["-O2", "-std=c99", "-Wall", "-fpic", "-Wextra", _march, "-DADD_EXPORTS"],
"lib_lflags": ["-shared", _lflags],
# Python extension part
# always define PY_SSIZE_T_CLEAN , see https://docs.python.org/3/extending/extending.html
"extension_define_macros": [("PY_SSIZE_T_CLEAN", None), ("ADD_EXPORTS", None)],
"extension_language": "c99",
"extension_extra_compile_args": [],
"extension_extra_link_args": [],
"extension_library": ['code128'],
},
)
]
class CustomDevelop(develop):
"""Custom install procedure.
When declaring a ``build_extension.py`` poetry switches to setuptools during
installation, i.e., it generates a temporary ``setup.py`` and then calls
``setup.py develop`` on it when you call ``poetry install``.
Consequentially, we can hook into the develop command and customize the
build to compile our source :) Note that this is only needed for the
``develop`` command, because the ``build`` command (``poetry build``)
already includes ``build_clib``.
This class then is the hook that will compile the source when we call
``poetry install``.
"""
def run(self) -> None: # type: ignore
# build archives (.lib) these are declared in the `libraries` kwarg of
# setup(). Extensions may depend on these, so we have to build the libs
# them first.
self.run_command("build_clib")
super().run()
# custom_extension = Extension(
# "pycode128.pycode128",
# sources=["pycode128/pycode128.c"],
# # define_macros=[("PY_SSIZE_T_CLEAN",)],
# # we need to declare the extenal dependencies
# include_dirs=["libs/code128"],
# libraries=["code128"], # see below
# )
def _add_compiler_to_setup_cfg(_compiler):
conf = configparser.ConfigParser(inline_comment_prefixes="#")
with open(Path(os.path.realpath(os.path.dirname(__file__))) / 'setup.cfg', mode='r', encoding="cp1252") as _fp:
try:
conf.read_file(_fp)
except configparser.Error:
pass
try:
conf.set('build_clib', 'compiler', _compiler)
except configparser.NoSectionError:
conf.add_section('build_clib')
conf.set('build_clib', 'compiler', _compiler)
try:
conf.set('build_ext', 'compiler', _compiler)
except configparser.NoSectionError:
conf.add_section('build_ext')
conf.set('build_ext', 'compiler', _compiler)
with open('setup.cfg', mode='w', encoding="cp1252") as _fp:
conf.write(_fp, space_around_delimiters=True)
# setuptools.extension requires unix separator
def _unix_form(file_path: str) -> str:
return file_path.replace('\\', '/')
def build(setup_kwargs):
"""
This is a callback for poetry used to hook in our extensions.
"""
_add_compiler_to_setup_cfg(_c_compiler)
setup_kwargs.update(
{
# declare archives (.lib) to build. These will be linked to
# statically by extensions, cython, ...
"libraries": [
(
"code128",
{
"sources": [
_unix_form(path)
for _, _source_libs in _LIBS
for _source_folder in _source_libs['libs']
for root, _, _ in os.walk(os.sep.join([_source_folder]))
for path in glob(join(root, '*.c'))
if 'code128png' not in path
],
# flags and dependencies of this library
# "include_dirs": ...
# "libraries": ...
"cflags": [
itm
for pylib, _source_libs in _LIBS
for itm in _source_libs['lib_cflags']
if pylib == 'pycode128'
],
"lflags": [
itm
for pylib, _source_libs in _LIBS
for itm in _source_libs['lib_lflags']
if pylib == 'pycode128'
],
},
),
],
"ext_modules": [
Extension(
splitext(relpath(_unix_form(path), start='.').replace(os.sep, '.'))[0],
sources=[_unix_form(path)],
define_macros=_source_libs['extension_define_macros'],
include_dirs=_source_libs['libs'],
libraries=_source_libs['extension_library'],
language=_source_libs["extension_language"],
extra_compile_args=_source_libs["extension_extra_compile_args"],
extra_link_args=_source_libs["extension_extra_link_args"],
)
for _py_lib, _source_libs in _LIBS
for root, _, _ in os.walk(os.sep.join([_py_lib]))
for path in glob(join(root, '*.c'))
],
# hook into the build process to build our external sources before
# we build and install the package.
"cmdclass": {"develop": CustomDevelop},
}
)
#
# if __name__ == '__main__':
# # _LIBS = [('py', 'libs/code128')]
#
# for _py_lib, _source_libs in _LIBS:
# for _source_folder in _source_libs['libs']:
# for root, _, _ in os.walk(os.sep.join([_source_folder])):
# print(root)
# for path in glob(join(root, '*.c')):
# print(path)
# print(dirname(path))
#
# _a = [_unix_form(path)
# for _py_lib, _source_lib in _LIBS
# for source_fold in _source_lib['libs']
# for root, _, _ in os.walk(os.sep.join([source_fold]))
# for path in glob(join(root, '*.c'))
# if 'code128png' not in path]
#
# for _py_lib, _ in _LIBS:
# for root, _, _ in os.walk(os.sep.join([_py_lib])):
# print(root)
# for path in glob(join(root, '*.c')):
# print(path)
# print(dirname(path))
# print(splitext(relpath(path, start='.').replace(os.sep, '.'))[0])
#
# ext = [Extension(splitext(relpath(_unix_form(path), start='.').replace(os.sep, '.'))[0],
# sources=[_unix_form(path)],
# define_macros=_source_libs['define_macros'],
# include_dirs=_source_libs['libs'])
# for _py_lib, _source_libs in _LIBS
# for root, _, _ in os.walk(os.sep.join([_py_lib]))
# for path in glob(join(root, '*.c'))
# ]
# print(ext)
#
#