Home | History | Annotate | Download | only in vim
      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 
     22 python << endpython
     23 import os
     24 import vim
     25 
     26 
     27 def path_to_current_buffer():
     28   """Returns the absolute path of the current buffer."""
     29   return vim.current.buffer.name
     30 
     31 
     32 def 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 
     46 def 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', 'ninja'))
     51   from ninja_output import GetNinjaOutputDirectory
     52   return GetNinjaOutputDirectory(v8_root, configuration)
     53 
     54 
     55 def compute_ninja_command_for_targets(targets='', configuration=None):
     56   flags = []
     57   if "use_goma=1" in os.getenv('GYP_DEFINES', '').split(' '):
     58     flags = ['-j', '512']
     59   build_dir = path_to_build_dir(configuration);
     60   build_cmd = ' '.join(['ninja'] + flags + ['-C', build_dir, targets])
     61   vim.command('return "%s"' % build_cmd)
     62 
     63 
     64 def compute_ninja_command_for_current_buffer(configuration=None):
     65   """Returns the shell command to compile the file in the current buffer."""
     66   build_dir = path_to_build_dir(configuration)
     67 
     68   # ninja needs filepaths for the ^ syntax to be relative to the
     69   # build directory.
     70   file_to_build = path_to_current_buffer()
     71   file_to_build = os.path.relpath(file_to_build, build_dir) + '^'
     72   if sys.platform == 'win32':
     73     # Escape \ for Vim, and ^ for both Vim and shell.
     74     file_to_build = file_to_build.replace('\\', '\\\\').replace('^', '^^^^')
     75   compute_ninja_command_for_targets(file_to_build, configuration)
     76 endpython
     77 
     78 fun! s:MakeWithCustomCommand(build_cmd)
     79   let l:oldmakepgr = &makeprg
     80   let &makeprg=a:build_cmd
     81   silent make | cwindow
     82   if !has('gui_running')
     83     redraw!
     84   endif
     85   let &makeprg = l:oldmakepgr
     86 endfun
     87 
     88 fun! s:NinjaCommandForCurrentBuffer()
     89   python compute_ninja_command_for_current_buffer()
     90 endfun
     91 
     92 fun! s:NinjaCommandForTargets(targets)
     93   python compute_ninja_command_for_targets(vim.eval('a:targets'))
     94 endfun
     95 
     96 fun! CrCompileFile()
     97   call s:MakeWithCustomCommand(s:NinjaCommandForCurrentBuffer())
     98 endfun
     99 
    100 fun! CrBuild(...)
    101   let l:targets = a:0 > 0 ? join(a:000, ' ') : ''
    102   if (l:targets !~ '\i')
    103     let l:targets = 'd8'
    104   endif
    105   call s:MakeWithCustomCommand(s:NinjaCommandForTargets(l:targets))
    106 endfun
    107 
    108 command! CrCompileFile call CrCompileFile()
    109 command! -nargs=* CrBuild call CrBuild(<q-args>)
    110 
    111 if has('mac')
    112   map <D-k> :CrCompileFile<cr>
    113   imap <D-k> <esc>:CrCompileFile<cr>
    114 elseif has('win32')
    115   map <C-F7> :CrCompileFile<cr>
    116   imap <C-F7> <esc>:CrCompileFile<cr>
    117 elseif has('unix')
    118   map <Leader>o :CrCompileFile<cr>
    119 endif
    120