compile_single_file.py 2.5 KB

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