compile_single_file.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. import argparse
  6. import os
  7. import subprocess
  8. import sys
  9. DEPOT_TOOLS_DIR = os.path.dirname(os.path.realpath(__file__))
  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
  19. # is 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. carets = '^'
  45. if sys.platform == 'win32':
  46. # The caret character has to be escaped on Windows as it's an escape
  47. # character.
  48. carets = '^^'
  49. command = [
  50. sys.executable,
  51. os.path.join(DEPOT_TOOLS_DIR, 'autoninja.py'), '-C', abs_build_dir,
  52. '%s%s' % (src_relpath, carets)
  53. ]
  54. # |shell| should be set to True on Windows otherwise the carets characters
  55. # get dropped from the command line.
  56. return subprocess.call(command, shell=sys.platform == 'win32')
  57. if __name__ == '__main__':
  58. sys.exit(main())