1# Copyright 2019 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"""Abstractions for the Elbrus family of compilers."""
16
17import os
18import typing as T
19import subprocess
20import re
21
22from .gnu import GnuLikeCompiler
23from .gnu import gnu_optimization_args
24from ...mesonlib import Popen_safe
25
26if T.TYPE_CHECKING:
27    from ...environment import Environment
28
29
30class ElbrusCompiler(GnuLikeCompiler):
31    # Elbrus compiler is nearly like GCC, but does not support
32    # PCH, LTO, sanitizers and color output as of version 1.21.x.
33    def __init__(self):
34        super().__init__()
35        self.id = 'lcc'
36        self.base_options = ['b_pgo', 'b_coverage',
37                             'b_ndebug', 'b_staticpic',
38                             'b_lundef', 'b_asneeded']
39
40    # FIXME: use _build_wrapper to call this so that linker flags from the env
41    # get applied
42    def get_library_dirs(self, env: 'Environment', elf_class: T.Optional[int] = None) -> T.List[str]:
43        os_env = os.environ.copy()
44        os_env['LC_ALL'] = 'C'
45        stdo = Popen_safe(self.exelist + ['--print-search-dirs'], env=os_env)[1]
46        for line in stdo.split('\n'):
47            if line.startswith('libraries:'):
48                # lcc does not include '=' in --print-search-dirs output. Also it could show nonexistent dirs.
49                libstr = line.split(' ', 1)[1]
50                return [os.path.realpath(p) for p in libstr.split(':') if os.path.exists(p)]
51        return []
52
53    def get_program_dirs(self, env: 'Environment') -> T.List[str]:
54        os_env = os.environ.copy()
55        os_env['LC_ALL'] = 'C'
56        stdo = Popen_safe(self.exelist + ['--print-search-dirs'], env=os_env)[1]
57        for line in stdo.split('\n'):
58            if line.startswith('programs:'):
59                # lcc does not include '=' in --print-search-dirs output.
60                libstr = line.split(' ', 1)[1]
61                return [os.path.realpath(p) for p in libstr.split(':')]
62        return []
63
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.exelist + ['-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 = []
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_pch_suffix(self) -> str:
79        # Actually it's not supported for now, but probably will be supported in future
80        return 'pch'
81
82    def openmp_flags(self) -> T.List[str]:
83        return ['-fopenmp']
84