@@ -57,6 +57,41 @@ def __str__(self):
5757fftw3_lib = os .path .join (BUILD_DIR , "_deps/fftw3-build/libfftw3.a" )
5858fftw3f_lib = os .path .join (BUILD_DIR , "_deps/fftw3f-build/libfftw3f.a" )
5959
60+
61+ def find_libomp_prefix ():
62+ """Return the macOS libomp prefix without relying on shell flags."""
63+ if platform .system () != "Darwin" :
64+ return None
65+ configured = os .environ .get ("LIBOMP_PREFIX" )
66+ if configured :
67+ prefix = configured
68+ else :
69+ brew = shutil .which ("brew" )
70+ if brew is None :
71+ raise RuntimeError (
72+ "Building senpy on macOS requires libomp. Install it with "
73+ "'brew install libomp', or set LIBOMP_PREFIX to its install prefix."
74+ )
75+ try :
76+ prefix = subprocess .check_output (
77+ [brew , "--prefix" , "libomp" ], text = True
78+ ).strip ()
79+ except subprocess .CalledProcessError as exc :
80+ raise RuntimeError (
81+ "Homebrew could not locate libomp. Install it with "
82+ "'brew install libomp', or set LIBOMP_PREFIX to its install prefix."
83+ ) from exc
84+ library = os .path .join (prefix , "lib" , "libomp.dylib" )
85+ if not os .path .exists (library ):
86+ raise RuntimeError (
87+ f"libomp was not found at { library } . Install it with "
88+ "'brew install libomp', or correct LIBOMP_PREFIX."
89+ )
90+ return prefix
91+
92+
93+ LIBOMP_PREFIX = find_libomp_prefix ()
94+
6095def required_artifacts ():
6196 return [finufft_include_dir , finufft_lib_so , fftw3_lib , fftw3f_lib ]
6297
@@ -111,20 +146,29 @@ def _copy_runtime_libraries(self):
111146
112147include_dirs .append (finufft_include_dir )
113148
149+ extra_link_args = [
150+ f'-L{ finufft_lib_dir } ' ,
151+ '-lfinufft' ,
152+ fftw3_lib , fftw3f_lib ,
153+ '-Wl,-rpath,@loader_path' if platform .system () == 'Darwin' else '-Wl,-rpath,$ORIGIN' ,
154+ ]
155+ if LIBOMP_PREFIX is not None :
156+ extra_link_args .extend ([
157+ f'-L{ os .path .join (LIBOMP_PREFIX , "lib" )} ' ,
158+ f'-Wl,-rpath,{ os .path .join (LIBOMP_PREFIX , "lib" )} ' ,
159+ '-lomp' ,
160+ ])
161+ else :
162+ extra_link_args .append ('-lgomp' )
163+
114164ext_modules = [
115165 Extension (
116166 'senpy._core' , # Full module path - creates senpy/_core.so
117167 sources = sources ,
118168 include_dirs = include_dirs ,
119169 language = 'c++' ,
120170 extra_compile_args = ['-std=c++17' , '-O3' , '-march=x86-64-v3' if os .environ .get ('CI' ) == 'true' else '-march=native' , '-DPYTHON' ],
121- extra_link_args = [
122- f'-L{ finufft_lib_dir } ' ,
123- '-lfinufft' ,
124- fftw3_lib , fftw3f_lib ,
125- '-Wl,-rpath,@loader_path' if platform .system () == 'Darwin' else '-Wl,-rpath,$ORIGIN' ,
126- '-lomp' if platform .system () == 'Darwin' else '-lgomp' ,
127- ],
171+ extra_link_args = extra_link_args ,
128172 ),
129173]
130174
0 commit comments