Skip to content

Commit ce31702

Browse files
committed
WL11472: Allow OpenSSL linkage in the C extension
1 parent 66a1f16 commit ce31702

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

lib/cpy_distutils.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
from distutils.errors import DistutilsExecError
3131
from distutils.util import get_platform
3232
from distutils.version import LooseVersion
33-
from distutils.dir_util import copy_tree
33+
from distutils.dir_util import copy_tree, mkpath
34+
from distutils.sysconfig import get_python_lib
3435
from distutils import log
3536
from glob import glob
3637
import os
@@ -167,6 +168,8 @@ def parse_mysql_config_info(options, stdout):
167168

168169
info['version'] = tuple([int(v) for v in ver.split('.')[0:3]])
169170
libs = shlex.split(info['libs'])
171+
if ',' in libs[1]:
172+
libs.pop(1)
170173
info['lib_dir'] = libs[0].replace('-L', '')
171174
info['libs'] = [ lib.replace('-l', '') for lib in libs[1:] ]
172175
if platform.uname()[0] == 'SunOS':
@@ -176,9 +179,10 @@ def parse_mysql_config_info(options, stdout):
176179
for lib in info['libs']:
177180
log.debug("# {0}".format(lib))
178181
libs = shlex.split(info['libs_r'])
182+
if ',' in libs[1]:
183+
libs.pop(1)
179184
info['lib_r_dir'] = libs[0].replace('-L', '')
180185
info['libs_r'] = [ lib.replace('-l', '') for lib in libs[1:] ]
181-
182186
info['include'] = [x.strip() for x in info['include'].split('-I')[1:]]
183187

184188
return info
@@ -301,6 +305,28 @@ def initialize_options(self):
301305
self.with_protobuf_lib_dir = None
302306
self.with_protoc = None
303307

308+
def _copy_vendor_libraries(self):
309+
if self.with_mysql_capi and platform.system() == "Darwin":
310+
data_files = []
311+
openssl_libs = ["libssl.1.0.0.dylib", "libcrypto.1.0.0.dylib"]
312+
vendor_folder = "vendor"
313+
mkpath(os.path.join(os.getcwd(), vendor_folder))
314+
315+
# Copy OpenSSL libraries to 'vendor' folder
316+
log.info("Copying OpenSSL libraries")
317+
for filename in openssl_libs:
318+
data_files.append(os.path.join(vendor_folder, filename))
319+
src = os.path.join(self.with_mysql_capi, "lib", filename)
320+
dst = os.path.join(os.getcwd(), vendor_folder)
321+
log.info("Copying {0} -> {1}".format(src, dst))
322+
shutil.copy(src, dst)
323+
324+
# Add data_files to distribution
325+
self.distribution.data_files = [(
326+
os.path.join(get_python_lib(), vendor_folder),
327+
data_files
328+
)]
329+
304330
def _finalize_connector_c(self, connc_loc):
305331
"""Finalize the --with-connector-c command line argument
306332
"""
@@ -427,6 +453,8 @@ def finalize_options(self):
427453
('with_protobuf_lib_dir', 'with_protobuf_lib_dir'),
428454
('with_protoc', 'with_protoc'))
429455

456+
self._copy_vendor_libraries()
457+
430458
build_ext.finalize_options(self)
431459

432460
print("# Python architecture: {0}".format(py_arch))
@@ -534,7 +562,7 @@ def fix_compiler(self):
534562
if self.extra_compile_args:
535563
ext.extra_compile_args.extend(self.extra_compile_args.split())
536564
# Add extra link args
537-
if self.extra_link_args:
565+
if self.extra_link_args and ext.name != "_mysqlxpb":
538566
ext.extra_link_args.extend(self.extra_link_args.split())
539567
# Add system headers
540568
for sysheader in sysheaders:
@@ -562,7 +590,7 @@ def run(self):
562590
if self.extra_compile_args:
563591
ext.extra_compile_args.extend(self.extra_compile_args.split())
564592
# Add extra link args
565-
if self.extra_link_args:
593+
if self.extra_link_args and ext.name != "_mysqlxpb":
566594
ext.extra_link_args.extend(self.extra_link_args.split())
567595
if self.with_mysqlxpb_cext:
568596
self.run_protoc()
@@ -576,6 +604,25 @@ def run(self):
576604
self.run_protoc()
577605
self.real_build_extensions()
578606

607+
if platform.system() == "Darwin":
608+
cmd_libssl = [
609+
"install_name_tool", "-change", "libssl.1.0.0.dylib",
610+
"@loader_path/vendor/libssl.1.0.0.dylib",
611+
build_ext.get_ext_fullpath(self, "_mysql_connector")
612+
]
613+
log.info("Executing: {0}".format(" ".join(cmd_libssl)))
614+
proc = Popen(cmd_libssl, stdout=PIPE, universal_newlines=True)
615+
stdout, _ = proc.communicate()
616+
617+
cmd_libcrypto = [
618+
"install_name_tool", "-change", "libcrypto.1.0.0.dylib",
619+
"@loader_path/vendor/libcrypto.1.0.0.dylib",
620+
build_ext.get_ext_fullpath(self, "_mysql_connector")
621+
]
622+
log.info("Executing: {0}".format(" ".join(cmd_libcrypto)))
623+
proc = Popen(cmd_libcrypto, stdout=PIPE, universal_newlines=True)
624+
stdout, _ = proc.communicate()
625+
579626

580627
class BuildExtStatic(BuildExtDynamic):
581628

@@ -584,6 +631,8 @@ class BuildExtStatic(BuildExtDynamic):
584631
user_options = build_ext.user_options + CEXT_OPTIONS
585632

586633
def finalize_options(self):
634+
self._copy_vendor_libraries()
635+
587636
install_obj = self.distribution.get_command_obj('install')
588637
install_obj.with_mysql_capi = self.with_mysql_capi
589638
install_obj.with_protobuf_include_dir = self.with_protobuf_include_dir
@@ -746,7 +795,7 @@ def fix_compiler(self):
746795
if self.extra_compile_args:
747796
ext.extra_compile_args.extend(self.extra_compile_args.split())
748797
# Add extra link args
749-
if self.extra_link_args:
798+
if self.extra_link_args and ext.name != "_mysqlxpb":
750799
ext.extra_link_args.extend(self.extra_link_args.split())
751800

752801

0 commit comments

Comments
 (0)