FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
xtensor-python/src/xtensor_python/build_helpers.py at master · maxstidbits/xtensor-python · GitHub
maxstidbits
xtensor-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
xtensor-python
/
src
/
xtensor_python
/
build_helpers.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
401 lines (325 loc) · 11.6 KB
Breadcrumbs
xtensor-python
/
src
/
xtensor_python
/
build_helpers.py
Copy path
File metadata and controls
401 lines (325 loc) · 11.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""Build helpers for xtensor-python.
This module provides utilities for building C++ extensions that use xtensor-python,
including functions to get include directories, compiler flags, and pre-configured
extension classes.
"""
import
os
import
platform
import
sys
import
warnings
from
pathlib
import
Path
from
typing
import
List
,
Dict
,
Optional
,
Union
try
:
import
numpy
as
np
except
ImportError
:
np
=
None
try
:
from
pybind11
import
get_cmake_dir
as
pybind11_get_cmake_dir
from
pybind11
.
setup_helpers
import
Pybind11Extension
,
build_ext
from
pybind11
.
setup_helpers
import
ParallelCompile
except
ImportError
:
# Fallback for older pybind11 versions
try
:
from
setuptools
import
Extension
as
Pybind11Extension
from
setuptools
.
command
.
build_ext
import
build_ext
ParallelCompile
=
None
pybind11_get_cmake_dir
=
None
except
ImportError
:
Pybind11Extension
=
None
build_ext
=
None
ParallelCompile
=
None
pybind11_get_cmake_dir
=
None
def
get_include_dirs
()
->
List
[
str
]:
"""Get all required include directories for xtensor-python.
Returns:
List of include directory paths
Raises:
ImportError: If required dependencies are not available
"""
include_dirs
=
[]
# Add xtensor-python include directory
package_dir
=
Path
(
__file__
).
parent
xtensor_python_include
=
str
(
package_dir
/
"include"
)
include_dirs
.
append
(
xtensor_python_include
)
# Add NumPy include directory
if
np
is
not
None
:
numpy_include
=
np
.
get_include
()
include_dirs
.
append
(
numpy_include
)
else
:
raise
ImportError
(
"NumPy is required for xtensor-python. "
"Please install NumPy: pip install numpy"
)
# Add pybind11 include directory
try
:
import
pybind11
pybind11_include
=
pybind11
.
get_include
()
include_dirs
.
append
(
pybind11_include
)
except
ImportError
:
raise
ImportError
(
"pybind11 is required for xtensor-python. "
"Please install pybind11: pip install pybind11"
)
return
include_dirs
def
get_compiler_flags
()
->
List
[
str
]:
"""Get platform-specific compiler flags for C++17 and xtensor-python.
Returns:
List of compiler flags
"""
flags
=
[]
# C++17 standard
if
platform
.
system
()
==
"Windows"
:
# MSVC flags
flags
.
extend
([
"/std:c++17"
,
"/EHsc"
])
# Disable some MSVC warnings that are common with xtensor
flags
.
extend
([
"/wd4244"
,
# conversion warnings
"/wd4267"
,
# size_t conversion warnings
"/wd4996"
,
# deprecated function warnings
])
else
:
# GCC/Clang flags
flags
.
extend
([
"-std=c++17"
])
# macOS specific flags
if
platform
.
system
()
==
"Darwin"
:
# Use libc++ on macOS
flags
.
extend
([
"-stdlib=libc++"
])
# Handle macOS deployment target
macos_target
=
os
.
environ
.
get
(
"MACOSX_DEPLOYMENT_TARGET"
)
if
macos_target
:
flags
.
append
(
f"-mmacosx-version-min=
{
macos_target
}
"
)
# Optimization and warning flags
flags
.
extend
([
"-O3"
,
"-ffast-math"
,
"-march=native"
,
# Optimize for current CPU
])
# Disable some warnings that are common with xtensor
flags
.
extend
([
"-Wno-unused-variable"
,
"-Wno-unused-parameter"
,
])
return
flags
def
get_numpy_flags
()
->
List
[
str
]:
"""Get NumPy C API specific compiler flags.
Returns:
List of NumPy-specific compiler flags
"""
flags
=
[]
# NumPy C API version
flags
.
append
(
"-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION"
)
# Import array macro handling
flags
.
append
(
"-DFORCE_IMPORT_ARRAY"
)
return
flags
def
get_link_flags
()
->
List
[
str
]:
"""Get platform-specific linker flags.
Returns:
List of linker flags
"""
flags
=
[]
if
platform
.
system
()
==
"Darwin"
:
# macOS specific linker flags
flags
.
extend
([
"-stdlib=libc++"
])
elif
platform
.
system
()
==
"Linux"
:
# Linux specific flags if needed
pass
elif
platform
.
system
()
==
"Windows"
:
# Windows specific flags if needed
pass
return
flags
def
get_define_macros
()
->
List
[
tuple
]:
"""Get preprocessor macro definitions.
Returns:
List of (name, value) tuples for preprocessor macros
"""
macros
=
[]
# Version information
try
:
from
.
_version
import
get_version_info
version_info
=
get_version_info
()
macros
.
extend
([
(
"XTENSOR_PYTHON_VERSION_MAJOR"
,
str
(
version_info
.
major
)),
(
"XTENSOR_PYTHON_VERSION_MINOR"
,
str
(
version_info
.
minor
)),
(
"XTENSOR_PYTHON_VERSION_PATCH"
,
str
(
version_info
.
patch
)),
])
except
ImportError
:
# Fallback if version module is not available
pass
# Platform-specific macros
if
platform
.
system
()
==
"Windows"
:
macros
.
append
((
"WIN32_LEAN_AND_MEAN"
,
None
))
return
macros
class
BuildExtension
(
build_ext
):
"""Enhanced build_ext class with xtensor-python optimizations.
This class extends the standard build_ext to provide better defaults
and optimizations for xtensor-python extensions.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
# Enable parallel compilation if available
if
ParallelCompile
is
not
None
:
ParallelCompile
(
"NPY_NUM_BUILD_JOBS"
,
needs_recompile
=
lambda
:
False
).
install
()
def
build_extensions
(
self
):
"""Build extensions with xtensor-python optimizations."""
# Set compiler-specific options
compiler_type
=
self
.
compiler
.
compiler_type
if
compiler_type
==
"msvc"
:
# MSVC specific optimizations
for
ext
in
self
.
extensions
:
ext
.
extra_compile_args
.
extend
([
"/O2"
,
# Optimize for speed
"/GL"
,
# Whole program optimization
])
ext
.
extra_link_args
.
extend
([
"/LTCG"
,
# Link-time code generation
])
elif
compiler_type
in
(
"unix"
,
"cygwin"
,
"mingw32"
):
# GCC/Clang optimizations
for
ext
in
self
.
extensions
:
# Add optimization flags if not already present
if
"-O3"
not
in
ext
.
extra_compile_args
:
ext
.
extra_compile_args
.
append
(
"-O3"
)
# Enable link-time optimization
if
"-flto"
not
in
ext
.
extra_compile_args
:
ext
.
extra_compile_args
.
append
(
"-flto"
)
ext
.
extra_link_args
.
append
(
"-flto"
)
super
().
build_extensions
()
def
create_extension
(
name
:
str
,
sources
:
List
[
str
],
include_dirs
:
Optional
[
List
[
str
]]
=
None
,
extra_compile_args
:
Optional
[
List
[
str
]]
=
None
,
extra_link_args
:
Optional
[
List
[
str
]]
=
None
,
define_macros
:
Optional
[
List
[
tuple
]]
=
None
,
language
:
str
=
"c++"
,
**
kwargs
)
->
Union
[
Pybind11Extension
,
object
]:
"""Create a pre-configured extension for xtensor-python.
Args:
name: Extension module name
sources: List of source file paths
include_dirs: Additional include directories
extra_compile_args: Additional compiler flags
extra_link_args: Additional linker flags
define_macros: Additional preprocessor macros
language: Programming language (default: 'c++')
**kwargs: Additional arguments passed to Extension constructor
Returns:
Configured Extension object
Raises:
ImportError: If required dependencies are not available
"""
if
Pybind11Extension
is
None
:
raise
ImportError
(
"Could not import extension class. "
"Please install pybind11: pip install pybind11"
)
# Combine include directories
all_include_dirs
=
get_include_dirs
()
if
include_dirs
:
all_include_dirs
.
extend
(
include_dirs
)
# Combine compiler flags
all_compile_args
=
get_compiler_flags
()
all_compile_args
.
extend
(
get_numpy_flags
())
if
extra_compile_args
:
all_compile_args
.
extend
(
extra_compile_args
)
# Combine linker flags
all_link_args
=
get_link_flags
()
if
extra_link_args
:
all_link_args
.
extend
(
extra_link_args
)
# Combine preprocessor macros
all_define_macros
=
get_define_macros
()
if
define_macros
:
all_define_macros
.
extend
(
define_macros
)
# Create extension
extension
=
Pybind11Extension
(
name
=
name
,
sources
=
sources
,
include_dirs
=
all_include_dirs
,
extra_compile_args
=
all_compile_args
,
extra_link_args
=
all_link_args
,
define_macros
=
all_define_macros
,
language
=
language
,
**
kwargs
)
return
extension
def
get_build_requirements
()
->
List
[
str
]:
"""Get the list of build requirements for xtensor-python.
Returns:
List of package requirements
"""
requirements
=
[
"numpy>=2.0"
,
"pybind11>=2.6.1,<4"
,
]
return
requirements
def
check_dependencies
()
->
Dict
[
str
,
bool
]:
"""Check if all required dependencies are available.
Returns:
Dictionary mapping dependency names to availability status
"""
dependencies
=
{}
# Check NumPy
try
:
import
numpy
dependencies
[
"numpy"
]
=
True
dependencies
[
"numpy_version"
]
=
numpy
.
__version__
except
ImportError
:
dependencies
[
"numpy"
]
=
False
# Check pybind11
try
:
import
pybind11
dependencies
[
"pybind11"
]
=
True
dependencies
[
"pybind11_version"
]
=
pybind11
.
__version__
except
ImportError
:
dependencies
[
"pybind11"
]
=
False
# Check compiler
try
:
from
distutils
import
ccompiler
compiler
=
ccompiler
.
new_compiler
()
dependencies
[
"compiler"
]
=
True
dependencies
[
"compiler_type"
]
=
compiler
.
compiler_type
except
Exception
:
dependencies
[
"compiler"
]
=
False
return
dependencies
def
print_build_info
():
"""Print build environment information for debugging."""
print
(
"xtensor-python Build Information"
)
print
(
"="
*
40
)
# Version information
try
:
from
.
_version
import
__version__
,
__version_info__
print
(
f"xtensor-python version:
{
__version__
}
"
)
print
(
f"Version info:
{
__version_info__
}
"
)
except
ImportError
:
print
(
"xtensor-python version: Unknown"
)
# Platform information
print
(
f"Platform:
{
platform
.
system
()
}
{
platform
.
release
()
}
"
)
print
(
f"Architecture:
{
platform
.
machine
()
}
"
)
print
(
f"Python:
{
sys
.
version
}
"
)
# Dependencies
deps
=
check_dependencies
()
print
(
"
\n
Dependencies:"
)
for
name
,
available
in
deps
.
items
():
if
name
.
endswith
(
"_version"
)
or
name
.
endswith
(
"_type"
):
continue
status
=
"✓"
if
available
else
"✗"
print
(
f"
{
status
}
{
name
}
"
)
if
available
and
f"
{
name
}
_version"
in
deps
:
print
(
f" Version:
{
deps
[
f'
{
name
}
_version'
]
}
"
)
# Include directories
try
:
includes
=
get_include_dirs
()
print
(
f"
\n
Include directories:"
)
for
inc
in
includes
:
print
(
f"
{
inc
}
"
)
except
Exception
as
e
:
print
(
f"
\n
Include directories: Error -
{
e
}
"
)
# Compiler flags
try
:
flags
=
get_compiler_flags
()
print
(
f"
\n
Compiler flags:"
)
for
flag
in
flags
:
print
(
f"
{
flag
}
"
)
except
Exception
as
e
:
print
(
f"
\n
Compiler flags: Error -
{
e
}
"
)
Back
|
FazBrowse Home
|
New Git URL