-
Notifications
You must be signed in to change notification settings - Fork 58
/
macos_postbuild_library_fixup
executable file
·483 lines (370 loc) · 15.6 KB
/
macos_postbuild_library_fixup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/usr/bin/env python3
from codecs import ascii_decode
from itertools import chain
import logging
import os
import re
import shutil
import subprocess
import sys
logger = logging.getLogger('postbuild_library_fixup')
def main():
# Parse arguments.
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-q', '--quiet', action='store_true')
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-n', '--dry-run', action='store_true')
parser.add_argument('--starlink-dir')
parser.add_argument('--sign', action='store_true')
parser.add_argument('--backup', action='store_true')
parser.add_argument('--backup-delete', action='store_true')
parser.add_argument('--backup-restore', action='store_true')
args = parser.parse_args()
# Configure logger.
logging.basicConfig(level=(
logging.WARNING if args.quiet else (
logging.DEBUG if args.verbose else logging.INFO)))
# Determine location of Starlink installation to process.
starlink_dir = args.starlink_dir
if starlink_dir is None:
starlink_dir = os.environ.get('STARLINK_DIR')
if starlink_dir is None:
logger.error('Neither --starlink-dir or $STARLINK_DIR were set')
return 1
while starlink_dir.endswith('/'):
starlink_dir = starlink_dir[:-1]
logger.debug('STARLINK_DIR: %s', starlink_dir)
if sys.platform == 'darwin':
fixup = FixupMacOs()
elif sys.platform == 'linux':
fixup = FixupLinux()
else:
logger.error('Platform not recognized')
return 1
if args.backup_delete or args.backup_restore:
if args.backup_delete and args.backup_restore:
logger.error('Both backup delete and restore specified')
return 1
fixup.process_backup(
starlink_dir, delete=args.backup_delete, dry_run=args.dry_run)
return 0
(executables, libraries, library_links) = fixup.find_files(starlink_dir)
library_paths = fixup.make_library_map(
chain(libraries, library_links), starlink_dir)
kwargs = {
'library_paths': library_paths,
'starlink_dir': starlink_dir,
'sign': args.sign,
'backup': args.backup,
'dry_run': args.dry_run,
}
logger.critical('Applying fixes')
for (is_library, filepaths) in ((False, executables), (True, libraries)):
for filepath in filepaths:
fixup.fix_file(is_library=is_library, filepath=filepath, **kwargs)
class FixupBase():
# NOTE: previous script would have included some of these.
non_executable_extensions = (
'.tcl', '.pl', '.py', '.sh', '.csh', '.icl', '.pro',
'.la', '.a', '.o')
pattern_so_filename = re.compile(r'\.so(\.\d+)*$')
def find_files(self, starlink_dir):
logger.critical('Looking for libraries and executables')
executables = []
libraries = []
library_links = []
for (dirpath, dirnames, filenames) in os.walk(starlink_dir):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
if os.path.islink(filepath):
if self.is_library(filepath):
logger.debug('Found library link: %s', filepath)
library_links.append(filepath)
else:
logger.debug('Ignoring link: %s', filepath)
elif self.is_library(filepath):
logger.debug('Found library: %s', filepath)
libraries.append(filepath)
elif self.is_executable(filepath):
logger.debug('Found executable: %s', filepath)
executables.append(filepath)
return (executables, libraries, library_links)
def make_library_map(self, libraries, starlink_dir):
logger.critical('Checking for duplicate library names')
library_paths = {}
starperl_lib_dir = os.path.join(starlink_dir, 'Perl', 'lib')
for filepath in libraries:
# Skip Perl libraries.
if filepath.startswith(starperl_lib_dir):
continue
# Skip .dylib.dSYM?
if '.dylib.dSYM/' in filepath:
continue
basename = os.path.basename(filepath)
if basename in library_paths:
logger.warning(
'Duplicate library: %s %s',
filepath, library_paths[basename])
library_paths[basename] = filepath
return library_paths
def is_library(self, filepath):
return (self.pattern_so_filename.search(filepath)
or filepath.endswith('.dylib')
or filepath.endswith('.bundle'))
def is_executable(self, filepath):
for extension in self.non_executable_extensions:
if filepath.endswith(extension):
return False
return os.access(filepath, os.X_OK)
def get_library_rel_path(
self, filepath, library, starlink_dir, library_paths):
if library in library_paths.values():
logger.debug('%s: found %s', filepath, library)
library_path = library
elif os.path.basename(library) in library_paths:
logger.debug('%s: found basename %s', filepath, library)
library_path = library_paths[os.path.basename(library)]
else:
logger.warning('%s: not found: %s', filepath, library)
return
if not library_path.startswith(starlink_dir):
logger.error('%s: not in STARLINK_DIR: %s', filepath, library_path)
return
prefix_len = len(starlink_dir) + 1
return os.path.join('@rpath', library_path[prefix_len:])
def process_backup(self, starlink_dir, delete, dry_run):
logger.critical('Looking for backup files')
for (dirpath, dirnames, filenames) in os.walk(starlink_dir):
for filename in filenames:
if not filename.endswith('.bak'):
continue
filepath = os.path.join(dirpath, filename)
if delete:
if dry_run:
logger.debug(
'would have deleted backup (dry-run mode): %s',
filepath)
else:
logger.debug('deleting backup: %s', filepath)
os.remove(filepath)
else:
original_filepath = filepath[:-4]
assert '{}.bak'.format(original_filepath) == filepath
if dry_run:
logger.debug(
'would have restored backup (dry-run mode): %s %s',
filepath, original_filepath)
else:
logger.debug(
'restoring backup: %s %s',
filepath, original_filepath)
shutil.move(filepath, original_filepath)
def backup_file(self, filepath, dry_run):
"""Generate backup file path by adding ".bak". If this file
does not already exist, copy the file to it."""
backup_filepath = '{}.bak'.format(filepath)
if not os.path.exists(backup_filepath):
if dry_run:
logger.debug(
'would have created backup (dry-run mode): %s %s',
filepath, backup_filepath)
else:
logger.debug(
'creating backup: %s %s',
filepath, backup_filepath)
shutil.copy(filepath, backup_filepath)
def _run_command(self, command, dry_run):
command_str = ' '.join(command)
if dry_run:
logger.debug('would have run (dry-run mode): %s', command_str)
else:
logger.debug('running: %s', command_str)
try:
subprocess.check_call(command)
except:
logger.exception('error running command: %s', command_str)
raise
class FixupMacOs(FixupBase):
library_paths_ignore = (
'/usr/lib',
'/opt/X11/lib',
'/System/Library/Frameworks',
)
# NOTE: previous script just checked for "Mach-O" so would have
# matched various other things, e.g. .o
pattern_executable = re.compile(b'Mach-O .* executable')
def is_executable(self, filepath):
if not super().is_executable(filepath):
return False
output = subprocess.check_output(['/usr/bin/file', filepath])
if self.pattern_executable.search(output):
return True
return False
def fix_file(
self, is_library, filepath,
library_paths, starlink_dir, sign, backup, dry_run):
# Skip .dylib.dSYM?
if '.dylib.dSYM/' in filepath:
return
# Skip ELF .so files in starjava?
if ('starjava/lib' in filepath) and filepath.endswith('.so'):
return
updates = []
if is_library and filepath.endswith('.dylib'):
current_library_id = self.get_library_id(filepath)
if current_library_id:
library_id = self.get_library_rel_path(
filepath, current_library_id,
starlink_dir=starlink_dir, library_paths=library_paths)
if library_id != current_library_id:
updates.append(('-id', library_id))
logger.debug(
'%s: setting library id: %s', filepath, library_id)
linked_libraries = self.get_libraries(filepath)
for library in linked_libraries:
if any(library.startswith(ignore)
for ignore in self.library_paths_ignore):
continue
replacement = self.get_library_rel_path(
filepath, library,
starlink_dir=starlink_dir, library_paths=library_paths)
if (replacement is not None) and (replacement != library):
updates.append(('-change', library, replacement))
logger.debug(
'%s: replacement library: %s', filepath, replacement)
rpath = '@loader_path/{}/'.format(
os.path.relpath(starlink_dir, os.path.dirname(filepath)))
current_rpaths = self.get_rpaths(filepath)
if not current_rpaths:
logger.debug('%s: setting rpath: %s', filepath, rpath)
updates.append(('-add_rpath', rpath))
else:
if rpath in current_rpaths:
logger.debug(
'%s: already have rpath %s in %r',
filepath, rpath, current_rpaths)
for current_rpath in current_rpaths:
if current_rpath != rpath:
updates.append(('-delete_rpath', current_rpath))
else:
updates.append(('-rpath', current_rpaths[0], rpath))
for current_rpath in current_rpaths[1:]:
updates.append(('-delete_rpath', current_rpath))
if not (updates or sign):
logger.debug('%s: no updates', filepath)
return
logger.info('Applying fixes to: %s', filepath)
if backup:
self.backup_file(filepath, dry_run=dry_run)
if updates:
command = list(chain(('install_name_tool',), *updates, (filepath,)))
self._run_command(command, dry_run=dry_run)
if sign:
self._sign_file(filepath, dry_run=dry_run)
def _sign_file(self, filepath, identity='-', dry_run=False):
command = [
'codesign', '--force',
'--sign', identity,
filepath,
]
self._run_command(command, dry_run=dry_run)
def get_library_id(self, filepath):
output = subprocess.check_output(['/usr/bin/otool', '-XD', filepath])
return ascii_decode(output)[0].strip()
def get_libraries(self, filepath):
output = subprocess.check_output(['/usr/bin/otool', '-XL', filepath])
result = []
for line in ascii_decode(output)[0].splitlines():
result.append(line.strip().split(' ')[0])
return result
def get_rpaths(self, filepath):
output = subprocess.check_output(['/usr/bin/otool', '-Xl', filepath])
result = []
found_rpath = False
for line in ascii_decode(output)[0].splitlines():
line = line.strip()
if line == 'cmd LC_RPATH':
found_rpath = True
elif found_rpath and line.startswith('path '):
result.append(line.split(' ')[1])
found_path = False
return result
class FixupLinux(FixupBase):
library_paths_ignore = (
'/lib64',
'/usr/lib64',
)
pattern_executable = re.compile(b'ELF .* executable')
pattern_ldd_output = re.compile(r'=>\s*(\S+)\s*\(')
pattern_objdump_output = re.compile(r'(\S+)\s*(\S+)')
def is_executable(self, filepath):
if not super().is_executable(filepath):
return False
output = subprocess.check_output(['/usr/bin/file', filepath])
if self.pattern_executable.search(output):
return True
return False
def fix_file(
self, is_library, filepath,
library_paths, starlink_dir, sign, backup, dry_run):
if sign:
raise Exception('Signing not supported')
updates = []
linked_libraries = self.get_libraries(filepath)
for library in linked_libraries:
if any(library.startswith(ignore)
for ignore in self.library_paths_ignore):
continue
# Compute replacement in order to be able to issue warnings.
self.get_library_rel_path(
filepath, library,
starlink_dir=starlink_dir, library_paths=library_paths)
# TODO: also process libraries?
if is_library:
return
current_rpaths = self.get_rpaths(filepath)
# TODO: also include StarJava path?
rpath = os.path.join(
'$ORIGIN',
os.path.relpath(
os.path.join(starlink_dir, 'lib'),
os.path.dirname(filepath)))
if ((len(current_rpaths) != 1) or (current_rpaths[0] != rpath)):
updates.extend((
('--remove-rpath',),
('--force-rpath', '--set-rpath', rpath),
))
if not updates:
return
logger.info('Applying fixes to: %s', filepath)
if backup:
self.backup_file(filepath, dry_run=dry_run)
for update in updates:
command = list(chain(('patchelf',), update, (filepath,)))
self._run_command(command, dry_run=dry_run)
def get_libraries(self, filepath):
output = subprocess.check_output(['ldd', filepath])
result = []
for line in ascii_decode(output)[0].splitlines():
m = self.pattern_ldd_output.search(line)
if not m:
continue
result.append(m.group(1))
return result
def get_rpaths(self, filepath):
output = subprocess.check_output(['objdump', '-p', filepath])
result = []
for line in ascii_decode(output)[0].splitlines():
m = self.pattern_objdump_output.search(line)
if not m:
continue
# TODO: check also for runpath?
if m.group(1) == 'RPATH':
# TODO: check if correct separator?
result.extend(m.group(2).split(':'))
return result
if __name__ == '__main__':
status = main()
if status:
sys.exit(status)