1# Copyright 2016 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
16import subprocess
17import shutil
18import tempfile
19from ..environment import detect_ninja, detect_scanbuild
20
21
22def scanbuild(exelist, srcdir, blddir, privdir, logdir, args):
23    with tempfile.TemporaryDirectory(dir=privdir) as scandir:
24        meson_cmd = exelist + args
25        build_cmd = exelist + ['-o', logdir] + detect_ninja() + ['-C', scandir]
26        rc = subprocess.call(meson_cmd + [srcdir, scandir])
27        if rc != 0:
28            return rc
29        return subprocess.call(build_cmd)
30
31
32def run(args):
33    srcdir = args[0]
34    blddir = args[1]
35    meson_cmd = args[2:]
36    privdir = os.path.join(blddir, 'meson-private')
37    logdir = os.path.join(blddir, 'meson-logs/scanbuild')
38    shutil.rmtree(logdir, ignore_errors=True)
39
40    exelist = detect_scanbuild()
41    if not exelist:
42        print('Could not execute scan-build "%s"' % ' '.join(exelist))
43        return 1
44
45    return scanbuild(exelist, srcdir, blddir, privdir, logdir, meson_cmd)
46