Sebastien Marchand | 4dd9682 | 2017-09-06 14:16:29 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2017 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 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 6 | from __future__ import print_function |
| 7 | |
Sebastien Marchand | 4dd9682 | 2017-09-06 14:16:29 -0400 | [diff] [blame] | 8 | import argparse |
| 9 | import os |
| 10 | import subprocess |
| 11 | import sys |
| 12 | |
| 13 | |
| 14 | # This function is inspired from the one in src/tools/vim/ninja-build.vim in the |
| 15 | # Chromium repository. |
| 16 | def path_to_source_root(path): |
| 17 | """Returns the absolute path to the chromium source root.""" |
| 18 | candidate = os.path.dirname(path) |
| 19 | # This is a list of directories that need to identify the src directory. The |
| 20 | # shorter it is, the more likely it's wrong (checking for just |
| 21 | # "build/common.gypi" would find "src/v8" for files below "src/v8", as |
| 22 | # "src/v8/build/common.gypi" exists). The longer it is, the more likely it is |
| 23 | # to break when we rename directories. |
| 24 | fingerprints = ['chrome', 'net', 'v8', 'build', 'skia'] |
| 25 | while candidate and not all( |
| 26 | [os.path.isdir(os.path.join(candidate, fp)) for fp in fingerprints]): |
| 27 | new_candidate = os.path.dirname(candidate) |
| 28 | if new_candidate == candidate: |
| 29 | raise Exception("Couldn't find source-dir from %s" % path) |
| 30 | candidate = os.path.dirname(candidate) |
| 31 | return candidate |
| 32 | |
| 33 | |
| 34 | def main(): |
| 35 | parser = argparse.ArgumentParser() |
| 36 | parser.add_argument( |
| 37 | '--file-path', |
| 38 | help='The file path, could be absolute or relative to the current ' |
| 39 | 'directory.', |
| 40 | required=True) |
| 41 | parser.add_argument( |
| 42 | '--build-dir', |
| 43 | help='The build directory, relative to the source directory.', |
| 44 | required=True) |
| 45 | |
| 46 | options = parser.parse_args() |
| 47 | |
| 48 | src_dir = path_to_source_root(os.path.abspath(options.file_path)) |
| 49 | abs_build_dir = os.path.join(src_dir, options.build_dir) |
| 50 | src_relpath = os.path.relpath(options.file_path, abs_build_dir) |
| 51 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 52 | print('Building %s' % options.file_path) |
Sebastien Marchand | 4dd9682 | 2017-09-06 14:16:29 -0400 | [diff] [blame] | 53 | |
| 54 | ninja_exec = 'ninja' |
| 55 | carets = '^' |
| 56 | # We need to make sure that we call the ninja executable, calling |ninja| |
| 57 | # directly might end up calling a wrapper script that'll remove the caret |
| 58 | # characters. |
| 59 | if sys.platform == 'win32': |
| 60 | ninja_exec = 'ninja.exe' |
| 61 | # The caret character has to be escaped on Windows as it's an escape |
| 62 | # character. |
| 63 | carets = '^^' |
| 64 | |
| 65 | command = [ |
| 66 | ninja_exec, |
| 67 | '-C', abs_build_dir, |
| 68 | '%s%s' % (src_relpath, carets) |
| 69 | ] |
| 70 | # |shell| should be set to True on Windows otherwise the carets characters |
| 71 | # get dropped from the command line. |
| 72 | return subprocess.call(command, shell=sys.platform=='win32') |
| 73 | |
| 74 | |
| 75 | if __name__ == '__main__': |
| 76 | sys.exit(main()) |