diff options
| author | Dylan Baker <dylan@pnwbakers.com> | 2025-01-06 15:15:44 -0800 |
|---|---|---|
| committer | Dylan Baker <dylan@pnwbakers.com> | 2025-01-08 10:04:43 -0800 |
| commit | 9f0ac314ba0c54cc18c2499845324efc14c1849e (patch) | |
| tree | 0de4a06b9f1e4c5a2f3ea8498b2a29a153552408 | |
| parent | 55e3a5ece5d90ea29dcb8ab2d0bb6898a71e8971 (diff) | |
| download | meson-9f0ac314ba0c54cc18c2499845324efc14c1849e.tar.gz | |
templates: re-use existing template code for existing libraries
This reduces duplication around the meson version, default options, etc.
It also allows us to use the default initialization for libraries
| -rw-r--r-- | mesonbuild/templates/mesontemplates.py | 72 |
1 files changed, 7 insertions, 65 deletions
diff --git a/mesonbuild/templates/mesontemplates.py b/mesonbuild/templates/mesontemplates.py index 5fe73930f..23269392f 100644 --- a/mesonbuild/templates/mesontemplates.py +++ b/mesonbuild/templates/mesontemplates.py @@ -6,73 +6,15 @@ from __future__ import annotations import typing as T +from .samplefactory import sample_generator + if T.TYPE_CHECKING: from ..minit import Arguments -meson_executable_template = '''project( - '{project_name}', - {language}, - version : '{version}', - meson_version : '>= {meson_version}', - default_options : [{default_options}], -) - -executable( - '{executable}', - {sourcespec},{depspec} - install : true, -) - -''' - - -meson_jar_template = '''project( - '{project_name}', - '{language}', - version : '{version}', - meson_version : '>= {meson_version}', - default_options : [{default_options}], -) - -jar( - '{executable}', - {sourcespec},{depspec} - main_class : '{main_class}', - install : true, -) - -''' - def create_meson_build(options: Arguments) -> None: - if options.type != 'executable': - raise SystemExit('\nGenerating a meson.build file from existing sources is\n' - 'supported only for project type "executable".\n' - 'Run meson init in an empty directory to create a sample project.') - default_options = ['warning_level=3'] - if options.language == 'cpp': - # This shows how to set this very common option. - default_options += ['cpp_std=c++14'] - # If we get a meson.build autoformatter one day, this code could - # be simplified quite a bit. - formatted_default_options = ', '.join(f"'{x}'" for x in default_options) - sourcespec = ',\n '.join(f"'{x}'" for x in options.srcfiles) - depspec = '' - if options.deps: - depspec = '\n dependencies : [\n ' - depspec += ',\n '.join(f"dependency('{x}')" - for x in options.deps.split(',')) - depspec += '],' - tmpl = meson_executable_template if options.language != 'java' else meson_jar_template - content = tmpl.format(project_name=options.name, - language=options.language, - version=options.version, - meson_version='1.0.0' if options.language != 'rust' else '1.3.0', - main_class=options.name, - executable=options.executable, - sourcespec=sourcespec, - depspec=depspec, - default_options=formatted_default_options) - with open('meson.build', 'w', encoding='utf-8') as f: - f.write(content) - print('Generated meson.build file:\n\n' + content) + proj = sample_generator(options) + if options.type == 'executable': + proj.create_executable() + else: + proj.create_library() |
