diff options
| author | Charles Brunet <charles.brunet@optelgroup.com> | 2023-03-17 16:27:37 -0400 |
|---|---|---|
| committer | Eli Schwartz <eschwartz93@gmail.com> | 2023-08-31 07:52:41 -0400 |
| commit | cf5adf0c646474f0259d123fad60ca5ed38ec891 (patch) | |
| tree | 85ccd32aa73eecf274a937f1fc3b6f4d484b77da /mesonbuild/utils/universal.py | |
| parent | cd30d1889f2bae34f17231b9cabe665edd57e7ba (diff) | |
| download | meson-cf5adf0c646474f0259d123fad60ca5ed38ec891.tar.gz | |
add json output format to configure file
Diffstat (limited to 'mesonbuild/utils/universal.py')
| -rw-r--r-- | mesonbuild/utils/universal.py | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/mesonbuild/utils/universal.py b/mesonbuild/utils/universal.py index a39825faf..d44ec03a8 100644 --- a/mesonbuild/utils/universal.py +++ b/mesonbuild/utils/universal.py @@ -32,6 +32,7 @@ import textwrap import copy import pickle import errno +import json from mesonbuild import mlog from .core import MesonException, HoldableObject @@ -1361,34 +1362,43 @@ CONF_NASM_PRELUDE = '''; Autogenerated by the Meson build system. ''' -def dump_conf_header(ofilename: str, cdata: 'ConfigurationData', output_format: Literal['c', 'nasm']) -> None: +def _dump_c_header(ofile: T.TextIO, cdata: ConfigurationData, output_format: Literal['c', 'nasm']) -> None: + format_desc: T.Callable[[str], str] if output_format == 'c': prelude = CONF_C_PRELUDE prefix = '#' - else: + format_desc = lambda desc: f'/* {desc} */\n' + else: # nasm prelude = CONF_NASM_PRELUDE prefix = '%' + format_desc = lambda desc: '; ' + '\n; '.join(desc.splitlines()) + '\n' + + ofile.write(prelude) + for k in sorted(cdata.keys()): + (v, desc) = cdata.get(k) + if desc: + ofile.write(format_desc(desc)) + if isinstance(v, bool): + if v: + ofile.write(f'{prefix}define {k}\n\n') + else: + ofile.write(f'{prefix}undef {k}\n\n') + elif isinstance(v, (int, str)): + ofile.write(f'{prefix}define {k} {v}\n\n') + else: + raise MesonException('Unknown data type in configuration file entry: ' + k) + +def dump_conf_header(ofilename: str, cdata: ConfigurationData, + output_format: Literal['c', 'nasm', 'json']) -> None: ofilename_tmp = ofilename + '~' with open(ofilename_tmp, 'w', encoding='utf-8') as ofile: - ofile.write(prelude) - for k in sorted(cdata.keys()): - (v, desc) = cdata.get(k) - if desc: - if output_format == 'c': - ofile.write('/* %s */\n' % desc) - elif output_format == 'nasm': - for line in desc.split('\n'): - ofile.write('; %s\n' % line) - if isinstance(v, bool): - if v: - ofile.write(f'{prefix}define {k}\n\n') - else: - ofile.write(f'{prefix}undef {k}\n\n') - elif isinstance(v, (int, str)): - ofile.write(f'{prefix}define {k} {v}\n\n') - else: - raise MesonException('Unknown data type in configuration file entry: ' + k) + 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) + replace_if_different(ofilename, ofilename_tmp) |
