bash-completion.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. # Inspired by and based on:
  29. # https://chromium.googlesource.com/chromium/src/+/master/tools/bash-completion
  30. # Flag completion rule for bash.
  31. # To load in your shell, "source path/to/this/file".
  32. v8_source="$(realpath "$(dirname "$BASH_SOURCE")"/..)"
  33. _get_v8_flags() {
  34. # The first `sed` command joins lines when a line ends with '('.
  35. # See http://sed.sourceforge.net/sedfaq3.html#s3.2
  36. local flags_file="$v8_source/src/flags/flag-definitions.h"
  37. local defines=$( \
  38. sed -e :a -e '/($/N; s/(\n\s*/(/; ta' < "$flags_file" \
  39. | grep "^DEFINE" \
  40. | grep -v "DEFINE_IMPLICATION" \
  41. | grep -v "DEFINE_NEG_IMPLICATION" \
  42. | grep -v "DEFINE_VALUE_IMPLICATION" \
  43. | sed -e 's/_/-/g'; \
  44. grep "^ V(harmony_" "$flags_file" \
  45. | sed -e 's/^ V/DEFINE-BOOL/' \
  46. | sed -e 's/_/-/g'; \
  47. grep "^ V(" "$v8_source/src/wasm/wasm-feature-flags.h" \
  48. | sed -e 's/^ V(/DEFINE-BOOL(experimental-wasm-/' \
  49. | sed -e 's/_/-/g')
  50. sed -ne 's/^DEFINE-[^(]*(\([^,]*\).*/--\1/p' <<< "$defines"
  51. sed -ne 's/^DEFINE-BOOL(\([^,]*\).*/--no\1/p' <<< "$defines"
  52. }
  53. _get_d8_flags() {
  54. grep "strcmp(argv\[i\]" "$v8_source/src/d8/d8.cc" \
  55. | sed -ne 's/^[^"]*"--\([^"]*\)".*/--\1/p'
  56. }
  57. _d8_flag() {
  58. local targets
  59. targets=$(_get_v8_flags ; _get_d8_flags)
  60. COMPREPLY=($(compgen -W "$targets" -- "${COMP_WORDS[COMP_CWORD]}"))
  61. return 0
  62. }
  63. _test_flag() {
  64. local targets
  65. targets=$(_get_v8_flags)
  66. COMPREPLY=($(compgen -W "$targets" -- "${COMP_WORDS[COMP_CWORD]}"))
  67. return 0
  68. }
  69. complete -F _d8_flag -f d8 v8 v8-debug
  70. complete -F _test_flag -f cctest unittests
  71. # Many distros set up their own GDB completion scripts. The logic below is
  72. # careful to wrap any such functions (with additional logic), rather than
  73. # overwriting them.
  74. # An additional complication is that tested distros dynamically load their
  75. # completion scripts on first use. So in order to be able to detect their
  76. # presence, we have to force-load them first.
  77. _maybe_setup_gdb_completions() {
  78. # We only want to set up the wrapping once:
  79. [[ -n "$_ORIGINAL_GDB_COMPLETIONS" ]] && return 0;
  80. # This path works for Debian, Ubuntu, and Gentoo; other distros unknown.
  81. # Feel free to submit patches to extend the logic.
  82. local GDB_COMP
  83. for GDB_COMP in "/usr/share/bash-completion/completions/gdb"; do
  84. [[ -f "$GDB_COMP" ]] && source $GDB_COMP
  85. done
  86. _ORIGINAL_GDB_COMPLETIONS="$(complete -p gdb 2>/dev/null \
  87. | sed -e 's/^.*-F \([^ ]*\).*/\1/')"
  88. _gdb_v8_flag() {
  89. local c i next
  90. for (( i=1; i<$(($COMP_CWORD-1)); i++ )); do
  91. c=${COMP_WORDS[$i]}
  92. if [ "$c" = "-args" ] || [ "$c" = "--args" ] || [ "$c" == "--" ]; then
  93. next=$(basename -- ${COMP_WORDS[$(($i+1))]})
  94. if [ "$next" = "d8" ] ; then
  95. _d8_flag
  96. return 0
  97. elif [ "$next" = "unittests" ] || [ "$next" = "cctest" ]; then
  98. _test_flag
  99. return 0
  100. fi
  101. fi
  102. done
  103. [[ -n "$_ORIGINAL_GDB_COMPLETIONS" ]] && $_ORIGINAL_GDB_COMPLETIONS
  104. return 0
  105. }
  106. complete -F _gdb_v8_flag -f gdb
  107. }
  108. _maybe_setup_gdb_completions
  109. unset _maybe_setup_gdb_completions
  110. _get_gm_flags() {
  111. "$v8_source/tools/dev/gm.py" --print-completions
  112. # cctest ignore directory structure, it's always "cctest/filename".
  113. find "$v8_source/test/cctest/" -type f -name 'test-*' | \
  114. xargs basename -a -s ".cc" | \
  115. while read -r item; do echo "cctest/$item/*"; done
  116. }
  117. _gm_flag() {
  118. local targets=$(_get_gm_flags)
  119. COMPREPLY=($(compgen -W "$targets" -- "${COMP_WORDS[COMP_CWORD]}"))
  120. return 0
  121. }
  122. # gm might be an alias, based on https://v8.dev/docs/build-gn#gm.
  123. complete -F _gm_flag gm.py gm