1#!/usr/bin/env python
2#
3# Copyright (C) 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import logging
19import os
20import subprocess
21import sys
22import tempfile
23import zipfile
24
25THIS_DIR = os.path.realpath(os.path.dirname(__file__))
26
27
28def logger():
29    """Returns the module level logger."""
30    return logging.getLogger(__name__)
31
32
33def unchecked_call(cmd, *args, **kwargs):
34    """subprocess.call with logging."""
35    logger().info('unchecked_call: %s', subprocess.list2cmdline(cmd))
36    return subprocess.call(cmd, *args, **kwargs)
37
38
39def check_call(cmd, *args, **kwargs):
40    """subprocess.check_call with logging."""
41    logger().info('check_call: %s', subprocess.list2cmdline(cmd))
42    subprocess.check_call(cmd, *args, **kwargs)
43
44
45def android_build_top():
46    return os.path.realpath(os.path.join(THIS_DIR, '../../..'))
47
48
49def clang_build():
50    gofile = os.path.join(android_build_top(), 'build', 'soong', 'cc', 'config', 'global.go')
51    try:
52        lines = file(gofile).readlines()
53        versionLine = [l for l in lines if 'ClangDefaultVersion' in l][0]
54        start, end = versionLine.index('"'), versionLine.rindex('"')
55        return versionLine[start + 1: end]
56    except Exception as err:
57        raise RuntimeError("Extracting Clang version failed with {0}".format(err))
58
59
60def llvm_profdata():
61    return os.path.join(android_build_top(), 'prebuilts', 'clang', 'host',
62                        'linux-x86', clang_build(), 'bin', 'llvm-profdata')
63
64
65def run_llvm_profdata(inputs, output):
66    check_call([llvm_profdata(), 'merge', '-output=' + output] + inputs)
67