aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild
diff options
context:
space:
mode:
authorDavid Fort <contact@hardening-consulting.com>2018-02-21 10:43:56 +0100
committerJussi Pakkanen <jpakkane@gmail.com>2018-04-08 22:00:45 +0300
commit6dea1777743fe26173d9c674b0870790a7caae92 (patch)
tree115139f40952d714faed6cf8248b771137f289ea /mesonbuild
parentaed11affd3a68ec22a0527d82457c8ff365639ec (diff)
downloadmeson-6dea1777743fe26173d9c674b0870790a7caae92.zip
meson-6dea1777743fe26173d9c674b0870790a7caae92.tar.gz
meson-6dea1777743fe26173d9c674b0870790a7caae92.tar.bz2
add support for cmakedefine in configure_file()
The added format argument for configure_file allows to specify the kind of file that is treated. It defaults to 'meson', but can also have the 'cmake' or 'cmake@' value to treat config.h.in files in the cmake format with #cmakedefine statements.
Diffstat (limited to 'mesonbuild')
-rw-r--r--mesonbuild/interpreter.py14
-rw-r--r--mesonbuild/mesonlib.py29
2 files changed, 34 insertions, 9 deletions
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py
index 6e3b864..6ecd285 100644
--- a/mesonbuild/interpreter.py
+++ b/mesonbuild/interpreter.py
@@ -1463,7 +1463,7 @@ permitted_kwargs = {'add_global_arguments': {'language'},
'add_test_setup': {'exe_wrapper', 'gdb', 'timeout_multiplier', 'env'},
'benchmark': {'args', 'env', 'should_fail', 'timeout', 'workdir', 'suite'},
'build_target': known_build_target_kwargs,
- 'configure_file': {'input', 'output', 'configuration', 'command', 'install_dir', 'capture', 'install'},
+ 'configure_file': {'input', 'output', 'configuration', 'command', 'install_dir', 'capture', 'install', 'format'},
'custom_target': {'input', 'output', 'command', 'install', 'install_dir', 'build_always', 'capture', 'depends', 'depend_files', 'depfile', 'build_by_default'},
'dependency': {'default_options', 'fallback', 'language', 'main', 'method', 'modules', 'optional_modules', 'native', 'required', 'static', 'version'},
'declare_dependency': {'include_directories', 'link_with', 'sources', 'dependencies', 'compile_args', 'link_args', 'link_whole', 'version'},
@@ -2885,6 +2885,16 @@ root and issuing %s.
if 'command' not in kwargs:
raise InterpreterException('"capture" keyword requires "command" keyword.')
+ if 'format' in kwargs:
+ format = kwargs['format']
+ if not isinstance(format, str):
+ raise InterpreterException('"format" keyword must be a string.')
+ else:
+ format = 'meson'
+
+ if format not in ('meson', 'cmake', 'cmake@'):
+ raise InterpreterException('"format" possible values are "meson", "cmake" or "cmake@".')
+
# Validate input
inputfile = None
ifile_abs = None
@@ -2924,7 +2934,7 @@ root and issuing %s.
if inputfile is not None:
os.makedirs(os.path.join(self.environment.build_dir, self.subdir), exist_ok=True)
missing_variables = mesonlib.do_conf_file(ifile_abs, ofile_abs,
- conf.held_object)
+ conf.held_object, format)
if missing_variables:
var_list = ", ".join(map(repr, sorted(missing_variables)))
mlog.warning(
diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py
index a076e3e..9b00c28 100644
--- a/mesonbuild/mesonlib.py
+++ b/mesonbuild/mesonlib.py
@@ -541,8 +541,13 @@ def has_path_sep(name, sep='/\\'):
return True
return False
-def do_replacement(regex, line, confdata):
+def do_replacement(regex, line, format, confdata):
missing_variables = set()
+ start_tag = '@'
+ backslash_tag = '\\@'
+ if format == 'cmake':
+ start_tag = '${'
+ backslash_tag = '\\${'
def variable_replace(match):
# Pairs of escape characters before '@' or '\@'
@@ -550,8 +555,8 @@ def do_replacement(regex, line, confdata):
num_escapes = match.end(0) - match.start(0)
return '\\' * (num_escapes // 2)
# Single escape character and '@'
- elif match.group(0) == '\\@':
- return '@'
+ elif match.group(0) == backslash_tag:
+ return start_tag
# Template variable to be replaced
else:
varname = match.group(1)
@@ -591,7 +596,7 @@ def do_mesondefine(line, confdata):
raise MesonException('#mesondefine argument "%s" is of unknown type.' % varname)
-def do_conf_file(src, dst, confdata):
+def do_conf_file(src, dst, confdata, format):
try:
with open(src, encoding='utf-8') as f:
data = f.readlines()
@@ -599,14 +604,24 @@ def do_conf_file(src, dst, confdata):
raise MesonException('Could not read input file %s: %s' % (src, str(e)))
# Only allow (a-z, A-Z, 0-9, _, -) as valid characters for a define
# Also allow escaping '@' with '\@'
- regex = re.compile(r'(?:\\\\)+(?=\\?@)|\\@|@([-a-zA-Z0-9_]+)@')
+ if format in ['meson', 'cmake@']:
+ regex = re.compile(r'(?:\\\\)+(?=\\?@)|\\@|@([-a-zA-Z0-9_]+)@')
+ elif format == 'cmake':
+ regex = re.compile(r'(?:\\\\)+(?=\\?\$)|\\\${|\${([-a-zA-Z0-9_]+)}')
+ else:
+ raise MesonException('Format "{}" not handled'.format(format))
+
+ search_token = '#mesondefine'
+ if format != 'meson':
+ search_token = '#cmakedefine'
+
result = []
missing_variables = set()
for line in data:
- if line.startswith('#mesondefine'):
+ if line.startswith(search_token):
line = do_mesondefine(line, confdata)
else:
- line, missing = do_replacement(regex, line, confdata)
+ line, missing = do_replacement(regex, line, format, confdata)
missing_variables.update(missing)
result.append(line)
dst_tmp = dst + '~'