Debian Codegraph @

mesonbuild/modules/simd.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2017 The Meson development team
3
4 from __future__ import annotations
5
6 import typing as T
7
8 from .. import mlog
9 from .. import build
10 from ..compilers import Compiler
11 from ..interpreter.type_checking import BT_SOURCES_KW, STATIC_LIB_KWS
12 from ..interpreterbase.decorators import KwargInfo, typed_pos_args, typed_kwargs
13
14 from . import ExtensionModule, ModuleInfo
15
16 if T.TYPE_CHECKING:
17 from typing_extensions import Literal
18
19 from . import ModuleState
20 from ..interpreter import Interpreter, kwargs as kwtypes
21 from ..interpreter.type_checking import SourcesVarargsType
22
23 class CheckKw(kwtypes.StaticLibrary):
24
25 compiler: Compiler
26 mmx: SourcesVarargsType
27 sse: SourcesVarargsType
28 sse2: SourcesVarargsType
29 sse3: SourcesVarargsType
30 ssse3: SourcesVarargsType
31 sse41: SourcesVarargsType
32 sse42: SourcesVarargsType
33 avx: SourcesVarargsType
34 avx2: SourcesVarargsType
35 neon: SourcesVarargsType
36
37
38 # FIXME add Altivec and AVX512.
39 ISETS = (
40 'mmx',
41 'sse',
42 'sse2',
43 'sse3',
44 'ssse3',
45 'sse41',
46 'sse42',
47 'avx',
48 'avx2',
49 'neon',
50 )
51
52
53 class SimdModule(ExtensionModule):
54
55 INFO = ModuleInfo('SIMD', '0.42.0', unstable=True)
56
57 def __init__(self, interpreter: Interpreter):
58 super().__init__(interpreter)
59 self.methods.update({
60 'check': self.check,
61 })
62
63 @typed_pos_args('simd.check', str)
64 @typed_kwargs('simd.check',
65 KwargInfo('compiler', Compiler, required=True),
66 *[BT_SOURCES_KW.evolve(name=iset, default=None) for iset in ISETS],
67 *[a for a in STATIC_LIB_KWS if a.name != 'sources'])
68 def check(self, state: ModuleState, args: T.Tuple[str], kwargs: CheckKw) -> T.List[T.Union[T.List[build.StaticLibrary], build.ConfigurationData]]:
69 result: T.List[build.StaticLibrary] = []
70
71 local_kwargs = set((*ISETS, 'compiler'))
72 static_lib_kwargs = T.cast('kwtypes.StaticLibrary', {k: v for k, v in kwargs.items() if k not in local_kwargs})
73
74 prefix = args[0]
75 compiler = kwargs['compiler']
76 conf = build.ConfigurationData()
77
78 for iset in ISETS:
79 sources = kwargs[iset] # type: ignore[literal-required]
80 if sources is None:
81 continue
82
83 compile_args = compiler.get_instruction_set_args(iset)
84 if compile_args is None:
85 mlog.log(f'Compiler supports {iset}:', mlog.red('NO'))
86 continue
87
88 if not compiler.has_multi_arguments(compile_args)[0]:
89 mlog.log(f'Compiler supports {iset}:', mlog.red('NO'))
90 continue
91 mlog.log(f'Compiler supports {iset}:', mlog.green('YES'))
92 conf.values['HAVE_' + iset.upper()] = ('1', f'Compiler supports {iset}.')
93
94 libname = prefix + '_' + iset
95 lib_kwargs = static_lib_kwargs.copy()
96 lib_kwargs['sources'] = sources
97
98 # Add compile args we derived above to those the user provided us
99 # This cast is mostly a lie, but it makes mypy happy and calculating the actual key name would be difficult
100 langarg_key = T.cast('Literal["c_args"]', compiler.get_language() + '_args')
101 old_lang_args = lib_kwargs[langarg_key]
102 all_lang_args = old_lang_args + compile_args
103 lib_kwargs[langarg_key] = all_lang_args
104
105 lib = self.interpreter.build_target(state.current_node, (libname, []), lib_kwargs, build.StaticLibrary)
106
107 result.append(lib)
108
109 return [result, conf]
110
111 def initialize(interp: Interpreter) -> SimdModule:
112 return SimdModule(interp)