Debian Codegraph @

mesonbuild/modules/snippets.py

raw
1 # Copyright 2025 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.
14
15 from __future__ import annotations
16 import textwrap
17 import typing as T
18
19 from pathlib import Path
20
21 from . import NewExtensionModule, ModuleInfo
22 from ..interpreterbase import KwargInfo, typed_kwargs, typed_pos_args
23 from ..interpreter.type_checking import NoneType
24 from .. import mesonlib
25
26 if T.TYPE_CHECKING:
27 from typing_extensions import TypedDict
28 from . import ModuleState
29
30 class SymbolVisibilityHeaderKW(TypedDict):
31 namespace: T.Optional[str]
32 api: T.Optional[str]
33 compilation: T.Optional[str]
34 static_compilation: T.Optional[str]
35 static_only: bool
36
37
38 class SnippetsModule(NewExtensionModule):
39
40 INFO = ModuleInfo('snippets', '1.10.0')
41
42 def __init__(self) -> None:
43 super().__init__()
44 self.methods.update({
45 'symbol_visibility_header': self.symbol_visibility_header_method,
46 })
47
48 @typed_kwargs('snippets.symbol_visibility_header',
49 KwargInfo('namespace', (str, NoneType)),
50 KwargInfo('api', (str, NoneType)),
51 KwargInfo('compilation', (str, NoneType)),
52 KwargInfo('static_compilation', (str, NoneType)),
53 KwargInfo('static_only', (bool, NoneType)))
54 @typed_pos_args('snippets.symbol_visibility_header', str)
55 def symbol_visibility_header_method(self, state: ModuleState, args: T.Tuple[str], kwargs: 'SymbolVisibilityHeaderKW') -> mesonlib.File:
56 header_name = args[0]
57 namespace = kwargs['namespace'] or state.project_name
58 namespace = mesonlib.underscorify(namespace).upper()
59 if namespace[0].isdigit():
60 namespace = f'_{namespace}'
61 api = kwargs['api'] or f'{namespace}_API'
62 compilation = kwargs['compilation'] or f'{namespace}_COMPILATION'
63 static_compilation = kwargs['static_compilation'] or f'{namespace}_STATIC_COMPILATION'
64 static_only = kwargs['static_only']
65 if static_only is None:
66 default_library = state.get_option('default_library')
67 static_only = default_library == 'static'
68 content = textwrap.dedent('''\
69 // SPDX-license-identifier: 0BSD OR CC0-1.0 OR WTFPL OR Apache-2.0 OR LGPL-2.0-or-later
70 #pragma once
71 ''')
72 if static_only:
73 content += textwrap.dedent(f'''
74 #ifndef {static_compilation}
75 # define {static_compilation}
76 #endif /* {static_compilation} */
77 ''')
78 content += textwrap.dedent(f'''
79 #if (defined(_WIN32) || defined(__CYGWIN__)) && !defined({static_compilation})
80 # define {api}_EXPORT __declspec(dllexport)
81 # define {api}_IMPORT __declspec(dllimport)
82 #elif defined(__OS2__) && !defined({static_compilation})
83 # define {api}_EXPORT __declspec(dllexport)
84 # define {api}_IMPORT
85 #elif __GNUC__ >= 4
86 # define {api}_EXPORT __attribute__((visibility("default")))
87 # define {api}_IMPORT
88 #else
89 # define {api}_EXPORT
90 # define {api}_IMPORT
91 #endif
92
93 #ifdef {compilation}
94 # define {api} {api}_EXPORT extern
95 #else
96 # define {api} {api}_IMPORT extern
97 #endif
98 ''')
99 header_path = Path(state.environment.get_build_dir(), state.subdir, header_name)
100 header_path.write_text(content, encoding='utf-8')
101 return mesonlib.File.from_built_file(state.subdir, header_name)
102
103 def initialize(*args: T.Any, **kwargs: T.Any) -> SnippetsModule:
104 return SnippetsModule()