xref: /llvm-project/llvm/utils/docker/scripts/checkout.sh (revision 5136c6d9d207b72135c92567e58e5cdbb86efc09)
1#!/usr/bin/env bash
2#===- llvm/utils/docker/scripts/checkout.sh ---------------------===//
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#
8#===-----------------------------------------------------------------------===//
9
10set -e
11
12function show_usage() {
13  cat << EOF
14Usage: checkout.sh [options]
15
16Checkout git sources into /tmp/clang-build/src. Used inside a docker container.
17
18Available options:
19  -h|--help           show this help message
20  -b|--branch         git branch to checkout, i.e. 'main',
21                      'release/19.x'
22                      (default: 'main')
23  -r|--revision       git revision to checkout
24  -c|--cherrypick     revision to cherry-pick. Can be specified multiple times.
25                      Cherry-picks are performed in the sorted order using the
26                      following command:
27                      'git cherry-pick \$rev)'.
28EOF
29}
30
31LLVM_GIT_REV=""
32CHERRYPICKS=""
33LLVM_BRANCH=""
34
35while [[ $# -gt 0 ]]; do
36  case "$1" in
37    -r|--revision)
38      shift
39      LLVM_GIT_REV="$1"
40      shift
41      ;;
42    -c|--cherrypick)
43      shift
44      CHERRYPICKS="$CHERRYPICKS $1"
45      shift
46      ;;
47    -b|--branch)
48      shift
49      LLVM_BRANCH="$1"
50      shift
51      ;;
52    -h|--help)
53      show_usage
54      exit 0
55      ;;
56    *)
57      echo "Unknown option: $1"
58      exit 1
59  esac
60done
61
62if [ "$LLVM_BRANCH" == "" ]; then
63  LLVM_BRANCH="main"
64fi
65
66if [ "$LLVM_GIT_REV" != "" ]; then
67  GIT_REV_ARG="$LLVM_GIT_REV"
68  echo "Checking out git revision $LLVM_GIT_REV."
69else
70  GIT_REV_ARG=""
71  echo "Checking out latest git revision."
72fi
73
74# Sort cherrypicks and remove duplicates.
75CHERRYPICKS="$(echo "$CHERRYPICKS" | xargs -n1 | sort | uniq | xargs)"
76
77function apply_cherrypicks() {
78  local CHECKOUT_DIR="$1"
79
80  [ "$CHERRYPICKS" == "" ] || echo "Applying cherrypicks"
81  pushd "$CHECKOUT_DIR"
82
83  # This function is always called on a sorted list of cherrypicks.
84  for CHERRY_REV in $CHERRYPICKS; do
85    echo "Cherry-picking $CHERRY_REV into $CHECKOUT_DIR"
86    EMAIL="someone@somewhere.net" git cherry-pick "$CHERRY_REV"
87  done
88
89  popd
90}
91
92CLANG_BUILD_DIR=/tmp/clang-build
93
94# Get the sources from git.
95echo "Checking out sources from git"
96mkdir -p "$CLANG_BUILD_DIR/src"
97CHECKOUT_DIR="$CLANG_BUILD_DIR/src"
98
99echo "Checking out https://github.com/llvm/llvm-project.git to $CHECKOUT_DIR"
100git clone -b "$LLVM_BRANCH" --single-branch \
101  "https://github.com/llvm/llvm-project.git" \
102  "$CHECKOUT_DIR"
103
104pushd $CHECKOUT_DIR
105git checkout -q $GIT_REV_ARG
106popd
107
108  # We apply cherrypicks to all repositories regardless of whether the revision
109  # changes this repository or not. For repositories not affected by the
110  # cherrypick, applying the cherrypick is a no-op.
111  apply_cherrypicks "$CHECKOUT_DIR"
112
113CHECKSUMS_FILE="/tmp/checksums/checksums.txt"
114
115if [ -f "$CHECKSUMS_FILE" ]; then
116  echo "Validating checksums for LLVM checkout..."
117  python "$(dirname "$0")/llvm_checksum/llvm_checksum.py" -c "$CHECKSUMS_FILE" \
118    --partial --multi_dir "$CLANG_BUILD_DIR/src"
119else
120  echo "Skipping checksumming checks..."
121fi
122
123echo "Done"
124