Skip to content

Commit

Permalink
Added macOS platform support for compilation module, fix sympy#24058
Browse files Browse the repository at this point in the history
  • Loading branch information
vxst committed Sep 15, 2022
1 parent ec74eb1 commit 5900766
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions sympy/utilities/_compilation/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,39 @@ def link_py_so(obj_files, so_file=None, cwd=None, libraries=None,
include_dirs = kwargs.pop('include_dirs', [])
library_dirs = kwargs.pop('library_dirs', [])

# from distutils/command/build_ext.py:
# Add Python include and library directories
# PY_LDFLAGS does not available on all python implementations
# e.g. when with pypy, so it's LDFLAGS we need to use
if sys.platform == "win32":
warnings.warn("Windows not yet supported.")
elif sys.platform == 'darwin':
# Don't use the default code below
pass
cfgDict = get_config_vars()
kwargs['linkline'] = kwargs.get('linkline', []) + [cfgDict['LDFLAGS']]
library_dirs += [cfgDict['LIBDIR']]

# In macOS, linker needs to compile frameworks
# e.g. "-framework CoreFoundation"
is_framework = False
for opt in cfgDict['LIBS'].split():
if is_framework:
kwargs['linkline'] = kwargs.get('linkline', []) + ['-framework', opt]
is_framework = False
elif opt.startswith('-l'):
libraries.append(opt[2:])
elif opt.startswith('-framework'):
is_framework = True
# The python library is not included in LIBS
libfile = cfgDict['LIBRARY']
libname = ".".join(libfile.split('.')[:-1])[3:]
libraries.append(libname)

elif sys.platform[:3] == 'aix':
# Don't use the default code below
pass
else:
if get_config_var('Py_ENABLE_SHARED'):
cfgDict = get_config_vars()
kwargs['linkline'] = kwargs.get('linkline', []) + [cfgDict['PY_LDFLAGS']] # PY_LDFLAGS or just LDFLAGS?
kwargs['linkline'] = kwargs.get('linkline', []) + [cfgDict['LDFLAGS']]
library_dirs += [cfgDict['LIBDIR']]
for opt in cfgDict['BLDLIBRARY'].split():
if opt.startswith('-l'):
Expand Down Expand Up @@ -291,7 +311,11 @@ def simple_cythonize(src, destdir=None, cwd=None, **cy_kwargs):
cy_result = cy_compile([src], cy_options)
if cy_result.num_errors > 0:
raise ValueError("Cython compilation failed.")
if os.path.abspath(os.path.dirname(src)) != os.path.abspath(destdir):

# Move generated C file to destination
# In macOS, the generated C file is in the same directory as the source
# but the /var is a symlink to /private/var, so we need to use realpath
if os.path.realpath(os.path.dirname(src)) != os.path.realpath(destdir):
if os.path.exists(dstfile):
os.unlink(dstfile)
shutil.move(os.path.join(os.path.dirname(src), c_name), destdir)
Expand Down

0 comments on commit 5900766

Please sign in to comment.