blob: b766e0073bef2a16eb9a24ac8d1ae610ca8aec1f [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
Junji Watanabefc04ff12022-11-29 03:42:01 +000013DEPOT_TOOLS_DIR = os.path.dirname(os.path.realpath(__file__))
14
Mike Frysinger124bb8e2023-09-06 05:48:55 +000015
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):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000019 """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
25 # is to break when we rename directories.
26 fingerprints = ['chrome', 'net', 'v8', 'build', 'skia']
27 while candidate and not all(
28 os.path.isdir(os.path.join(candidate, fp)) for fp in fingerprints):
29 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
Sebastien Marchand4dd96822017-09-06 14:16:29 -040034
35
36def main():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000037 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',
45 help='The build directory, relative to the source directory.',
46 required=True)
Sebastien Marchand4dd96822017-09-06 14:16:29 -040047
Mike Frysinger124bb8e2023-09-06 05:48:55 +000048 options = parser.parse_args()
Sebastien Marchand4dd96822017-09-06 14:16:29 -040049
Mike Frysinger124bb8e2023-09-06 05:48:55 +000050 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)
Sebastien Marchand4dd96822017-09-06 14:16:29 -040053
Mike Frysinger124bb8e2023-09-06 05:48:55 +000054 print('Building %s' % options.file_path)
Sebastien Marchand4dd96822017-09-06 14:16:29 -040055
Mike Frysinger124bb8e2023-09-06 05:48:55 +000056 carets = '^'
57 if sys.platform == 'win32':
58 # The caret character has to be escaped on Windows as it's an escape
59 # character.
60 carets = '^^'
Sebastien Marchand4dd96822017-09-06 14:16:29 -040061
Mike Frysinger124bb8e2023-09-06 05:48:55 +000062 command = [
63 'python3',
64 os.path.join(DEPOT_TOOLS_DIR, 'ninja.py'), '-C', abs_build_dir,
65 '%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')
Sebastien Marchand4dd96822017-09-06 14:16:29 -040070
71
72if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000073 sys.exit(main())