11cb0ef41Sopenharmony_ci#!/bin/bash
21cb0ef41Sopenharmony_ci# Copyright 2012 the V8 project authors. All rights reserved.
31cb0ef41Sopenharmony_ci# Redistribution and use in source and binary forms, with or without
41cb0ef41Sopenharmony_ci# modification, are permitted provided that the following conditions are
51cb0ef41Sopenharmony_ci# met:
61cb0ef41Sopenharmony_ci#
71cb0ef41Sopenharmony_ci#     * Redistributions of source code must retain the above copyright
81cb0ef41Sopenharmony_ci#       notice, this list of conditions and the following disclaimer.
91cb0ef41Sopenharmony_ci#     * Redistributions in binary form must reproduce the above
101cb0ef41Sopenharmony_ci#       copyright notice, this list of conditions and the following
111cb0ef41Sopenharmony_ci#       disclaimer in the documentation and/or other materials provided
121cb0ef41Sopenharmony_ci#       with the distribution.
131cb0ef41Sopenharmony_ci#     * Neither the name of Google Inc. nor the names of its
141cb0ef41Sopenharmony_ci#       contributors may be used to endorse or promote products derived
151cb0ef41Sopenharmony_ci#       from this software without specific prior written permission.
161cb0ef41Sopenharmony_ci#
171cb0ef41Sopenharmony_ci# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
181cb0ef41Sopenharmony_ci# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191cb0ef41Sopenharmony_ci# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
201cb0ef41Sopenharmony_ci# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
211cb0ef41Sopenharmony_ci# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
221cb0ef41Sopenharmony_ci# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
231cb0ef41Sopenharmony_ci# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241cb0ef41Sopenharmony_ci# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251cb0ef41Sopenharmony_ci# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261cb0ef41Sopenharmony_ci# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
271cb0ef41Sopenharmony_ci# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci# Inspired by and based on:
301cb0ef41Sopenharmony_ci# https://chromium.googlesource.com/chromium/src/+/master/tools/bash-completion
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci# Flag completion rule for bash.
331cb0ef41Sopenharmony_ci# To load in your shell, "source path/to/this/file".
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_civ8_source="$(realpath "$(dirname "$BASH_SOURCE")"/..)"
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci_get_v8_flags() {
381cb0ef41Sopenharmony_ci  # The first `sed` command joins lines when a line ends with '('.
391cb0ef41Sopenharmony_ci  # See http://sed.sourceforge.net/sedfaq3.html#s3.2
401cb0ef41Sopenharmony_ci  local flags_file="$v8_source/src/flags/flag-definitions.h"
411cb0ef41Sopenharmony_ci  local defines=$( \
421cb0ef41Sopenharmony_ci    sed -e :a -e '/($/N; s/(\n\s*/(/; ta' < "$flags_file" \
431cb0ef41Sopenharmony_ci    | grep "^DEFINE" \
441cb0ef41Sopenharmony_ci    | grep -v "DEFINE_IMPLICATION" \
451cb0ef41Sopenharmony_ci    | grep -v "DEFINE_NEG_IMPLICATION" \
461cb0ef41Sopenharmony_ci    | grep -v "DEFINE_VALUE_IMPLICATION" \
471cb0ef41Sopenharmony_ci    | sed -e 's/_/-/g'; \
481cb0ef41Sopenharmony_ci    grep "^  V(harmony_" "$flags_file" \
491cb0ef41Sopenharmony_ci    | sed -e 's/^  V/DEFINE-BOOL/' \
501cb0ef41Sopenharmony_ci    | sed -e 's/_/-/g'; \
511cb0ef41Sopenharmony_ci    grep "^  V(" "$v8_source/src/wasm/wasm-feature-flags.h" \
521cb0ef41Sopenharmony_ci    | sed -e 's/^  V(/DEFINE-BOOL(experimental-wasm-/' \
531cb0ef41Sopenharmony_ci    | sed -e 's/_/-/g')
541cb0ef41Sopenharmony_ci  sed -ne 's/^DEFINE-[^(]*(\([^,]*\).*/--\1/p' <<< "$defines"
551cb0ef41Sopenharmony_ci  sed -ne 's/^DEFINE-BOOL(\([^,]*\).*/--no\1/p' <<< "$defines"
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci_get_d8_flags() {
591cb0ef41Sopenharmony_ci  grep "strcmp(argv\[i\]" "$v8_source/src/d8/d8.cc" \
601cb0ef41Sopenharmony_ci    | sed -ne 's/^[^"]*"--\([^"]*\)".*/--\1/p'
611cb0ef41Sopenharmony_ci}
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci_d8_flag() {
641cb0ef41Sopenharmony_ci  local targets
651cb0ef41Sopenharmony_ci  targets=$(_get_v8_flags ; _get_d8_flags)
661cb0ef41Sopenharmony_ci  COMPREPLY=($(compgen -W "$targets" -- "${COMP_WORDS[COMP_CWORD]}"))
671cb0ef41Sopenharmony_ci  return 0
681cb0ef41Sopenharmony_ci}
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci_test_flag() {
711cb0ef41Sopenharmony_ci  local targets
721cb0ef41Sopenharmony_ci  targets=$(_get_v8_flags)
731cb0ef41Sopenharmony_ci  COMPREPLY=($(compgen -W "$targets" -- "${COMP_WORDS[COMP_CWORD]}"))
741cb0ef41Sopenharmony_ci  return 0
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_cicomplete -F _d8_flag -f d8 v8 v8-debug
781cb0ef41Sopenharmony_cicomplete -F _test_flag -f cctest unittests
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci# Many distros set up their own GDB completion scripts. The logic below is
811cb0ef41Sopenharmony_ci# careful to wrap any such functions (with additional logic), rather than
821cb0ef41Sopenharmony_ci# overwriting them.
831cb0ef41Sopenharmony_ci# An additional complication is that tested distros dynamically load their
841cb0ef41Sopenharmony_ci# completion scripts on first use. So in order to be able to detect their
851cb0ef41Sopenharmony_ci# presence, we have to force-load them first.
861cb0ef41Sopenharmony_ci_maybe_setup_gdb_completions() {
871cb0ef41Sopenharmony_ci  # We only want to set up the wrapping once:
881cb0ef41Sopenharmony_ci  [[ -n "$_ORIGINAL_GDB_COMPLETIONS" ]] && return 0;
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  # This path works for Debian, Ubuntu, and Gentoo; other distros unknown.
911cb0ef41Sopenharmony_ci  # Feel free to submit patches to extend the logic.
921cb0ef41Sopenharmony_ci  local GDB_COMP
931cb0ef41Sopenharmony_ci  for GDB_COMP in "/usr/share/bash-completion/completions/gdb"; do
941cb0ef41Sopenharmony_ci    [[ -f "$GDB_COMP" ]] && source $GDB_COMP
951cb0ef41Sopenharmony_ci  done
961cb0ef41Sopenharmony_ci  _ORIGINAL_GDB_COMPLETIONS="$(complete -p gdb 2>/dev/null \
971cb0ef41Sopenharmony_ci                               | sed -e 's/^.*-F \([^ ]*\).*/\1/')"
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  _gdb_v8_flag() {
1001cb0ef41Sopenharmony_ci    local c i next
1011cb0ef41Sopenharmony_ci    for (( i=1; i<$(($COMP_CWORD-1)); i++ )); do
1021cb0ef41Sopenharmony_ci      c=${COMP_WORDS[$i]}
1031cb0ef41Sopenharmony_ci      if [ "$c" = "-args" ] || [ "$c" = "--args" ] || [ "$c" == "--" ]; then
1041cb0ef41Sopenharmony_ci        next=$(basename -- ${COMP_WORDS[$(($i+1))]})
1051cb0ef41Sopenharmony_ci        if [ "$next" = "d8" ] ; then
1061cb0ef41Sopenharmony_ci          _d8_flag
1071cb0ef41Sopenharmony_ci          return 0
1081cb0ef41Sopenharmony_ci        elif [ "$next" = "unittests" ] || [ "$next" = "cctest" ]; then
1091cb0ef41Sopenharmony_ci          _test_flag
1101cb0ef41Sopenharmony_ci          return 0
1111cb0ef41Sopenharmony_ci        fi
1121cb0ef41Sopenharmony_ci      fi
1131cb0ef41Sopenharmony_ci    done
1141cb0ef41Sopenharmony_ci    [[ -n "$_ORIGINAL_GDB_COMPLETIONS" ]] && $_ORIGINAL_GDB_COMPLETIONS
1151cb0ef41Sopenharmony_ci    return 0
1161cb0ef41Sopenharmony_ci  }
1171cb0ef41Sopenharmony_ci  complete -F _gdb_v8_flag -f gdb
1181cb0ef41Sopenharmony_ci}
1191cb0ef41Sopenharmony_ci_maybe_setup_gdb_completions
1201cb0ef41Sopenharmony_ciunset _maybe_setup_gdb_completions
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci_get_gm_flags() {
1231cb0ef41Sopenharmony_ci  "$v8_source/tools/dev/gm.py" --print-completions
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  # cctest ignore directory structure, it's always "cctest/filename".
1261cb0ef41Sopenharmony_ci  find "$v8_source/test/cctest/" -type f -name 'test-*' | \
1271cb0ef41Sopenharmony_ci    xargs basename -a -s ".cc" | \
1281cb0ef41Sopenharmony_ci    while read -r item; do echo "cctest/$item/*"; done
1291cb0ef41Sopenharmony_ci}
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci_gm_flag() {
1321cb0ef41Sopenharmony_ci  local targets=$(_get_gm_flags)
1331cb0ef41Sopenharmony_ci  COMPREPLY=($(compgen -W "$targets" -- "${COMP_WORDS[COMP_CWORD]}"))
1341cb0ef41Sopenharmony_ci  return 0
1351cb0ef41Sopenharmony_ci}
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci# gm might be an alias, based on https://v8.dev/docs/build-gn#gm.
1381cb0ef41Sopenharmony_cicomplete -F _gm_flag gm.py gm
139