diff options
author | Nicholas Vinson <nvinson234@gmail.com> | 2023-09-04 13:04:00 -0400 |
---|---|---|
committer | Xavier Claessens <xclaesse@gmail.com> | 2023-09-09 07:30:56 -0400 |
commit | b51bce070eead2b65e56f087acf23829a6304ae2 (patch) | |
tree | 57ce789fe3fce4ddb8fbd98d33d2cf1d697166f2 /mesonbuild | |
parent | 3c47216fe945a45834daa38f71e287dcfaf345c7 (diff) | |
download | meson-b51bce070eead2b65e56f087acf23829a6304ae2.zip meson-b51bce070eead2b65e56f087acf23829a6304ae2.tar.gz meson-b51bce070eead2b65e56f087acf23829a6304ae2.tar.bz2 |
Add macro_name option to configure_file
Allow macro_name to be speficied as a parameter to configure_file().
This allows C macro-style include guards to be added to
configure_file()'s output when a template file is not given. This change
simplifies the creation of configure files that define macros with
dynamic names and want the C-style include guards.
Diffstat (limited to 'mesonbuild')
-rw-r--r-- | mesonbuild/interpreter/interpreter.py | 4 | ||||
-rw-r--r-- | mesonbuild/interpreter/kwargs.py | 1 | ||||
-rw-r--r-- | mesonbuild/utils/universal.py | 19 |
3 files changed, 18 insertions, 6 deletions
diff --git a/mesonbuild/interpreter/interpreter.py b/mesonbuild/interpreter/interpreter.py index d9a260c..9b005cb 100644 --- a/mesonbuild/interpreter/interpreter.py +++ b/mesonbuild/interpreter/interpreter.py @@ -2585,6 +2585,7 @@ class Interpreter(InterpreterBase, HoldableObject): OUTPUT_KW, KwargInfo('output_format', str, default='c', since='0.47.0', since_values={'json': '1.3.0'}, validator=in_set_validator({'c', 'json', 'nasm'})), + KwargInfo('macro_name', (str, NoneType), default=None, since='1.3.0'), ) def func_configure_file(self, node: mparser.BaseNode, args: T.List[TYPE_var], kwargs: kwtypes.ConfigureFile): @@ -2676,7 +2677,8 @@ class Interpreter(InterpreterBase, HoldableObject): 'copy a file to the build dir, use the \'copy:\' keyword ' 'argument added in 0.47.0', location=node) else: - mesonlib.dump_conf_header(ofile_abs, conf, output_format) + macro_name = kwargs['macro_name'] + mesonlib.dump_conf_header(ofile_abs, conf, output_format, macro_name) conf.used = True elif kwargs['command'] is not None: if len(inputs) > 1: diff --git a/mesonbuild/interpreter/kwargs.py b/mesonbuild/interpreter/kwargs.py index 0aee164..1aee414 100644 --- a/mesonbuild/interpreter/kwargs.py +++ b/mesonbuild/interpreter/kwargs.py @@ -296,6 +296,7 @@ class ConfigureFile(TypedDict): command: T.Optional[T.List[T.Union[build.Executable, ExternalProgram, Compiler, File, str]]] input: T.List[FileOrString] configuration: T.Optional[T.Union[T.Dict[str, T.Union[str, int, bool]], build.ConfigurationData]] + macro_name: T.Optional[str] class Subproject(ExtractRequired): diff --git a/mesonbuild/utils/universal.py b/mesonbuild/utils/universal.py index 7f0f385..1d0e382 100644 --- a/mesonbuild/utils/universal.py +++ b/mesonbuild/utils/universal.py @@ -1351,7 +1351,7 @@ CONF_C_PRELUDE = '''/* * Do not edit, your changes will be lost. */ -#pragma once +{} ''' @@ -1360,10 +1360,16 @@ CONF_NASM_PRELUDE = '''; Autogenerated by the Meson build system. ''' -def _dump_c_header(ofile: T.TextIO, cdata: ConfigurationData, output_format: Literal['c', 'nasm']) -> None: +def _dump_c_header(ofile: T.TextIO, + cdata: ConfigurationData, + output_format: Literal['c', 'nasm'], + macro_name: T.Optional[str]) -> None: format_desc: T.Callable[[str], str] if output_format == 'c': - prelude = CONF_C_PRELUDE + if macro_name: + prelude = CONF_C_PRELUDE.format('#ifndef {0}\n#define {0}'.format(macro_name)) + else: + prelude = CONF_C_PRELUDE.format('#pragma once') prefix = '#' format_desc = lambda desc: f'/* {desc} */\n' else: # nasm @@ -1385,17 +1391,20 @@ def _dump_c_header(ofile: T.TextIO, cdata: ConfigurationData, output_format: Lit ofile.write(f'{prefix}define {k} {v}\n\n') else: raise MesonException('Unknown data type in configuration file entry: ' + k) + if output_format == 'c' and macro_name: + ofile.write('#endif\n') def dump_conf_header(ofilename: str, cdata: ConfigurationData, - output_format: Literal['c', 'nasm', 'json']) -> None: + output_format: Literal['c', 'nasm', 'json'], + macro_name: T.Optional[str]) -> None: ofilename_tmp = ofilename + '~' with open(ofilename_tmp, 'w', encoding='utf-8') as ofile: if output_format == 'json': data = {k: v[0] for k, v in cdata.values.items()} json.dump(data, ofile, sort_keys=True) else: # c, nasm - _dump_c_header(ofile, cdata, output_format) + _dump_c_header(ofile, cdata, output_format, macro_name) replace_if_different(ofilename, ofilename_tmp) |