1# Copyright 2012-2017 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
15import os.path
16import typing as T
17
18from .. import mlog
19from ..mesonlib import EnvironmentException, MachineChoice, version_compare
20
21from .compilers import Compiler
22
23if T.TYPE_CHECKING:
24    from ..envconfig import MachineInfo
25
26class ValaCompiler(Compiler):
27
28    language = 'vala'
29
30    def __init__(self, exelist, version, for_machine: MachineChoice,
31                 is_cross, info: 'MachineInfo'):
32        super().__init__(exelist, version, for_machine, info)
33        self.version = version
34        self.is_cross = is_cross
35        self.id = 'valac'
36        self.base_options = ['b_colorout']
37
38    def name_string(self):
39        return ' '.join(self.exelist)
40
41    def needs_static_linker(self):
42        return False # Because compiles into C.
43
44    def get_optimization_args(self, optimization_level):
45        return []
46
47    def get_debug_args(self, is_debug):
48        return ['--debug'] if is_debug else []
49
50    def get_output_args(self, target):
51        return [] # Because compiles into C.
52
53    def get_compile_only_args(self):
54        return [] # Because compiles into C.
55
56    def get_pic_args(self):
57        return []
58
59    def get_pie_args(self):
60        return []
61
62    def get_pie_link_args(self):
63        return []
64
65    def get_always_args(self):
66        return ['-C']
67
68    def get_warn_args(self, warning_level):
69        return []
70
71    def get_no_warn_args(self):
72        return ['--disable-warnings']
73
74    def get_werror_args(self):
75        return ['--fatal-warnings']
76
77    def get_colorout_args(self, colortype):
78        if version_compare(self.version, '>=0.37.1'):
79            return ['--color=' + colortype]
80        return []
81
82    def compute_parameters_with_absolute_paths(self, parameter_list, build_dir):
83        for idx, i in enumerate(parameter_list):
84            if i[:9] == '--girdir=':
85                parameter_list[idx] = i[:9] + os.path.normpath(os.path.join(build_dir, i[9:]))
86            if i[:10] == '--vapidir=':
87                parameter_list[idx] = i[:10] + os.path.normpath(os.path.join(build_dir, i[10:]))
88            if i[:13] == '--includedir=':
89                parameter_list[idx] = i[:13] + os.path.normpath(os.path.join(build_dir, i[13:]))
90            if i[:14] == '--metadatadir=':
91                parameter_list[idx] = i[:14] + os.path.normpath(os.path.join(build_dir, i[14:]))
92
93        return parameter_list
94
95    def sanity_check(self, work_dir, environment):
96        code = 'class MesonSanityCheck : Object { }'
97        extra_flags = []
98        extra_flags += environment.coredata.get_external_args(self.for_machine, self.language)
99        if self.is_cross:
100            extra_flags += self.get_compile_only_args()
101        else:
102            extra_flags += environment.coredata.get_external_link_args(self.for_machine, self.language)
103        with self.cached_compile(code, environment.coredata, extra_args=extra_flags, mode='compile') as p:
104            if p.returncode != 0:
105                msg = 'Vala compiler {!r} can not compile programs' \
106                      ''.format(self.name_string())
107                raise EnvironmentException(msg)
108
109    def get_buildtype_args(self, buildtype):
110        if buildtype == 'debug' or buildtype == 'debugoptimized' or buildtype == 'minsize':
111            return ['--debug']
112        return []
113
114    def find_library(self, libname, env, extra_dirs, *args):
115        if extra_dirs and isinstance(extra_dirs, str):
116            extra_dirs = [extra_dirs]
117        # Valac always looks in the default vapi dir, so only search there if
118        # no extra dirs are specified.
119        if not extra_dirs:
120            code = 'class MesonFindLibrary : Object { }'
121            args = []
122            args += env.coredata.get_external_args(self.for_machine, self.language)
123            vapi_args = ['--pkg', libname]
124            args += vapi_args
125            with self.cached_compile(code, env.coredata, extra_args=args, mode='compile') as p:
126                if p.returncode == 0:
127                    return vapi_args
128        # Not found? Try to find the vapi file itself.
129        for d in extra_dirs:
130            vapi = os.path.join(d, libname + '.vapi')
131            if os.path.isfile(vapi):
132                return [vapi]
133        mlog.debug('Searched {!r} and {!r} wasn\'t found'.format(extra_dirs, libname))
134        return None
135
136    def thread_flags(self, env):
137        return []
138
139    def thread_link_flags(self, env):
140        return []
141