blob: e60f830cbd174b855e6ab0de1dd6a22c66321a0c [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
6import argparse
7import os
8import subprocess
9import sys
10
11
12# This function is inspired from the one in src/tools/vim/ninja-build.vim in the
13# Chromium repository.
14def path_to_source_root(path):
15 """Returns the absolute path to the chromium source root."""
16 candidate = os.path.dirname(path)
17 # This is a list of directories that need to identify the src directory. The
18 # shorter it is, the more likely it's wrong (checking for just
19 # "build/common.gypi" would find "src/v8" for files below "src/v8", as
20 # "src/v8/build/common.gypi" exists). The longer it is, the more likely it is
21 # to break when we rename directories.
22 fingerprints = ['chrome', 'net', 'v8', 'build', 'skia']
23 while candidate and not all(
24 [os.path.isdir(os.path.join(candidate, fp)) for fp in fingerprints]):
25 new_candidate = os.path.dirname(candidate)
26 if new_candidate == candidate:
27 raise Exception("Couldn't find source-dir from %s" % path)
28 candidate = os.path.dirname(candidate)
29 return candidate
30
31
32def main():
33 parser = argparse.ArgumentParser()
34 parser.add_argument(
35 '--file-path',
36 help='The file path, could be absolute or relative to the current '
37 'directory.',
38 required=True)
39 parser.add_argument(
40 '--build-dir',
41 help='The build directory, relative to the source directory.',
42 required=True)
43
44 options = parser.parse_args()
45
46 src_dir = path_to_source_root(os.path.abspath(options.file_path))
47 abs_build_dir = os.path.join(src_dir, options.build_dir)
48 src_relpath = os.path.relpath(options.file_path, abs_build_dir)
49
50 print 'Building %s' % options.file_path
51
52 ninja_exec = 'ninja'
53 carets = '^'
54 # We need to make sure that we call the ninja executable, calling |ninja|
55 # directly might end up calling a wrapper script that'll remove the caret
56 # characters.
57 if sys.platform == 'win32':
58 ninja_exec = 'ninja.exe'
59 # The caret character has to be escaped on Windows as it's an escape
60 # character.
61 carets = '^^'
62
63 command = [
64 ninja_exec,
65 '-C', abs_build_dir,
66 '%s%s' % (src_relpath, carets)
67 ]
68 # |shell| should be set to True on Windows otherwise the carets characters
69 # get dropped from the command line.
70 return subprocess.call(command, shell=sys.platform=='win32')
71
72
73if __name__ == '__main__':
74 sys.exit(main())