kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/extractors/cmd/gotool/analyze_packages.sh (about)

     1  #!/bin/bash -e
     2  #
     3  # Copyright 2018 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  # Download, build, and extract a set of Go packages.  Resulting compilations
    18  # will be merged into a single .kzip archive in the "$OUTPUT" directory.
    19  
    20  : "${TMPDIR:=/tmp}" "${OUTPUT:=/output}"
    21  
    22  if [[ -z "${GOPATH}" ]]; then
    23    echo "GOPATH environment variable is required" >&2
    24    exit 1
    25  fi
    26  if [[ -z "${KYTHE_CORPUS}" ]]; then
    27    echo "KYTHE_CORPUS environment variable is required" >&2
    28    exit 1
    29  fi
    30  
    31  FLAGS=()
    32  PACKAGES=()
    33  while [[ $# -gt 0 ]]; do
    34    case "$1" in
    35      -*)
    36        FLAGS+=("$1") ;;
    37      *)
    38        PACKAGES+=("$1") ;;
    39    esac
    40    shift
    41  done
    42  
    43  if [[ ${#PACKAGES[@]} -ne 1 ]]; then
    44    echo "Please specify exactly one go package to extract" >&2
    45    exit 1
    46  fi
    47  PACKAGE="${PACKAGES[0]}"
    48  
    49  # golang build tags can optionally be specified with the `KYTHE_GO_BUILD_TAGS`
    50  # env variable.
    51  if [ -n "$KYTHE_GO_BUILD_TAGS" ]; then
    52    FLAGS+=( "--buildtags=$KYTHE_GO_BUILD_TAGS" )
    53  fi
    54  
    55  if [ -n "$KYTHE_PRE_BUILD_STEP" ]; then
    56    eval "$KYTHE_PRE_BUILD_STEP"
    57  fi
    58  
    59  if [ -n "$KYTHE_SKIP_MOD_VENDOR" ]; then
    60    if [ "$KYTHE_SKIP_MOD_VENDOR" == "1" ] || \
    61       [ "$KYTHE_SKIP_MOD_VENDOR" == "True" ] || \
    62       [ "$KYTHE_SKIP_MOD_VENDOR" == "true" ]; then
    63      echo "$PACKAGE: Skipping 'go mod vendor' due to KYTHE_SKIP_MOD_VENDOR=$KYTHE_SKIP_MOD_VENDOR" >&2
    64      SKIP_MOD_VENDOR=true
    65    fi
    66  fi
    67  
    68  if [ -z "$SKIP_MOD_VENDOR" ]; then
    69    echo "$PACKAGE: go mod vendor" >&2
    70    pushd "$GOPATH/src/$PACKAGE"
    71    go mod vendor
    72    popd
    73  fi
    74  
    75  echo "Extracting ${PACKAGE}" >&2
    76    extract_go --continue -v \
    77    --goroot="$(go env GOROOT)" \
    78    --output="$TMPDIR/out.kzip" \
    79    --use_default_corpus_for_deps \
    80    --use_default_corpus_for_stdlib \
    81    --corpus="$KYTHE_CORPUS" \
    82    "${FLAGS[@]}" \
    83    "${PACKAGE}/vendor/..." \
    84    "${PACKAGE}/..."
    85  
    86  mkdir -p "$OUTPUT"
    87  OUT="$OUTPUT/compilations.kzip"
    88  if [[ -f "$OUT" ]]; then
    89    OUT="$(mktemp -p "$OUTPUT/" compilations.XXXXX.kzip)"
    90  fi
    91  
    92  # cd into the top-level git directory of our package and query git for the
    93  # commit timestamp.
    94  pushd "$(go env GOPATH)/src/${PACKAGE}"
    95  TIMESTAMP="$(git -c safe.directory='*' log --pretty='%ad' -n 1 HEAD)"
    96  popd
    97  
    98  # Record the timestamp of the git commit in a metadata kzip.
    99  kzip create_metadata \
   100    --output "$OUTPUT/buildmetadata.kzip" \
   101    --corpus "$KYTHE_CORPUS" \
   102    --commit_timestamp "$TIMESTAMP"
   103  
   104  echo "Merging compilations into $OUT" >&2
   105  kzip merge --output "$OUT" "$TMPDIR/out.kzip" "$OUTPUT/buildmetadata.kzip"
   106  fix_permissions.sh "$OUTPUT"