From 67bb8614007ddf4a6663ee832fb8f19612cb90ff Mon Sep 17 00:00:00 2001 From: "brettw@chromium.org" Date: Fri, 8 Nov 2013 20:51:40 +0000 Subject: [PATCH] Add a wrapper script for GN to depot_tools This automatically searches the path for a source root (or takes one from the command line), and then executes the current platform's GN binary inside that source tree. This will allow the user to have a "gn" command on their path, and allow our scripts to run GN without having to bake-in the logic for finding the correct platform's GN binary. Review URL: https://codereview.chromium.org/66013002 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@233983 0039d316-1c4b-4281-b951-d872f2087c98 --- gn | 8 +++++++ gn.bat | 10 +++++++++ gn.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 gn create mode 100644 gn.bat create mode 100644 gn.py diff --git a/gn b/gn new file mode 100644 index 000000000..93c16e1d6 --- /dev/null +++ b/gn @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +# Copyright 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +base_dir=$(dirname "$0") + +PYTHONDONTWRITEBYTECODE=1 exec python "$base_dir/gn.py" "$@" diff --git a/gn.bat b/gn.bat new file mode 100644 index 000000000..c020d89d8 --- /dev/null +++ b/gn.bat @@ -0,0 +1,10 @@ +@echo off +:: Copyright 2013 The Chromium Authors. All rights reserved. +:: Use of this source code is governed by a BSD-style license that can be +:: found in the LICENSE file. + +:: This is required with cygwin only. +PATH=%~dp0;%PATH% + +:: Defer control. +%~dp0python "%~dp0\gn.py" %* diff --git a/gn.py b/gn.py new file mode 100644 index 000000000..c8af3302e --- /dev/null +++ b/gn.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# Copyright 2013 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""This script is a wrapper around the GN binary that is pulled from Google +Cloud Storage when you sync Chrome. The binaries go into platform-specific +subdirectories in the source tree. + +This script makes there be one place for forwarding to the correct platform's +binary. It will also automatically try to find the gn binary when run inside +the chrome source tree, so users can just type "gn" on the command line +(normally depot_tools is on the path).""" + +import os +import subprocess +import sys + + +class PlatformUnknownError(IOError): + pass + + +def HasDotfile(path): + """Returns True if the given path has a .gn file in it.""" + return os.path.exists(path + '/.gn') + + +def FindSourceRootOnPath(): + """Searches upward from the current directory for the root of the source + tree and returns the found path. Returns None if no source root could + be found.""" + cur = os.getcwd() + while True: + if HasDotfile(cur): + return cur + up_one = os.path.dirname(cur) + if up_one == cur: + return None # Reached the top of the directory tree + cur = up_one + + +def RunGN(sourceroot): + # The binaries in platform-specific subdirectories in src/tools/gn/bin. + gnpath = sourceroot + '/tools/gn/bin/' + if sys.platform == 'win32': + gnpath += 'win/gn.exe' + elif sys.platform.startswith('linux'): + gnpath += 'linux/gn' + elif sys.platform == 'darwin': + gnpath += 'mac/gn' + else: + raise PlatformUnknownError('Unknown platform for GN: ' + sys.platform) + + return subprocess.call([gnpath] + sys.argv[1:]) + + +def main(args): + sourceroot = FindSourceRootOnPath() + if not sourceroot: + print >> sys.stderr, '.gn file not found in any parent of the current path.' + sys.exit(1) + return RunGN(sourceroot) + +if __name__ == '__main__': + sys.exit(main(sys.argv))