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# This class contains the basic functionality needed to run any interpreter
16# or an interpreter-based tool.
17
18from .common import CMakeException, CMakeConfiguration, CMakeBuildFile
19from .. import mlog
20from contextlib import contextmanager
21from subprocess import Popen, PIPE, TimeoutExpired
22from pathlib import Path
23import typing as T
24import json
25
26if T.TYPE_CHECKING:
27    from ..environment import Environment
28    from .executor import CMakeExecutor
29
30CMAKE_SERVER_BEGIN_STR = '[== "CMake Server" ==['
31CMAKE_SERVER_END_STR = ']== "CMake Server" ==]'
32
33CMAKE_MESSAGE_TYPES = {
34    'error': ['cookie', 'errorMessage'],
35    'hello': ['supportedProtocolVersions'],
36    'message': ['cookie', 'message'],
37    'progress': ['cookie'],
38    'reply': ['cookie', 'inReplyTo'],
39    'signal': ['cookie', 'name'],
40}  # type: T.Dict[str, T.List[str]]
41
42CMAKE_REPLY_TYPES = {
43    'handshake': [],
44    'configure': [],
45    'compute': [],
46    'cmakeInputs': ['buildFiles', 'cmakeRootDirectory', 'sourceDirectory'],
47    'codemodel': ['configurations']
48}  # type: T.Dict[str, T.List[str]]
49
50# Base CMake server message classes
51
52class MessageBase:
53    def __init__(self, msg_type: str, cookie: str) -> None:
54        self.type = msg_type
55        self.cookie = cookie
56
57    def to_dict(self) -> T.Dict[str, T.Union[str, T.List[str], T.Dict[str, int]]]:
58        return {'type': self.type, 'cookie': self.cookie}
59
60    def log(self) -> None:
61        mlog.warning('CMake server message of type', mlog.bold(type(self).__name__), 'has no log function')
62
63class RequestBase(MessageBase):
64    cookie_counter = 0
65
66    def __init__(self, msg_type: str) -> None:
67        super().__init__(msg_type, self.gen_cookie())
68
69    @staticmethod
70    def gen_cookie() -> str:
71        RequestBase.cookie_counter += 1
72        return f'meson_{RequestBase.cookie_counter}'
73
74class ReplyBase(MessageBase):
75    def __init__(self, cookie: str, in_reply_to: str) -> None:
76        super().__init__('reply', cookie)
77        self.in_reply_to = in_reply_to
78
79class SignalBase(MessageBase):
80    def __init__(self, cookie: str, signal_name: str) -> None:
81        super().__init__('signal', cookie)
82        self.signal_name = signal_name
83
84    def log(self) -> None:
85        mlog.log(mlog.bold('CMake signal:'), mlog.yellow(self.signal_name))
86
87# Special Message classes
88
89class Error(MessageBase):
90    def __init__(self, cookie: str, message: str) -> None:
91        super().__init__('error', cookie)
92        self.message = message
93
94    def log(self) -> None:
95        mlog.error(mlog.bold('CMake server error:'), mlog.red(self.message))
96
97class Message(MessageBase):
98    def __init__(self, cookie: str, message: str) -> None:
99        super().__init__('message', cookie)
100        self.message = message
101
102    def log(self) -> None:
103        #mlog.log(mlog.bold('CMake:'), self.message)
104        pass
105
106class Progress(MessageBase):
107    def __init__(self, cookie: str) -> None:
108        super().__init__('progress', cookie)
109
110    def log(self) -> None:
111        pass
112
113class MessageHello(MessageBase):
114    def __init__(self, supported_protocol_versions: T.List[T.Dict[str, int]]) -> None:
115        super().__init__('hello', '')
116        self.supported_protocol_versions = supported_protocol_versions
117
118    def supports(self, major: int, minor: T.Optional[int] = None) -> bool:
119        for i in self.supported_protocol_versions:
120            assert 'major' in i
121            assert 'minor' in i
122            if major == i['major']:
123                if minor is None or minor == i['minor']:
124                    return True
125        return False
126
127# Request classes
128
129class RequestHandShake(RequestBase):
130    def __init__(self, src_dir: Path, build_dir: Path, generator: str, vers_major: int, vers_minor: T.Optional[int] = None) -> None:
131        super().__init__('handshake')
132        self.src_dir = src_dir
133        self.build_dir = build_dir
134        self.generator = generator
135        self.vers_major = vers_major
136        self.vers_minor = vers_minor
137
138    def to_dict(self) -> T.Dict[str, T.Union[str, T.List[str], T.Dict[str, int]]]:
139        vers = {'major': self.vers_major}
140        if self.vers_minor is not None:
141            vers['minor'] = self.vers_minor
142
143        # Old CMake versions (3.7) want '/' even on Windows
144        self.src_dir   = self.src_dir.resolve()
145        self.build_dir = self.build_dir.resolve()
146
147        return {
148            **super().to_dict(),
149            'sourceDirectory': self.src_dir.as_posix(),
150            'buildDirectory': self.build_dir.as_posix(),
151            'generator': self.generator,
152            'protocolVersion': vers
153        }
154
155class RequestConfigure(RequestBase):
156    def __init__(self, args: T.Optional[T.List[str]] = None):
157        super().__init__('configure')
158        self.args = args
159
160    def to_dict(self) -> T.Dict[str, T.Union[str, T.List[str], T.Dict[str, int]]]:
161        res = super().to_dict()
162        if self.args:
163            res['cacheArguments'] = self.args
164        return res
165
166class RequestCompute(RequestBase):
167    def __init__(self) -> None:
168        super().__init__('compute')
169
170class RequestCMakeInputs(RequestBase):
171    def __init__(self) -> None:
172        super().__init__('cmakeInputs')
173
174class RequestCodeModel(RequestBase):
175    def __init__(self) -> None:
176        super().__init__('codemodel')
177
178# Reply classes
179
180class ReplyHandShake(ReplyBase):
181    def __init__(self, cookie: str) -> None:
182        super().__init__(cookie, 'handshake')
183
184class ReplyConfigure(ReplyBase):
185    def __init__(self, cookie: str) -> None:
186        super().__init__(cookie, 'configure')
187
188class ReplyCompute(ReplyBase):
189    def __init__(self, cookie: str) -> None:
190        super().__init__(cookie, 'compute')
191
192class ReplyCMakeInputs(ReplyBase):
193    def __init__(self, cookie: str, cmake_root: Path, src_dir: Path, build_files: T.List[CMakeBuildFile]) -> None:
194        super().__init__(cookie, 'cmakeInputs')
195        self.cmake_root = cmake_root
196        self.src_dir = src_dir
197        self.build_files = build_files
198
199    def log(self) -> None:
200        mlog.log('CMake root: ', mlog.bold(self.cmake_root.as_posix()))
201        mlog.log('Source dir: ', mlog.bold(self.src_dir.as_posix()))
202        mlog.log('Build files:', mlog.bold(str(len(self.build_files))))
203        with mlog.nested():
204            for i in self.build_files:
205                mlog.log(str(i))
206
207class ReplyCodeModel(ReplyBase):
208    def __init__(self, data: T.Dict[str, T.Any]) -> None:
209        super().__init__(data['cookie'], 'codemodel')
210        self.configs = []
211        for i in data['configurations']:
212            self.configs += [CMakeConfiguration(i)]
213
214    def log(self) -> None:
215        mlog.log('CMake code mode:')
216        for idx, i in enumerate(self.configs):
217            mlog.log(f'Configuration {idx}:')
218            with mlog.nested():
219                i.log()
220
221# Main client class
222
223class CMakeClient:
224    def __init__(self, env: 'Environment') -> None:
225        self.env = env
226        self.proc = None  # type: T.Optional[Popen]
227        self.type_map = {
228            'error': lambda data: Error(data['cookie'], data['errorMessage']),
229            'hello': lambda data: MessageHello(data['supportedProtocolVersions']),
230            'message': lambda data: Message(data['cookie'], data['message']),
231            'progress': lambda data: Progress(data['cookie']),
232            'reply': self.resolve_type_reply,
233            'signal': lambda data: SignalBase(data['cookie'], data['name'])
234        }  # type: T.Dict[str, T.Callable[[T.Dict[str, T.Any]], MessageBase]]
235
236        self.reply_map = {
237            'handshake': lambda data: ReplyHandShake(data['cookie']),
238            'configure': lambda data: ReplyConfigure(data['cookie']),
239            'compute': lambda data: ReplyCompute(data['cookie']),
240            'cmakeInputs': self.resolve_reply_cmakeInputs,
241            'codemodel': lambda data: ReplyCodeModel(data),
242        }  # type: T.Dict[str, T.Callable[[T.Dict[str, T.Any]], ReplyBase]]
243
244    def readMessageRaw(self) -> T.Dict[str, T.Any]:
245        assert self.proc is not None
246        rawData = []
247        begin = False
248        while self.proc.poll() is None:
249            line = self.proc.stdout.readline()
250            if not line:
251                break
252            line = line.decode('utf-8')
253            line = line.strip()
254
255            if begin and line == CMAKE_SERVER_END_STR:
256                break # End of the message
257            elif begin:
258                rawData += [line]
259            elif line == CMAKE_SERVER_BEGIN_STR:
260                begin = True # Begin of the message
261
262        if rawData:
263            res = json.loads('\n'.join(rawData))
264            assert isinstance(res, dict)
265            for i in res.keys():
266                assert isinstance(i, str)
267            return res
268        raise CMakeException('Failed to read data from the CMake server')
269
270    def readMessage(self) -> MessageBase:
271        raw_data = self.readMessageRaw()
272        if 'type' not in raw_data:
273            raise CMakeException('The "type" attribute is missing from the message')
274        msg_type = raw_data['type']
275        func = self.type_map.get(msg_type, None)
276        if not func:
277            raise CMakeException(f'Recieved unknown message type "{msg_type}"')
278        for i in CMAKE_MESSAGE_TYPES[msg_type]:
279            if i not in raw_data:
280                raise CMakeException(f'Key "{i}" is missing from CMake server message type {msg_type}')
281        return func(raw_data)
282
283    def writeMessage(self, msg: MessageBase) -> None:
284        raw_data = '\n{}\n{}\n{}\n'.format(CMAKE_SERVER_BEGIN_STR, json.dumps(msg.to_dict(), indent=2), CMAKE_SERVER_END_STR)
285        self.proc.stdin.write(raw_data.encode('ascii'))
286        self.proc.stdin.flush()
287
288    def query(self, request: RequestBase) -> MessageBase:
289        self.writeMessage(request)
290        while True:
291            reply = self.readMessage()
292            if reply.cookie == request.cookie and reply.type in ['reply', 'error']:
293                return reply
294
295            reply.log()
296
297    def query_checked(self, request: RequestBase, message: str) -> MessageBase:
298        reply = self.query(request)
299        h = mlog.green('SUCCEEDED') if reply.type == 'reply' else mlog.red('FAILED')
300        mlog.log(message + ':', h)
301        if reply.type != 'reply':
302            reply.log()
303            raise CMakeException('CMake server query failed')
304        return reply
305
306    def do_handshake(self, src_dir: Path, build_dir: Path, generator: str, vers_major: int, vers_minor: T.Optional[int] = None) -> None:
307        # CMake prints the hello message on startup
308        msg = self.readMessage()
309        if not isinstance(msg, MessageHello):
310            raise CMakeException('Recieved an unexpected message from the CMake server')
311
312        request = RequestHandShake(src_dir, build_dir, generator, vers_major, vers_minor)
313        self.query_checked(request, 'CMake server handshake')
314
315    def resolve_type_reply(self, data: T.Dict[str, T.Any]) -> ReplyBase:
316        reply_type = data['inReplyTo']
317        func = self.reply_map.get(reply_type, None)
318        if not func:
319            raise CMakeException(f'Recieved unknown reply type "{reply_type}"')
320        for i in ['cookie'] + CMAKE_REPLY_TYPES[reply_type]:
321            if i not in data:
322                raise CMakeException(f'Key "{i}" is missing from CMake server message type {type}')
323        return func(data)
324
325    def resolve_reply_cmakeInputs(self, data: T.Dict[str, T.Any]) -> ReplyCMakeInputs:
326        files = []
327        for i in data['buildFiles']:
328            for j in i['sources']:
329                files += [CMakeBuildFile(Path(j), i['isCMake'], i['isTemporary'])]
330        return ReplyCMakeInputs(data['cookie'], Path(data['cmakeRootDirectory']), Path(data['sourceDirectory']), files)
331
332    @contextmanager
333    def connect(self, cmake_exe: 'CMakeExecutor') -> T.Generator[None, None, None]:
334        self.startup(cmake_exe)
335        try:
336            yield
337        finally:
338            self.shutdown()
339
340    def startup(self, cmake_exe: 'CMakeExecutor') -> None:
341        if self.proc is not None:
342            raise CMakeException('The CMake server was already started')
343        assert cmake_exe.found()
344
345        mlog.debug('Starting CMake server with CMake', mlog.bold(' '.join(cmake_exe.get_command())), 'version', mlog.cyan(cmake_exe.version()))
346        self.proc = Popen(cmake_exe.get_command() + ['-E', 'server', '--experimental', '--debug'], stdin=PIPE, stdout=PIPE)
347
348    def shutdown(self) -> None:
349        if self.proc is None:
350            return
351
352        mlog.debug('Shutting down the CMake server')
353
354        # Close the pipes to exit
355        self.proc.stdin.close()
356        self.proc.stdout.close()
357
358        # Wait for CMake to finish
359        try:
360            self.proc.wait(timeout=2)
361        except TimeoutExpired:
362            # Terminate CMake if there is a timeout
363            # terminate() may throw a platform specific exception if the process has already
364            # terminated. This may be the case if there is a race condition (CMake exited after
365            # the timeout but before the terminate() call). Additionally, this behavior can
366            # also be triggered on cygwin if CMake crashes.
367            # See https://github.com/mesonbuild/meson/pull/4969#issuecomment-499413233
368            try:
369                self.proc.terminate()
370            except Exception:
371                pass
372
373        self.proc = None
374