blob: d1974006d5d5159b3ccf2cfcc6b573a90cb3242e [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
6import argparse
7import os
8import subprocess
9import sys
10
Junji Watanabefc04ff12022-11-29 03:42:01 +000011DEPOT_TOOLS_DIR = os.path.dirname(os.path.realpath(__file__))
12
Mike Frysinger124bb8e2023-09-06 05:48:55 +000013
Sebastien Marchand4dd96822017-09-06 14:16:29 -040014# 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):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000017 """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
23 # is 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
Sebastien Marchand4dd96822017-09-06 14:16:29 -040032
33
34def main():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000035 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',
43 help='The build directory, relative to the source directory.',
44 required=True)
Sebastien Marchand4dd96822017-09-06 14:16:29 -040045
Mike Frysinger124bb8e2023-09-06 05:48:55 +000046 options = parser.parse_args()
Sebastien Marchand4dd96822017-09-06 14:16:29 -040047
Mike Frysinger124bb8e2023-09-06 05:48:55 +000048 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)
Sebastien Marchand4dd96822017-09-06 14:16:29 -040051
Mike Frysinger124bb8e2023-09-06 05:48:55 +000052 print('Building %s' % options.file_path)
Sebastien Marchand4dd96822017-09-06 14:16:29 -040053
Mike Frysinger124bb8e2023-09-06 05:48:55 +000054 carets = '^'
55 if sys.platform == 'win32':
56 # The caret character has to be escaped on Windows as it's an escape
57 # character.
58 carets = '^^'
Sebastien Marchand4dd96822017-09-06 14:16:29 -040059
Mike Frysinger124bb8e2023-09-06 05:48:55 +000060 command = [
61 'python3',
Sunny Sachanandani44d6d1a2023-11-17 01:01:46 +000062 os.path.join(DEPOT_TOOLS_DIR, 'autoninja.py'), '-C', abs_build_dir,
Mike Frysinger124bb8e2023-09-06 05:48:55 +000063 '%s%s' % (src_relpath, carets)
64 ]
65 # |shell| should be set to True on Windows otherwise the carets characters
66 # get dropped from the command line.
67 return subprocess.call(command, shell=sys.platform == 'win32')
Sebastien Marchand4dd96822017-09-06 14:16:29 -040068
69
70if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000071 sys.exit(main())