Skip to content

Commit abb00c7

Browse files
committed
Movement towards the --bytecode -b option (for archives)
1 parent 9113ec1 commit abb00c7

File tree

1 file changed

+62
-53
lines changed

1 file changed

+62
-53
lines changed

extract-bc

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def getSectionSizeAndOffset(sectionName, filename):
7979
continue
8080

8181
# The needed section could not be found
82-
logging.error('Could not find "{0}" ELF section in "{1}"'.format(sectionName,filename))
82+
logging.warning('Could not find "{0}" ELF section in "{1}", so skipping this entry.'.format(sectionName,filename))
8383
return None
8484

8585
# Read the entire content of an ELF section into a string
@@ -140,24 +140,9 @@ def extract_section_linux(inputFile):
140140
return contents
141141

142142

143-
def handleExecutable(inputFile, outputFile, extractor, llvmLinker, manifestFlag):
144-
145-
fileNames = extractor(inputFile)
146-
147-
if not fileNames:
148-
return 1
149-
150-
if manifestFlag:
151-
manifestFile = '{0}.wllvm.manifest'.format(inputFile)
152-
with open(manifestFile, 'w') as output:
153-
for f in fileNames:
154-
output.write('{0}\n'.format(f))
155-
156-
if outputFile == None:
157-
outputFile = inputFile + '.' + moduleExtension
158-
143+
def linkFiles(outputFile, llvmLinker, fileNames):
159144
linkCmd = [ llvmLinker, '-v' ] if verboseFlag else [ llvmLinker ]
160-
145+
161146
linkCmd.extend(['-o', outputFile ])
162147

163148
linkCmd.extend([x for x in fileNames if x != ''])
@@ -171,12 +156,69 @@ def handleExecutable(inputFile, outputFile, extractor, llvmLinker, manifestFlag)
171156
errorMsg = "OS error({0}): {1}".format(e.errno, e.strerror)
172157
logging.error(errorMsg)
173158
raise Exception(errorMsg)
174-
159+
175160
else:
176161
exitCode = linkProc.wait()
177162
logging.info('{0} returned {1}'.format(llvmLinker, str(exitCode)))
178163
return exitCode
179164

165+
166+
def archiveFiles(outputFile, llvmArchiver, fileNames):
167+
retCode = 0
168+
# We do not want full paths in the archive so we need to chdir into each
169+
# bitcode's folder. Handle this by calling llvm-ar once for all bitcode
170+
# files in the same directory
171+
172+
# Map of directory names to list of bitcode files in that directory
173+
dirToBCMap = {}
174+
for bitCodeFile in fileNames:
175+
dirName = os.path.dirname(bitCodeFile)
176+
basename = os.path.basename(bitCodeFile)
177+
if dirName in dirToBCMap:
178+
dirToBCMap[dirName].append(basename)
179+
else:
180+
dirToBCMap[dirName] = [ basename ]
181+
182+
logging.debug('Built up directory to bitcode file list map:\n{0}'.format(
183+
pprint.pformat(dirToBCMap)))
184+
185+
for (dirname, bcList) in dirToBCMap.items():
186+
logging.debug('Changing directory to "{0}"'.format(dirname))
187+
os.chdir(dirname)
188+
larCmd = [llvmArchiver, 'rs', outputFile ] + bcList
189+
larProc = Popen(larCmd)
190+
retCode = larProc.wait()
191+
if retCode != 0:
192+
logging.error('Failed to execute:\n{0}'.format(pprint.pformat(larCmd)))
193+
break
194+
195+
if retCode == 0:
196+
logging.info('Generated LLVM bitcode archive {0}'.format(outputFile))
197+
else:
198+
logging.error('Failed to generate LLVM bitcode archive')
199+
200+
return retCode
201+
pass
202+
203+
204+
def handleExecutable(inputFile, outputFile, extractor, llvmLinker, manifestFlag):
205+
206+
fileNames = extractor(inputFile)
207+
208+
if not fileNames:
209+
return 1
210+
211+
if manifestFlag:
212+
manifestFile = '{0}.wllvm.manifest'.format(inputFile)
213+
with open(manifestFile, 'w') as output:
214+
for f in fileNames:
215+
output.write('{0}\n'.format(f))
216+
217+
if outputFile == None:
218+
outputFile = inputFile + '.' + moduleExtension
219+
220+
return linkFiles(outputFile, llvmLinker, fileNames)
221+
180222

181223

182224

@@ -252,7 +294,6 @@ def handleArchive(inputFile, outputFile, arCmd, fileType, extractor, llvmArchive
252294
return buildArchive(inputFile, outputFile, llvmArchiver, bitCodeFiles)
253295

254296
def buildArchive(inputFile, outputFile, llvmArchiver, bitCodeFiles):
255-
retCode=0
256297
# Pick output file path if outputFile not set
257298
if outputFile == None:
258299
if inputFile.endswith('.a'):
@@ -264,39 +305,7 @@ def buildArchive(inputFile, outputFile, llvmArchiver, bitCodeFiles):
264305

265306
logging.info('Writing output to {0}'.format(outputFile))
266307

267-
# We do not want full paths in the archive so we need to chdir into each
268-
# bitcode's folder. Handle this by calling llvm-ar once for all bitcode
269-
# files in the same directory
270-
271-
# Map of directory names to list of bitcode files in that directory
272-
dirToBCMap = {}
273-
for bitCodeFile in bitCodeFiles:
274-
dirName = os.path.dirname(bitCodeFile)
275-
basename = os.path.basename(bitCodeFile)
276-
if dirName in dirToBCMap:
277-
dirToBCMap[dirName].append(basename)
278-
else:
279-
dirToBCMap[dirName] = [ basename ]
280-
281-
logging.debug('Built up directory to bitcode file list map:\n{0}'.format(
282-
pprint.pformat(dirToBCMap)))
283-
284-
for (dirname, bcList) in dirToBCMap.items():
285-
logging.debug('Changing directory to "{0}"'.format(dirname))
286-
os.chdir(dirname)
287-
larCmd = [llvmArchiver, 'rs', outputFile ] + bcList
288-
larProc = Popen(larCmd)
289-
retCode = larProc.wait()
290-
if retCode != 0:
291-
logging.error('Failed to execute:\n{0}'.format(pprint.pformat(larCmd)))
292-
break
293-
294-
if retCode == 0:
295-
logging.info('Generated LLVM bitcode archive {0}'.format(outputFile))
296-
else:
297-
logging.error('Failed to generate LLVM bitcode archive')
298-
299-
return retCode
308+
return archiveFiles(outputFile, llvmArchiver, bitCodeFiles)
300309

301310

302311

0 commit comments

Comments
 (0)