|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +set -e |
| 4 | +# Set pipefail if it works in a subshell, disregard if unsupported |
| 5 | +# shellcheck disable=SC3040 |
| 6 | +if (set -o pipefail 2>/dev/null); then |
| 7 | + set -o pipefail |
| 8 | +fi |
| 9 | +# |
| 10 | +# (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. |
| 11 | +# |
| 12 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 13 | +# you may not use this file except in compliance with the License. |
| 14 | +# You may obtain a copy of the License at |
| 15 | +# |
| 16 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +# |
| 18 | +# Unless required by applicable law or agreed to in writing, software |
| 19 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | +# See the License for the specific language governing permissions and |
| 22 | +# limitations under the License. |
| 23 | +# |
| 24 | + |
| 25 | +TMP_WORK_DIR=$(mktemp -d) |
| 26 | +export TMP_WORK_DIR |
| 27 | + |
| 28 | +# writing to stderr |
| 29 | +write() { echo "$*" >&2; } |
| 30 | + |
| 31 | +cleanup() { |
| 32 | + [ -d "$TMP_WORK_DIR" ] && rm -rf "$TMP_WORK_DIR" |
| 33 | +} |
| 34 | + |
| 35 | +die() { |
| 36 | + write |
| 37 | + write "$*" |
| 38 | + write |
| 39 | + cleanup |
| 40 | + exit 1 |
| 41 | +} >&2 |
| 42 | + |
| 43 | +read_value() { |
| 44 | + if [ ! -f "$1" ]; then |
| 45 | + die "ERROR: $1 not found, aborting Gradle JDK setup" |
| 46 | + fi |
| 47 | + read -r value < "$1" || die "ERROR: Unable to read value from $1. Make sure the file ends with a newline." |
| 48 | + echo "$value" |
| 49 | +} |
| 50 | + |
| 51 | +get_os() { |
| 52 | + # OS specific support; same as gradle-jdks:com.palantir.gradle.jdks.setup.common.CurrentOs.java |
| 53 | + case "$( uname )" in #( |
| 54 | + Linux* ) os_name="linux" ;; #( |
| 55 | + Darwin* ) os_name="macos" ;; #( |
| 56 | + * ) os_name="unsupported";; |
| 57 | + esac |
| 58 | + |
| 59 | + if [ "$os_name" = "linux" ]; then |
| 60 | + ldd_output=$(ldd --version 2>&1 || true) |
| 61 | + if echo "$ldd_output" | grep -qi glibc; then |
| 62 | + os_name="linux-glibc" |
| 63 | + elif echo "$ldd_output" | grep -qi "gnu libc"; then |
| 64 | + os_name="linux-glibc" |
| 65 | + elif echo "$ldd_output" | grep -qi musl; then |
| 66 | + os_name="linux-musl" |
| 67 | + else |
| 68 | + die "Unable to determine glibc or musl based Linux distribution: ldd_output: $ldd_output" |
| 69 | + fi |
| 70 | + fi |
| 71 | + |
| 72 | + echo "$os_name" |
| 73 | +} |
| 74 | + |
| 75 | +get_arch() { |
| 76 | + # Arch specific support, see: gradle-jdks:com.palantir.gradle.jdks.setup.common.CurrentArch.java |
| 77 | + case "$(uname -m)" in #( |
| 78 | + x86_64* ) arch_name="x86-64" ;; #( |
| 79 | + x64* ) arch_name="x86-64" ;; #( |
| 80 | + amd64* ) arch_name="x86-64" ;; #( |
| 81 | + arm64* ) arch_name="aarch64" ;; #( |
| 82 | + arm* ) arch_name="aarch64" ;; #( |
| 83 | + aarch64* ) arch_name="aarch64" ;; #( |
| 84 | + x86* ) arch_name="x86" ;; #( |
| 85 | + i686* ) arch_name="x86" ;; #( |
| 86 | + * ) arch_name="unsupported";; |
| 87 | + esac |
| 88 | + |
| 89 | + echo "$arch_name" |
| 90 | +} |
| 91 | + |
| 92 | +get_gradle_jdks_home() { |
| 93 | + gradle_user_home=${GRADLE_USER_HOME:-"$HOME"/.gradle} |
| 94 | + gradle_jdks_home="$gradle_user_home"/gradle-jdks |
| 95 | + echo "$gradle_jdks_home" |
| 96 | +} |
| 97 | + |
| 98 | +get_java_home() { |
| 99 | + java_bin=$(find "$1" -type f -name "java" -path "*/bin/java" ! -type l -print -quit) |
| 100 | + echo "${java_bin%/*/*}" |
| 101 | +} |
| 102 | + |
| 103 | +GRADLE_JDKS_HOME=$(get_gradle_jdks_home) |
| 104 | +mkdir -p "$GRADLE_JDKS_HOME" |
| 105 | +export GRADLE_JDKS_HOME |
| 106 | + |
| 107 | +OS=$(get_os) |
| 108 | +export OS |
| 109 | + |
| 110 | +ARCH=$(get_arch) |
| 111 | +export ARCH |
| 112 | + |
| 113 | +is_arch_os_supported() { |
| 114 | + if [ "$OS" = "unsupported" ] || [ "$ARCH" = "unsupported" ]; then |
| 115 | + echo false |
| 116 | + fi |
| 117 | + echo true |
| 118 | +} |
| 119 | + |
| 120 | +install_and_setup_jdks() { |
| 121 | + gradle_dir=$1 |
| 122 | + scripts_dir=${2:-"$1"} |
| 123 | + |
| 124 | + for dir in "$gradle_dir"/jdks/*/; do |
| 125 | + major_version_dir=${dir%*/} |
| 126 | + major_version=${major_version_dir##*/} |
| 127 | + if [ "$major_version" = "8" ]; then |
| 128 | + write "Skipping JDK 8 installation as it is not supported by Gradle JDKs Setup." |
| 129 | + continue |
| 130 | + fi |
| 131 | + distribution_local_path=$(read_value "$major_version_dir"/"$OS"/"$ARCH"/local-path) |
| 132 | + distribution_url=$(read_value "$major_version_dir"/"$OS"/"$ARCH"/download-url) |
| 133 | + # Check if distribution exists in $GRADLE_JDKS_HOME |
| 134 | + jdk_installation_directory="$GRADLE_JDKS_HOME"/"$distribution_local_path" |
| 135 | + if [ ! -d "$jdk_installation_directory" ]; then |
| 136 | + write "JDK installation '$jdk_installation_directory' does not exist, installing '$distribution_url' in progress ..." |
| 137 | + elif [ ! -f "$jdk_installation_directory/bin/java" ]; then |
| 138 | + write "Java executable not found in $jdk_installation_directory/bin/java, re-installing the JDK...." |
| 139 | + else |
| 140 | + continue |
| 141 | + fi |
| 142 | + # Download and extract the distribution into a temporary directory |
| 143 | + in_progress_dir="$TMP_WORK_DIR/$distribution_local_path.in-progress" |
| 144 | + mkdir -p "$in_progress_dir" |
| 145 | + cd "$in_progress_dir" || die "failed to change dir to $in_progress_dir" |
| 146 | + if command -v curl > /dev/null 2>&1; then |
| 147 | + write "Using curl to download $distribution_url" |
| 148 | + case "$distribution_url" in |
| 149 | + *.zip) |
| 150 | + distribution_name=${distribution_url##*/} |
| 151 | + curl -L -C - "$distribution_url" -o "$distribution_name" |
| 152 | + tar -xzf "$distribution_name" |
| 153 | + ;; |
| 154 | + *) |
| 155 | + curl -L -C - "$distribution_url" | tar -xzf - |
| 156 | + ;; |
| 157 | + esac |
| 158 | + elif command -v wget > /dev/null 2>&1; then |
| 159 | + write "Using wget to download $distribution_url" |
| 160 | + case "$distribution_url" in |
| 161 | + *.zip) |
| 162 | + distribution_name=${distribution_url##*/} |
| 163 | + wget -c "$distribution_url" -O "$distribution_name" |
| 164 | + tar -xzf "$distribution_name" |
| 165 | + ;; |
| 166 | + *) |
| 167 | + wget -qO- -c "$distribution_url" | tar -xzf - |
| 168 | + ;; |
| 169 | + esac |
| 170 | + else |
| 171 | + die "ERROR: Neither curl nor wget are installed, Could not set up JAVA_HOME" |
| 172 | + fi |
| 173 | + cd - > /dev/null || die "failed to change dir to old pwd: $OLDPWD" |
| 174 | + |
| 175 | + # Finding the java_home |
| 176 | + java_home=$(get_java_home "$in_progress_dir") |
| 177 | + "$java_home"/bin/java -cp "$scripts_dir"/gradle-jdks-setup.jar com.palantir.gradle.jdks.setup.GradleJdkInstallationSetup jdkSetup "$jdk_installation_directory" || die "Failed to set up JDK $jdk_installation_directory" |
| 178 | + write "Successfully installed JDK distribution in $jdk_installation_directory" |
| 179 | + done |
| 180 | +} |
0 commit comments