blob: c143fed55d3c85e3e3c93cb4b257d03de88d7ebd [file] [log] [blame]
Josip Sokcevic39120912021-07-20 18:35:42 +00001#!/usr/bin/env python3
Sebastien Marchand4dd96822017-09-06 14:16:29 -04002# 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 Tambre80ee78e2019-05-06 22:41:05 +00006from __future__ import print_function
7
Sebastien Marchand4dd96822017-09-06 14:16:29 -04008import argparse
9import os
10import subprocess
11import sys
12
13
Junji Watanabefc04ff12022-11-29 03:42:01 +000014DEPOT_TOOLS_DIR = os.path.dirname(os.path.realpath(__file__))
15
Sebastien Marchand4dd96822017-09-06 14:16:29 -040016# This function is inspired from the one in src/tools/vim/ninja-build.vim in the
17# Chromium repository.
18def 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 Vasudevanc5f0cbb2022-01-24 23:56:57 +000028 os.path.isdir(os.path.join(candidate, fp)) for fp in fingerprints):
Sebastien Marchand4dd96822017-09-06 14:16:29 -040029 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
36def 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 Ikuta6c7b8292020-08-03 17:47:51 +000045 help='The build directory, relative to the source directory.',
Sebastien Marchand4dd96822017-09-06 14:16:29 -040046 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 Tambre80ee78e2019-05-06 22:41:05 +000054 print('Building %s' % options.file_path)
Sebastien Marchand4dd96822017-09-06 14:16:29 -040055
Sebastien Marchand4dd96822017-09-06 14:16:29 -040056 carets = '^'
Sebastien Marchand4dd96822017-09-06 14:16:29 -040057 if sys.platform == 'win32':
Sebastien Marchand4dd96822017-09-06 14:16:29 -040058 # The caret character has to be escaped on Windows as it's an escape
59 # character.
60 carets = '^^'
61
62 command = [
Junji Watanabefc04ff12022-11-29 03:42:01 +000063 'python3',
64 os.path.join(DEPOT_TOOLS_DIR, 'ninja.py'), '-C', abs_build_dir,
Sebastien Marchand4dd96822017-09-06 14:16:29 -040065 '%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
72if __name__ == '__main__':
73 sys.exit(main())