From 3905355522068f9b39b4dae336f9fe85178a1b68 Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Mon, 6 Apr 2026 23:21:33 -0500 Subject: [PATCH] Detect OS for release-sample make target --- Makefile | 6 +- buildtools/detect-local-target | 119 +++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 2 deletions(-) create mode 100755 buildtools/detect-local-target diff --git a/Makefile b/Makefile index 405c20df..cbbfdd83 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,8 @@ TOUCH ?= touch BUILDDIR := build GITHUB_REPO ?= "https://github.com/trapexit/mergerfs" -RELEASE_SAMPLE ?= "debian:13.amd64" + +DEFAULT_RELEASE_SAMPLE := debian:13.amd64 ifndef GIT_REPO ifneq ($(shell $(GIT) --version 2> /dev/null),) @@ -365,7 +366,8 @@ endef release: $(call build_release,"all") release-sample: - $(call build_release,$(RELEASE_SAMPLE)) + $(if $(RELEASE_SAMPLE),,$(eval RELEASE_SAMPLE := $(shell ./buildtools/detect-local-target 2>/dev/null))) + $(call build_release,$(if $(RELEASE_SAMPLE),$(RELEASE_SAMPLE),$(DEFAULT_RELEASE_SAMPLE))) release-amd64: $(call build_release,"amd64") release-arm64: diff --git a/buildtools/detect-local-target b/buildtools/detect-local-target new file mode 100755 index 00000000..865d0204 --- /dev/null +++ b/buildtools/detect-local-target @@ -0,0 +1,119 @@ +#!/bin/bash +# Detects local OS and maps to containerfiles target +# Returns target if found in buildtools/containerfiles/, empty string otherwise + +set -euo pipefail + +CONTAINERFILES_DIR="buildtools/containerfiles" + +# Detect system architecture and map to containerfiles format +detect_arch() { + local arch + arch=$(uname -m) + case "$arch" in + x86_64) echo "amd64" ;; + aarch64) echo "arm64" ;; + armv7l) echo "armhf" ;; + i686|i386) echo "i386" ;; + riscv64) echo "riscv64" ;; + *) echo "" ;; + esac +} + +# Detect distro and version from os-release +detect_distro() { + local os_release="${1:-}" + if [[ ! -r "$os_release" ]]; then + return 1 + fi + + # Source the file safely - ID and VERSION_ID are environment variables + . "$os_release" + + local distro="$ID" + local version="$VERSION_ID" + + # Map distro ID to containerfiles naming convention + case "$distro" in + ubuntu|debian|fedora) ;; + rocky|rhel|centos|almalinux) distro="rockylinux" ;; + *) + # Fallback to ID_LIKE for derivatives + if [[ -n "${ID_LIKE:-}" ]]; then + case "$ID_LIKE" in + *ubuntu*) distro="ubuntu" ;; + *debian*) distro="debian" ;; + *fedora*) distro="fedora" ;; + esac + fi + ;; + esac + + # Only return if we have valid distro and version + if [[ -n "$distro" && -n "$version" ]]; then + echo "$distro:$version" + else + return 1 + fi +} + +# Try to find a matching containerfile target +find_containerfile() { + local target="$1" + local containerfile="${CONTAINERFILES_DIR}/${target}" + + # Direct match + if [[ -d "$containerfile" || -f "$containerfile" ]]; then + echo "$target" + return 0 + fi + + return 1 +} + +# Main detection logic +main() { + local arch + + # Detect architecture + arch=$(detect_arch) + if [[ -z "$arch" ]]; then + echo "WARNING: Could not detect system architecture" >&2 + exit 1 + fi + + # Try to detect distro from multiple locations + local distro_ver + distro_ver=$(detect_distro "/etc/os-release") || \ + distro_ver=$(detect_distro "/usr/lib/os-release") || { + echo "WARNING: Could not detect distro from os-release" >&2 + exit 1 + } + + # Build target string + local target="${distro_ver}.${arch}" + + # Try to find matching containerfile + if find_containerfile "$target"; then + exit 0 + fi + + # Try with partial version matching (e.g., 25.10 → 25) + local distro="${distro_ver%%:*}" + local version="${distro_ver##*:}" + + # Strip last minor version number and try again + local short_version="${version%.*}" + if [[ -n "$short_version" ]]; then + target="${distro}:${short_version}.${arch}" + if find_containerfile "$target"; then + exit 0 + fi + fi + + # No match found + echo "WARNING: No containerfile found for target: ${distro_ver}.${arch}" >&2 + exit 1 +} + +main "$@"