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