Skip to content

Commit

Permalink
Fix bug dashingsoft#1234:
Browse files Browse the repository at this point in the history
Add plugin DylibPlugin and enable it when packing in Darwn
  • Loading branch information
jondy committed May 18, 2023
1 parent c2bf598 commit a58db9d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/cli/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,15 @@ def process(self, options, packer=None):
finder = Finder(self.ctx)
finder.process()

if packer and options.get('self_contained'):
finder.process_extra(packer.contents)
if packer:
if options.get('self_contained'):
finder.process_extra(packer.contents)
if getattr(packer, 'is_darwin_python', None):
sect = self.ctx.cfg['builder']
plugins = sect.get('plugins', '').split()
if 'DylibPlugin' not in plugins:
logger.info('implicitly enable plugin "DylibPlugin"')
sect.set('plugins', ' '.join(plugins + ['DylibPlugin']))

Pytransform3.pre_build(self.ctx)

Expand Down
43 changes: 43 additions & 0 deletions src/cli/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def post_runtime(ctx, source, dest, platform):
identity = '-'
cmdlist = ['codesign', '-s', identity, '--force',
'--all-architectures', '--timestamp', dest]
logger.info('%s', ' '.join(cmdlist))
p = Popen(cmdlist, stdout=PIPE, stderr=PIPE, shell=True)
stdout, stderr = p.communicate()
if p.returncode != 0:
Expand All @@ -112,3 +113,45 @@ def post_runtime(ctx, source, dest, platform):
'stdout: %r\n'
'stderr: %r',
cmdlist, p.returncode, stdout, stderr)


class DylibPlugin:

@staticmethod
def post_runtime(ctx, source, dest, platform):
if platform.startswith('darwin'):
from subprocess import Popen, PIPE, check_output
from sys import version_info

output = check_output(['otool', '-L', dest])
for line in output.splitlines():
if line.find(b'@rpath/Python'):
logger.debug('"%s" has been patched by DylibPlugin', dest)
return

pyver = '%s.%s' % version_info[:2]
cmdlist = ['install_name_tool', '-change',
'@rpath/lib/libpython%s.so' % pyver, '@rpath/Python',
dest]
logger.info('%s', ' '.join(cmdlist))
p = Popen(cmdlist, stdout=PIPE, stderr=PIPE, shell=True)
stdout, stderr = p.communicate()
if p.returncode != 0:
logger.warning(
'install_name_tool command failed with error code %d!\n'
'stdout: %r\n'
'stderr: %r',
p.returncode, stdout, stderr)

identity = '-'
cmdlist = ['codesign', '-s', identity, '--force',
'--all-architectures', '--timestamp', dest]
logger.info('%s', ' '.join(cmdlist))
p = Popen(cmdlist, stdout=PIPE, stderr=PIPE, shell=True)
stdout, stderr = p.communicate()
if p.returncode != 0:
logger.warning(
'codesign command failed with error code %d!\n'
'stdout: %r\n'
'stderr: %r',
p.returncode, stdout, stderr)
4 changes: 4 additions & 0 deletions src/cli/repack.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ def extract_carchive(self, executable, buildpath, clean=True):
self.pyztoc = pyzarch.toc
contents.append(extract_pyzarchive(name, pyzarch, buildpath))

elif (is_darwin and typecode == PKG_ITEM_BINARY and
name.strip('.') == 'Python'):
self.is_darwin_python = True

self.contents = contents
self.one_file_mode = len(pkgtoc) > 10

Expand Down

0 comments on commit a58db9d

Please sign in to comment.