Debian Codegraph @

mesonbuild/templates/objctemplates.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 lib_h_template = '''#pragma once
15 #if defined _WIN32 || defined __CYGWIN__
16 #ifdef BUILDING_{utoken}
17 #define {utoken}_PUBLIC __declspec(dllexport)
18 #else
19 #define {utoken}_PUBLIC __declspec(dllimport)
20 #endif
21 #elif defined __OS2__
22 #ifdef BUILDING_{utoken}
23 #define {utoken}_PUBLIC __declspec(dllexport)
24 #else
25 #define {utoken}_PUBLIC
26 #endif
27 #else
28 #ifdef BUILDING_{utoken}
29 #define {utoken}_PUBLIC __attribute__ ((visibility ("default")))
30 #else
31 #define {utoken}_PUBLIC
32 #endif
33 #endif
34
35 int {utoken}_PUBLIC {function_name}();
36
37 '''
38
39 lib_objc_template = '''#import <{header_file}>
40
41 /* This function will not be exported and is not
42 * directly callable by users of this library.
43 */
44 int internal_function() {{
45 return 0;
46 }}
47
48 int {function_name}() {{
49 return internal_function();
50 }}
51 '''
52
53 lib_objc_test_template = '''#import <{header_file}>
54 #import <stdio.h>
55
56 int main(int argc, char **argv) {{
57 if (argc != 1) {{
58 printf("%s takes no arguments.\\n", argv[0]);
59 return 1;
60 }}
61 return {function_name}();
62 }}
63 '''
64
65 lib_objc_meson_template = '''project(
66 '{project_name}',
67 'objc',
68 version : '{version}',
69 meson_version : '>= {meson_version}',
70 default_options : ['warning_level=3'],
71 )
72
73 dependencies = [{dependencies}
74 ]
75
76 sources = [{source_files}
77 ]
78
79 # These arguments are only used to build the shared library
80 # not the executables that use the library.
81 lib_args = ['-DBUILDING_{utoken}']
82
83 lib = library(
84 '{lib_name}',
85 sources,
86 install : true,
87 objc_shared_args : lib_args,
88 dependencies : dependencies,
89 gnu_symbol_visibility : 'hidden',
90 )
91
92 test_exe = executable(
93 '{test_exe_name}',
94 '{test_source_file}',
95 dependencies : dependencies,
96 link_with : lib)
97 test('{test_name}', test_exe)
98
99 # Make this library usable as a Meson subproject.
100 {ltoken}_dep = declare_dependency(
101 include_directories : include_directories('.'),
102 dependencies : dependencies,
103 link_with : lib,
104 )
105 meson.override_dependency('{project_name}', {ltoken}_dep)
106
107 # Make this library usable from the system's
108 # package manager.
109 install_headers('{header_file}', subdir : '{header_dir}')
110
111 pkg_mod = import('pkgconfig')
112 pkg_mod.generate(
113 lib,
114 description : 'Meson sample project.',
115 subdirs : '{header_dir}',
116 )
117 '''
118
119 hello_objc_template = '''#import <stdio.h>
120
121 #define PROJECT_NAME "{project_name}"
122
123 int main(int argc, char **argv) {{
124 if (argc != 1) {{
125 printf("%s takes no arguments.\\n", argv[0]);
126 return 1;
127 }}
128 printf("This is project %s.\\n", PROJECT_NAME);
129 return 0;
130 }}
131 '''
132
133 hello_objc_meson_template = '''project(
134 '{project_name}',
135 'objc',
136 version : '{version}',
137 meson_version : '>= {meson_version}',
138 default_options : ['warning_level=3'],
139 )
140
141 dependencies = [{dependencies}
142 ]
143
144 sources = [{source_files}
145 ]
146
147 exe = executable(
148 '{exe_name}',
149 sources,
150 dependencies : dependencies,
151 install : true,
152 )
153
154 test('basic', exe)
155 '''
156
157
158 class ObjCProject(FileHeaderImpl):
159
160 source_ext = 'm'
161 header_ext = 'h'
162 exe_template = hello_objc_template
163 exe_meson_template = hello_objc_meson_template
164 lib_template = lib_objc_template
165 lib_header_template = lib_h_template
166 lib_test_template = lib_objc_test_template
167 lib_meson_template = lib_objc_meson_template
168
169 def __init__(self, args: Arguments):
170 super().__init__(args)
171 self.meson_version = '1.3.0'