Skip to content

Commit 29a0147

Browse files
committed
Add cross-compilation support.
1 parent 26b52aa commit 29a0147

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ original bitcode file. For convenience, when using both the manifest
237237
feature of `extract-bc` and the store, the manifest will contain both
238238
the original path, and the store path.
239239

240+
Cross-Compilation
241+
-----------------
242+
243+
To support cross-compilation WLLVM supports the `-target` triple used by clang.
244+
More information can be found
245+
[here.](https://clang.llvm.org/docs/CrossCompilation.html#target-triple).
246+
247+
Additionall, WLLVM leverages `objcopy` for some of its heavy lifting. When
248+
cross-compiling you must ensure to use the appropriate `objcopy` for the target
249+
architecture. The `BINUTILS_TARGET_PREFIX` environment variable can be used to
250+
set the objcopy of choice, for example, `arm-linux-gnueabihf`.
240251

241252
Debugging
242253
---------

wllvm/arglistfilter.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ def __init__(self, inputList, exactMatches={}, patternMatches={}):
127127
'-iquote' : (1, ArgumentListFilter.compileBinaryCallback),
128128
'-imultilib' : (1, ArgumentListFilter.compileBinaryCallback),
129129

130+
# Architecture
131+
'-target' : (1, ArgumentListFilter.compileBinaryCallback),
132+
'-marm' : (0, ArgumentListFilter.compileUnaryCallback),
133+
130134
# Language
131135
'-ansi' : (0, ArgumentListFilter.compileUnaryCallback),
132136
'-pedantic' : (0, ArgumentListFilter.compileUnaryCallback),
@@ -268,6 +272,7 @@ def __init__(self, inputList, exactMatches={}, patternMatches={}):
268272
r'-mmacosx-version-min=.+$' : (0, ArgumentListFilter.compileUnaryCallback),
269273

270274
r'^--sysroot=.+$' : (0, ArgumentListFilter.compileUnaryCallback),
275+
r'^--gcc-toolchain=.+$' : (0, ArgumentListFilter.compileUnaryCallback),
271276
r'^-print-prog-name=.*$' : (0, ArgumentListFilter.compileUnaryCallback),
272277
r'^-print-file-name=.*$' : (0, ArgumentListFilter.compileUnaryCallback),
273278
#iam: -xc from yices. why BD?

wllvm/compilers.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ def wcompile(mode):
7070
# Environmental variable for path to compiler tools (clang/llvm-link etc..)
7171
llvmCompilerPathEnv = 'LLVM_COMPILER_PATH'
7272

73+
# Environmental variable for cross-compilation target.
74+
binutilsTargetPrefixEnv = 'BINUTILS_TARGET_PREFIX'
75+
7376
# This is the ELF section name inserted into binaries
7477
elfSectionName = '.llvm_bc'
7578

@@ -130,12 +133,15 @@ def attachBitcodePathToObject(bcPath, outFileName):
130133
os.fsync(f.fileno())
131134
f.close()
132135

136+
binUtilsTargetPrefix = os.getenv(binutilsTargetPrefixEnv)
133137

134138
# Now write our bitcode section
135139
if sys.platform.startswith('darwin'):
136-
objcopyCmd = ['ld', '-r', '-keep_private_externs', outFileName, '-sectcreate', darwinSegmentName, darwinSectionName, f.name, '-o', outFileName]
140+
objcopyBin = '{}-{}'.format(binUtilsTargetPrefix, 'ld') if binUtilsTargetPrefix else 'ld'
141+
objcopyCmd = [objcopyBin, '-r', '-keep_private_externs', outFileName, '-sectcreate', darwinSegmentName, darwinSectionName, f.name, '-o', outFileName]
137142
else:
138-
objcopyCmd = ['objcopy', '--add-section', '{0}={1}'.format(elfSectionName, f.name), outFileName]
143+
objcopyBin = '{}-{}'.format(binUtilsTargetPrefix, 'objcopy') if binUtilsTargetPrefix else 'objcopy'
144+
objcopyCmd = [objcopyBin, '--add-section', '{0}={1}'.format(elfSectionName, f.name), outFileName]
139145
orc = 0
140146

141147
# loicg: If the environment variable WLLVM_BC_STORE is set, copy the bitcode
@@ -238,6 +244,7 @@ def getBuilder(cmd, mode):
238244
compilerEnv = 'LLVM_COMPILER'
239245
cstring = os.getenv(compilerEnv)
240246
pathPrefix = os.getenv(llvmCompilerPathEnv) # Optional
247+
241248
_logger.debug('WLLVM compiler using %s', cstring)
242249
if pathPrefix:
243250
_logger.debug('WLLVM compiler path prefix "%s"', pathPrefix)

wllvm/extraction.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,20 @@ def extraction():
5050
bitCodeArchiveExtension = 'bca'
5151
moduleExtension = 'bc'
5252

53+
# Environmental variable for cross-compilation target.
54+
binutilsTargetPrefixEnv = 'BINUTILS_TARGET_PREFIX'
55+
5356
def getSectionSizeAndOffset(sectionName, filename):
5457
"""Returns the size and offset of the section, both in bytes.
5558
5659
Use objdump on the provided binary; parse out the fields
5760
to find the given section. Parses the output,and
5861
extracts thesize and offset of that section (in bytes).
5962
"""
60-
objdumpCmd = ['objdump', '-h', '-w', filename]
63+
64+
binUtilsTargetPrefix = os.getenv(binutilsTargetPrefixEnv)
65+
objdumpBin = '{}-{}'.format(binUtilsTargetPrefix, 'objdump') if binUtilsTargetPrefix else 'objdump'
66+
objdumpCmd = [objdumpBin, '-h', '-w', filename]
6167
objdumpProc = Popen(objdumpCmd, stdout=sp.PIPE)
6268

6369
objdumpOutput = objdumpProc.communicate()[0]

0 commit comments

Comments
 (0)