xref: /aosp_15_r20/external/perfetto/tools/run_buildtools_binary.py (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1#!/usr/bin/env python3
2# Copyright (C) 2021 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""" A wrapper to run gn, ninja and other buildtools/ for all platforms. """
16
17from __future__ import print_function
18
19import os
20import subprocess
21import sys
22
23from platform import system
24
25ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
26
27
28def run_buildtools_binary(args):
29  if len(args) < 1:
30    print('Usage %s command [args]\n' % sys.argv[0])
31    return 1
32
33  sys_name = system().lower()
34  os_dir = None
35  ext = ''
36  if sys_name == 'windows':
37    os_dir = 'win'
38    ext = '.exe'
39  elif sys_name == 'darwin':
40    os_dir = 'mac'
41  elif sys_name == 'linux':
42    os_dir = 'linux64'
43  else:
44    print('OS not supported: %s\n' % sys_name)
45    return 1
46
47  cmd = args[0]
48  args = args[1:]
49
50  # Some binaries have been migrated to third_party/xxx. Look into that path
51  # first (see b/261398524)
52  exe_path = os.path.join(ROOT_DIR, 'third_party', cmd, cmd) + ext
53  if not os.path.exists(exe_path):
54    exe_path = os.path.join(ROOT_DIR, 'buildtools', os_dir, cmd) + ext
55
56  if sys_name == 'windows':
57    # execl() behaves oddly on Windows: the spawned process doesn't seem to
58    # receive CTRL+C. Use subprocess instead.
59    sys.exit(subprocess.call([exe_path] + args))
60  else:
61    os.execl(exe_path, os.path.basename(exe_path), *args)
62
63
64if __name__ == '__main__':
65  run_buildtools_binary(sys.argv[1:])
66