Skip to content

Commit c7b16b9

Browse files
committed
Handle thin archives directly, rather than rely on a buggy(?) conversion script.
1 parent 2f68e25 commit c7b16b9

File tree

2 files changed

+58
-15
lines changed

2 files changed

+58
-15
lines changed

wllvm/extraction.py

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,24 @@ def archiveFiles(pArgs, fileNames):
236236

237237
return retCode
238238

239+
def extract_from_thin_archive(inputFile):
240+
"""Extracts the paths from the thin archive.
241+
242+
"""
243+
retval = None
244+
245+
arCmd = ['ar', '-t', inputFile] #might be os dependent
246+
arProc = Popen(arCmd, stdout=sp.PIPE)
247+
248+
arOutput = arProc.communicate()[0]
249+
if arProc.returncode != 0:
250+
_logger.error('ar failed on %s', inputFile)
251+
sys.exit(-1)
252+
253+
lines = arOutput.splitlines()
254+
return lines
255+
256+
239257

240258
def handleExecutable(pArgs):
241259

@@ -253,10 +271,37 @@ def handleExecutable(pArgs):
253271
return linkFiles(pArgs, fileNames)
254272

255273

274+
def handleThinArchive(pArgs):
275+
276+
if pArgs.bitcodeModuleFlag:
277+
_logger.info('Generating LLVM Bitcode module from an archive')
278+
else:
279+
_logger.info('Generating LLVM Bitcode archive from an archive')
280+
281+
objectPaths = extract_from_thin_archive(pArgs.inputFile)
282+
283+
if not objectPaths:
284+
return 1
285+
286+
bcFiles = []
287+
for p in objectPaths:
288+
_logger.info('handleThinArchive: processing {0}'.format(p))
289+
contents = pArgs.extractor(p)
290+
for c in contents:
291+
if len(c) > 0:
292+
_logger.info('\t including {0}'.format(c))
293+
bcFiles.append(str(c))
294+
295+
return buildArchive(pArgs, bcFiles)
256296

257297

258298
def handleArchive(pArgs):
259299

300+
if pArgs.bitcodeModuleFlag:
301+
_logger.info('Generating LLVM Bitcode module from an archive')
302+
else:
303+
_logger.info('Generating LLVM Bitcode archive from an archive')
304+
260305
originalDir = os.getcwd() # This will be the destination
261306

262307
pArgs.arCmd.append(pArgs.inputFile)
@@ -314,17 +359,17 @@ def handleArchive(pArgs):
314359
_logger.debug('Deleting temporary folder "%s"', tempDir)
315360
shutil.rmtree(tempDir)
316361

317-
#write the manifest file if asked for
318-
if pArgs.manifestFlag:
319-
writeManifest('{0}.llvm.manifest'.format(pArgs.inputFile), bitCodeFiles)
320-
321-
# Build bitcode archive
362+
# Build bitcode archive
322363
os.chdir(originalDir)
323364

324365
return buildArchive(pArgs, bitCodeFiles)
325366

326367
def buildArchive(pArgs, bitCodeFiles):
327368

369+
#write the manifest file if asked for
370+
if pArgs.manifestFlag:
371+
writeManifest('{0}.llvm.manifest'.format(pArgs.inputFile), bitCodeFiles)
372+
328373
if pArgs.bitcodeModuleFlag:
329374

330375
# Pick output file path if outputFile not set
@@ -355,7 +400,7 @@ def buildArchive(pArgs, bitCodeFiles):
355400

356401
def writeManifest(manifestFile, bitCodeFiles):
357402
with open(manifestFile, 'w') as output:
358-
for f in bitCodeFiles:
403+
for f in sorted(bitCodeFiles):
359404
output.write('{0}\n'.format(f))
360405
sf = getStorePath(f)
361406
if sf:
@@ -464,11 +509,9 @@ def process_file_unix(pArgs):
464509
_logger.info('Generating LLVM Bitcode module')
465510
return handleExecutable(pArgs)
466511
elif ft == FileType.ARCHIVE:
467-
if pArgs.bitcodeModuleFlag:
468-
_logger.info('Generating LLVM Bitcode module from an archive')
469-
else:
470-
_logger.info('Generating LLVM Bitcode archive from an archive')
471512
return handleArchive(pArgs)
513+
elif ft == FileType.THIN_ARCHIVE:
514+
return handleThinArchive(pArgs)
472515
else:
473516
_logger.error('File "%s" of type %s cannot be used', pArgs.inputFile, FileType.revMap[ft])
474517
return 1
@@ -488,10 +531,6 @@ def process_file_darwin(pArgs):
488531
_logger.info('Generating LLVM Bitcode module')
489532
return handleExecutable(pArgs)
490533
elif ft == FileType.ARCHIVE:
491-
if pArgs.bitcodeModuleFlag:
492-
_logger.info('Generating LLVM Bitcode module from an archive')
493-
else:
494-
_logger.info('Generating LLVM Bitcode archive from an archive')
495534
return handleArchive(pArgs)
496535
else:
497536
_logger.error('File "%s" of type %s cannot be used', pArgs.inputFile, FileType.revMap[ft])

wllvm/filetype.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def getFileType(cls, fileName):
4747
retval = cls.MACH_SHARED
4848
elif 'current ar archive' in foutput:
4949
retval = cls.ARCHIVE
50+
elif 'thin archive' in foutput:
51+
retval = cls.THIN_ARCHIVE
5052
elif 'ELF' in foutput and 'relocatable' in foutput:
5153
retval = cls.ELF_OBJECT
5254
elif 'Mach-O' in foutput and 'object' in foutput:
@@ -78,7 +80,9 @@ def init(cls):
7880
'MACH_EXECUTABLE',
7981
'MACH_OBJECT',
8082
'MACH_SHARED',
81-
'ARCHIVE')):
83+
'ARCHIVE',
84+
'THIN_ARCHIVE',
85+
)):
8286
setattr(cls, name, index)
8387
cls.revMap[index] = name
8488

0 commit comments

Comments
 (0)