brettw@chromium.org | 67bb861 | 2013-11-08 20:51:40 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2013 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """This script is a wrapper around the GN binary that is pulled from Google |
| 7 | Cloud Storage when you sync Chrome. The binaries go into platform-specific |
| 8 | subdirectories in the source tree. |
| 9 | |
| 10 | This script makes there be one place for forwarding to the correct platform's |
| 11 | binary. It will also automatically try to find the gn binary when run inside |
| 12 | the chrome source tree, so users can just type "gn" on the command line |
| 13 | (normally depot_tools is on the path).""" |
| 14 | |
| 15 | import os |
| 16 | import subprocess |
| 17 | import sys |
| 18 | |
| 19 | |
| 20 | class PlatformUnknownError(IOError): |
| 21 | pass |
| 22 | |
| 23 | |
| 24 | def HasDotfile(path): |
| 25 | """Returns True if the given path has a .gn file in it.""" |
| 26 | return os.path.exists(path + '/.gn') |
| 27 | |
| 28 | |
| 29 | def FindSourceRootOnPath(): |
| 30 | """Searches upward from the current directory for the root of the source |
| 31 | tree and returns the found path. Returns None if no source root could |
| 32 | be found.""" |
| 33 | cur = os.getcwd() |
| 34 | while True: |
| 35 | if HasDotfile(cur): |
| 36 | return cur |
| 37 | up_one = os.path.dirname(cur) |
| 38 | if up_one == cur: |
| 39 | return None # Reached the top of the directory tree |
| 40 | cur = up_one |
| 41 | |
| 42 | |
| 43 | def RunGN(sourceroot): |
| 44 | # The binaries in platform-specific subdirectories in src/tools/gn/bin. |
| 45 | gnpath = sourceroot + '/tools/gn/bin/' |
brettw@chromium.org | 7d6ffa5 | 2013-12-05 18:43:11 +0000 | [diff] [blame] | 46 | if sys.platform in ('cygwin', 'win32'): |
brettw@chromium.org | 67bb861 | 2013-11-08 20:51:40 +0000 | [diff] [blame] | 47 | gnpath += 'win/gn.exe' |
| 48 | elif sys.platform.startswith('linux'): |
| 49 | gnpath += 'linux/gn' |
| 50 | elif sys.platform == 'darwin': |
| 51 | gnpath += 'mac/gn' |
| 52 | else: |
| 53 | raise PlatformUnknownError('Unknown platform for GN: ' + sys.platform) |
| 54 | |
| 55 | return subprocess.call([gnpath] + sys.argv[1:]) |
| 56 | |
| 57 | |
| 58 | def main(args): |
| 59 | sourceroot = FindSourceRootOnPath() |
| 60 | if not sourceroot: |
| 61 | print >> sys.stderr, '.gn file not found in any parent of the current path.' |
| 62 | sys.exit(1) |
| 63 | return RunGN(sourceroot) |
| 64 | |
| 65 | if __name__ == '__main__': |
| 66 | sys.exit(main(sys.argv)) |