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.
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)
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.
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).
16 " Add the following to your .vimrc file:
17 " so /path/to/src/tools/vim/ninja-build.vim
24 def path_to_current_buffer():
25 """Returns the absolute path of the current buffer."""
26 return vim.current.buffer.name
29 def path_to_source_root():
30 """Returns the absolute path to the chromium source root."""
31 candidate = os.path.dirname(path_to_current_buffer())
32 # This is a list of files that need to identify the src directory. The shorter
33 # it is, the more likely it's wrong (checking for just "build/common.gypi"
34 # would find "src/v8" for files below "src/v8", as "src/v8/build/common.gypi"
35 # exists). The longer it is, the more likely it is to break when we rename
37 fingerprints = ['chrome', 'net', 'v8', 'build', 'skia']
38 while candidate and not all(
39 [os.path.isdir(os.path.join(candidate, fp)) for fp in fingerprints]):
40 candidate = os.path.dirname(candidate)
44 def path_to_build_dir(configuration):
45 """Returns <chrome_root>/<output_dir>/(Release|Debug)."""
47 chrome_root = path_to_source_root()
48 sys.path.append(os.path.join(chrome_root, 'tools', 'vim'))
49 from ninja_output import GetNinjaOutputDirectory
50 return GetNinjaOutputDirectory(chrome_root, configuration)
52 def compute_ninja_command_for_current_buffer(configuration=None):
53 """Returns the shell command to compile the file in the current buffer."""
54 build_dir = path_to_build_dir(configuration)
56 # ninja needs filepaths for the ^ syntax to be relative to the
58 file_to_build = path_to_current_buffer()
59 file_to_build = os.path.relpath(file_to_build, build_dir)
61 build_cmd = ' '.join(['ninja', '-C', build_dir, file_to_build + '^'])
62 if sys.platform == 'win32':
63 # Escape \ for Vim, and ^ for both Vim and shell.
64 build_cmd = build_cmd.replace('\\', '\\\\').replace('^', '^^^^')
65 vim.command('return "%s"' % build_cmd)
68 def compute_ninja_command_for_targets(targets='', configuration=None):
69 build_cmd = ' '.join(['ninja', '-C', path_to_build_dir(configuration),
71 vim.command('return "%s"' % build_cmd)
74 fun! s:MakeWithCustomCommand(build_cmd)
75 let l:oldmakepgr = &makeprg
76 let &makeprg=a:build_cmd
77 if exists(':Make') == 2
82 if !has('gui_running')
85 let &makeprg = l:oldmakepgr
88 fun! s:NinjaCommandForCurrentBuffer()
89 python compute_ninja_command_for_current_buffer()
92 fun! s:NinjaCommandForTargets(targets)
93 python compute_ninja_command_for_targets(vim.eval('a:targets'))
97 call s:MakeWithCustomCommand(s:NinjaCommandForCurrentBuffer())
101 let l:targets = a:0 > 0 ? join(a:000, ' ') : ''
102 if (l:targets !~ '\i')
103 let l:targets = 'chrome'
105 call s:MakeWithCustomCommand(s:NinjaCommandForTargets(l:targets))
108 command! CrCompileFile call CrCompileFile()
109 command! -nargs=* CrBuild call CrBuild(<q-args>)
112 map <D-k> :CrCompileFile<cr>
113 imap <D-k> <esc>:CrCompileFile<cr>
115 map <C-F7> :CrCompileFile<cr>
116 imap <C-F7> <esc>:CrCompileFile<cr>
118 map <Leader>o :CrCompileFile<cr>