|
| 1 | +#! /bin/bash -e |
| 2 | + |
| 3 | +trap "echo -e '\nScript interrupted. Exiting gracefully.'; exit 1" SIGINT |
| 4 | + |
| 5 | +# Copyright (C) 2024-2025 Red Hat, Inc. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | +# SPDX-License-Identifier: Apache-2.0 |
| 20 | +set -euo pipefail |
| 21 | + |
| 22 | +WORKSPACE=${WORKSPACE:-${HOME}} |
| 23 | + |
| 24 | +LLVM_DIR=${WORKSPACE}/llvm-project |
| 25 | +LLVM_REPO=https://github.com/llvm/llvm-project.git |
| 26 | +LLVM_BUILD_PATH=$LLVM_DIR/build |
| 27 | + |
| 28 | +pip_install() { |
| 29 | + if command -v uv &>/dev/null; then |
| 30 | + uv pip install "$@" |
| 31 | + else |
| 32 | + pip install "$@" |
| 33 | + fi |
| 34 | +} |
| 35 | + |
| 36 | +setup_src() { |
| 37 | + echo "Downloading LLVM source code and setting up the environment for building from source..." |
| 38 | + |
| 39 | + if [ ! -d "$LLVM_DIR" ]; then |
| 40 | + echo "Cloning the LLVM Project repo $LLVM_REPO to $LLVM_DIR ..." |
| 41 | + git clone "$LLVM_REPO" "$LLVM_DIR" |
| 42 | + if [ ! -d "$LLVM_DIR" ]; then |
| 43 | + echo "$LLVM_DIR not found. ERROR Cloning repository..." |
| 44 | + exit 1 |
| 45 | + else |
| 46 | + pushd "$LLVM_DIR" 1>/dev/null || exit 1 |
| 47 | + git fetch origin |
| 48 | + |
| 49 | + # shellcheck source=/dev/null |
| 50 | + [ -f "${HOME}"/.bashrc ] && source "${HOME}"/.bashrc |
| 51 | + |
| 52 | + if [ -n "${LLVM_GITREF:-}" ]; then |
| 53 | + git checkout "$LLVM_GITREF" |
| 54 | + fi |
| 55 | + popd 1>/dev/null |
| 56 | + fi |
| 57 | + else |
| 58 | + echo "LLVM repo already present, not cloning ..." |
| 59 | + fi |
| 60 | + |
| 61 | + echo "Adding LLVM_BUILD_PATH to ${HOME}/.bashrc ..." |
| 62 | + echo "export LLVM_BUILD_PATH=$LLVM_BUILD_PATH" >>"${HOME}/.bashrc" |
| 63 | + echo "Run 'source ${HOME}/.bashrc' to update the current shell" |
| 64 | +} |
| 65 | + |
| 66 | +install_build_deps() { |
| 67 | + pushd "$LLVM_DIR" 1>/dev/null || exit 1 |
| 68 | + if [ -f mlir/python/requirements.txt ]; then |
| 69 | + echo "Installing LLVM build dependencies ..." |
| 70 | + pip_install -r mlir/python/requirements.txt |
| 71 | + fi |
| 72 | + popd 1>/dev/null |
| 73 | +} |
| 74 | + |
| 75 | +usage() { |
| 76 | + cat >&2 <<EOF |
| 77 | +Usage: $(basename "$0") [COMMAND] |
| 78 | + source Download LLVM's source (if needed) and install the build deps |
| 79 | +EOF |
| 80 | +} |
| 81 | + |
| 82 | +## |
| 83 | +## Main |
| 84 | +## |
| 85 | + |
| 86 | +if [ $# -ne 1 ]; then |
| 87 | + usage |
| 88 | + exit 1 |
| 89 | +fi |
| 90 | + |
| 91 | +COMMAND=${1,,} |
| 92 | + |
| 93 | +case $COMMAND in |
| 94 | +source) |
| 95 | + setup_src |
| 96 | + install_build_deps |
| 97 | + ;; |
| 98 | +*) |
| 99 | + usage |
| 100 | + exit 1 |
| 101 | + ;; |
| 102 | +esac |
0 commit comments