summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mesonbuild/templates/cpptemplates.py66
-rw-r--r--mesonbuild/templates/cstemplates.py51
-rw-r--r--mesonbuild/templates/ctemplates.py66
-rw-r--r--mesonbuild/templates/cudatemplates.py67
-rw-r--r--mesonbuild/templates/dlangtemplates.py62
-rw-r--r--mesonbuild/templates/fortrantemplates.py66
-rw-r--r--mesonbuild/templates/javatemplates.py51
-rw-r--r--mesonbuild/templates/mesontemplates.py64
-rw-r--r--mesonbuild/templates/objcpptemplates.py66
-rw-r--r--mesonbuild/templates/objctemplates.py65
-rw-r--r--mesonbuild/templates/rusttemplates.py58
-rw-r--r--mesonbuild/templates/samplefactory.py1
-rw-r--r--mesonbuild/templates/sampleimpl.py103
-rw-r--r--mesonbuild/templates/valatemplates.py62
14 files changed, 590 insertions, 258 deletions
diff --git a/mesonbuild/templates/cpptemplates.py b/mesonbuild/templates/cpptemplates.py
index 70e4dd42b..eead3cf05 100644
--- a/mesonbuild/templates/cpptemplates.py
+++ b/mesonbuild/templates/cpptemplates.py
@@ -1,10 +1,14 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
+import typing as T
from mesonbuild.templates.sampleimpl import FileHeaderImpl
+if T.TYPE_CHECKING:
+ from ..minit import Arguments
hello_cpp_template = '''#include <iostream>
@@ -20,13 +24,23 @@ int main(int argc, char **argv) {{
}}
'''
-hello_cpp_meson_template = '''project('{project_name}', 'cpp',
+hello_cpp_meson_template = '''project(
+ '{project_name}',
+ 'cpp',
version : '{version}',
- default_options : ['warning_level=3',
- 'cpp_std=c++14'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3', 'cpp_std=c++14'],
+)
-exe = executable('{exe_name}', '{source_name}',
- install : true)
+dependencies = [{dependencies}
+]
+
+exe = executable(
+ '{exe_name}',
+ '{source_name}',
+ install : true,
+ dependencies : dependencies,
+)
test('basic', exe)
'''
@@ -92,28 +106,45 @@ int main(int argc, char **argv) {{
}}
'''
-lib_cpp_meson_template = '''project('{project_name}', 'cpp',
+lib_cpp_meson_template = '''project(
+ '{project_name}',
+ 'cpp',
version : '{version}',
- default_options : ['warning_level=3', 'cpp_std=c++14'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3', 'cpp_std=c++14'],
+)
+
+dependencies = [{dependencies}
+]
# These arguments are only used to build the shared library
# not the executables that use the library.
lib_args = ['-DBUILDING_{utoken}']
-shlib = shared_library('{lib_name}', '{source_file}',
+lib = library(
+ '{lib_name}',
+ '{source_file}',
install : true,
- cpp_args : lib_args,
+ cpp_shared_args : lib_args,
gnu_symbol_visibility : 'hidden',
+ dependencies : dependencies,
)
-test_exe = executable('{test_exe_name}', '{test_source_file}',
- link_with : shlib)
+test_exe = executable(
+ '{test_exe_name}',
+ '{test_source_file}',
+ dependencies : dependencies,
+ link_with : lib,
+)
test('{test_name}', test_exe)
# Make this library usable as a Meson subproject.
{ltoken}_dep = declare_dependency(
- include_directories: include_directories('.'),
- link_with : shlib)
+ include_directories : include_directories('.'),
+ dependencies : dependencies,
+ link_with : lib,
+)
+meson.override_dependency('{project_name}', {ltoken}_dep)
# Make this library usable from the system's
# package manager.
@@ -121,12 +152,9 @@ install_headers('{header_file}', subdir : '{header_dir}')
pkg_mod = import('pkgconfig')
pkg_mod.generate(
- name : '{project_name}',
- filebase : '{ltoken}',
+ lib,
description : 'Meson sample project.',
subdirs : '{header_dir}',
- libraries : shlib,
- version : '{version}',
)
'''
@@ -141,3 +169,7 @@ class CppProject(FileHeaderImpl):
lib_header_template = lib_hpp_template
lib_test_template = lib_cpp_test_template
lib_meson_template = lib_cpp_meson_template
+
+ def __init__(self, args: Arguments):
+ super().__init__(args)
+ self.meson_version = '1.3.0'
diff --git a/mesonbuild/templates/cstemplates.py b/mesonbuild/templates/cstemplates.py
index 4b16b7265..59c718953 100644
--- a/mesonbuild/templates/cstemplates.py
+++ b/mesonbuild/templates/cstemplates.py
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
@@ -23,12 +24,23 @@ public class {class_name} {{
'''
-hello_cs_meson_template = '''project('{project_name}', 'cs',
+hello_cs_meson_template = '''project(
+ '{project_name}',
+ 'cs',
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
+
+dependencies = [{dependencies}
+]
-exe = executable('{exe_name}', '{source_name}',
- install : true)
+exe = executable(
+ '{exe_name}',
+ '{source_name}',
+ install : true,
+ dependencies : dependencies,
+)
test('basic', exe)
'''
@@ -60,22 +72,39 @@ public class {class_test} {{
'''
-lib_cs_meson_template = '''project('{project_name}', 'cs',
+lib_cs_meson_template = '''project(
+ '{project_name}',
+ 'cs',
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
+
+dependencies = [{dependencies}
+]
-stlib = shared_library('{lib_name}', '{source_file}',
+stlib = shared_library(
+ '{lib_name}',
+ '{source_file}',
+ dependencies : dependencies,
install : true,
)
-test_exe = executable('{test_exe_name}', '{test_source_file}',
- link_with : stlib)
+test_exe = executable(
+ '{test_exe_name}',
+ '{test_source_file}',
+ dependencies : dependencies,
+ link_with : stlib,
+)
test('{test_name}', test_exe)
# Make this library usable as a Meson subproject.
{ltoken}_dep = declare_dependency(
- include_directories: include_directories('.'),
- link_with : stlib)
+ include_directories : include_directories('.'),
+ dependencies : dependencies,
+ link_with : stlib,
+)
+meson.override_dependency('{project_name}', {ltoken}_dep)
'''
diff --git a/mesonbuild/templates/ctemplates.py b/mesonbuild/templates/ctemplates.py
index d7616054a..7cf04f6aa 100644
--- a/mesonbuild/templates/ctemplates.py
+++ b/mesonbuild/templates/ctemplates.py
@@ -1,10 +1,15 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
+import typing as T
from mesonbuild.templates.sampleimpl import FileHeaderImpl
+if T.TYPE_CHECKING:
+ from ..minit import Arguments
+
lib_h_template = '''#pragma once
#if defined _WIN32 || defined __CYGWIN__
@@ -51,28 +56,45 @@ int main(int argc, char **argv) {{
}}
'''
-lib_c_meson_template = '''project('{project_name}', 'c',
+lib_c_meson_template = '''project(
+ '{project_name}',
+ 'c',
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
# These arguments are only used to build the shared library
# not the executables that use the library.
lib_args = ['-DBUILDING_{utoken}']
-shlib = shared_library('{lib_name}', '{source_file}',
+dependencies = [{dependencies}
+]
+
+lib = library(
+ '{lib_name}',
+ '{source_file}',
install : true,
- c_args : lib_args,
+ c_shared_args : lib_args,
gnu_symbol_visibility : 'hidden',
+ dependencies : dependencies,
)
-test_exe = executable('{test_exe_name}', '{test_source_file}',
- link_with : shlib)
+test_exe = executable(
+ '{test_exe_name}',
+ '{test_source_file}',
+ dependencies : dependencies,
+ link_with : lib,
+)
test('{test_name}', test_exe)
# Make this library usable as a Meson subproject.
{ltoken}_dep = declare_dependency(
- include_directories: include_directories('.'),
- link_with : shlib)
+ include_directories : include_directories('.'),
+ dependencies : dependencies,
+ link_with : lib,
+)
+meson.override_dependency('{project_name}', {ltoken}_dep)
# Make this library usable from the system's
# package manager.
@@ -80,12 +102,9 @@ install_headers('{header_file}', subdir : '{header_dir}')
pkg_mod = import('pkgconfig')
pkg_mod.generate(
- name : '{project_name}',
- filebase : '{ltoken}',
+ lib,
description : 'Meson sample project.',
subdirs : '{header_dir}',
- libraries : shlib,
- version : '{version}',
)
'''
@@ -103,12 +122,23 @@ int main(int argc, char **argv) {{
}}
'''
-hello_c_meson_template = '''project('{project_name}', 'c',
+hello_c_meson_template = '''project(
+ '{project_name}',
+ 'c',
+ meson_version : '>= {meson_version}',
version : '{version}',
- default_options : ['warning_level=3'])
+ default_options : ['warning_level=3'],
+)
-exe = executable('{exe_name}', '{source_name}',
- install : true)
+dependencies = [{dependencies}
+]
+
+exe = executable(
+ '{exe_name}',
+ '{source_name}',
+ dependencies : dependencies,
+ install : true,
+)
test('basic', exe)
'''
@@ -124,3 +154,7 @@ class CProject(FileHeaderImpl):
lib_header_template = lib_h_template
lib_test_template = lib_c_test_template
lib_meson_template = lib_c_meson_template
+
+ def __init__(self, args: Arguments):
+ super().__init__(args)
+ self.meson_version = '1.3.0'
diff --git a/mesonbuild/templates/cudatemplates.py b/mesonbuild/templates/cudatemplates.py
index 12eefa5a8..416ba376a 100644
--- a/mesonbuild/templates/cudatemplates.py
+++ b/mesonbuild/templates/cudatemplates.py
@@ -1,10 +1,15 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
+import typing as T
from mesonbuild.templates.sampleimpl import FileHeaderImpl
+if T.TYPE_CHECKING:
+ from ..minit import Arguments
+
hello_cuda_template = '''#include <iostream>
@@ -20,13 +25,23 @@ int main(int argc, char **argv) {{
}}
'''
-hello_cuda_meson_template = '''project('{project_name}', ['cuda', 'cpp'],
+hello_cuda_meson_template = '''project(
+ '{project_name}',
+ ['cuda', 'cpp'],
version : '{version}',
- default_options : ['warning_level=3',
- 'cpp_std=c++14'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3', 'cpp_std=c++14'],
+)
+
+dependencies = [{dependencies}
+]
-exe = executable('{exe_name}', '{source_name}',
- install : true)
+exe = executable(
+ '{exe_name}',
+ '{source_name}',
+ dependencies : dependencies,
+ install : true,
+)
test('basic', exe)
'''
@@ -92,28 +107,45 @@ int main(int argc, char **argv) {{
}}
'''
-lib_cuda_meson_template = '''project('{project_name}', ['cuda', 'cpp'],
+lib_cuda_meson_template = '''project(
+ '{project_name}',
+ ['cuda', 'cpp'],
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
# These arguments are only used to build the shared library
# not the executables that use the library.
lib_args = ['-DBUILDING_{utoken}']
-shlib = shared_library('{lib_name}', '{source_file}',
+dependencies = [{dependencies}
+]
+
+lib = library(
+ '{lib_name}',
+ '{source_file}',
install : true,
- cpp_args : lib_args,
+ cpp_shared_args : lib_args,
gnu_symbol_visibility : 'hidden',
+ dependencies : dependencies,
)
-test_exe = executable('{test_exe_name}', '{test_source_file}',
- link_with : shlib)
+test_exe = executable(
+ '{test_exe_name}',
+ '{test_source_file}',
+ link_with : lib,
+ dependencies : dependencies,
+)
test('{test_name}', test_exe)
# Make this library usable as a Meson subproject.
{ltoken}_dep = declare_dependency(
- include_directories: include_directories('.'),
- link_with : shlib)
+ include_directories : include_directories('.'),
+ dependencies : dependencies,
+ link_with : lib,
+)
+meson.override_dependency('{project_name}', {ltoken}_dep)
# Make this library usable from the system's
# package manager.
@@ -121,12 +153,9 @@ install_headers('{header_file}', subdir : '{header_dir}')
pkg_mod = import('pkgconfig')
pkg_mod.generate(
- name : '{project_name}',
- filebase : '{ltoken}',
+ lib,
description : 'Meson sample project.',
subdirs : '{header_dir}',
- libraries : shlib,
- version : '{version}',
)
'''
@@ -141,3 +170,7 @@ class CudaProject(FileHeaderImpl):
lib_header_template = lib_h_template
lib_test_template = lib_cuda_test_template
lib_meson_template = lib_cuda_meson_template
+
+ def __init__(self, args: Arguments):
+ super().__init__(args)
+ self.meson_version = '1.3.0'
diff --git a/mesonbuild/templates/dlangtemplates.py b/mesonbuild/templates/dlangtemplates.py
index 2e9a32915..db3bdbf16 100644
--- a/mesonbuild/templates/dlangtemplates.py
+++ b/mesonbuild/templates/dlangtemplates.py
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
@@ -23,12 +24,23 @@ int main(string[] args) {{
}}
'''
-hello_d_meson_template = '''project('{project_name}', 'd',
- version : '{version}',
- default_options: ['warning_level=3'])
+hello_d_meson_template = '''project(
+ '{project_name}',
+ 'd',
+ version : '{version}',
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
-exe = executable('{exe_name}', '{source_name}',
- install : true)
+dependencies = [{dependencies}
+]
+
+exe = executable(
+ '{exe_name}',
+ '{source_name}',
+ dependencies : dependencies,
+ install : true,
+)
test('basic', exe)
'''
@@ -61,31 +73,51 @@ int main(string[] args) {{
}}
'''
-lib_d_meson_template = '''project('{project_name}', 'd',
+lib_d_meson_template = '''project(
+ '{project_name}',
+ 'd',
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
+
+dependencies = [{dependencies}
+]
+
-stlib = static_library('{lib_name}', '{source_file}',
+stlib = static_library(
+ '{lib_name}',
+ '{source_file}',
install : true,
gnu_symbol_visibility : 'hidden',
+ dependencies : dependencies,
)
-test_exe = executable('{test_exe_name}', '{test_source_file}',
- link_with : stlib)
+test_exe = executable(
+ '{test_exe_name}',
+ '{test_source_file}',
+ link_with : stlib,
+ dependencies : dependencies,
+)
test('{test_name}', test_exe)
# Make this library usable as a Meson subproject.
{ltoken}_dep = declare_dependency(
- include_directories: include_directories('.'),
- link_with : stlib)
+ include_directories : include_directories('.'),
+ dependencies : dependencies,
+ link_with : stlib,
+)
+meson.override_dependency('{project_name}', {ltoken}_dep)
# Make this library usable from the Dlang
# build system.
dlang_mod = import('dlang')
-if find_program('dub', required: false).found()
- dlang_mod.generate_dub_file(meson.project_name().to_lower(), meson.source_root(),
+if find_program('dub', required : false).found()
+ dlang_mod.generate_dub_file(
+ meson.project_name().to_lower(),
+ meson.source_root(),
name : meson.project_name(),
- license: meson.project_license(),
+ license : meson.project_license(),
sourceFiles : '{source_file}',
description : 'Meson sample project.',
version : '{version}',
diff --git a/mesonbuild/templates/fortrantemplates.py b/mesonbuild/templates/fortrantemplates.py
index 9ac001564..7aaa9d39c 100644
--- a/mesonbuild/templates/fortrantemplates.py
+++ b/mesonbuild/templates/fortrantemplates.py
@@ -1,10 +1,15 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
+import typing as T
from mesonbuild.templates.sampleimpl import FileImpl
+if T.TYPE_CHECKING:
+ from ..minit import Arguments
+
lib_fortran_template = '''
! This procedure will not be exported and is not
! directly callable by users of this library.
@@ -36,37 +41,51 @@ print *,{function_name}()
end program
'''
-lib_fortran_meson_template = '''project('{project_name}', 'fortran',
+lib_fortran_meson_template = '''project(
+ '{project_name}',
+ 'fortran',
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
# These arguments are only used to build the shared library
# not the executables that use the library.
lib_args = ['-DBUILDING_{utoken}']
-shlib = shared_library('{lib_name}', '{source_file}',
+dependencies = [{dependencies}
+]
+
+lib = library(
+ '{lib_name}',
+ '{source_file}',
install : true,
- fortran_args : lib_args,
+ fortran_shared_args : lib_args,
gnu_symbol_visibility : 'hidden',
+ dependencies : dependencies,
)
-test_exe = executable('{test_exe_name}', '{test_source_file}',
- link_with : shlib)
+test_exe = executable(
+ '{test_exe_name}',
+ '{test_source_file}',
+ link_with : lib,
+ dependencies : dependencies,
+)
test('{test_name}', test_exe)
# Make this library usable as a Meson subproject.
{ltoken}_dep = declare_dependency(
- include_directories: include_directories('.'),
- link_with : shlib)
+ include_directories : include_directories('.'),
+ dependencies : dependencies,
+ link_with : lib,
+)
+meson.override_dependency('{project_name}', {ltoken}_dep)
pkg_mod = import('pkgconfig')
pkg_mod.generate(
- name : '{project_name}',
- filebase : '{ltoken}',
+ lib,
description : 'Meson sample project.',
subdirs : '{header_dir}',
- libraries : shlib,
- version : '{version}',
)
'''
@@ -80,12 +99,23 @@ print *,"This is project ", PROJECT_NAME
end program
'''
-hello_fortran_meson_template = '''project('{project_name}', 'fortran',
+hello_fortran_meson_template = '''project(
+ '{project_name}',
+ 'fortran',
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
-exe = executable('{exe_name}', '{source_name}',
- install : true)
+dependencies = [{dependencies}
+]
+
+exe = executable(
+ '{exe_name}',
+ '{source_name}',
+ dependencies : dependencies,
+ install : true,
+)
test('basic', exe)
'''
@@ -99,3 +129,7 @@ class FortranProject(FileImpl):
lib_template = lib_fortran_template
lib_meson_template = lib_fortran_meson_template
lib_test_template = lib_fortran_test_template
+
+ def __init__(self, args: Arguments):
+ super().__init__(args)
+ self.meson_version = '1.3.0'
diff --git a/mesonbuild/templates/javatemplates.py b/mesonbuild/templates/javatemplates.py
index e229d7add..a2b3ec4dc 100644
--- a/mesonbuild/templates/javatemplates.py
+++ b/mesonbuild/templates/javatemplates.py
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
@@ -23,13 +24,24 @@ public class {class_name} {{
'''
-hello_java_meson_template = '''project('{project_name}', 'java',
+hello_java_meson_template = '''project(
+ '{project_name}',
+ 'java',
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
+
+dependencies = [{dependencies}
+]
-exe = jar('{exe_name}', '{source_name}',
+exe = jar(
+ '{exe_name}',
+ '{source_name}',
main_class : '{exe_name}',
- install : true)
+ dependencies : dependencies,
+ install : true,
+)
test('basic', exe)
'''
@@ -63,24 +75,41 @@ public class {class_test} {{
'''
-lib_java_meson_template = '''project('{project_name}', 'java',
+lib_java_meson_template = '''project(
+ '{project_name}',
+ 'java',
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
+
+dependencies = [{dependencies}
+]
-jarlib = jar('{class_name}', '{source_file}',
+jarlib = jar(
+ '{class_name}',
+ '{source_file}',
+ dependencies : dependencies,
main_class : '{class_name}',
install : true,
)
-test_jar = jar('{class_test}', '{test_source_file}',
+test_jar = jar(
+ '{class_test}',
+ '{test_source_file}',
main_class : '{class_test}',
- link_with : jarlib)
+ dependencies : dependencies,
+ link_with : jarlib,
+)
test('{test_name}', test_jar)
# Make this library usable as a Meson subproject.
{ltoken}_dep = declare_dependency(
- include_directories: include_directories('.'),
- link_with : jarlib)
+ include_directories : include_directories('.'),
+ dependencies : dependencies,
+ link_with : jarlib,
+)
+meson.override_dependency('{project_name}', {ltoken}_dep)
'''
diff --git a/mesonbuild/templates/mesontemplates.py b/mesonbuild/templates/mesontemplates.py
index db553c09d..23269392f 100644
--- a/mesonbuild/templates/mesontemplates.py
+++ b/mesonbuild/templates/mesontemplates.py
@@ -1,70 +1,20 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
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}',
- default_options : [{default_options}])
-
-executable('{executable}',
- {sourcespec},{depspec}
- install : true)
-'''
-
-
-meson_jar_template = '''project('{project_name}', '{language}',
- version : '{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 += '],'
- if options.language != 'java':
- language = f"'{options.language}'" if options.language != 'vala' else ['c', 'vala']
- content = meson_executable_template.format(project_name=options.name,
- language=language,
- version=options.version,
- executable=options.executable,
- sourcespec=sourcespec,
- depspec=depspec,
- default_options=formatted_default_options)
+ proj = sample_generator(options)
+ if options.type == 'executable':
+ proj.create_executable()
else:
- content = meson_jar_template.format(project_name=options.name,
- language=options.language,
- version=options.version,
- executable=options.executable,
- main_class=options.name,
- sourcespec=sourcespec,
- depspec=depspec,
- default_options=formatted_default_options)
- open('meson.build', 'w', encoding='utf-8').write(content)
- print('Generated meson.build file:\n\n' + content)
+ proj.create_library()
diff --git a/mesonbuild/templates/objcpptemplates.py b/mesonbuild/templates/objcpptemplates.py
index 33bff2d79..45e70c6c9 100644
--- a/mesonbuild/templates/objcpptemplates.py
+++ b/mesonbuild/templates/objcpptemplates.py
@@ -1,10 +1,15 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
+import typing as T
from mesonbuild.templates.sampleimpl import FileHeaderImpl
+if T.TYPE_CHECKING:
+ from ..minit import Arguments
+
lib_h_template = '''#pragma once
#if defined _WIN32 || defined __CYGWIN__
@@ -51,28 +56,45 @@ int main(int argc, char **argv) {{
}}
'''
-lib_objcpp_meson_template = '''project('{project_name}', 'objcpp',
+lib_objcpp_meson_template = '''project(
+ '{project_name}',
+ 'objcpp',
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
+
+dependencies = [{dependencies}
+]
# These arguments are only used to build the shared library
# not the executables that use the library.
lib_args = ['-DBUILDING_{utoken}']
-shlib = shared_library('{lib_name}', '{source_file}',
+lib = library(
+ '{lib_name}',
+ '{source_file}',
install : true,
- objcpp_args : lib_args,
+ objcpp_shared_args : lib_args,
+ dependencies : dependencies,
gnu_symbol_visibility : 'hidden',
)
-test_exe = executable('{test_exe_name}', '{test_source_file}',
- link_with : shlib)
+test_exe = executable(
+ '{test_exe_name}',
+ '{test_source_file}',
+ dependencies : dependencies,
+ link_with : lib,
+)
test('{test_name}', test_exe)
# Make this library usable as a Meson subproject.
{ltoken}_dep = declare_dependency(
- include_directories: include_directories('.'),
- link_with : shlib)
+ include_directories : include_directories('.'),
+ dependencies : dependencies,
+ link_with : lib,
+)
+meson.override_dependency('{project_name}', {ltoken}_dep)
# Make this library usable from the system's
# package manager.
@@ -80,12 +102,9 @@ install_headers('{header_file}', subdir : '{header_dir}')
pkg_mod = import('pkgconfig')
pkg_mod.generate(
- name : '{project_name}',
- filebase : '{ltoken}',
+ lib,
description : 'Meson sample project.',
subdirs : '{header_dir}',
- libraries : shlib,
- version : '{version}',
)
'''
@@ -103,12 +122,23 @@ int main(int argc, char **argv) {{
}}
'''
-hello_objcpp_meson_template = '''project('{project_name}', 'objcpp',
+hello_objcpp_meson_template = '''project(
+ '{project_name}',
+ 'objcpp',
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
-exe = executable('{exe_name}', '{source_name}',
- install : true)
+dependencies = [{dependencies}
+]
+
+exe = executable(
+ '{exe_name}',
+ '{source_name}',
+ dependencies : dependencies,
+ install : true,
+)
test('basic', exe)
'''
@@ -124,3 +154,7 @@ class ObjCppProject(FileHeaderImpl):
lib_header_template = lib_h_template
lib_test_template = lib_objcpp_test_template
lib_meson_template = lib_objcpp_meson_template
+
+ def __init__(self, args: Arguments):
+ super().__init__(args)
+ self.meson_version = '1.3.0'
diff --git a/mesonbuild/templates/objctemplates.py b/mesonbuild/templates/objctemplates.py
index 8f46d91fd..0c7891fdb 100644
--- a/mesonbuild/templates/objctemplates.py
+++ b/mesonbuild/templates/objctemplates.py
@@ -1,10 +1,15 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
+import typing as T
from mesonbuild.templates.sampleimpl import FileHeaderImpl
+if T.TYPE_CHECKING:
+ from ..minit import Arguments
+
lib_h_template = '''#pragma once
#if defined _WIN32 || defined __CYGWIN__
@@ -51,28 +56,44 @@ int main(int argc, char **argv) {{
}}
'''
-lib_objc_meson_template = '''project('{project_name}', 'objc',
+lib_objc_meson_template = '''project(
+ '{project_name}',
+ 'objc',
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
+
+dependencies = [{dependencies}
+]
# These arguments are only used to build the shared library
# not the executables that use the library.
lib_args = ['-DBUILDING_{utoken}']
-shlib = shared_library('{lib_name}', '{source_file}',
+lib = library(
+ '{lib_name}',
+ '{source_file}',
install : true,
- objc_args : lib_args,
+ objc_shared_args : lib_args,
+ dependencies : dependencies,
gnu_symbol_visibility : 'hidden',
)
-test_exe = executable('{test_exe_name}', '{test_source_file}',
- link_with : shlib)
+test_exe = executable(
+ '{test_exe_name}',
+ '{test_source_file}',
+ dependencies : dependencies,
+ link_with : lib)
test('{test_name}', test_exe)
# Make this library usable as a Meson subproject.
{ltoken}_dep = declare_dependency(
- include_directories: include_directories('.'),
- link_with : shlib)
+ include_directories : include_directories('.'),
+ dependencies : dependencies,
+ link_with : lib,
+)
+meson.override_dependency('{project_name}', {ltoken}_dep)
# Make this library usable from the system's
# package manager.
@@ -80,12 +101,9 @@ install_headers('{header_file}', subdir : '{header_dir}')
pkg_mod = import('pkgconfig')
pkg_mod.generate(
- name : '{project_name}',
- filebase : '{ltoken}',
+ lib,
description : 'Meson sample project.',
subdirs : '{header_dir}',
- libraries : shlib,
- version : '{version}',
)
'''
@@ -103,12 +121,23 @@ int main(int argc, char **argv) {{
}}
'''
-hello_objc_meson_template = '''project('{project_name}', 'objc',
+hello_objc_meson_template = '''project(
+ '{project_name}',
+ 'objc',
version : '{version}',
- default_options : ['warning_level=3'])
+ meson_version : '>= {meson_version}',
+ default_options : ['warning_level=3'],
+)
-exe = executable('{exe_name}', '{source_name}',
- install : true)
+dependencies = [{dependencies}
+]
+
+exe = executable(
+ '{exe_name}',
+ '{source_name}',
+ dependencies : dependencies,
+ install : true,
+)
test('basic', exe)
'''
@@ -124,3 +153,7 @@ class ObjCProject(FileHeaderImpl):
lib_header_template = lib_h_template
lib_test_template = lib_objc_test_template
lib_meson_template = lib_objc_meson_template
+
+ def __init__(self, args: Arguments):
+ super().__init__(args)
+ self.meson_version = '1.3.0'
diff --git a/mesonbuild/templates/rusttemplates.py b/mesonbuild/templates/rusttemplates.py
index 1dbf5b614..ee1f0081d 100644
--- a/mesonbuild/templates/rusttemplates.py
+++ b/mesonbuild/templates/rusttemplates.py
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
@@ -7,6 +8,9 @@ import typing as T
from mesonbuild.templates.sampleimpl import FileImpl
+if T.TYPE_CHECKING:
+ from ..minit import Arguments
+
lib_rust_template = '''#![crate_name = "{crate_file}"]
@@ -33,20 +37,35 @@ mod tests {{
'''
-lib_rust_meson_template = '''project('{project_name}', 'rust',
- version : '{version}', meson_version: '>=1.3.0',
- default_options : ['rust_std=2021', 'warning_level=3'])
+lib_rust_meson_template = '''project(
+ '{project_name}',
+ 'rust',
+ version : '{version}',
+ meson_version : '>= {meson_version}',
+ default_options : ['rust_std=2021', 'warning_level=3'],
+)
rust = import('rust')
-shlib = static_library('{lib_name}', '{source_file}', install : true)
+dependencies = [{dependencies}
+]
+
+lib = static_library(
+ '{lib_name}',
+ '{source_file}',
+ dependencies : dependencies,
+ install : true,
+)
-rust.test('{test_name}', shlib)
+rust.test('{test_name}', lib)
# Make this library usable as a Meson subproject.
{ltoken}_dep = declare_dependency(
- include_directories: include_directories('.'),
- link_with : shlib)
+ include_directories : include_directories('.'),
+ dependencies : dependencies,
+ link_with : lib,
+)
+meson.override_dependency('{project_name}', {ltoken}_dep)
'''
hello_rust_template = '''
@@ -56,12 +75,23 @@ fn main() {{
}}
'''
-hello_rust_meson_template = '''project('{project_name}', 'rust',
- version : '{version}', meson_version: '>=1.3.0',
- default_options : ['rust_std=2021', 'warning_level=3'])
+hello_rust_meson_template = '''project(
+ '{project_name}',
+ 'rust',
+ version : '{version}',
+ meson_version : '>= {meson_version}',
+ default_options : ['rust_std=2021', 'warning_level=3'],
+)
-exe = executable('{exe_name}', '{source_name}',
- install : true)
+dependencies = [{dependencies}
+]
+
+exe = executable(
+ '{exe_name}',
+ '{source_name}',
+ dependencies : dependencies,
+ install : true,
+)
test('basic', exe)
'''
@@ -76,6 +106,10 @@ class RustProject(FileImpl):
lib_test_template = None
lib_meson_template = lib_rust_meson_template
+ def __init__(self, args: Arguments):
+ super().__init__(args)
+ self.meson_version = '1.3.0'
+
def lib_kwargs(self) -> T.Dict[str, str]:
kwargs = super().lib_kwargs()
kwargs['crate_file'] = self.lowercase_token
diff --git a/mesonbuild/templates/samplefactory.py b/mesonbuild/templates/samplefactory.py
index 0083c614a..438f90c9a 100644
--- a/mesonbuild/templates/samplefactory.py
+++ b/mesonbuild/templates/samplefactory.py
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
diff --git a/mesonbuild/templates/sampleimpl.py b/mesonbuild/templates/sampleimpl.py
index c222a1bf9..d033f3c14 100644
--- a/mesonbuild/templates/sampleimpl.py
+++ b/mesonbuild/templates/sampleimpl.py
@@ -1,9 +1,11 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
import abc
+import os
import re
import typing as T
@@ -19,6 +21,9 @@ class SampleImpl(metaclass=abc.ABCMeta):
self.lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower())
self.uppercase_token = self.lowercase_token.upper()
self.capitalized_token = self.lowercase_token.capitalize()
+ self.meson_version = '1.0.0'
+ self.force = args.force
+ self.dependencies = args.deps.split(',') if args.deps else []
@abc.abstractmethod
def create_executable(self) -> None:
@@ -28,30 +33,39 @@ class SampleImpl(metaclass=abc.ABCMeta):
def create_library(self) -> None:
pass
- @abc.abstractproperty
+ @property
+ @abc.abstractmethod
def exe_template(self) -> str:
pass
- @abc.abstractproperty
+ @property
+ @abc.abstractmethod
def exe_meson_template(self) -> str:
pass
- @abc.abstractproperty
+ @property
+ @abc.abstractmethod
def lib_template(self) -> str:
pass
- @abc.abstractproperty
+ @property
+ @abc.abstractmethod
def lib_test_template(self) -> T.Optional[str]:
pass
- @abc.abstractproperty
+ @property
+ @abc.abstractmethod
def lib_meson_template(self) -> str:
pass
- @abc.abstractproperty
+ @property
+ @abc.abstractmethod
def source_ext(self) -> str:
pass
+ def _format_dependencies(self) -> str:
+ return ''.join(f"\n dependency('{d}')," for d in self.dependencies)
+
class ClassImpl(SampleImpl):
@@ -59,14 +73,18 @@ class ClassImpl(SampleImpl):
def create_executable(self) -> None:
source_name = f'{self.capitalized_token}.{self.source_ext}'
- with open(source_name, 'w', encoding='utf-8') as f:
- f.write(self.exe_template.format(project_name=self.name,
- class_name=self.capitalized_token))
- with open('meson.build', 'w', encoding='utf-8') as f:
- f.write(self.exe_meson_template.format(project_name=self.name,
- exe_name=self.name,
- source_name=source_name,
- version=self.version))
+ if not os.path.exists(source_name):
+ with open(source_name, 'w', encoding='utf-8') as f:
+ f.write(self.exe_template.format(project_name=self.name,
+ class_name=self.capitalized_token))
+ if self.force or not os.path.exists('meson.build'):
+ with open('meson.build', 'w', encoding='utf-8') as f:
+ f.write(self.exe_meson_template.format(project_name=self.name,
+ exe_name=self.name,
+ source_name=source_name,
+ version=self.version,
+ meson_version=self.meson_version,
+ dependencies=self._format_dependencies()))
def create_library(self) -> None:
lib_name = f'{self.capitalized_token}.{self.source_ext}'
@@ -82,14 +100,18 @@ class ClassImpl(SampleImpl):
'lib_name': self.lowercase_token,
'test_name': self.lowercase_token,
'version': self.version,
+ 'meson_version': self.meson_version,
+ 'dependencies': self._format_dependencies(),
}
- with open(lib_name, 'w', encoding='utf-8') as f:
- f.write(self.lib_template.format(**kwargs))
- if self.lib_test_template:
+ if not os.path.exists(lib_name):
+ with open(lib_name, 'w', encoding='utf-8') as f:
+ f.write(self.lib_template.format(**kwargs))
+ if self.lib_test_template and not os.path.exists(test_name):
with open(test_name, 'w', encoding='utf-8') as f:
f.write(self.lib_test_template.format(**kwargs))
- with open('meson.build', 'w', encoding='utf-8') as f:
- f.write(self.lib_meson_template.format(**kwargs))
+ if self.force or not os.path.exists('meson.build'):
+ with open('meson.build', 'w', encoding='utf-8') as f:
+ f.write(self.lib_meson_template.format(**kwargs))
class FileImpl(SampleImpl):
@@ -98,13 +120,17 @@ class FileImpl(SampleImpl):
def create_executable(self) -> None:
source_name = f'{self.lowercase_token}.{self.source_ext}'
- with open(source_name, 'w', encoding='utf-8') as f:
- f.write(self.exe_template.format(project_name=self.name))
- with open('meson.build', 'w', encoding='utf-8') as f:
- f.write(self.exe_meson_template.format(project_name=self.name,
- exe_name=self.name,
- source_name=source_name,
- version=self.version))
+ if not os.path.exists(source_name):
+ with open(source_name, 'w', encoding='utf-8') as f:
+ f.write(self.exe_template.format(project_name=self.name))
+ if self.force or not os.path.exists('meson.build'):
+ with open('meson.build', 'w', encoding='utf-8') as f:
+ f.write(self.exe_meson_template.format(project_name=self.name,
+ exe_name=self.name,
+ source_name=source_name,
+ version=self.version,
+ meson_version=self.meson_version,
+ dependencies=self._format_dependencies()))
def lib_kwargs(self) -> T.Dict[str, str]:
"""Get Language specific keyword arguments
@@ -125,28 +151,34 @@ class FileImpl(SampleImpl):
'lib_name': self.lowercase_token,
'test_name': self.lowercase_token,
'version': self.version,
+ 'meson_version': self.meson_version,
+ 'dependencies': self._format_dependencies(),
}
def create_library(self) -> None:
lib_name = f'{self.lowercase_token}.{self.source_ext}'
test_name = f'{self.lowercase_token}_test.{self.source_ext}'
kwargs = self.lib_kwargs()
- with open(lib_name, 'w', encoding='utf-8') as f:
- f.write(self.lib_template.format(**kwargs))
- if self.lib_test_template:
+ if not os.path.exists(lib_name):
+ with open(lib_name, 'w', encoding='utf-8') as f:
+ f.write(self.lib_template.format(**kwargs))
+ if self.lib_test_template and not os.path.exists(test_name):
with open(test_name, 'w', encoding='utf-8') as f:
f.write(self.lib_test_template.format(**kwargs))
- with open('meson.build', 'w', encoding='utf-8') as f:
- f.write(self.lib_meson_template.format(**kwargs))
+ if self.force or not os.path.exists('meson.build'):
+ with open('meson.build', 'w', encoding='utf-8') as f:
+ f.write(self.lib_meson_template.format(**kwargs))
class FileHeaderImpl(FileImpl):
- @abc.abstractproperty
+ @property
+ @abc.abstractmethod
def header_ext(self) -> str:
pass
- @abc.abstractproperty
+ @property
+ @abc.abstractmethod
def lib_header_template(self) -> str:
pass
@@ -158,5 +190,6 @@ class FileHeaderImpl(FileImpl):
def create_library(self) -> None:
super().create_library()
kwargs = self.lib_kwargs()
- with open(kwargs['header_file'], 'w', encoding='utf-8') as f:
- f.write(self.lib_header_template.format_map(kwargs))
+ if not os.path.exists(kwargs['header_file']):
+ with open(kwargs['header_file'], 'w', encoding='utf-8') as f:
+ f.write(self.lib_header_template.format_map(kwargs))
diff --git a/mesonbuild/templates/valatemplates.py b/mesonbuild/templates/valatemplates.py
index 1520de0a7..b2aab3f31 100644
--- a/mesonbuild/templates/valatemplates.py
+++ b/mesonbuild/templates/valatemplates.py
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2019 The Meson development team
+# Copyright © 2023-2025 Intel Corporation
from __future__ import annotations
@@ -11,16 +12,24 @@ hello_vala_template = '''void main (string[] args) {{
}}
'''
-hello_vala_meson_template = '''project('{project_name}', ['c', 'vala'],
- version : '{version}')
+hello_vala_meson_template = '''project(
+ '{project_name}',
+ 'vala',
+ meson_version : '>= {meson_version}',
+ version : '{version}',
+)
dependencies = [
- dependency('glib-2.0'),
- dependency('gobject-2.0'),
+ dependency('glib-2.0'),
+ dependency('gobject-2.0'),{dependencies}
]
-exe = executable('{exe_name}', '{source_name}', dependencies : dependencies,
- install : true)
+exe = executable(
+ '{exe_name}',
+ '{source_name}',
+ dependencies : dependencies,
+ install : true,
+)
test('basic', exe)
'''
@@ -46,29 +55,44 @@ public void main() {{
}}
'''
-lib_vala_meson_template = '''project('{project_name}', ['c', 'vala'],
- version : '{version}')
+lib_vala_meson_template = '''project(
+ '{project_name}',
+ 'vala',
+ meson_version : '>= {meson_version}',
+ version : '{version}',
+)
dependencies = [
- dependency('glib-2.0'),
- dependency('gobject-2.0'),
+ dependency('glib-2.0'),
+ dependency('gobject-2.0'),{dependencies}
]
# These arguments are only used to build the shared library
# not the executables that use the library.
-shlib = shared_library('foo', '{source_file}',
- dependencies: dependencies,
- install: true,
- install_dir: [true, true, true])
-
-test_exe = executable('{test_exe_name}', '{test_source_file}', dependencies : dependencies,
- link_with : shlib)
+lib = shared_library(
+ 'foo',
+ '{source_file}',
+ dependencies : dependencies,
+ install : true,
+ install_dir : [true, true, true],
+)
+
+test_exe = executable(
+ '{test_exe_name}',
+ '{test_source_file}',
+ dependencies : dependencies,
+ link_with : lib,
+)
test('{test_name}', test_exe)
# Make this library usable as a Meson subproject.
{ltoken}_dep = declare_dependency(
- include_directories: include_directories('.'),
- link_with : shlib)
+ include_directories : include_directories('.'),
+ dependencies : dependencies,
+ link_with : lib,
+)
+meson.override_dependency('{project_name}', {ltoken}_dep)
+
'''