[vim] Update vim scripts

Mostly a port of changes from chromium to support building from
directories other than out/Release or out/Debug.

Also moved tools/ninja/ninja_output.py to tools/vim/ninja_output.py to
be more consistent with chromium's layout.

Change-Id: I73199f10e48b6d9a229bef6af14b84b1ae6be1c2
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2797536
Reviewed-by: Benedikt Meurer <bmeurer@chromium.org>
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#73777}
This commit is contained in:
Ng Zhi An 2021-03-31 10:57:25 -07:00 committed by Commit Bot
parent 599f4ae1ca
commit 31663e6a32
3 changed files with 81 additions and 49 deletions

View File

@ -1,44 +0,0 @@
# Copyright 2015 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import os.path
def GetNinjaOutputDirectory(v8_root, configuration=None):
"""Returns <v8_root>/<output_dir>/(Release|Debug).
The configuration chosen is the one most recently generated/built, but can be
overriden via the <configuration> parameter. Detects a custom output_dir
specified by GYP_GENERATOR_FLAGS."""
output_dir = 'out'
generator_flags = os.getenv('GYP_GENERATOR_FLAGS', '').split(' ')
for flag in generator_flags:
name_value = flag.split('=', 1)
if len(name_value) == 2 and name_value[0] == 'output_dir':
output_dir = name_value[1]
root = os.path.join(v8_root, output_dir)
if configuration:
return os.path.join(root, configuration)
debug_path = os.path.join(root, 'Debug')
release_path = os.path.join(root, 'Release')
def is_release_newer(test_path):
try:
debug_mtime = os.path.getmtime(os.path.join(debug_path, test_path))
except os.error:
debug_mtime = 0
try:
rel_mtime = os.path.getmtime(os.path.join(release_path, test_path))
except os.error:
rel_mtime = 0
return rel_mtime >= debug_mtime
if is_release_newer('.ninja_log') or is_release_newer('.ninja_deps'):
return release_path
return debug_path

View File

@ -19,7 +19,7 @@
" Add the following to your .vimrc file:
" so /path/to/src/tools/vim/ninja-build.vim
python << endpython
pythonx << endpython
import os
import vim
@ -47,7 +47,7 @@ def path_to_build_dir(configuration):
"""Returns <v8_root>/<output_dir>/(Release|Debug)."""
v8_root = path_to_source_root()
sys.path.append(os.path.join(v8_root, 'tools', 'ninja'))
sys.path.append(os.path.join(v8_root, 'tools', 'vim'))
from ninja_output import GetNinjaOutputDirectory
return GetNinjaOutputDirectory(v8_root, configuration)
@ -75,7 +75,11 @@ endpython
fun! s:MakeWithCustomCommand(build_cmd)
let l:oldmakepgr = &makeprg
let &makeprg=a:build_cmd
silent make | cwindow
if exists(':Make') == 2
Make
else
silent make | cwindow
endif
if !has('gui_running')
redraw!
endif
@ -83,11 +87,11 @@ fun! s:MakeWithCustomCommand(build_cmd)
endfun
fun! s:NinjaCommandForCurrentBuffer()
python compute_ninja_command_for_current_buffer()
pythonx compute_ninja_command_for_current_buffer()
endfun
fun! s:NinjaCommandForTargets(targets)
python compute_ninja_command_for_targets(vim.eval('a:targets'))
pythonx compute_ninja_command_for_targets(vim.eval('a:targets'))
endfun
fun! CrCompileFile()

72
tools/vim/ninja_output.py Normal file
View File

@ -0,0 +1,72 @@
# Copyright 2015 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import print_function
import sys
import os
import itertools
import re
try:
from exceptions import RuntimeError
except ImportError:
pass
def GetNinjaOutputDirectory(v8_root, configuration=None):
"""Returns <v8_root>/<output_dir>/(Release|Debug|<other>).
The configuration chosen is the one most recently generated/built, but can be
overriden via the <configuration> parameter. Detects a custom output_dir
specified by GYP_GENERATOR_FLAGS."""
output_dirs = []
generator_flags = os.getenv('GYP_GENERATOR_FLAGS', '').split(' ')
for flag in generator_flags:
name_value = flag.split('=', 1)
if (len(name_value) == 2 and name_value[0] == 'output_dir' and
os.path.isdir(os.path.join(v8_root, name_value[1]))):
output_dirs = [name_value[1]]
if configuration:
output_dir = 'out' if len(output_dirs) == 0 else output_dirs[-1]
return os.path.join(os.path.join(v8_root, output_dir), configuration)
if not output_dirs:
for f in os.listdir(v8_root):
if re.match(r'out(\b|_)', f):
if os.path.isdir(os.path.join(v8_root, f)):
output_dirs.append(f)
def generate_paths():
for out_dir in output_dirs:
out_path = os.path.join(v8_root, out_dir)
for config in os.listdir(out_path):
path = os.path.join(out_path, config)
if os.path.exists(os.path.join(path, 'build.ninja')):
yield path
def approx_directory_mtime(path):
# This is a heuristic; don't recurse into subdirectories.
paths = [path] + [os.path.join(path, f) for f in os.listdir(path)]
return max(filter(None, [safe_mtime(p) for p in paths]))
def safe_mtime(path):
try:
return os.path.getmtime(path)
except OSError:
return None
try:
return max(generate_paths(), key=approx_directory_mtime)
except ValueError:
raise RuntimeError('Unable to find a valid ninja output directory.')
if __name__ == '__main__':
if len(sys.argv) != 2:
raise RuntimeError('Expected a single path argument.')
print(GetNinjaOutputDirectory(sys.argv[1]))