6262"""
6363
6464
65- def _compile_array_wrappers_if_needed (tempdir , include_dirs , debug = False ):
66- """
67- Compile ArrayWrappers module if it's needed (i.e., if numpy is being used).
68- This makes ArrayWrappers available to the module being compiled.
69- """
70- import os
71- import os .path
72- import shutil
73- import subprocess
74- import sys
75-
76- # Check if ArrayWrappers source files exist
77- autowrap_dir = os .path .dirname (os .path .abspath (__file__ ))
78- array_wrappers_dir = os .path .join (autowrap_dir , "data_files" , "autowrap" )
79- array_wrappers_pyx = os .path .join (array_wrappers_dir , "ArrayWrappers.pyx" )
80- array_wrappers_pxd = os .path .join (array_wrappers_dir , "ArrayWrappers.pxd" )
81-
82- if not os .path .exists (array_wrappers_pyx ):
83- # ArrayWrappers not available, skip
84- return
85-
86- if debug :
87- print ("Compiling ArrayWrappers module..." )
88-
89- # Copy only ArrayWrappers.pyx to tempdir
90- # Don't copy .pxd - Cython will auto-generate it from .pyx during compilation
91- # The .pxd is only needed by OTHER modules that import ArrayWrappers
92- shutil .copy (array_wrappers_pyx , tempdir )
93-
94- # Create a simple setup.py for ArrayWrappers
95- compile_args = []
96- link_args = []
97-
98- if sys .platform == "darwin" :
99- compile_args += ["-stdlib=libc++" , "-std=c++17" ]
100- link_args += ["-stdlib=libc++" ]
101-
102- if sys .platform == "linux" or sys .platform == "linux2" :
103- compile_args += ["-std=c++17" ]
104-
105- if sys .platform != "win32" :
106- compile_args += ["-Wno-unused-but-set-variable" ]
107-
108- # Get numpy include directory if available
109- try :
110- import numpy
111- numpy_include = numpy .get_include ()
112- except ImportError :
113- numpy_include = None
114-
115- # Filter include_dirs to exclude the autowrap data_files directory
116- # to prevent Cython from finding ArrayWrappers.pxd during its own compilation
117- filtered_include_dirs = [d for d in include_dirs if array_wrappers_dir not in os .path .abspath (d )]
118- if numpy_include and numpy_include not in filtered_include_dirs :
119- filtered_include_dirs .append (numpy_include )
120-
121- include_dirs_abs = [os .path .abspath (d ) for d in filtered_include_dirs ]
122-
123- setup_code = """
124- from distutils.core import setup, Extension
125- from Cython.Distutils import build_ext
126-
127- ext = Extension("ArrayWrappers",
128- sources=["ArrayWrappers.pyx"],
129- language="c++",
130- include_dirs=%r,
131- extra_compile_args=%r,
132- extra_link_args=%r)
133-
134- setup(cmdclass={'build_ext': build_ext},
135- name="ArrayWrappers",
136- ext_modules=[ext])
137- """ % (include_dirs_abs , compile_args , link_args )
138-
139- # Write and build ArrayWrappers
140- setup_file = os .path .join (tempdir , "setup_arraywrappers.py" )
141- with open (setup_file , "w" ) as fp :
142- fp .write (setup_code )
143-
144- # Build ArrayWrappers in the tempdir
145- result = subprocess .Popen (
146- "%s %s build_ext --inplace" % (sys .executable , setup_file ),
147- shell = True ,
148- cwd = tempdir
149- ).wait ()
150-
151- if result != 0 :
152- print ("Warning: Failed to compile ArrayWrappers module" )
153- elif debug :
154- print ("ArrayWrappers compiled successfully" )
155-
156- # After building successfully, copy the .pxd file so other modules can cimport from it
157- # We do this AFTER compilation to avoid conflicts during ArrayWrappers own compilation
158- if result == 0 and os .path .exists (array_wrappers_pxd ):
159- shutil .copy (array_wrappers_pxd , tempdir )
160- if debug :
161- print ("Copied ArrayWrappers.pxd to tempdir" )
162-
163-
16465def compile_and_import (name , source_files , include_dirs = None , ** kws ):
16566 if include_dirs is None :
16667 include_dirs = []
@@ -178,9 +79,6 @@ def compile_and_import(name, source_files, include_dirs=None, **kws):
17879 print ("tempdir=" , tempdir )
17980 print ("\n " )
18081
181- # Note: ArrayWrappers classes are now inlined into generated modules,
182- # so we don't need to compile them separately anymore
183-
18482 for source_file in source_files :
18583 if source_file [- 4 :] != ".pyx" and source_file [- 4 :] != ".cpp" :
18684 raise NameError ("Expected pyx and/or cpp files as source files for compilation." )
0 commit comments