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/utils/universal.py | |
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/utils/universal.py')
-rw-r--r-- | mesonbuild/utils/universal.py | 19 |
1 files changed, 14 insertions, 5 deletions
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) |