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 | |
| 6 | import argparse |
| 7 | import os |
| 8 | import subprocess |
| 9 | import sys |
| 10 | |
Junji Watanabe | fc04ff1 | 2022-11-29 03:42:01 +0000 | [diff] [blame] | 11 | DEPOT_TOOLS_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 12 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 13 | |
Sebastien Marchand | 4dd9682 | 2017-09-06 14:16:29 -0400 | [diff] [blame] | 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): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 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 |
| 23 | # is 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 |
Sebastien Marchand | 4dd9682 | 2017-09-06 14:16:29 -0400 | [diff] [blame] | 32 | |
| 33 | |
| 34 | def main(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 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) |
Sebastien Marchand | 4dd9682 | 2017-09-06 14:16:29 -0400 | [diff] [blame] | 45 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 46 | options = parser.parse_args() |
Sebastien Marchand | 4dd9682 | 2017-09-06 14:16:29 -0400 | [diff] [blame] | 47 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 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) |
Sebastien Marchand | 4dd9682 | 2017-09-06 14:16:29 -0400 | [diff] [blame] | 51 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 52 | print('Building %s' % options.file_path) |
Sebastien Marchand | 4dd9682 | 2017-09-06 14:16:29 -0400 | [diff] [blame] | 53 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 54 | carets = '^' |
| 55 | if sys.platform == 'win32': |
| 56 | # The caret character has to be escaped on Windows as it's an escape |
| 57 | # character. |
| 58 | carets = '^^' |
Sebastien Marchand | 4dd9682 | 2017-09-06 14:16:29 -0400 | [diff] [blame] | 59 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 60 | command = [ |
| 61 | 'python3', |
| 62 | os.path.join(DEPOT_TOOLS_DIR, 'ninja.py'), '-C', abs_build_dir, |
| 63 | '%s%s' % (src_relpath, carets) |
| 64 | ] |
| 65 | # |shell| should be set to True on Windows otherwise the carets characters |
| 66 | # get dropped from the command line. |
| 67 | return subprocess.call(command, shell=sys.platform == 'win32') |
Sebastien Marchand | 4dd9682 | 2017-09-06 14:16:29 -0400 | [diff] [blame] | 68 | |
| 69 | |
| 70 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 71 | sys.exit(main()) |