Skip to content

Commit b5b3e0e

Browse files
make setup work on homebrew python
1 parent 657a47a commit b5b3e0e

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

setup.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ def _build_dependencies_impl(self, build_dir, install_path, osx_arch=None):
315315
run_cmd(build_cmd)
316316

317317
def _build_dependencies(self):
318+
318319
build_dir = os.path.join(self.build_temp, 'deps')
319320
install_path = os.path.join(self.build_temp, 'deps', 'install')
320321

@@ -375,6 +376,32 @@ def _build_dependencies(self):
375376

376377
self.library_dirs.insert(0, os.path.join(install_path, lib_dir))
377378

379+
def build_extension(self, ext):
380+
381+
# Warning: very hacky. feel free to replace with something cleaner
382+
# Problem: if you install python through homebrew, python config ldflags
383+
# will point to homebrew lib folder.
384+
# setuptools puts python ldflags before any of our lib paths, so if there is openssl or
385+
# another libcrypto in homebrew libs, it will get picked up before aws-lc we are building against.
386+
# And then we have fun failures due to lib mismatch.
387+
# I could not find a cleaner way, so lets just hook into linker command and make sure
388+
# our libs appear before other libs.
389+
if sys.platform == 'darwin' and using_libcrypto() and not using_system_libs() and not using_system_libcrypto():
390+
391+
orig_linker_so = self.compiler.linker_so[:]
392+
393+
for i, item in enumerate(self.compiler.linker_so):
394+
if item.startswith('-L'):
395+
self.compiler.linker_so[i:i] = [f"-L{item}" for item in self.library_dirs] + ['-Wl,-search_paths_first']
396+
break
397+
398+
try:
399+
super().build_extension(ext)
400+
finally:
401+
self.compiler.linker_so = orig_linker_so
402+
else:
403+
super().build_extension(ext)
404+
378405
def run(self):
379406
if using_system_libs():
380407
print("Skip building dependencies")
@@ -422,7 +449,6 @@ def awscrt_ext():
422449

423450
elif sys.platform == 'darwin':
424451
extra_link_args += ['-framework', 'Security']
425-
426452
else: # unix
427453
if forcing_static_libs():
428454
# linker will prefer shared libraries over static if it can find both.
@@ -502,6 +528,8 @@ def awscrt_ext():
502528
define_macros.append(('Py_LIMITED_API', '0x030B0000'))
503529
py_limited_api = True
504530

531+
print
532+
505533
return setuptools.Extension(
506534
'_awscrt',
507535
language='c',
@@ -514,7 +542,6 @@ def awscrt_ext():
514542
py_limited_api=py_limited_api,
515543
)
516544

517-
518545
def _load_version():
519546
init_path = os.path.join(PROJECT_DIR, 'awscrt', '__init__.py')
520547
with open(init_path) as fp:

0 commit comments

Comments
 (0)