Skip to content

Commit

Permalink
Add a CrBuild command to ninja-build.vim
Browse files Browse the repository at this point in the history
BUG=none

Review URL: https://codereview.chromium.org/11090084

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@162079 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
enne@chromium.org committed Oct 16, 2012
1 parent 169adeb commit 91b43bf
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions tools/vim/ninja-build.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
" Adds a "Compile this file" function, using ninja. On Mac, binds Cmd-k to
" this command. On Windows, Ctrl-F7 (which is the same as the VS default).
"
" Adds a "Build this target" function, using ninja. This is not bound
" to any key by default, but can be used via the :CrBuild command.
" It builds 'chrome' by default, but :CrBuild target1 target2 etc works as well.
"
" Requires that gyp has already generated build.ninja files, and that ninja is
" in your path (which it is automatically if depot_tools is in your path).
"
Expand Down Expand Up @@ -56,7 +60,7 @@ def guess_configuration():
return configuration


def compute_ninja_command(configuration=None):
def compute_ninja_command_for_current_buffer(configuration=None):
"""Returns the shell command to compile the file in the current buffer."""
if not configuration: configuration = guess_configuration()
build_dir = os.path.join(path_to_source_root(), 'out', configuration)
Expand All @@ -66,28 +70,52 @@ def compute_ninja_command(configuration=None):
file_to_build = path_to_current_buffer()
file_to_build = os.path.relpath(file_to_build, build_dir)

return ' '.join(['ninja', '-C', build_dir, file_to_build + '^'])


def set_makepgr_to_single_file_ninja():
build_cmd = compute_ninja_command()
build_cmd = ' '.join(['ninja', '-C', build_dir, file_to_build + '^'])
if sys.platform == 'win32':
# Escape \ for Vim, and ^ for both Vim and shell.
build_cmd = build_cmd.replace('\\', '\\\\').replace('^', '^^^^')
vim.command('let &makeprg=\'%s\'' % build_cmd)
vim.command('return "%s"' % build_cmd)


def compute_ninja_command_for_targets(targets='', configuration=None):
if not configuration: configuration = guess_configuration()
build_dir = os.path.join(path_to_source_root(), 'out', configuration)
build_cmd = ' '.join(['ninja', '-C', build_dir, targets])
vim.command('return "%s"' % build_cmd)
endpython

fun! CrCompileFile()
fun! s:MakeWithCustomCommand(build_cmd)
let l:oldmakepgr = &makeprg
python set_makepgr_to_single_file_ninja()
let &makeprg=a:build_cmd
silent make | cwindow
if !has('gui_running')
redraw!
endif
let &makeprg = l:oldmakepgr
endfun

fun! s:NinjaCommandForCurrentBuffer()
python compute_ninja_command_for_current_buffer()
endfun

fun! s:NinjaCommandForTargets(targets)
python compute_ninja_command_for_targets(vim.eval('a:targets'))
endfun

fun! CrCompileFile()
call s:MakeWithCustomCommand(s:NinjaCommandForCurrentBuffer())
endfun

fun! CrBuild(...)
let l:targets = a:0 > 0 ? join(a:000, ' ') : ''
if (l:targets !~ "\i")
let l:targets = 'chrome'
endif
call s:MakeWithCustomCommand(s:NinjaCommandForTargets(l:targets))
endfun

command! CrCompileFile call CrCompileFile()
command! -nargs=* CrBuild call CrBuild(<q-args>)

if has('mac')
map <D-k> :CrCompileFile<cr>
Expand Down

0 comments on commit 91b43bf

Please sign in to comment.