• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright (C) 2017 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""" Wrapper to invoke compiled build tools from the build system.
16
17This is just a workaround for GN assuming that all external scripts are
18python sources. It is used to invoke tools like the protoc compiler.
19"""
20
21from __future__ import print_function
22
23import argparse
24import os
25import subprocess
26import sys
27
28
29def main():
30  parser = argparse.ArgumentParser()
31  parser.add_argument('--chdir', default=None)
32  parser.add_argument('--stamp', default=None)
33  parser.add_argument('--path', default=None)
34  parser.add_argument('--noop', default=False, action='store_true')
35  parser.add_argument('--suppress_stdout', default=False, action='store_true')
36  parser.add_argument('--suppress_stderr', default=False, action='store_true')
37  parser.add_argument('cmd', nargs=argparse.REMAINDER)
38  args = parser.parse_args()
39
40  if args.noop:
41    return 0
42
43  if args.chdir and not os.path.exists(args.chdir):
44    print(
45        'Cannot chdir to %s from %s' % (workdir, os.getcwd()), file=sys.stderr)
46    return 1
47
48  exe = os.path.abspath(args.cmd[0]) if os.sep in args.cmd[0] else args.cmd[0]
49  env = os.environ.copy()
50  if args.path:
51    env['PATH'] = os.path.abspath(args.path) + os.pathsep + env['PATH']
52
53  devnull = open(os.devnull, 'wb')
54  stdout = devnull if args.suppress_stdout else None
55  stderr = devnull if args.suppress_stderr else None
56
57  try:
58    proc = subprocess.Popen(
59        [exe] + args.cmd[1:],
60        cwd=args.chdir,
61        env=env,
62        stderr=stderr,
63        stdout=stdout)
64    ret = proc.wait()
65    if ret == 0 and args.stamp:
66      with open(args.stamp, 'w'):
67        os.utime(args.stamp, None)
68    return ret
69  except OSError as e:
70    print('Error running: "%s" (%s)' % (args.cmd[0], e.strerror))
71    print('PATH=%s' % env.get('PATH'))
72    return 127
73
74
75if __name__ == '__main__':
76  sys.exit(main())
77