| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2019 The Meson development team |
| 3 |
# Copyright © 2023-2025 Intel Corporation |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from mesonbuild.templates.sampleimpl import FileImpl |
| 8 |
|
| 9 |
import typing as T |
| 10 |
|
| 11 |
|
| 12 |
hello_d_template = '''module main; |
| 13 |
import std.stdio; |
| 14 |
|
| 15 |
enum PROJECT_NAME = "{project_name}"; |
| 16 |
|
| 17 |
int main(string[] args) {{ |
| 18 |
if (args.length != 1){{ |
| 19 |
writefln("%s takes no arguments.\\n", args[0]); |
| 20 |
return 1; |
| 21 |
}} |
| 22 |
writefln("This is project %s.\\n", PROJECT_NAME); |
| 23 |
return 0; |
| 24 |
}} |
| 25 |
''' |
| 26 |
|
| 27 |
hello_d_meson_template = '''project( |
| 28 |
'{project_name}', |
| 29 |
'd', |
| 30 |
version : '{version}', |
| 31 |
meson_version : '>= {meson_version}', |
| 32 |
default_options : ['warning_level=3'], |
| 33 |
) |
| 34 |
|
| 35 |
dependencies = [{dependencies} |
| 36 |
] |
| 37 |
|
| 38 |
sources = [{source_files} |
| 39 |
] |
| 40 |
|
| 41 |
exe = executable( |
| 42 |
'{exe_name}', |
| 43 |
sources, |
| 44 |
dependencies : dependencies, |
| 45 |
install : true, |
| 46 |
) |
| 47 |
|
| 48 |
test('basic', exe) |
| 49 |
''' |
| 50 |
|
| 51 |
lib_d_template = '''module {module_file}; |
| 52 |
|
| 53 |
/* This function will not be exported and is not |
| 54 |
* directly callable by users of this library. |
| 55 |
*/ |
| 56 |
int internal_function() {{ |
| 57 |
return 0; |
| 58 |
}} |
| 59 |
|
| 60 |
int {function_name}() {{ |
| 61 |
return internal_function(); |
| 62 |
}} |
| 63 |
''' |
| 64 |
|
| 65 |
lib_d_test_template = '''module {module_file}_test; |
| 66 |
import std.stdio; |
| 67 |
import {module_file}; |
| 68 |
|
| 69 |
|
| 70 |
int main(string[] args) {{ |
| 71 |
if (args.length != 1){{ |
| 72 |
writefln("%s takes no arguments.\\n", args[0]); |
| 73 |
return 1; |
| 74 |
}} |
| 75 |
return {function_name}(); |
| 76 |
}} |
| 77 |
''' |
| 78 |
|
| 79 |
lib_d_meson_template = '''project( |
| 80 |
'{project_name}', |
| 81 |
'd', |
| 82 |
version : '{version}', |
| 83 |
meson_version : '>= {meson_version}', |
| 84 |
default_options : ['warning_level=3'], |
| 85 |
) |
| 86 |
|
| 87 |
dependencies = [{dependencies} |
| 88 |
] |
| 89 |
|
| 90 |
sources = [{source_files} |
| 91 |
] |
| 92 |
|
| 93 |
stlib = static_library( |
| 94 |
'{lib_name}', |
| 95 |
sources, |
| 96 |
install : true, |
| 97 |
gnu_symbol_visibility : 'hidden', |
| 98 |
dependencies : dependencies, |
| 99 |
) |
| 100 |
|
| 101 |
test_exe = executable( |
| 102 |
'{test_exe_name}', |
| 103 |
'{test_source_file}', |
| 104 |
link_with : stlib, |
| 105 |
dependencies : dependencies, |
| 106 |
) |
| 107 |
test('{test_name}', test_exe) |
| 108 |
|
| 109 |
# Make this library usable as a Meson subproject. |
| 110 |
{ltoken}_dep = declare_dependency( |
| 111 |
include_directories : include_directories('.'), |
| 112 |
dependencies : dependencies, |
| 113 |
link_with : stlib, |
| 114 |
) |
| 115 |
meson.override_dependency('{project_name}', {ltoken}_dep) |
| 116 |
|
| 117 |
# Make this library usable from the Dlang |
| 118 |
# build system. |
| 119 |
dlang_mod = import('dlang') |
| 120 |
if find_program('dub', required : false).found() |
| 121 |
dlang_mod.generate_dub_file( |
| 122 |
meson.project_name().to_lower(), |
| 123 |
meson.project_source_root(), |
| 124 |
name : meson.project_name(), |
| 125 |
license : meson.project_license()[0], |
| 126 |
sourceFiles : ['{source_file}'], |
| 127 |
description : 'Meson sample project.', |
| 128 |
version : '{version}', |
| 129 |
) |
| 130 |
endif |
| 131 |
''' |
| 132 |
|
| 133 |
|
| 134 |
class DlangProject(FileImpl): |
| 135 |
|
| 136 |
source_ext = 'd' |
| 137 |
exe_template = hello_d_template |
| 138 |
exe_meson_template = hello_d_meson_template |
| 139 |
lib_template = lib_d_template |
| 140 |
lib_test_template = lib_d_test_template |
| 141 |
lib_meson_template = lib_d_meson_template |
| 142 |
|
| 143 |
def lib_kwargs(self) -> T.Dict[str, str]: |
| 144 |
kwargs = super().lib_kwargs() |
| 145 |
kwargs['module_file'] = self.lowercase_token |
| 146 |
return kwargs |