github.com/apache/beam/sdks/v2@v2.48.2/java/build-tools/beam-linkage-check.sh (about)

     1  #!/bin/bash
     2  #
     3  #    Licensed to the Apache Software Foundation (ASF) under one or more
     4  #    contributor license agreements.  See the NOTICE file distributed with
     5  #    this work for additional information regarding copyright ownership.
     6  #    The ASF licenses this file to You under the Apache License, Version 2.0
     7  #    (the "License"); you may not use this file except in compliance with
     8  #    the License.  You may obtain a copy of the License at
     9  #
    10  #       http://www.apache.org/licenses/LICENSE-2.0
    11  #
    12  #    Unless required by applicable law or agreed to in writing, software
    13  #    distributed under the License is distributed on an "AS IS" BASIS,
    14  #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  #    See the License for the specific language governing permissions and
    16  #    limitations under the License.
    17  #
    18  
    19  # This script compares linkage errors (checkJavaLinkage task in the root gradle project) between
    20  # one branch and another.
    21  
    22  # Usage:
    23  #  /bin/bash sdks/java/build-tools/beam-linkage-check.sh origin/master <your branch>
    24  #
    25  #  By default, this checks the Maven artifacts listed in ARTIFACTS variable below.
    26  #
    27  #  Optionally, you can pass artifact ID to overwrite the ARTIFACTS, such as:
    28  #    /bin/bash sdks/java/build-tools/beam-linkage-check.sh "beam-runners-google-cloud-dataflow-java"
    29  #
    30  #  Multiple artifact IDs (separated by a comma) will check linkage errors that would appear when
    31  #  the two artifacts are used together. For example:
    32  #    /bin/bash sdks/java/build-tools/beam-linkage-check.sh "beam-runners-google-cloud-dataflow-java,beam-sdks-java-io-hadoop-format"
    33  
    34  set -o pipefail
    35  set -e
    36  
    37  # These default artifacts are common causes of linkage errors.
    38  DEFAULT_ARTIFACT_LISTS=" \
    39    beam-sdks-java-core \
    40    beam-sdks-java-io-google-cloud-platform \
    41    beam-runners-google-cloud-dataflow-java \
    42    beam-sdks-java-io-hadoop-format \
    43  "
    44  
    45  BASELINE_REF=$1
    46  PROPOSED_REF=$2
    47  ARTIFACT_LISTS=$3
    48  
    49  if [ -z "$ARTIFACT_LISTS" ]; then
    50    ARTIFACT_LISTS=$DEFAULT_ARTIFACT_LISTS
    51  fi
    52  
    53  if [ -z "$BASELINE_REF" ] || [ -z "$PROPOSED_REF" ] || [ -z "$ARTIFACT_LISTS" ] ; then
    54    echo "Usage: $0 <baseline ref> <proposed ref> [artifact lists]"
    55    exit 1
    56  fi
    57  
    58  if [ ! -d buildSrc ]; then
    59    echo "Directory 'buildSrc' not found. Please run this script from the root directory of a clone of the Beam git repo."
    60  fi
    61  
    62  if [ "$BASELINE_REF" = "$PROPOSED_REF" ]; then
    63    echo "Baseline and proposed refs are identical; cannot compare their linkage errors!"
    64    exit 1
    65  fi
    66  
    67  if [ ! -z "$(git diff)" ]; then
    68    echo "Uncommited change detected. Please commit changes and ensure 'git diff' is empty."
    69    exit 1
    70  fi
    71  
    72  STARTING_REF=$(git rev-parse --abbrev-ref HEAD)
    73  function cleanup() {
    74    git checkout $STARTING_REF
    75  }
    76  trap cleanup EXIT
    77  
    78  echo "Comparing linkage of artifact lists $ARTIFACT_LISTS using baseline $BASELINE_REF and proposal $PROPOSED_REF"
    79  
    80  OUTPUT_DIR=build/linkagecheck
    81  mkdir -p $OUTPUT_DIR
    82  
    83  ACCUMULATED_RESULT=0
    84  
    85  function runLinkageCheck () {
    86    MODE=$1 # "baseline" or "validate"
    87  
    88    for ARTIFACT_LIST in $ARTIFACT_LISTS; do
    89      echo "`date`:" "Running linkage check (${MODE}) for ${ARTIFACT_LIST}"
    90  
    91      BASELINE_FILE=${OUTPUT_DIR}/baseline-${ARTIFACT_LIST}.xml
    92      if [ "$MODE" = "baseline" ]; then
    93        BASELINE_OPTION="-PjavaLinkageWriteBaseline=${BASELINE_FILE}"
    94        echo "`date`:" "to create a baseline (existing errors before change) $BASELINE_FILE"
    95      elif [ "$MODE" = "validate" ]; then
    96        BASELINE_OPTION="-PjavaLinkageReadBaseline=${BASELINE_FILE}"
    97        echo "`date`:" "using baseline $BASELINE_FILE"
    98        if [ ! -r "${BASELINE_FILE}" ]; then
    99          # If baseline generation failed in previous baseline step, no need to build the project.
   100          echo "`date`:" "Error: Baseline file not found"
   101          ACCUMULATED_RESULT=1
   102          continue
   103        fi
   104      else
   105        BASELINE_OPTION=""
   106        echo "`date`:" "Unexpected mode: ${MODE}. Not using baseline file."
   107      fi
   108  
   109      set +e
   110      set -x
   111      ./gradlew -Ppublishing -PskipCheckerFramework -PjavaLinkageArtifactIds=$ARTIFACT_LIST ${BASELINE_OPTION} :checkJavaLinkage
   112      RESULT=$?
   113      set -e
   114      set +x
   115      if [ "$MODE" = "baseline" ] && [ ! -r "${BASELINE_FILE}" ]; then
   116        echo "`date`:" "Failed to generate the baseline file. Check the build error above."
   117        ACCUMULATED_RESULT=1
   118      elif [ "$MODE" = "validate" ]; then
   119        echo "`date`:" "Done: ${RESULT}"
   120        ACCUMULATED_RESULT=$((ACCUMULATED_RESULT | RESULT))
   121      fi
   122    done
   123  }
   124  
   125  echo "Establishing baseline linkage for $(git rev-parse --abbrev-ref $BASELINE_REF)"
   126  git -c advice.detachedHead=false checkout $BASELINE_REF
   127  runLinkageCheck baseline
   128  
   129  echo "Checking linkage for $(git rev-parse --abbrev-ref $PROPOSED_REF)"
   130  git -c advice.detachedHEad=false checkout $PROPOSED_REF
   131  runLinkageCheck validate
   132  
   133  if [ "${ACCUMULATED_RESULT}" = "0" ]; then
   134    echo "No new linkage errors"
   135  else
   136    echo "There's new linkage errors. See above for details."
   137  fi
   138  
   139  # CI-friendly way to tell the result
   140  exit $ACCUMULATED_RESULT