Debian Codegraph @

mesonbuild/compilers/mixins/elbrus.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright © 2023-2025 Intel Corporation
3
4 from __future__ import annotations
5
6 """Abstractions for the Elbrus family of compilers."""
7
8 import functools
9 import os
10 import typing as T
11 import subprocess
12 import re
13
14 from .gnu import GnuLikeCompiler
15 from .gnu import gnu_optimization_args
16 from ...mesonlib import Popen_safe
17 from ...options import OptionKey
18
19 if T.TYPE_CHECKING:
20 from ...build import BuildTarget
21
22
23 class ElbrusCompiler(GnuLikeCompiler):
24 # Elbrus compiler is nearly like GCC, but does not support
25 # PCH, LTO, sanitizers and color output as of version 1.21.x.
26
27 id = 'lcc'
28
29 def __init__(self) -> None:
30 super().__init__()
31 self.base_options = {OptionKey(o) for o in ['b_pgo', 'b_coverage', 'b_ndebug', 'b_staticpic', 'b_lundef', 'b_asneeded']}
32 default_warn_args = ['-Wall']
33 self.warn_args = {'0': [],
34 '1': default_warn_args,
35 '2': default_warn_args + ['-Wextra'],
36 '3': default_warn_args + ['-Wextra', '-Wpedantic'],
37 'everything': default_warn_args + ['-Wextra', '-Wpedantic']}
38
39 # FIXME: use _build_wrapper to call this so that linker flags from the env
40 # get applied
41 def get_library_dirs(self, elf_class: T.Optional[int] = None) -> T.List[str]:
42 os_env = os.environ.copy()
43 os_env['LC_ALL'] = 'C'
44 stdo = Popen_safe(self.get_exelist(ccache=False) + ['--print-search-dirs'], env=os_env)[1]
45 for line in stdo.split('\n'):
46 if line.startswith('libraries:'):
47 # lcc does not include '=' in --print-search-dirs output. Also it could show nonexistent dirs.
48 libstr = line.split(' ', 1)[1]
49 return [os.path.realpath(p) for p in libstr.split(':') if os.path.exists(p)]
50 return []
51
52 def get_program_dirs(self) -> T.List[str]:
53 os_env = os.environ.copy()
54 os_env['LC_ALL'] = 'C'
55 stdo = Popen_safe(self.get_exelist(ccache=False) + ['--print-search-dirs'], env=os_env)[1]
56 for line in stdo.split('\n'):
57 if line.startswith('programs:'):
58 # lcc does not include '=' in --print-search-dirs output.
59 libstr = line.split(' ', 1)[1]
60 return [os.path.realpath(p) for p in libstr.split(':')]
61 return []
62
63 @functools.lru_cache(maxsize=None)
64 def get_default_include_dirs(self) -> T.List[str]:
65 os_env = os.environ.copy()
66 os_env['LC_ALL'] = 'C'
67 p = subprocess.Popen(self.get_exelist(ccache=False) + ['-xc', '-E', '-v', '-'], env=os_env, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
68 stderr = p.stderr.read().decode('utf-8', errors='replace')
69 includes: T.List[str] = []
70 for line in stderr.split('\n'):
71 if line.lstrip().startswith('--sys_include'):
72 includes.append(re.sub(r'\s*\\$', '', re.sub(r'^\s*--sys_include\s*', '', line)))
73 return includes
74
75 def get_optimization_args(self, optimization_level: str) -> T.List[str]:
76 return gnu_optimization_args[optimization_level]
77
78 def get_prelink_args(self, prelink_name: str, obj_list: T.List[str]) -> T.Tuple[T.List[str], T.List[str]]:
79 return [prelink_name], ['-r', '-nodefaultlibs', '-nostartfiles', '-o', prelink_name] + obj_list
80
81 def get_pch_suffix(self) -> str:
82 # Actually it's not supported for now, but probably will be supported in future
83 return 'pch'
84
85 def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]:
86 args: T.List[str] = []
87 key = OptionKey(f'{self.language}_std', subproject=subproject, machine=self.for_machine)
88 if target:
89 std = self.environment.coredata.get_option_for_target(target, key)
90 else:
91 std = self.environment.coredata.optstore.get_value_for(key)
92 assert isinstance(std, str)
93 if std != 'none':
94 args.append('-std=' + std)
95 return args
96
97 def openmp_flags(self) -> T.List[str]:
98 return ['-fopenmp']
99
100 @classmethod
101 def use_linker_args(cls, linker: str, version: str) -> T.List[str]:
102 return []