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