Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / tools / vim / ninja-build.vim
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.
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 'chrome' by default, but :CrBuild target1 target2 etc works as well.
12 "
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).
15 "
16 " Add the following to your .vimrc file:
17 "     so /path/to/src/tools/vim/ninja-build.vim
18
19 python << endpython
20 import os
21 import vim
22
23
24 def path_to_current_buffer():
25   """Returns the absolute path of the current buffer."""
26   return vim.current.buffer.name
27
28
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
36   # directories.
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)
41   return candidate
42
43
44 def path_to_build_dir(configuration):
45   """Returns <chrome_root>/<output_dir>/(Release|Debug)."""
46
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)
51
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)
55
56   # ninja needs filepaths for the ^ syntax to be relative to the
57   # build directory.
58   file_to_build = path_to_current_buffer()
59   file_to_build = os.path.relpath(file_to_build, build_dir)
60
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)
66
67
68 def compute_ninja_command_for_targets(targets='', configuration=None):
69   build_cmd = ' '.join(['ninja', '-C', path_to_build_dir(configuration),
70                         targets])
71   vim.command('return "%s"' % build_cmd)
72 endpython
73
74 fun! s:MakeWithCustomCommand(build_cmd)
75   let l:oldmakepgr = &makeprg
76   let &makeprg=a:build_cmd
77   silent make | cwindow
78   if !has('gui_running')
79     redraw!
80   endif
81   let &makeprg = l:oldmakepgr
82 endfun
83
84 fun! s:NinjaCommandForCurrentBuffer()
85   python compute_ninja_command_for_current_buffer()
86 endfun
87
88 fun! s:NinjaCommandForTargets(targets)
89   python compute_ninja_command_for_targets(vim.eval('a:targets'))
90 endfun
91
92 fun! CrCompileFile()
93   call s:MakeWithCustomCommand(s:NinjaCommandForCurrentBuffer())
94 endfun
95
96 fun! CrBuild(...)
97   let l:targets = a:0 > 0 ? join(a:000, ' ') : ''
98   if (l:targets !~ '\i')
99     let l:targets = 'chrome'
100   endif
101   call s:MakeWithCustomCommand(s:NinjaCommandForTargets(l:targets))
102 endfun
103
104 command! CrCompileFile call CrCompileFile()
105 command! -nargs=* CrBuild call CrBuild(<q-args>)
106
107 if has('mac')
108   map <D-k> :CrCompileFile<cr>
109   imap <D-k> <esc>:CrCompileFile<cr>
110 elseif has('win32')
111   map <C-F7> :CrCompileFile<cr>
112   imap <C-F7> <esc>:CrCompileFile<cr>
113 elseif has('unix')
114   map <Leader>o :CrCompileFile<cr>
115 endif