blob: bb0a5416d7effc14a2b51b4dfaeb936aaa469628 [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
14# This function is inspired from the one in src/tools/vim/ninja-build.vim in the
15# Chromium repository.
16def 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
34def 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',
Takuto Ikuta6c7b8292020-08-03 17:47:51 +000043 help='The build directory, relative to the source directory.',
Sebastien Marchand4dd96822017-09-06 14:16:29 -040044 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 Tambre80ee78e2019-05-06 22:41:05 +000052 print('Building %s' % options.file_path)
Sebastien Marchand4dd96822017-09-06 14:16:29 -040053
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
75if __name__ == '__main__':
76 sys.exit(main())