1#!/usr/bin/env python3
2# Copyright (C) 2019 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16from __future__ import print_function
17
18import os
19import shutil
20import subprocess
21import sys
22
23from compat import quote
24from platform import system
25
26GN_ARGS = ' '.join(
27    quote(s) for s in (
28        'is_debug=false',
29        'is_perfetto_build_generator=true',
30        'is_perfetto_embedder=true',
31        'use_custom_libcxx=false',
32        'enable_perfetto_ipc=true',
33    ))
34
35ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
36OUT_DIR = os.path.join('out', 'amalgamated')
37GEN_AMALGAMATED = os.path.join('tools', 'gen_amalgamated')
38
39
40def call(cmd, *args):
41  command = [cmd] + list(args)
42  print('Running:', ' '.join(quote(c) for c in command))
43  try:
44    return subprocess.check_output(command, cwd=ROOT_DIR).decode()
45  except subprocess.CalledProcessError as e:
46    assert False, 'Command: %s failed: %s' % (' '.join(command), e)
47
48
49def check_amalgamated_output():
50  call(GEN_AMALGAMATED, '--quiet')
51
52
53def check_amalgamated_build():
54  args = [
55      '-std=c++11', '-Werror', '-Wall', '-Wextra',
56      '-DPERFETTO_AMALGAMATED_SDK_TEST', '-I' + OUT_DIR,
57      OUT_DIR + '/perfetto.cc', 'test/client_api_example.cc', '-o',
58      OUT_DIR + '/test'
59  ]
60  if system().lower() == 'linux':
61    args += ['-lpthread', '-lrt']
62
63  if sys.platform.startswith('linux'):
64    llvm_script = os.path.join(ROOT_DIR, 'gn', 'standalone', 'toolchain',
65                               'linux_find_llvm.py')
66    cxx = subprocess.check_output([llvm_script]).splitlines()[2].decode()
67  else:
68    cxx = 'clang++'
69  call(cxx, *args)
70
71
72def check_amalgamated_dependencies():
73  os_deps = {}
74  for os_name in ['android', 'linux', 'mac']:
75    gn_args = (' target_os="%s"' % os_name) + GN_ARGS
76    os_deps[os_name] = call(GEN_AMALGAMATED, '--gn_args', gn_args, '--out',
77                            OUT_DIR, '--dump-deps', '--quiet').split('\n')
78  for os_name, deps in os_deps.items():
79    for dep in deps:
80      for other_os, other_deps in os_deps.items():
81        if not dep in other_deps:
82          raise AssertionError('Discrepancy in amalgamated build dependencies: '
83                               '%s is missing on %s.' % (dep, other_os))
84
85
86def main():
87  check_amalgamated_dependencies()
88  check_amalgamated_output()
89  check_amalgamated_build()
90  shutil.rmtree(OUT_DIR)
91
92
93if __name__ == '__main__':
94  exit(main())
95