1#!/bin/bash 2# Copyright 2012 the V8 project authors. All rights reserved. 3# Redistribution and use in source and binary forms, with or without 4# modification, are permitted provided that the following conditions are 5# met: 6# 7# * Redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer. 9# * Redistributions in binary form must reproduce the above 10# copyright notice, this list of conditions and the following 11# disclaimer in the documentation and/or other materials provided 12# with the distribution. 13# * Neither the name of Google Inc. nor the names of its 14# contributors may be used to endorse or promote products derived 15# from this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29# Inspired by and based on: 30# https://chromium.googlesource.com/chromium/src/+/master/tools/bash-completion 31 32# Flag completion rule for bash. 33# To load in your shell, "source path/to/this/file". 34 35v8_source="$(realpath "$(dirname "$BASH_SOURCE")"/..)" 36 37_get_v8_flags() { 38 # The first `sed` command joins lines when a line ends with '('. 39 # See http://sed.sourceforge.net/sedfaq3.html#s3.2 40 local flags_file="$v8_source/src/flags/flag-definitions.h" 41 local defines=$( \ 42 sed -e :a -e '/($/N; s/(\n\s*/(/; ta' < "$flags_file" \ 43 | grep "^DEFINE" \ 44 | grep -v "DEFINE_IMPLICATION" \ 45 | grep -v "DEFINE_NEG_IMPLICATION" \ 46 | grep -v "DEFINE_VALUE_IMPLICATION" \ 47 | sed -e 's/_/-/g'; \ 48 grep "^ V(harmony_" "$flags_file" \ 49 | sed -e 's/^ V/DEFINE-BOOL/' \ 50 | sed -e 's/_/-/g'; \ 51 grep "^ V(" "$v8_source/src/wasm/wasm-feature-flags.h" \ 52 | sed -e 's/^ V(/DEFINE-BOOL(experimental-wasm-/' \ 53 | sed -e 's/_/-/g') 54 sed -ne 's/^DEFINE-[^(]*(\([^,]*\).*/--\1/p' <<< "$defines" 55 sed -ne 's/^DEFINE-BOOL(\([^,]*\).*/--no\1/p' <<< "$defines" 56} 57 58_get_d8_flags() { 59 grep "strcmp(argv\[i\]" "$v8_source/src/d8/d8.cc" \ 60 | sed -ne 's/^[^"]*"--\([^"]*\)".*/--\1/p' 61} 62 63_d8_flag() { 64 local targets 65 targets=$(_get_v8_flags ; _get_d8_flags) 66 COMPREPLY=($(compgen -W "$targets" -- "${COMP_WORDS[COMP_CWORD]}")) 67 return 0 68} 69 70_test_flag() { 71 local targets 72 targets=$(_get_v8_flags) 73 COMPREPLY=($(compgen -W "$targets" -- "${COMP_WORDS[COMP_CWORD]}")) 74 return 0 75} 76 77complete -F _d8_flag -f d8 v8 v8-debug 78complete -F _test_flag -f cctest unittests 79 80# Many distros set up their own GDB completion scripts. The logic below is 81# careful to wrap any such functions (with additional logic), rather than 82# overwriting them. 83# An additional complication is that tested distros dynamically load their 84# completion scripts on first use. So in order to be able to detect their 85# presence, we have to force-load them first. 86_maybe_setup_gdb_completions() { 87 # We only want to set up the wrapping once: 88 [[ -n "$_ORIGINAL_GDB_COMPLETIONS" ]] && return 0; 89 90 # This path works for Debian, Ubuntu, and Gentoo; other distros unknown. 91 # Feel free to submit patches to extend the logic. 92 local GDB_COMP 93 for GDB_COMP in "/usr/share/bash-completion/completions/gdb"; do 94 [[ -f "$GDB_COMP" ]] && source $GDB_COMP 95 done 96 _ORIGINAL_GDB_COMPLETIONS="$(complete -p gdb 2>/dev/null \ 97 | sed -e 's/^.*-F \([^ ]*\).*/\1/')" 98 99 _gdb_v8_flag() { 100 local c i next 101 for (( i=1; i<$(($COMP_CWORD-1)); i++ )); do 102 c=${COMP_WORDS[$i]} 103 if [ "$c" = "-args" ] || [ "$c" = "--args" ] || [ "$c" == "--" ]; then 104 next=$(basename -- ${COMP_WORDS[$(($i+1))]}) 105 if [ "$next" = "d8" ] ; then 106 _d8_flag 107 return 0 108 elif [ "$next" = "unittests" ] || [ "$next" = "cctest" ]; then 109 _test_flag 110 return 0 111 fi 112 fi 113 done 114 [[ -n "$_ORIGINAL_GDB_COMPLETIONS" ]] && $_ORIGINAL_GDB_COMPLETIONS 115 return 0 116 } 117 complete -F _gdb_v8_flag -f gdb 118} 119_maybe_setup_gdb_completions 120unset _maybe_setup_gdb_completions 121 122_get_gm_flags() { 123 "$v8_source/tools/dev/gm.py" --print-completions 124 125 # cctest ignore directory structure, it's always "cctest/filename". 126 find "$v8_source/test/cctest/" -type f -name 'test-*' | \ 127 xargs basename -a -s ".cc" | \ 128 while read -r item; do echo "cctest/$item/*"; done 129} 130 131_gm_flag() { 132 local targets=$(_get_gm_flags) 133 COMPREPLY=($(compgen -W "$targets" -- "${COMP_WORDS[COMP_CWORD]}")) 134 return 0 135} 136 137# gm might be an alias, based on https://v8.dev/docs/build-gn#gm. 138complete -F _gm_flag gm.py gm 139