compile_single_file.py 2.5 KB

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