| 1 | #!/usr/bin/env bash |
| 2 | # ========================================================================== |
| 3 | # ____ _ _ _ _____ _ |
| 4 | # | __ ) _ _(_) | __| | |_ _|__ ___ | |___ |
| 5 | # | _ \| | | | | |/ _` |_____| |/ _ \ / _ \| / __| |
| 6 | # | |_) | |_| | | | (_| |_____| | (_) | (_) | \__ \ |
| 7 | # |____/ \__,_|_|_|\__,_| |_|\___/ \___/|_|___/ |
| 8 | # |
| 9 | # --- Build-Tools --- |
| 10 | # https://www.nntb.no/~dreibh/system-tools/ |
| 11 | # ========================================================================== |
| 12 | # |
| 13 | # GitHub Actions Scripts |
| 14 | # Copyright (C) 2021-2026 by Thomas Dreibholz |
| 15 | # |
| 16 | # This program is free software: you can redistribute it and/or modify |
| 17 | # it under the terms of the GNU General Public License as published by |
| 18 | # the Free Software Foundation, either version 3 of the License, or |
| 19 | # (at your option) any later version. |
| 20 | # |
| 21 | # This program is distributed in the hope that it will be useful, |
| 22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | # GNU General Public License for more details. |
| 25 | # |
| 26 | # You should have received a copy of the GNU General Public License |
| 27 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | # |
| 29 | # Contact: [email protected] |
| 30 | |
| 31 | # Bash options: |
| 32 | set -euo pipefail |
| 33 | |
| 34 | |
| 35 | # ====== Check arguments ==================================================== |
| 36 | if [ $# -lt 1 ] ; then |
| 37 | echo >&2 "Usage: $0 compile|package ..." |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
| 41 | |
| 42 | # ====== Check arguments ==================================================== |
| 43 | USE_PACKAGING=0 |
| 44 | while [ $# -gt 0 ] ; do |
| 45 | TOOL="$1" |
| 46 | if [ "${TOOL}" == "compile" ] ; then |
| 47 | USE_PACKAGING=0 |
| 48 | elif [ "$1" == "package" ] ; then |
| 49 | USE_PACKAGING=1 |
| 50 | else |
| 51 | echo >&2 "ERROR: Invalid setting of TOOL=${TOOL}!" |
| 52 | exit 1 |
| 53 | fi |
| 54 | shift |
| 55 | done |
| 56 | |
| 57 | |
| 58 | # ====== Check/set environment variables ==================================== |
| 59 | if [ ! -e /etc/os-release ] ; then |
| 60 | echo >&2 "ERROR: /etc/os-release does not exist!" |
| 61 | exit 1 |
| 62 | fi |
| 63 | . /etc/os-release |
| 64 | |
| 65 | |
| 66 | # ====== Ubuntu/Deban ======================================================= |
| 67 | if [ "${ID}" == "ubuntu" ] || [ "${ID}" == "debian" ] ; then |
| 68 | PACKAGES="python3 python3-distro python3-packaging python3-urllib3" |
| 69 | if [ -v CC ] ; then |
| 70 | if [[ "${CC}" =~ .*gcc.* ]] ; then |
| 71 | PACKAGES="${PACKAGES} gcc" |
| 72 | elif [[ "${CC}" =~ .*clang.* ]] ; then |
| 73 | PACKAGES="${PACKAGES} clang" |
| 74 | fi |
| 75 | fi |
| 76 | if [ ${USE_PACKAGING} -eq 1 ] ; then |
| 77 | # Need to install pbuilder as well: |
| 78 | PACKAGES="${PACKAGES} build-essential debian-archive-keyring debian-ports-archive-keyring devscripts distro-info eatmydata fakeroot pbuilder qemu-user qemu-user-binfmt sudo" |
| 79 | fi |
| 80 | |
| 81 | apt-get update -qq |
| 82 | # DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade -qy |
| 83 | # shellcheck disable=SC2086 |
| 84 | DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::=--force-confold -o Dpkg::Options::=--force-confdef --no-install-recommends \ |
| 85 | ${PACKAGES} |
| 86 | |
| 87 | # ====== Add COPR PPA dreibh/ppa ========================================= |
| 88 | # NOTE: This is only available for Ubuntu! |
| 89 | if [ "${ID}" == "ubuntu" ] ; then |
| 90 | # Add PPA dreibh/ppa for Ubuntu: |
| 91 | DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::=--force-confold -o Dpkg::Options::=--force-confdef --no-install-recommends \ |
| 92 | software-properties-common |
| 93 | apt-add-repository -y ppa:dreibh/ppa || true |
| 94 | apt-get update -q |
| 95 | fi |
| 96 | |
| 97 | if [ ${USE_PACKAGING} -eq 1 ] ; then |
| 98 | # ====== pbuilder environment ========================================= |
| 99 | # Example for GitHub Actions: |
| 100 | # https://github.com/jrl-umi3218/github-actions/tree/master/setup-pbuilder |
| 101 | |
| 102 | if [ ! -v OS ] || [ "${OS}" == "" ] ; then |
| 103 | OS="${ID}" |
| 104 | fi |
| 105 | if [ ! -v DIST ] || [ "${DIST}" == "" ] ; then |
| 106 | DIST="${VERSION_CODENAME}" |
| 107 | fi |
| 108 | if [ ! -v ARCH ] || [ "${ARCH}" == "" ] ; then |
| 109 | ARCH="$(dpkg --print-architecture)" |
| 110 | fi |
| 111 | |
| 112 | if [ "${OS}" == "ubuntu" ] ; then |
| 113 | COMPONENTS="main universe" |
| 114 | MIRRORSITE=http://dk.archive.ubuntu.com/ubuntu/ |
| 115 | KEYRING="/usr/share/keyrings/ubuntu-archive-keyring.gpg" |
| 116 | elif [ "${OS}" == "debian" ] ; then |
| 117 | COMPONENTS="main" |
| 118 | if [ "${ARCH}" == "m68k" ] || [ "${ARCH}" == "riscv64" ] ; then |
| 119 | # Debian Ports (special architectures) |
| 120 | MIRRORSITE="http://ftp.ports.debian.org/debian-ports/" |
| 121 | KEYRING="/usr/share/keyrings/debian-ports-archive-keyring.gpg" |
| 122 | else |
| 123 | # Debian (supported architectures) |
| 124 | MIRRORSITE="http://ftp.dk.debian.org/debian/" |
| 125 | KEYRING="/usr/share/keyrings/debian-archive-keyring.gpg" |
| 126 | fi |
| 127 | else |
| 128 | echo >&2 "ERROR: Unknown distribution ${ID}!" |
| 129 | exit 1 |
| 130 | fi |
| 131 | |
| 132 | cores=$(getconf _NPROCESSORS_ONLN 2>/dev/null || true) |
| 133 | cat >/etc/pbuilderrc <<EOF |
| 134 | #!/bin/bash -e |
| 135 | |
| 136 | # ====== Settings =========================================================== |
| 137 | DISTRIBUTION="${DIST}" |
| 138 | COMPONENTS="${COMPONENTS}" |
| 139 | MIRRORSITE="${MIRRORSITE}" |
| 140 | DEBOOTSTRAPOPTS=("\${DEBOOTSTRAPOPTS[@]}" "--keyring=${KEYRING}") |
| 141 | DEBOOTSTRAPOPTS=("\${DEBOOTSTRAPOPTS[@]}" "--variant=buildd") |
| 142 | ARCHITECTURE="${ARCH}" |
| 143 | |
| 144 | APTCACHEHARDLINK=no |
| 145 | |
| 146 | USECOLORS=yes |
| 147 | COMPRESSPROG=cat |
| 148 | EXTRAPACKAGES=eatmydata |
| 149 | EATMYDATA=yes |
| 150 | |
| 151 | # Multi-core: set concurrency level. The packaging scripts will handle it properly: |
| 152 | export CONCURRENCY_LEVEL=${cores} |
| 153 | export DEB_BUILD_OPTIONS="parallel=${cores}" |
| 154 | |
| 155 | # ====== Directories ======================================================== |
| 156 | NAME="\${OS}-\${DISTRIBUTION}-\${ARCHITECTURE}" |
| 157 | BASETGZ="/var/cache/pbuilder/\${NAME}-base.tgz" |
| 158 | APTCACHE="/var/cache/pbuilder/aptcache/\${NAME}/" |
| 159 | BUILDPLACE="/var/cache/pbuilder/build/\${NAME}/" |
| 160 | BUILDRESULT="/var/cache/pbuilder/result/\${NAME}/" |
| 161 | HOOKDIR="/var/cache/pbuilder/hook.d/" |
| 162 | |
| 163 | mkdir -p \${APTCACHE} |
| 164 | mkdir -p \${BUILDRESULT} |
| 165 | mkdir -p \${HOOKDIR} |
| 166 | EOF |
| 167 | echo "----- /etc/pbuilderrc: ------------------------------------------------------" |
| 168 | cat /etc/pbuilderrc |
| 169 | echo "-----------------------------------------------------------------------------" |
| 170 | |
| 171 | OS="${OS}" DISTRIBUTION="${DIST}" ARCHITECTURE="${ARCH}" pbuilder create \ |
| 172 | --debootstrapopts --variant=buildd \ |
| 173 | | grep -v "^I: Retrieving\|^I: Validating\|^I: Unpacking\|^I: Extracting\|^I: Configuring\|^I: new cache content" |
| 174 | # --debootstrapopts --keyring=/etc/apt/trusted.gpg |
| 175 | |
| 176 | # Speed up pbuilder: |
| 177 | echo "echo \"force-unsafe-io\" > /etc/dpkg/dpkg.cfg.d/02apt-speedup" | \ |
| 178 | OS="${OS}" DISTRIBUTION="${DIST}" ARCHITECTURE="${ARCH}" pbuilder login --save-after-exec |
| 179 | |
| 180 | # ====== Add PPA dreibh/ppa =========================================== |
| 181 | if [ "${OS}" == "ubuntu" ] ; then |
| 182 | # Add PPA dreibh/ppa for Ubuntu: |
| 183 | OS="${OS}" DISTRIBUTION="${DIST}" ARCHITECTURE="${ARCH}" pbuilder login --save-after-login <<EOF |
| 184 | DEBIAN_FRONTEND=noninteractive apt-get install -qqy -o Dpkg::Options::=--force-confold -o Dpkg::Options::=--force-confdef --no-install-recommends software-properties-common |
| 185 | apt-add-repository -y ppa:dreibh/ppa |
| 186 | apt-get update -q |
| 187 | EOF |
| 188 | fi |
| 189 | fi |
| 190 | |
| 191 | |
| 192 | # ====== Fedora ============================================================= |
| 193 | elif [ "${ID}" == "fedora" ] ; then |
| 194 | PACKAGES="clang make python3 python3-distro python3-packaging python3-urllib3" |
| 195 | if [ ${USE_PACKAGING} -eq 1 ] ; then |
| 196 | # Need to install mock as well: |
| 197 | PACKAGES="${PACKAGES} fedora-release findutils mock nosync rpmdevtools which strace sudo" |
| 198 | fi |
| 199 | |
| 200 | # ====== Add COPR PPA dreibh/ppa ========================================= |
| 201 | dnf copr enable -y dreibh/ppa |
| 202 | |
| 203 | # dnf upgrade -qy |
| 204 | # shellcheck disable=SC2086 |
| 205 | dnf install -qy ${PACKAGES} |
| 206 | |
| 207 | if [ ${USE_PACKAGING} -eq 1 ] ; then |
| 208 | # ====== Mock environment ============================================= |
| 209 | groupadd -f mock |
| 210 | |
| 211 | if [ ! -v OS ] || [ "${OS}" == "" ] ; then |
| 212 | OS="${ID}" |
| 213 | fi |
| 214 | if [ ! -v DIST ] || [ "${DIST}" == "" ] ; then |
| 215 | DIST="${VERSION_ID}" |
| 216 | fi |
| 217 | if [ ! -v ARCH ] || [ "${ARCH}" == "" ] ; then |
| 218 | ARCH="$(uname -m)" |
| 219 | fi |
| 220 | |
| 221 | # ====== Enable COPR PPA ============================================== |
| 222 | if ! grep "^\[copr-dreibh-ppa\]" "/etc/mock/${OS}-${DIST}-${ARCH}.cfg" ; then |
| 223 | shopt -s extglob |
| 224 | ppa="config_opts['dnf.conf'] += \"\"\"\n[copr-dreibh-ppa]\nname=Copr repo for ppa owned by dreibh\nbaseurl=https://download.copr.fedorainfracloud.org/results/dreibh/ppa/fedora-\$releasever-\$basearch/\ntype=rpm-md\nskip_if_unavailable=True\ngpgcheck=1\ngpgkey=https://copr-be.cloud.fedoraproject.org/results/dreibh/ppa/pubkey.gpg\nrepo_gpgcheck=0\nenabled=1\nenabled_metadata=1\n\"\"\"" |
| 225 | ppa="${ppa//+( )$/\\n}" |
| 226 | echo -e "${ppa}" | tee -a "/etc/mock/${OS}-${DIST}-${ARCH}.cfg" |
| 227 | if ! grep "^\[copr-dreibh-ppa\]" "/etc/mock/${OS}-${DIST}-${ARCH}.cfg" ; then |
| 228 | echo >&2 "ERROR: Unable to inject PPA configuration into Mock configuration file /etc/mock/${OS}-${DIST}-${ARCH}.cfg!" |
| 229 | exit 1 |
| 230 | fi |
| 231 | fi |
| 232 | fi |
| 233 | |
| 234 | |
| 235 | # ====== FreeBSD ============================================================ |
| 236 | elif [ "${ID}" == "freebsd" ] ; then |
| 237 | PACKAGES="autoconf automake bash gcc libtool git python3 py312-distro py312-packaging py312-urllib3" |
| 238 | |
| 239 | # shellcheck disable=SC2086 |
| 240 | ASSUME_ALWAYS_YES=yes pkg install -y ${PACKAGES} |
| 241 | |
| 242 | if [ ! -e /usr/ports/.git ] ; then |
| 243 | rm -rf /usr/ports/* /usr/ports/.??* || true |
| 244 | mkdir -p /usr/ports |
| 245 | cd /usr/ports |
| 246 | git init -b main |
| 247 | git remote add freebsd https://git.freebsd.org/ports.git |
| 248 | git remote set-url --push freebsd ssh://[email protected]/ports.git |
| 249 | git config --add remote.freebsd.fetch "+refs/notes/*:refs/notes/*" |
| 250 | git config pull.rebase true |
| 251 | git pull --depth=1 freebsd main |
| 252 | git branch --set-upstream-to=freebsd/main main |
| 253 | fi |
| 254 | |
| 255 | # ====== Unknown ============================================================ |
| 256 | else |
| 257 | |
| 258 | echo >&2 "ERROR: Unknown distribution ${ID}!" |
| 259 | exit 1 |
| 260 | |
| 261 | fi |