kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/examples/bazel/kythe-index.sh (about)

     1  #!/bin/bash -e
     2  #
     3  # Copyright 2015 The Kythe Authors. All rights reserved.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #    http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  #
    17  # Utility script to run the Kythe toolset over the Bazel repository.  See the usage() function below
    18  # for more details.
    19  
    20  usage() {
    21    cat <<EOF
    22  Usage: kythe-index.sh (--all | [--extract] [--analyze] [--postprocess] [--serve])
    23                        [--graphstore path] [--serving_table path] [--serving_addr host:port]
    24                        [--bazel_root path] [--kythe_repo path] [--webui path]
    25  
    26  Utility script to run the Kythe toolset over the Bazel repository.  This script can run each
    27  component of Kythe separately with the --extract, --analyze, --postprocess, and --serve flags or all
    28  together with the --all flag.
    29  
    30  Paths to output artifacts are controlled with the --graphstore and --serving_table flags.  Their
    31  defaults are $TMPDIR/gs.bazel and $TMPDIR/serving.bazel, respectively.
    32  
    33  The --serving_addr flag controls the listening address of the Kythe http_server.
    34  Its default is localhost:8080.  ("localhost:8080" allows access from
    35  only the same machine the server is running on; ":8080" allows access
    36  from any machine.)  Any --webui path provided will be passed to the
    37  http_server as its --public_resources flag.
    38  
    39  The --bazel_root and --kythe_repo flags control which Bazel repository to index
    40  (default: $PWD) and an optional override for the Kythe repository.
    41  EOF
    42  }
    43  
    44  if [[ $# -eq 0 ]]; then
    45    usage
    46    exit 1
    47  fi
    48  
    49  if ! command -v bazel >/dev/null; then
    50    echo "ERROR: bazel not found on your PATH" >&2
    51    exit 1
    52  fi
    53  
    54  TMPDIR=${TMPDIR:-"/tmp"}
    55  
    56  BAZEL_ARGS=(--define kythe_corpus=github.com/bazel/bazel)
    57  BAZEL_ROOT="$PWD"
    58  
    59  GRAPHSTORE="$TMPDIR"/gs.bazel
    60  SERVING_TABLE="$TMPDIR"/serving.bazel
    61  SERVING_ADDR=localhost:8080  # :8080 allows access from any machine
    62  WEBUI=()
    63  
    64  EXTRACT=
    65  ANALYZE=
    66  POSTPROCESS=
    67  SERVE=
    68  
    69  while [[ $# -gt 0 ]]; do
    70    case "$1" in
    71      --help|-h)
    72        usage
    73        exit ;;
    74      --all|-a)
    75        EXTRACT=1
    76        ANALYZE=1
    77        POSTPROCESS=1
    78        SERVE=1 ;;
    79      --bazel_root)
    80        BAZEL_ROOT="$(realpath -s "$2")"
    81        shift ;;
    82      --kythe_repo)
    83        BAZEL_ARGS=("${BAZEL_ARGS[@]}" --override_repository "io_kythe=$(realpath -s "$2")")
    84        shift ;;
    85      --graphstore|-g)
    86        GRAPHSTORE="$2"
    87        shift ;;
    88      --serving_table|-s)
    89        SERVING_TABLE="$(realpath -s "$2")"
    90        shift ;;
    91      --serving_addr|-l)
    92        SERVING_ADDR="$2"
    93        shift ;;
    94      --webui|-w)
    95        WEBUI=(--public_resources "$2")
    96        shift ;;
    97      --extract)
    98        EXTRACT=1 ;;
    99      --analyze)
   100        ANALYZE=1 ;;
   101      --postprocess)
   102        POSTPROCESS=1 ;;
   103      --serve)
   104        SERVE=1 ;;
   105      *)
   106        echo "ERROR: unknown argument: $1" >&2
   107        exit 1 ;;
   108    esac
   109    shift
   110  done
   111  
   112  cd "$BAZEL_ROOT"
   113  KYTHE_BIN="$BAZEL_ROOT/bazel-bin/external/io_kythe"
   114  
   115  if ! grep -q 'Kythe extraction setup' WORKSPACE; then
   116    echo "ERROR: $BAZEL_ROOT is not a correctly configured Bazel repository" >&2
   117    echo "Please pass the --bazel_root flag after running setup-bazel-repo.sh on an existing Bazel repository." >&2
   118    echo "Example: setup-bazel-repo.sh $BAZEL_ROOT" >&2
   119    exit 1
   120  fi
   121  
   122  if [[ -n "$EXTRACT" ]]; then
   123    echo "Extracting Bazel compilations..."
   124    rm -rf bazel-out/*/extra_actions/external/io_kythe/
   125    bazel build "${BAZEL_ARGS[@]}" \
   126      --keep_going --output_groups=compilation_outputs \
   127      --experimental_extra_action_top_level_only \
   128      --experimental_action_listener=@io_kythe//kythe/extractors:extract_kzip_cxx \
   129      --experimental_action_listener=@io_kythe//kythe/extractors:extract_kzip_java \
   130      --experimental_action_listener=@io_kythe//kythe/extractors:extract_kzip_protobuf \
   131      //src/{main,test}/... || true
   132  fi
   133  
   134  analyze() {
   135    local lang="$1"
   136    shift 1
   137    local xa="io_kythe/kythe/build/extract_kzip_${lang}_extra_action"
   138    local dedup_stream="$KYTHE_BIN/kythe/go/platform/tools/dedup_stream/dedup_stream"
   139    find -L "$BAZEL_ROOT"/bazel-out/*/extra_actions/external/"$xa" -name '*.kzip' | \
   140      parallel -t -L1 "$@" | \
   141      "$dedup_stream" >>"$GRAPHSTORE"
   142  }
   143  
   144  if [[ -n "$ANALYZE" ]]; then
   145    echo "Analyzing compilations..."
   146    echo "GraphStore: $GRAPHSTORE"
   147  
   148    rm -rf "$GRAPHSTORE"
   149  
   150    bazel build "${BAZEL_ARGS[@]}" \
   151      @io_kythe//kythe/go/platform/tools/dedup_stream \
   152      @io_kythe//kythe/cxx/indexer/cxx:indexer \
   153      @io_kythe//kythe/java/com/google/devtools/kythe/analyzers/java:indexer \
   154      //kythe/cxx/indexer/proto:indexer
   155    analyze cxx "$KYTHE_BIN/kythe/cxx/indexer/cxx/indexer"
   156    analyze java "$KYTHE_BIN/kythe/java/com/google/devtools/kythe/analyzers/java/indexer"
   157    analyze protobuf "$KYTHE_BIN/kythe/cxx/indexer/proto/indexer"
   158    cd "$ROOT"
   159  fi
   160  
   161  if [[ -n "$POSTPROCESS" ]]; then
   162    echo "Post-processing GraphStore..."
   163    echo "Serving table: $SERVING_TABLE"
   164  
   165    rm -rf "$SERVING_TABLE"
   166  
   167    bazel build -c opt "${BAZEL_ARGS[@]}" \
   168      @io_kythe//kythe/go/serving/tools/write_tables
   169    "$KYTHE_BIN"/kythe/go/serving/tools/write_tables/write_tables \
   170      --entries "$GRAPHSTORE" --out "$SERVING_TABLE" \
   171      --experimental_beam_pipeline --experimental_beam_columnar_data
   172  fi
   173  
   174  if [[ -n "$SERVE" ]]; then
   175    echo "Launching Kythe server"
   176    echo "Address: http://$SERVING_ADDR"
   177  
   178    bazel build "${BAZEL_ARGS[@]}" \
   179      @io_kythe//kythe/go/serving/tools/http_server
   180    "$KYTHE_BIN"/kythe/go/serving/tools/http_server/http_server \
   181      "${WEBUI[@]}" \
   182      --listen "$SERVING_ADDR" \
   183      --serving_table "$SERVING_TABLE"
   184  fi