Skip to content

Instantly share code, notes, and snippets.

@thewtex
Created July 5, 2013 04:54
Show Gist options
  • Save thewtex/5931775 to your computer and use it in GitHub Desktop.
Save thewtex/5931775 to your computer and use it in GitHub Desktop.
When cross compiling with CMake, the CMake introspection process may involve "try_run" calls where an executable in run on the target system to learn some information about the target system. Since we are cross compiling, attempting to run the executable on the host is not possible or informative. As a result, the initial CMake configuration on …
#!/usr/bin/env python
"""When cross compiling with CMake, the CMake introspection process may involve
"try_run" calls where an executable in run on the target system to learn some
information about the target system. Since we are cross compiling, attempting
to run the executable on the host is not possible or informative.
As a result, the initial CMake configuration on the host system will fail, but
it will create a file called TryRunResults.cmake that contains entries that
must result from the target system. These entries can be found by running a
CMake configuration on the target system, and examining the CMakeCache.txt
that result. The variables can then be passed to CMake using the -C flag.
For more information, see http://www.cmake.org/Wiki/CMake_Cross_Compiling.
The purpose of this script is to automate the population of the
TryRunResults.cmake with the content of the target CMakeCache.txt.
Usage: ./CMakeCacheToTryRun.py \
InputTryRunResults.cmake CMakeCache.txt OutputTryRunResults.cmake
"""
__author__ = "Matt McCormick"
__email__ = "matt.mccormick@kitware.com"
__license__ = "Apache 2"
import sys
def substitute_with_cache(input_fp, output_fp, cache_entries):
replace_next = False
for line in input_fp.readlines():
if replace_next:
if replace_next in cache_entries:
output_fp.write(' "')
output_fp.write(cache_entries[replace_next])
output_fp.write('"\n')
else:
output_fp.write(line)
replace_next = False
else:
split = line.strip().split('(')
if split[0].lower() == 'set':
replace_next = split[1].strip()
output_fp.write(line)
def run(inputTryRun, cmakeCache, outputTryRun):
# Create a dictionary of INTERNAL CMake Cache entries.
cache_entries = dict()
with open(cmakeCache, 'r') as fp:
for line in fp.readlines():
line_split = line.strip().split(':INTERNAL=')
if len(line_split) > 1:
cache_entries[line_split[0]] = line_split[1]
with open(inputTryRun, 'r') as input_fp:
with open(outputTryRun, 'w') as output_fp:
substitute_with_cache(input_fp, output_fp, cache_entries)
if __name__ == '__main__':
usage = 'Usage: CMakeCacheToTryRun.py InputTryRunResults.cmake' + \
'CMakeCache.txt OutputTryRunResults.cmake'
if len(sys.argv) < 4:
print(usage)
sys.exit(1)
run(sys.argv[1], sys.argv[2], sys.argv[3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment