blob: 9c9952146db3e9717e2472be97a967b2883ca590 [file] [log] [blame]
Sebastien Marchand4dd96822017-09-06 14:16:29 -04001#!/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 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',
Sebastien Marchanda74bd782019-06-13 17:14:17 +000043 help='The build directory, relative to the source directory. Jumbo '
44 'builds aren\'t supported but you can create a non-jumbo build '
45 'config just for this script and keep using jumbo in your regular '
46 'builds.',
Sebastien Marchand4dd96822017-09-06 14:16:29 -040047 required=True)
48
49 options = parser.parse_args()
50
51 src_dir = path_to_source_root(os.path.abspath(options.file_path))
52 abs_build_dir = os.path.join(src_dir, options.build_dir)
53 src_relpath = os.path.relpath(options.file_path, abs_build_dir)
54
Raul Tambre80ee78e2019-05-06 22:41:05 +000055 print('Building %s' % options.file_path)
Sebastien Marchand4dd96822017-09-06 14:16:29 -040056
57 ninja_exec = 'ninja'
58 carets = '^'
59 # We need to make sure that we call the ninja executable, calling |ninja|
60 # directly might end up calling a wrapper script that'll remove the caret
61 # characters.
62 if sys.platform == 'win32':
63 ninja_exec = 'ninja.exe'
64 # The caret character has to be escaped on Windows as it's an escape
65 # character.
66 carets = '^^'
67
68 command = [
69 ninja_exec,
70 '-C', abs_build_dir,
71 '%s%s' % (src_relpath, carets)
72 ]
73 # |shell| should be set to True on Windows otherwise the carets characters
74 # get dropped from the command line.
75 return subprocess.call(command, shell=sys.platform=='win32')
76
77
78if __name__ == '__main__':
79 sys.exit(main())