#!/usr/bin/env bash set -euo pipefail DOTNET_VERSION="9.0" CHECK_ONLY=0 usage() { cat <<'EOF' Usage: ./prepare_linux_build_env.sh [options] Options: --dotnet-version Required .NET major.minor version (default: 9.0) --check-only Only validate prerequisites, do not install --help Show this help EOF } while [[ $# -gt 0 ]]; do case "$1" in --dotnet-version) DOTNET_VERSION="${2:?Missing value for --dotnet-version}" shift 2 ;; --check-only) CHECK_ONLY=1 shift ;; --help) usage exit 0 ;; *) echo "Unknown option: $1" >&2 usage >&2 exit 1 ;; esac done require_linux() { if [[ "$(uname -s)" != "Linux" ]]; then echo "This helper currently supports only Linux." >&2 exit 1 fi } have_command() { command -v "$1" >/dev/null 2>&1 } resolve_dotnet_hostdir() { local search_roots=() local candidate if [[ -n "${DOTNET_HOSTDIR:-}" && -f "${DOTNET_HOSTDIR}/hostfxr.h" ]]; then printf '%s\n' "${DOTNET_HOSTDIR}" return 0 fi if [[ -n "${DOTNET_ROOT:-}" ]]; then search_roots+=("${DOTNET_ROOT}") fi search_roots+=("/usr/share/dotnet" "/usr/lib/dotnet" "${HOME}/.dotnet") for root in "${search_roots[@]}"; do for candidate in \ "${root}/packs/Microsoft.NETCore.App.Host.linux-x64/${DOTNET_VERSION}"* \ "${root}/packs/Microsoft.NETCore.App.Host.linux-x64/"*; do if [[ -f "${candidate}/runtimes/linux-x64/native/hostfxr.h" ]]; then printf '%s\n' "${candidate}/runtimes/linux-x64/native" return 0 fi done done return 1 } have_dotnet_sdk() { if ! have_command dotnet; then return 1 fi dotnet --list-sdks 2>/dev/null | awk '{print $1}' | grep -Eq "^${DOTNET_VERSION//./\\.}" } have_glib_dev() { have_command pkg-config && pkg-config --exists glib-2.0 } report_missing_requirements() { local missing=() if ! have_command make; then missing+=("make") fi if ! have_command gcc; then missing+=("gcc") fi if ! have_command g++; then missing+=("g++") fi if ! have_glib_dev; then missing+=("glib-2.0 development files") fi if ! have_dotnet_sdk; then missing+=(".NET SDK ${DOTNET_VERSION}") fi if ! resolve_dotnet_hostdir >/dev/null; then missing+=(".NET host pack ${DOTNET_VERSION} (hostfxr.h / libnethost.so)") fi if [[ ${#missing[@]} -eq 0 ]]; then echo "PL/.NET build prerequisites are present." return 0 fi echo "Missing prerequisites for PL/.NET:" printf ' - %s\n' "${missing[@]}" return 1 } ensure_root() { if [[ "${EUID}" -ne 0 ]]; then echo "Please rerun with sudo to install missing packages." >&2 exit 1 fi } ensure_microsoft_repo() { if apt-cache show "dotnet-sdk-${DOTNET_VERSION}" >/dev/null 2>&1; then return 0 fi if [[ -f /etc/apt/sources.list.d/microsoft-prod.list ]]; then apt-get update return 0 fi if [[ ! -r /etc/os-release ]]; then echo "Cannot detect Linux distribution to configure Microsoft package feed." >&2 exit 1 fi # shellcheck disable=SC1091 . /etc/os-release local vendor="" local version="" local repo_deb="" case "${ID:-}" in debian) vendor="debian" version="${VERSION_ID%%.*}" ;; ubuntu) vendor="ubuntu" version="${VERSION_ID}" ;; astra) vendor="debian" case "${VERSION_ID}" in 1.8*) version="12" ;; 1.7*) version="11" ;; *) echo "Unsupported Astra Linux version: ${VERSION_ID}" >&2 echo "Install .NET SDK ${DOTNET_VERSION} manually, then rerun this script." >&2 exit 1 ;; esac ;; *) echo "Automatic .NET repository setup is supported for Debian, Ubuntu and Astra Linux." >&2 echo "Install .NET SDK ${DOTNET_VERSION} manually, then rerun this script." >&2 exit 1 ;; esac repo_deb="$(mktemp)" apt-get update apt-get install -y ca-certificates wget gnupg wget -qO "${repo_deb}" "https://packages.microsoft.com/config/${vendor}/${version}/packages-microsoft-prod.deb" dpkg -i "${repo_deb}" rm -f "${repo_deb}" apt-get update } install_packages() { local packages=( build-essential ca-certificates gnupg libglib2.0-dev make pkg-config wget "dotnet-sdk-${DOTNET_VERSION}" "dotnet-runtime-${DOTNET_VERSION}" ) apt-get install -y "${packages[@]}" } configure_dotnet_preferences() { if [[ ! -r /etc/os-release ]]; then return 0 fi # shellcheck disable=SC1091 . /etc/os-release if [[ "${ID:-}" != "astra" ]]; then return 0 fi cat > /etc/apt/preferences.d/microsoft-dotnet.pref <<'EOF' Package: dotnet* aspnetcore* netstandard-targeting-pack-* dotnet-targeting-pack-* dotnet-apphost-pack-* Pin: origin packages.microsoft.com Pin-Priority: 1001 EOF } require_linux if report_missing_requirements; then exit 0 fi if [[ "${CHECK_ONLY}" -eq 1 ]]; then exit 1 fi if ! have_command apt-get; then echo "Automatic dependency installation currently supports apt-based Linux distributions only." >&2 echo "Missing prerequisites can still be installed manually; then rerun with --check-only." >&2 exit 1 fi ensure_root ensure_microsoft_repo configure_dotnet_preferences apt-get update install_packages if ! report_missing_requirements; then echo "PL/.NET dependency installation did not complete successfully." >&2 exit 1 fi echo "PL/.NET Linux build environment is ready."