nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2014 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 | """Redirects to the version of clang-format checked into the Chrome tree. |
| 7 | |
| 8 | clang-format binaries are pulled down from Google Cloud Storage whenever you |
| 9 | sync Chrome, to platform-specific locations. This script knows how to locate |
| 10 | those tools, assuming the script is invoked from inside a Chromium checkout.""" |
| 11 | |
| 12 | import gclient_utils |
| 13 | import os |
| 14 | import subprocess |
| 15 | import sys |
| 16 | |
| 17 | |
| 18 | class NotFoundError(Exception): |
| 19 | """A file could not be found.""" |
| 20 | def __init__(self, e): |
| 21 | Exception.__init__(self, |
| 22 | 'Problem while looking for clang-format in Chromium source tree:\n' |
| 23 | ' %s' % e) |
| 24 | |
| 25 | |
ajm@chromium.org | 0981223 | 2014-02-04 01:38:36 +0000 | [diff] [blame] | 26 | def _FindChromiumSourceRoot(): |
| 27 | """Return the source root of the current chromium checkout, or die trying.""" |
| 28 | # The use of .gn is somewhat incongruous here, but we need a file uniquely |
| 29 | # existing at src/. GN does the same thing at least. |
| 30 | source_root = gclient_utils.FindFileUpwards('.gn') |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 31 | if not source_root: |
| 32 | raise NotFoundError( |
ajm@chromium.org | 0981223 | 2014-02-04 01:38:36 +0000 | [diff] [blame] | 33 | '.gn file not found in any parent of the current path.') |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 34 | return source_root |
| 35 | |
| 36 | |
| 37 | def FindClangFormatToolInChromiumTree(): |
| 38 | """Return a path to the clang-format executable, or die trying.""" |
ajm@chromium.org | 0981223 | 2014-02-04 01:38:36 +0000 | [diff] [blame] | 39 | tool_path = os.path.join(_FindChromiumSourceRoot(), 'third_party', |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 40 | 'clang_format', 'bin', |
| 41 | gclient_utils.GetMacWinOrLinux(), |
| 42 | 'clang-format' + gclient_utils.GetExeSuffix()) |
| 43 | if not os.path.exists(tool_path): |
| 44 | # TODO(nick): After March 2014, eliminate the following advisory. |
| 45 | error_text = '''\n GIT CL FORMAT - WINTER WEATHER ADVISORY |
| 46 | |
| 47 | clang-format binaries now come with every Chrome checkout! |
| 48 | |
| 49 | Unfortunately, your depot_tools scripts tried to find clang-format binaries |
| 50 | in your Chrome checkout, but failed. This is expected if you haven't synced |
| 51 | since the binaries were added. |
| 52 | |
| 53 | 'git cl format' will probably not work until you sync your Chrome tree. |
| 54 | Sorry about that. |
| 55 | |
| 56 | Contact nick@chromium.org if you have any additional questions.\n\n''' |
| 57 | |
| 58 | error_text += 'File does not exist: %s' % tool_path |
| 59 | |
| 60 | raise NotFoundError(error_text) |
| 61 | return tool_path |
| 62 | |
| 63 | |
| 64 | def FindClangFormatScriptInChromiumTree(script_name): |
| 65 | """Return a path to a clang-format helper script, or die trying.""" |
ajm@chromium.org | 0981223 | 2014-02-04 01:38:36 +0000 | [diff] [blame] | 66 | script_path = os.path.join(_FindChromiumSourceRoot(), 'third_party', |
thakis@chromium.org | 6813daf | 2014-02-02 19:03:18 +0000 | [diff] [blame] | 67 | 'clang_format', 'script', script_name) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 68 | if not os.path.exists(script_path): |
thakis@chromium.org | 6813daf | 2014-02-02 19:03:18 +0000 | [diff] [blame] | 69 | # TODO(thakis): Remove the fallback to the old location after a few weeks. |
ajm@chromium.org | 0981223 | 2014-02-04 01:38:36 +0000 | [diff] [blame] | 70 | script_path = os.path.join(_FindChromiumSourceRoot(), 'third_party', |
thakis@chromium.org | 6813daf | 2014-02-02 19:03:18 +0000 | [diff] [blame] | 71 | 'clang_format', 'scripts', script_name) |
| 72 | if not os.path.exists(script_path): |
| 73 | raise NotFoundError('File does not exist: %s' % script_path) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 74 | return script_path |
| 75 | |
| 76 | |
| 77 | def main(args): |
| 78 | try: |
| 79 | tool = FindClangFormatToolInChromiumTree() |
| 80 | except NotFoundError, e: |
| 81 | print >> sys.stderr, e |
| 82 | sys.exit(1) |
| 83 | |
| 84 | # Add some visibility to --help showing where the tool lives, since this |
| 85 | # redirection can be a little opaque. |
| 86 | help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list') |
| 87 | if any(match in args for match in help_syntax): |
| 88 | print '\nDepot tools redirects you to the clang-format at:\n %s\n' % tool |
| 89 | |
| 90 | return subprocess.call([tool] + sys.argv[1:]) |
| 91 | |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | sys.exit(main(sys.argv)) |