Debian Codegraph @

mesonbuild/templates/cudatemplates.py

raw
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 import typing as T
7
8 from mesonbuild.templates.sampleimpl import FileHeaderImpl
9
10 if T.TYPE_CHECKING:
11 from ..minit import Arguments
12
13
14 hello_cuda_template = '''#include <iostream>
15
16 #define PROJECT_NAME "{project_name}"
17
18 int main(int argc, char **argv) {{
19 if (argc != 1) {{
20 std::cout << argv[0] << " takes no arguments.\\n";
21 return 1;
22 }}
23 std::cout << "This is project " << PROJECT_NAME << ".\\n";
24 return 0;
25 }}
26 '''
27
28 hello_cuda_meson_template = '''project(
29 '{project_name}',
30 ['cuda', 'cpp'],
31 version : '{version}',
32 meson_version : '>= {meson_version}',
33 default_options : ['warning_level=3', 'cpp_std=c++14'],
34 )
35
36 dependencies = [{dependencies}
37 ]
38
39 sources = [{source_files}
40 ]
41
42 exe = executable(
43 '{exe_name}',
44 sources,
45 dependencies : dependencies,
46 install : true,
47 )
48
49 test('basic', exe)
50 '''
51
52 lib_h_template = '''#pragma once
53 #if defined _WIN32 || defined __CYGWIN__
54 #ifdef BUILDING_{utoken}
55 #define {utoken}_PUBLIC __declspec(dllexport)
56 #else
57 #define {utoken}_PUBLIC __declspec(dllimport)
58 #endif
59 #else
60 #ifdef BUILDING_{utoken}
61 #define {utoken}_PUBLIC __attribute__ ((visibility ("default")))
62 #else
63 #define {utoken}_PUBLIC
64 #endif
65 #endif
66
67 namespace {namespace} {{
68
69 class {utoken}_PUBLIC {class_name} {{
70
71 public:
72 {class_name}();
73 int get_number() const;
74
75 private:
76
77 int number;
78
79 }};
80
81 }}
82
83 '''
84
85 lib_cuda_template = '''#include <{header_file}>
86
87 namespace {namespace} {{
88
89 {class_name}::{class_name}() {{
90 number = 6;
91 }}
92
93 int {class_name}::get_number() const {{
94 return number;
95 }}
96
97 }}
98 '''
99
100 lib_cuda_test_template = '''#include <{header_file}>
101 #include <iostream>
102
103 int main(int argc, char **argv) {{
104 if (argc != 1) {{
105 std::cout << argv[0] << " takes no arguments.\\n";
106 return 1;
107 }}
108 {namespace}::{class_name} c;
109 return c.get_number() != 6;
110 }}
111 '''
112
113 lib_cuda_meson_template = '''project(
114 '{project_name}',
115 ['cuda', 'cpp'],
116 version : '{version}',
117 meson_version : '>= {meson_version}',
118 default_options : ['warning_level=3'],
119 )
120
121 # These arguments are only used to build the shared library
122 # not the executables that use the library.
123 lib_args = ['-DBUILDING_{utoken}']
124
125 dependencies = [{dependencies}
126 ]
127
128 sources = [{source_files}
129 ]
130
131 lib = library(
132 '{lib_name}',
133 sources,
134 install : true,
135 cpp_shared_args : lib_args,
136 gnu_symbol_visibility : 'hidden',
137 dependencies : dependencies,
138 )
139
140 test_exe = executable(
141 '{test_exe_name}',
142 '{test_source_file}',
143 link_with : lib,
144 dependencies : dependencies,
145 )
146 test('{test_name}', test_exe)
147
148 # Make this library usable as a Meson subproject.
149 {ltoken}_dep = declare_dependency(
150 include_directories : include_directories('.'),
151 dependencies : dependencies,
152 link_with : lib,
153 )
154 meson.override_dependency('{project_name}', {ltoken}_dep)
155
156 # Make this library usable from the system's
157 # package manager.
158 install_headers('{header_file}', subdir : '{header_dir}')
159
160 pkg_mod = import('pkgconfig')
161 pkg_mod.generate(
162 lib,
163 description : 'Meson sample project.',
164 subdirs : '{header_dir}',
165 )
166 '''
167
168
169 class CudaProject(FileHeaderImpl):
170
171 source_ext = 'cu'
172 header_ext = 'h'
173 exe_template = hello_cuda_template
174 exe_meson_template = hello_cuda_meson_template
175 lib_template = lib_cuda_template
176 lib_header_template = lib_h_template
177 lib_test_template = lib_cuda_test_template
178 lib_meson_template = lib_cuda_meson_template
179
180 def __init__(self, args: Arguments):
181 super().__init__(args)
182 self.meson_version = '1.3.0'