Skip to content

Commit 8ae255b

Browse files
committed
ar is not so strange on darwin.
1 parent 2402975 commit 8ae255b

File tree

1 file changed

+77
-5
lines changed

1 file changed

+77
-5
lines changed

wllvm/extraction.py

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,84 @@ def extractFile(archive, filename, instance):
336336

337337

338338

339+
def handleArchiveDarwin(pArgs):
340+
341+
originalDir = os.getcwd() # This will be the destination
342+
343+
pArgs.arCmd.append(pArgs.inputFile)
344+
345+
# Make temporary directory to extract objects to
346+
tempDir = ''
347+
bitCodeFiles = [ ]
348+
retCode=0
349+
try:
350+
tempDir = tempfile.mkdtemp(suffix='wllvm')
351+
os.chdir(tempDir)
352+
353+
# Extract objects from archive
354+
try:
355+
arP = Popen(pArgs.arCmd)
356+
except OSError as e:
357+
if e.errno == 2:
358+
errorMsg = 'Your ar does not seem to be easy to find.\n'
359+
else:
360+
errorMsg = 'OS error({0}): {1}'.format(e.errno, e.strerror)
361+
logging.error(errorMsg)
362+
raise Exception(errorMsg)
363+
364+
arPE = arP.wait()
365+
366+
if arPE != 0:
367+
errorMsg = 'Failed to execute archiver with command {0}'.format(pArgs.arCmd)
368+
logging.error(errorMsg)
369+
raise Exception(errorMsg)
370+
371+
# Iterate over objects and examine their bitcode inserts
372+
for (root, dirs, files) in os.walk(tempDir):
373+
logging.debug('Exploring "{0}"'.format(root))
374+
for f in files:
375+
fPath = os.path.join(root, f)
376+
if FileType.getFileType(fPath) == pArgs.fileType:
377+
378+
# Extract bitcode locations from object
379+
contents = pArgs.extractor(fPath)
380+
381+
for bcFile in contents:
382+
if bcFile != '':
383+
if not os.path.exists(bcFile):
384+
logging.warning('{0} lists bitcode library "{1}" but it could not be found'.format(f, bcFile))
385+
else:
386+
bitCodeFiles.append(bcFile)
387+
else:
388+
logging.info('Ignoring file "{0}" in archive'.format(f))
389+
390+
logging.info('Found the following bitcode file names to build bitcode archive:\n{0}'.format(
391+
pprint.pformat(bitCodeFiles)))
392+
393+
finally:
394+
# Delete the temporary folder
395+
logging.debug('Deleting temporary folder "{0}"'.format(tempDir))
396+
shutil.rmtree(tempDir)
397+
398+
#write the manifest file if asked for
399+
if pArgs.manifestFlag:
400+
manifestFile = '{0}.llvm.manifest'.format(pArgs.inputFile)
401+
with open(manifestFile, 'w') as output:
402+
for f in bitCodeFiles:
403+
output.write('{0}\n'.format(f))
404+
405+
# Build bitcode archive
406+
os.chdir(originalDir)
407+
408+
return buildArchive(pArgs, bitCodeFiles)
409+
410+
339411

340412
#iam: 5/1/2018
341-
def handleArchive(pArgs):
342-
""" handleArchive processes a archive, and creates either a bitcode archive, or a module, depending on the flags used.
413+
def handleArchiveLinux(pArgs):
414+
""" handleArchiveLinux processes a archive, and creates either a bitcode archive, or a module, depending on the flags used.
343415
344-
Archives are strange beasts. handleArchive processes the archive by:
416+
Archives on Linux are strange beasts. handleArchive processes the archive by:
345417
346418
1. first creating a table of contents of the archive, which maps file names (in the archive) to the number of
347419
times a file with that name is stored in the archive.
@@ -561,7 +633,7 @@ def process_file_unix(pArgs):
561633
_logger.info('Generating LLVM Bitcode module')
562634
retval = handleExecutable(pArgs)
563635
elif ft == FileType.ARCHIVE:
564-
retval = handleArchive(pArgs)
636+
retval = handleArchiveLinux(pArgs)
565637
elif ft == FileType.THIN_ARCHIVE:
566638
retval = handleThinArchive(pArgs)
567639
else:
@@ -583,7 +655,7 @@ def process_file_darwin(pArgs):
583655
_logger.info('Generating LLVM Bitcode module')
584656
retval = handleExecutable(pArgs)
585657
elif ft == FileType.ARCHIVE:
586-
retval = handleArchive(pArgs)
658+
retval = handleArchiveDarwin(pArgs, True)
587659
else:
588660
_logger.error('File "%s" of type %s cannot be used', pArgs.inputFile, FileType.revMap[ft])
589661
return retval

0 commit comments

Comments
 (0)