1" Copyright (c) 2015 the V8 project 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 'd8' by default, but :CrBuild target1 target2 etc works as well,
12" i.e. :CrBuild all or :CrBuild d8 cctest unittests.
13"
14" Requires that gyp has already generated build.ninja files, and that ninja is
15" in your path (which it is automatically if depot_tools is in your path).
16" Bumps the number of parallel jobs in ninja automatically if goma is
17" detected.
18"
19" Add the following to your .vimrc file:
20"     so /path/to/src/tools/vim/ninja-build.vim
21
22pythonx << endpython
23import os
24import vim
25
26
27def path_to_current_buffer():
28  """Returns the absolute path of the current buffer."""
29  return vim.current.buffer.name
30
31
32def path_to_source_root():
33  """Returns the absolute path to the V8 source root."""
34  candidate = os.path.dirname(path_to_current_buffer())
35  # This is a list of files that need to identify the src directory. The shorter
36  # it is, the more likely it's wrong. The longer it is, the more likely it is to
37  # break when we rename directories.
38  fingerprints = ['.git', 'build', 'include', 'samples', 'src', 'testing',
39                  'third_party', 'tools']
40  while candidate and not all(
41      [os.path.isdir(os.path.join(candidate, fp)) for fp in fingerprints]):
42    candidate = os.path.dirname(candidate)
43  return candidate
44
45
46def path_to_build_dir(configuration):
47  """Returns <v8_root>/<output_dir>/(Release|Debug)."""
48
49  v8_root = path_to_source_root()
50  sys.path.append(os.path.join(v8_root, 'tools', 'vim'))
51  from ninja_output import GetNinjaOutputDirectory
52  return GetNinjaOutputDirectory(v8_root, configuration)
53
54
55def compute_ninja_command_for_targets(targets='', configuration=None):
56  build_dir = path_to_build_dir(configuration);
57  build_cmd = ' '.join(['autoninja', '-C', build_dir, targets])
58  vim.command('return "%s"' % build_cmd)
59
60
61def compute_ninja_command_for_current_buffer(configuration=None):
62  """Returns the shell command to compile the file in the current buffer."""
63  build_dir = path_to_build_dir(configuration)
64
65  # ninja needs filepaths for the ^ syntax to be relative to the
66  # build directory.
67  file_to_build = path_to_current_buffer()
68  file_to_build = os.path.relpath(file_to_build, build_dir) + '^'
69  if sys.platform == 'win32':
70    # Escape \ for Vim, and ^ for both Vim and shell.
71    file_to_build = file_to_build.replace('\\', '\\\\').replace('^', '^^^^')
72  compute_ninja_command_for_targets(file_to_build, configuration)
73endpython
74
75fun! s:MakeWithCustomCommand(build_cmd)
76  let l:oldmakepgr = &makeprg
77  let &makeprg=a:build_cmd
78  if exists(':Make') == 2
79    Make
80  else
81    silent make | cwindow
82  endif
83  if !has('gui_running')
84    redraw!
85  endif
86  let &makeprg = l:oldmakepgr
87endfun
88
89fun! s:NinjaCommandForCurrentBuffer()
90  pythonx compute_ninja_command_for_current_buffer()
91endfun
92
93fun! s:NinjaCommandForTargets(targets)
94  pythonx compute_ninja_command_for_targets(vim.eval('a:targets'))
95endfun
96
97fun! CrCompileFile()
98  call s:MakeWithCustomCommand(s:NinjaCommandForCurrentBuffer())
99endfun
100
101fun! CrBuild(...)
102  let l:targets = a:0 > 0 ? join(a:000, ' ') : ''
103  if (l:targets !~ '\i')
104    let l:targets = 'd8'
105  endif
106  call s:MakeWithCustomCommand(s:NinjaCommandForTargets(l:targets))
107endfun
108
109command! CrCompileFile call CrCompileFile()
110command! -nargs=* CrBuild call CrBuild(<q-args>)
111
112if has('mac')
113  map <D-k> :CrCompileFile<cr>
114  imap <D-k> <esc>:CrCompileFile<cr>
115elseif has('win32')
116  map <C-F7> :CrCompileFile<cr>
117  imap <C-F7> <esc>:CrCompileFile<cr>
118elseif has('unix')
119  map <Leader>o :CrCompileFile<cr>
120endif
121