ninja-build.vim 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. " Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. " Use of this source code is governed by a BSD-style license that can be
  3. " found in the LICENSE file.
  4. "
  5. " Adds a "Compile this file" function, using ninja. On Mac, binds Cmd-k to
  6. " this command. On Windows, Ctrl-F7 (which is the same as the VS default).
  7. " On Linux, <Leader>o, which is \o by default ("o"=creates .o files)
  8. "
  9. " Adds a "Build this target" function, using ninja. This is not bound
  10. " to any key by default, but can be used via the :CrBuild command.
  11. " It builds 'chrome' by default, but :CrBuild target1 target2 etc works as well.
  12. "
  13. " Requires that gyp has already generated build.ninja files, and that ninja is
  14. " in your path (which it is automatically if depot_tools is in your path).
  15. "
  16. " Add the following to your .vimrc file:
  17. " so /path/to/src/tools/vim/ninja-build.vim
  18. pythonx << endpython
  19. import os
  20. import vim
  21. def path_to_current_buffer():
  22. """Returns the absolute path of the current buffer."""
  23. return vim.current.buffer.name
  24. def path_to_source_root():
  25. """Returns the absolute path to the chromium source root."""
  26. candidate = os.path.dirname(path_to_current_buffer())
  27. # This is a list of files that need to identify the src directory. The shorter
  28. # it is, the more likely it's wrong (checking for just "build/common.gypi"
  29. # would find "src/v8" for files below "src/v8", as "src/v8/build/common.gypi"
  30. # exists). The longer it is, the more likely it is to break when we rename
  31. # directories.
  32. fingerprints = ['chrome', 'net', 'v8', 'build', 'skia']
  33. while candidate and not all(
  34. [os.path.isdir(os.path.join(candidate, fp)) for fp in fingerprints]):
  35. candidate = os.path.dirname(candidate)
  36. return candidate
  37. def path_to_build_dir():
  38. """Returns <chrome_root>/<output_dir>/(Release|Debug|<other>)."""
  39. chrome_root = path_to_source_root()
  40. sys.path.append(os.path.join(chrome_root, 'tools', 'vim'))
  41. from ninja_output import GetNinjaOutputDirectory
  42. return GetNinjaOutputDirectory(chrome_root)
  43. def compute_ninja_command_for_current_buffer():
  44. """Returns the shell command to compile the file in the current buffer."""
  45. build_dir = path_to_build_dir()
  46. # ninja needs filepaths for the ^ syntax to be relative to the
  47. # build directory.
  48. file_to_build = path_to_current_buffer()
  49. file_to_build = os.path.relpath(file_to_build, build_dir)
  50. build_cmd = ' '.join(['autoninja', '-C', build_dir, file_to_build + '^'])
  51. if sys.platform == 'win32':
  52. # Escape \ for Vim, and ^ for both Vim and shell.
  53. build_cmd = build_cmd.replace('\\', '\\\\').replace('^', '^^^^')
  54. vim.command('return "%s"' % build_cmd)
  55. def compute_ninja_command_for_targets(targets=''):
  56. build_cmd = ' '.join(['autoninja', '-C', path_to_build_dir(),
  57. targets])
  58. vim.command('return "%s"' % build_cmd)
  59. endpython
  60. fun! s:MakeWithCustomCommand(build_cmd)
  61. let l:oldmakepgr = &makeprg
  62. let &makeprg=a:build_cmd
  63. if exists(':Make') == 2
  64. Make
  65. else
  66. silent make | cwindow
  67. endif
  68. if !has('gui_running')
  69. redraw!
  70. endif
  71. let &makeprg = l:oldmakepgr
  72. endfun
  73. fun! s:NinjaCommandForCurrentBuffer()
  74. pythonx compute_ninja_command_for_current_buffer()
  75. endfun
  76. fun! s:NinjaCommandForTargets(targets)
  77. pythonx compute_ninja_command_for_targets(vim.eval('a:targets'))
  78. endfun
  79. fun! CrCompileFile()
  80. call s:MakeWithCustomCommand(s:NinjaCommandForCurrentBuffer())
  81. endfun
  82. fun! CrBuild(...)
  83. let l:targets = a:0 > 0 ? join(a:000, ' ') : ''
  84. if (l:targets !~ '\i')
  85. let l:targets = 'chrome'
  86. endif
  87. call s:MakeWithCustomCommand(s:NinjaCommandForTargets(l:targets))
  88. endfun
  89. command! CrCompileFile call CrCompileFile()
  90. command! -nargs=* CrBuild call CrBuild(<q-args>)
  91. if has('mac')
  92. map <D-k> :CrCompileFile<cr>
  93. imap <D-k> <esc>:CrCompileFile<cr>
  94. elseif has('win32')
  95. map <C-F7> :CrCompileFile<cr>
  96. imap <C-F7> <esc>:CrCompileFile<cr>
  97. elseif has('unix')
  98. map <Leader>o :CrCompileFile<cr>
  99. endif