11cb0ef41Sopenharmony_ci" Copyright (c) 2015 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci" Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci" found in the LICENSE file.
41cb0ef41Sopenharmony_ci"
51cb0ef41Sopenharmony_ci" Adds a "Compile this file" function, using ninja. On Mac, binds Cmd-k to
61cb0ef41Sopenharmony_ci" this command. On Windows, Ctrl-F7 (which is the same as the VS default).
71cb0ef41Sopenharmony_ci" On Linux, <Leader>o, which is \o by default ("o"=creates .o files)
81cb0ef41Sopenharmony_ci"
91cb0ef41Sopenharmony_ci" Adds a "Build this target" function, using ninja. This is not bound
101cb0ef41Sopenharmony_ci" to any key by default, but can be used via the :CrBuild command.
111cb0ef41Sopenharmony_ci" It builds 'd8' by default, but :CrBuild target1 target2 etc works as well,
121cb0ef41Sopenharmony_ci" i.e. :CrBuild all or :CrBuild d8 cctest unittests.
131cb0ef41Sopenharmony_ci"
141cb0ef41Sopenharmony_ci" Requires that gyp has already generated build.ninja files, and that ninja is
151cb0ef41Sopenharmony_ci" in your path (which it is automatically if depot_tools is in your path).
161cb0ef41Sopenharmony_ci" Bumps the number of parallel jobs in ninja automatically if goma is
171cb0ef41Sopenharmony_ci" detected.
181cb0ef41Sopenharmony_ci"
191cb0ef41Sopenharmony_ci" Add the following to your .vimrc file:
201cb0ef41Sopenharmony_ci"     so /path/to/src/tools/vim/ninja-build.vim
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cipythonx << endpython
231cb0ef41Sopenharmony_ciimport os
241cb0ef41Sopenharmony_ciimport vim
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_cidef path_to_current_buffer():
281cb0ef41Sopenharmony_ci  """Returns the absolute path of the current buffer."""
291cb0ef41Sopenharmony_ci  return vim.current.buffer.name
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cidef path_to_source_root():
331cb0ef41Sopenharmony_ci  """Returns the absolute path to the V8 source root."""
341cb0ef41Sopenharmony_ci  candidate = os.path.dirname(path_to_current_buffer())
351cb0ef41Sopenharmony_ci  # This is a list of files that need to identify the src directory. The shorter
361cb0ef41Sopenharmony_ci  # it is, the more likely it's wrong. The longer it is, the more likely it is to
371cb0ef41Sopenharmony_ci  # break when we rename directories.
381cb0ef41Sopenharmony_ci  fingerprints = ['.git', 'build', 'include', 'samples', 'src', 'testing',
391cb0ef41Sopenharmony_ci                  'third_party', 'tools']
401cb0ef41Sopenharmony_ci  while candidate and not all(
411cb0ef41Sopenharmony_ci      [os.path.isdir(os.path.join(candidate, fp)) for fp in fingerprints]):
421cb0ef41Sopenharmony_ci    candidate = os.path.dirname(candidate)
431cb0ef41Sopenharmony_ci  return candidate
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cidef path_to_build_dir(configuration):
471cb0ef41Sopenharmony_ci  """Returns <v8_root>/<output_dir>/(Release|Debug)."""
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  v8_root = path_to_source_root()
501cb0ef41Sopenharmony_ci  sys.path.append(os.path.join(v8_root, 'tools', 'vim'))
511cb0ef41Sopenharmony_ci  from ninja_output import GetNinjaOutputDirectory
521cb0ef41Sopenharmony_ci  return GetNinjaOutputDirectory(v8_root, configuration)
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cidef compute_ninja_command_for_targets(targets='', configuration=None):
561cb0ef41Sopenharmony_ci  build_dir = path_to_build_dir(configuration);
571cb0ef41Sopenharmony_ci  build_cmd = ' '.join(['autoninja', '-C', build_dir, targets])
581cb0ef41Sopenharmony_ci  vim.command('return "%s"' % build_cmd)
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_cidef compute_ninja_command_for_current_buffer(configuration=None):
621cb0ef41Sopenharmony_ci  """Returns the shell command to compile the file in the current buffer."""
631cb0ef41Sopenharmony_ci  build_dir = path_to_build_dir(configuration)
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  # ninja needs filepaths for the ^ syntax to be relative to the
661cb0ef41Sopenharmony_ci  # build directory.
671cb0ef41Sopenharmony_ci  file_to_build = path_to_current_buffer()
681cb0ef41Sopenharmony_ci  file_to_build = os.path.relpath(file_to_build, build_dir) + '^'
691cb0ef41Sopenharmony_ci  if sys.platform == 'win32':
701cb0ef41Sopenharmony_ci    # Escape \ for Vim, and ^ for both Vim and shell.
711cb0ef41Sopenharmony_ci    file_to_build = file_to_build.replace('\\', '\\\\').replace('^', '^^^^')
721cb0ef41Sopenharmony_ci  compute_ninja_command_for_targets(file_to_build, configuration)
731cb0ef41Sopenharmony_ciendpython
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_cifun! s:MakeWithCustomCommand(build_cmd)
761cb0ef41Sopenharmony_ci  let l:oldmakepgr = &makeprg
771cb0ef41Sopenharmony_ci  let &makeprg=a:build_cmd
781cb0ef41Sopenharmony_ci  if exists(':Make') == 2
791cb0ef41Sopenharmony_ci    Make
801cb0ef41Sopenharmony_ci  else
811cb0ef41Sopenharmony_ci    silent make | cwindow
821cb0ef41Sopenharmony_ci  endif
831cb0ef41Sopenharmony_ci  if !has('gui_running')
841cb0ef41Sopenharmony_ci    redraw!
851cb0ef41Sopenharmony_ci  endif
861cb0ef41Sopenharmony_ci  let &makeprg = l:oldmakepgr
871cb0ef41Sopenharmony_ciendfun
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_cifun! s:NinjaCommandForCurrentBuffer()
901cb0ef41Sopenharmony_ci  pythonx compute_ninja_command_for_current_buffer()
911cb0ef41Sopenharmony_ciendfun
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_cifun! s:NinjaCommandForTargets(targets)
941cb0ef41Sopenharmony_ci  pythonx compute_ninja_command_for_targets(vim.eval('a:targets'))
951cb0ef41Sopenharmony_ciendfun
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_cifun! CrCompileFile()
981cb0ef41Sopenharmony_ci  call s:MakeWithCustomCommand(s:NinjaCommandForCurrentBuffer())
991cb0ef41Sopenharmony_ciendfun
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_cifun! CrBuild(...)
1021cb0ef41Sopenharmony_ci  let l:targets = a:0 > 0 ? join(a:000, ' ') : ''
1031cb0ef41Sopenharmony_ci  if (l:targets !~ '\i')
1041cb0ef41Sopenharmony_ci    let l:targets = 'd8'
1051cb0ef41Sopenharmony_ci  endif
1061cb0ef41Sopenharmony_ci  call s:MakeWithCustomCommand(s:NinjaCommandForTargets(l:targets))
1071cb0ef41Sopenharmony_ciendfun
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_cicommand! CrCompileFile call CrCompileFile()
1101cb0ef41Sopenharmony_cicommand! -nargs=* CrBuild call CrBuild(<q-args>)
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ciif has('mac')
1131cb0ef41Sopenharmony_ci  map <D-k> :CrCompileFile<cr>
1141cb0ef41Sopenharmony_ci  imap <D-k> <esc>:CrCompileFile<cr>
1151cb0ef41Sopenharmony_cielseif has('win32')
1161cb0ef41Sopenharmony_ci  map <C-F7> :CrCompileFile<cr>
1171cb0ef41Sopenharmony_ci  imap <C-F7> <esc>:CrCompileFile<cr>
1181cb0ef41Sopenharmony_cielseif has('unix')
1191cb0ef41Sopenharmony_ci  map <Leader>o :CrCompileFile<cr>
1201cb0ef41Sopenharmony_ciendif
121