1# Copyright 2019 The Meson development team
2
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6
7#     http://www.apache.org/licenses/LICENSE-2.0
8
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14from mesonbuild.templates.sampleimpl import SampleImpl
15import re
16
17
18hello_d_template = '''module main;
19import std.stdio;
20
21enum PROJECT_NAME = "{project_name}";
22
23int main(string[] args) {{
24    if (args.length != 1){{
25        writefln("%s takes no arguments.\\n", args[0]);
26        return 1;
27    }}
28    writefln("This is project %s.\\n", PROJECT_NAME);
29    return 0;
30}}
31'''
32
33hello_d_meson_template = '''project('{project_name}', 'd',
34    version : '{version}',
35    default_options: ['warning_level=3'])
36
37exe = executable('{exe_name}', '{source_name}',
38  install : true)
39
40test('basic', exe)
41'''
42
43lib_d_template = '''module {module_file};
44
45/* This function will not be exported and is not
46 * directly callable by users of this library.
47 */
48int internal_function() {{
49    return 0;
50}}
51
52int {function_name}() {{
53    return internal_function();
54}}
55'''
56
57lib_d_test_template = '''module {module_file}_test;
58import std.stdio;
59import {module_file};
60
61
62int main(string[] args) {{
63    if (args.length != 1){{
64        writefln("%s takes no arguments.\\n", args[0]);
65        return 1;
66    }}
67    return {function_name}();
68}}
69'''
70
71lib_d_meson_template = '''project('{project_name}', 'd',
72  version : '{version}',
73  default_options : ['warning_level=3'])
74
75stlib = static_library('{lib_name}', '{source_file}',
76  install : true,
77  gnu_symbol_visibility : 'hidden',
78)
79
80test_exe = executable('{test_exe_name}', '{test_source_file}',
81  link_with : stlib)
82test('{test_name}', test_exe)
83
84# Make this library usable as a Meson subproject.
85{ltoken}_dep = declare_dependency(
86  include_directories: include_directories('.'),
87  link_with : stlib)
88
89# Make this library usable from the Dlang
90# build system.
91dlang_mod = import('dlang')
92if find_program('dub', required: false).found()
93  dlang_mod.generate_dub_file(meson.project_name().to_lower(), meson.source_root(),
94    name : meson.project_name(),
95    license: meson.project_license(),
96    sourceFiles : '{source_file}',
97    description : 'Meson sample project.',
98    version : '{version}',
99  )
100endif
101'''
102
103
104class DlangProject(SampleImpl):
105    def __init__(self, options):
106        super().__init__()
107        self.name = options.name
108        self.version = options.version
109
110    def create_executable(self) -> None:
111        lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower())
112        source_name = lowercase_token + '.d'
113        open(source_name, 'w', encoding='utf-8').write(hello_d_template.format(project_name=self.name))
114        open('meson.build', 'w', encoding='utf-8').write(
115            hello_d_meson_template.format(project_name=self.name,
116                                          exe_name=lowercase_token,
117                                          source_name=source_name,
118                                          version=self.version))
119
120    def create_library(self) -> None:
121        lowercase_token = re.sub(r'[^a-z0-9]', '_', self.name.lower())
122        uppercase_token = lowercase_token.upper()
123        function_name = lowercase_token[0:3] + '_func'
124        test_exe_name = lowercase_token + '_test'
125        lib_m_name = lowercase_token
126        lib_d_name = lowercase_token + '.d'
127        test_d_name = lowercase_token + '_test.d'
128        kwargs = {'utoken': uppercase_token,
129                  'ltoken': lowercase_token,
130                  'header_dir': lowercase_token,
131                  'function_name': function_name,
132                  'module_file': lib_m_name,
133                  'source_file': lib_d_name,
134                  'test_source_file': test_d_name,
135                  'test_exe_name': test_exe_name,
136                  'project_name': self.name,
137                  'lib_name': lowercase_token,
138                  'test_name': lowercase_token,
139                  'version': self.version,
140                  }
141        open(lib_d_name, 'w', encoding='utf-8').write(lib_d_template.format(**kwargs))
142        open(test_d_name, 'w', encoding='utf-8').write(lib_d_test_template.format(**kwargs))
143        open('meson.build', 'w', encoding='utf-8').write(lib_d_meson_template.format(**kwargs))
144