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 guess_configuration():
45 """Default to the configuration with either a newer build.ninja or a newer
47 root = os.path.join(path_to_source_root(), 'out')
48 def is_release_15s_newer(test_path):
50 debug_mtime = os.path.getmtime(os.path.join(root, 'Debug', test_path))
54 rel_mtime = os.path.getmtime(os.path.join(root, 'Release', test_path))
57 return rel_mtime - debug_mtime >= 15
58 configuration = 'Debug'
59 if is_release_15s_newer('build.ninja') or is_release_15s_newer('protoc'):
60 configuration = 'Release'
64 def compute_ninja_command_for_current_buffer(configuration=None):
65 """Returns the shell command to compile the file in the current buffer."""
66 if not configuration: configuration = guess_configuration()
67 build_dir = os.path.join(path_to_source_root(), 'out', configuration)
69 # ninja needs filepaths for the ^ syntax to be relative to the
71 file_to_build = path_to_current_buffer()
72 file_to_build = os.path.relpath(file_to_build, build_dir)
74 build_cmd = ' '.join(['ninja', '-C', build_dir, file_to_build + '^'])
75 if sys.platform == 'win32':
76 # Escape \ for Vim, and ^ for both Vim and shell.
77 build_cmd = build_cmd.replace('\\', '\\\\').replace('^', '^^^^')
78 vim.command('return "%s"' % build_cmd)
81 def compute_ninja_command_for_targets(targets='', configuration=None):
82 if not configuration: configuration = guess_configuration()
83 build_dir = os.path.join(path_to_source_root(), 'out', configuration)
84 build_cmd = ' '.join(['ninja', '-C', build_dir, targets])
85 vim.command('return "%s"' % build_cmd)
88 fun! s:MakeWithCustomCommand(build_cmd)
89 let l:oldmakepgr = &makeprg
90 let &makeprg=a:build_cmd
92 if !has('gui_running')
95 let &makeprg = l:oldmakepgr
98 fun! s:NinjaCommandForCurrentBuffer()
99 python compute_ninja_command_for_current_buffer()
102 fun! s:NinjaCommandForTargets(targets)
103 python compute_ninja_command_for_targets(vim.eval('a:targets'))
107 call s:MakeWithCustomCommand(s:NinjaCommandForCurrentBuffer())
111 let l:targets = a:0 > 0 ? join(a:000, ' ') : ''
112 if (l:targets !~ '\i')
113 let l:targets = 'chrome'
115 call s:MakeWithCustomCommand(s:NinjaCommandForTargets(l:targets))
118 command! CrCompileFile call CrCompileFile()
119 command! -nargs=* CrBuild call CrBuild(<q-args>)
122 map <D-k> :CrCompileFile<cr>
123 imap <D-k> <esc>:CrCompileFile<cr>
125 map <C-F7> :CrCompileFile<cr>
126 imap <C-F7> <esc>:CrCompileFile<cr>
128 map <Leader>o :CrCompileFile<cr>