cmake: add extensions necessary for build command

Extend the CMake helper with some extra knowledge needed to support
'west build':

- add run_cmake() for general-purpose invocations of CMake
- teach the CMake cache some more Python protocol methods

Signed-off-by: Marti Bolivar <marti@foundries.io>
This commit is contained in:
Marti Bolivar 2018-08-10 16:13:00 -05:00
parent 3a24890a24
commit 53d5bf0e37
1 changed files with 21 additions and 7 deletions

View File

@ -5,6 +5,7 @@
'''Helpers for dealing with CMake''' '''Helpers for dealing with CMake'''
from collections import OrderedDict from collections import OrderedDict
import os.path
import re import re
import subprocess import subprocess
import shutil import shutil
@ -12,27 +13,33 @@ import shutil
from . import log from . import log
from .util import quote_sh_list from .util import quote_sh_list
__all__ = ['run_build', 'make_c_identifier', 'CMakeCacheEntry', 'CMakeCache'] __all__ = ['run_cmake', 'run_build',
'make_c_identifier',
'CMakeCacheEntry', 'CMakeCache']
DEFAULT_CACHE = 'CMakeCache.txt' DEFAULT_CACHE = 'CMakeCache.txt'
def run_build(build_directory, extra_args=[], quiet=False): def run_cmake(args, quiet=False):
'''Run cmake in build tool mode in `build_directory`''' '''Run cmake to (re)generate a build system'''
cmake = shutil.which('cmake') cmake = shutil.which('cmake')
if cmake is None: if cmake is None:
log.die('CMake is not installed or cannot be found; cannot build.') log.die('CMake is not installed or cannot be found; cannot build.')
cmd = [cmake, '--build', build_directory] + extra_args cmd = [cmake] + args
kwargs = {} kwargs = dict()
if quiet: if quiet:
kwargs['stdout'] = subprocess.DEVNULL kwargs['stdout'] = subprocess.DEVNULL
kwargs['stderr'] = subprocess.STDOUT kwargs['stderr'] = subprocess.STDOUT
log.dbg('Re-building', build_directory) log.dbg('Running CMake:', cmd, level=log.VERBOSE_VERY)
log.dbg('Build command list:', cmd, level=log.VERBOSE_VERY)
log.dbg('As command:', quote_sh_list(cmd), level=log.VERBOSE_VERY) log.dbg('As command:', quote_sh_list(cmd), level=log.VERBOSE_VERY)
subprocess.check_call(cmd, **kwargs) subprocess.check_call(cmd, **kwargs)
def run_build(build_directory, extra_args=(), quiet=False):
'''Run cmake in build tool mode in `build_directory`'''
run_cmake(['--build', build_directory] + list(extra_args), quiet=quiet)
def make_c_identifier(string): def make_c_identifier(string):
'''Make a C identifier from a string in the same way CMake does. '''Make a C identifier from a string in the same way CMake does.
''' '''
@ -154,6 +161,10 @@ class CMakeCacheEntry:
class CMakeCache: class CMakeCache:
'''Parses and represents a CMake cache file.''' '''Parses and represents a CMake cache file.'''
@staticmethod
def from_build_dir(build_dir):
return CMakeCache(os.path.join(build_dir, DEFAULT_CACHE))
def __init__(self, cache_file): def __init__(self, cache_file):
self.cache_file = cache_file self.cache_file = cache_file
self.load(cache_file) self.load(cache_file)
@ -190,6 +201,9 @@ class CMakeCache:
else: else:
return default return default
def __contains__(self, name):
return name in self._entries
def __getitem__(self, name): def __getitem__(self, name):
return self._entries[name].value return self._entries[name].value