Skip to content

eBPF-based security platform

8.6 KB
Raw Blame
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2026 Authors of shirudo
#
# Makefile.target-deps -- build and stage target-side static dependencies.
#
# OpenSSL is built here rather than taken from the distribution because the
# target needs 3.5 or newer (ML-DSA certificates, hybrid ML-KEM TLS) as a
# static archive, which no current distribution ships. libcurl and
# TPM2-TSS are then built against that same OpenSSL so the target image
# carries exactly one crypto and TLS implementation.

SHELL := /bin/sh

# The packaging build passes shirudo's install paths (prefix, libdir,
# DESTDIR) on the command line, and make hands those to every sub-make,
# where they override what each dependency's own configure chose: OpenSSL
# would install into the system libdir. The dependencies' makes get none.
MAKEOVERRIDES :=

TARGET_DEPS_DIR ?= build/target-deps
TARGET_STATIC_LIBDIR ?= $(TARGET_DEPS_DIR)/lib
TARGET_OPENSSL_PREFIX ?= $(TARGET_DEPS_DIR)/openssl
TARGET_CURL_PREFIX ?= $(TARGET_DEPS_DIR)/curl
TARGET_SOURCE_DIR ?= $(TARGET_DEPS_DIR)/src

# configure's --prefix/--libdir options require absolute paths. CURDIR is
# already absolute and avoids evaluating GNU Make's abspath function on
# host-mounted filesystems used by the CI VM.
target-absolute = $(if $(filter /%,$(1)),$(1),$(CURDIR)/$(1))
override TARGET_DEPS_DIR := $(call target-absolute,$(TARGET_DEPS_DIR))
override TARGET_STATIC_LIBDIR := $(call target-absolute,$(TARGET_STATIC_LIBDIR))
override TARGET_OPENSSL_PREFIX := $(call target-absolute,$(TARGET_OPENSSL_PREFIX))
override TARGET_CURL_PREFIX := $(call target-absolute,$(TARGET_CURL_PREFIX))
override TARGET_SOURCE_DIR := $(call target-absolute,$(TARGET_SOURCE_DIR))

# OpenSSL 3.5 is the LTS release that introduced ML-DSA and ML-KEM.
OPENSSL_VERSION ?= 3.5.9
CURL_VERSION ?= 8.18.0
TSS_VERSION ?= 4.2.0

TARGET_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH 2>/dev/null || gcc -print-multiarch)

# The multiarch directory on Debian, /usr/lib64 on Fedora and RHEL: the
# first that holds libelf.a, else the first candidate.
TARGET_SYSTEM_ROOT ?= /usr
TARGET_SYSTEM_LIBDIRS := \
	$(if $(TARGET_MULTIARCH),$(TARGET_SYSTEM_ROOT)/lib/$(TARGET_MULTIARCH)) \
	$(TARGET_SYSTEM_ROOT)/lib64 $(TARGET_SYSTEM_ROOT)/lib
TARGET_SYSTEM_LIBDIR ?= $(firstword \
	$(foreach d,$(TARGET_SYSTEM_LIBDIRS),$(if $(wildcard $(d)/libelf.a),$(d))) \
	$(TARGET_SYSTEM_LIBDIRS))

OPENSSL_PKG_CONFIG_PATH := $(TARGET_OPENSSL_PREFIX)/lib/pkgconfig

# shirudo's clients speak HTTPS to OCI registries and OTLP/S3 endpoints
# and nothing else. Pin every optional libcurl dependency off so the
# static closure does not change with the build host's -dev packages
# (zlib stays: it is already part of the target runtime archives).
CURL_FEATURE_FLAGS ?= \
	--without-libpsl --without-brotli --without-zstd \
	--without-nghttp2 --without-nghttp3 --without-ngtcp2 \
	--without-libidn2 --without-librtmp --without-libssh2 \
	--without-libssh --without-libgsasl --without-winidn \
	--disable-ldap --disable-ldaps --disable-rtsp --disable-dict \
	--disable-telnet --disable-tftp --disable-pop3 --disable-imap \
	--disable-smtp --disable-gopher --disable-mqtt --disable-smb \
	--disable-ftp --disable-file --disable-manual --disable-docs
OPENSSL_TARBALL := $(TARGET_SOURCE_DIR)/openssl-$(OPENSSL_VERSION).tar.gz
OPENSSL_SOURCE := $(TARGET_SOURCE_DIR)/openssl-$(OPENSSL_VERSION)
OPENSSL_STAMP := $(TARGET_OPENSSL_PREFIX)/.built

CURL_TARBALL := $(TARGET_SOURCE_DIR)/curl-$(CURL_VERSION).tar.xz
CURL_SOURCE := $(TARGET_SOURCE_DIR)/curl-$(CURL_VERSION)
CURL_STAMP := $(TARGET_CURL_PREFIX)/.built

TSS_SOURCE := $(TARGET_SOURCE_DIR)/tpm2-tss-$(TSS_VERSION)
TSS_STAMP := $(TARGET_STATIC_LIBDIR)/.tpm2-tss-built
STATIC_ARCHIVES_STAMP := $(TARGET_STATIC_LIBDIR)/.system-archives-staged
ENV_FILE := $(TARGET_DEPS_DIR)/target.env

# libzstd is libelf's compression dependency on current distributions.
SYSTEM_ARCHIVES := \
	$(TARGET_SYSTEM_LIBDIR)/libelf.a \
	$(TARGET_SYSTEM_LIBDIR)/libz.a \
	$(TARGET_SYSTEM_LIBDIR)/libzstd.a \
	$(TARGET_SYSTEM_LIBDIR)/libluajit-5.1.a

OPENSSL_ARCHIVES := \
	libssl.a \
	libcrypto.a

TSS_ARCHIVES := \
	libtss2-esys.a \
	libtss2-tctildr.a \
	libtss2-tcti-device.a \
	libtss2-sys.a \
	libtss2-mu.a

.PHONY: all openssl clean check-system-archives

all: $(OPENSSL_STAMP) $(CURL_STAMP) $(TSS_STAMP) $(STATIC_ARCHIVES_STAMP) $(ENV_FILE)
	@printf 'shirudo: target dependencies ready\n'
	@printf 'shirudo: run make target (or source %s)\n' '$(ENV_FILE)'

# The static OpenSSL alone, for shirudo-quote-check where the
# distribution ships no libcrypto.a.
openssl: $(OPENSSL_STAMP)

# Static-only, no shared objects or loadable modules: the target binary
# must not pick up a provider or engine from the host filesystem. The
# OPENSSLDIR is the Debian-family system location so default trust
# lookups land on the target's CA store, not on this build prefix.
$(OPENSSL_STAMP):
	@mkdir -p "$(TARGET_SOURCE_DIR)" "$(TARGET_OPENSSL_PREFIX)"
	@if test ! -f "$(OPENSSL_TARBALL)"; then \
		curl -fsSL "https://github.com/openssl/openssl/releases/download/openssl-$(OPENSSL_VERSION)/openssl-$(OPENSSL_VERSION).tar.gz" \
			-o "$(OPENSSL_TARBALL)"; \
	fi
	@rm -rf "$(OPENSSL_SOURCE)"
	@tar -C "$(TARGET_SOURCE_DIR)" -xf "$(OPENSSL_TARBALL)"
	@cd "$(OPENSSL_SOURCE)" && \
		./Configure \
			--prefix="$(TARGET_OPENSSL_PREFIX)" \
			--openssldir=/etc/ssl \
			--libdir=lib \
			no-shared no-module no-docs no-tests no-apps
	@$(MAKE) -C "$(OPENSSL_SOURCE)" -j$$(nproc)
	@$(MAKE) -C "$(OPENSSL_SOURCE)" DESTDIR= install_sw
	@touch "$@"

$(CURL_STAMP): $(OPENSSL_STAMP)
	@mkdir -p "$(TARGET_SOURCE_DIR)" "$(TARGET_CURL_PREFIX)"
	@if test ! -f "$(CURL_TARBALL)"; then \
		curl -fsSL "https://curl.se/download/curl-$(CURL_VERSION).tar.xz" \
			-o "$(CURL_TARBALL)"; \
	fi
	@rm -rf "$(CURL_SOURCE)"
	@tar -C "$(TARGET_SOURCE_DIR)" -xf "$(CURL_TARBALL)"
	@cd "$(CURL_SOURCE)" && \
		PKG_CONFIG_PATH="$(OPENSSL_PKG_CONFIG_PATH)" \
		CPPFLAGS="-I$(TARGET_OPENSSL_PREFIX)/include" \
		LDFLAGS="-L$(TARGET_OPENSSL_PREFIX)/lib" \
		./configure \
			--prefix="$(TARGET_CURL_PREFIX)" \
			--with-openssl="$(TARGET_OPENSSL_PREFIX)" \
			--with-ca-bundle=/etc/ssl/certs/ca-certificates.crt \
			--with-ca-path=/etc/ssl/certs \
			--enable-static \
			--disable-shared \
			$(CURL_FEATURE_FLAGS)
	@$(MAKE) -C "$(CURL_SOURCE)" -j$$(nproc)
	@$(MAKE) -C "$(CURL_SOURCE)" DESTDIR= install
	@touch "$@"

$(TSS_STAMP): Makefile.target-deps $(OPENSSL_STAMP)
	@mkdir -p "$(TARGET_SOURCE_DIR)" "$(TARGET_STATIC_LIBDIR)"
	@if test ! -d "$(TSS_SOURCE)"; then \
		git clone --depth 1 --branch "$(TSS_VERSION)" \
			https://github.com/tpm2-software/tpm2-tss.git "$(TSS_SOURCE)"; \
	fi
	@cd "$(TSS_SOURCE)" && ./bootstrap
	@cd "$(TSS_SOURCE)" && \
		PKG_CONFIG_PATH="$(OPENSSL_PKG_CONFIG_PATH)" \
		./configure \
			--prefix="$(TARGET_DEPS_DIR)" \
			--libdir="$(TARGET_STATIC_LIBDIR)" \
			--enable-static \
			--disable-shared \
			--enable-nodl \
			--disable-fapi \
			--disable-policy \
			--disable-tcti-cmd \
			--disable-tcti-i2c-ftdi \
			--disable-tcti-i2c-helper \
			--disable-tcti-libtpms \
			--disable-tcti-pcap \
			--disable-tcti-spi-ftdi \
			--disable-tcti-spi-helper \
			--disable-tcti-spi-ltt2go \
			--disable-tcti-spidev \
			--disable-tcti-mssim \
			--disable-tcti-swtpm
	@$(MAKE) -C "$(TSS_SOURCE)" -j$$(nproc)
	@$(MAKE) -C "$(TSS_SOURCE)" DESTDIR= install
	@touch "$@"

check-system-archives:
	@for archive in $(SYSTEM_ARCHIVES); do \
		if test ! -f "$$archive"; then \
			printf 'shirudo: missing static archive: %s\n' "$$archive" >&2; \
			exit 1; \
		fi; \
	done

$(STATIC_ARCHIVES_STAMP): check-system-archives $(OPENSSL_STAMP) $(TSS_STAMP)
	@mkdir -p "$(TARGET_STATIC_LIBDIR)"
	@for archive in $(SYSTEM_ARCHIVES); do \
		install -m 0644 "$$archive" "$(TARGET_STATIC_LIBDIR)/$$(basename "$$archive")"; \
	done
	@for archive in $(OPENSSL_ARCHIVES); do \
		if test ! -f "$(TARGET_OPENSSL_PREFIX)/lib/$$archive"; then \
			printf 'shirudo: OpenSSL build did not produce %s\n' "$$archive" >&2; \
			exit 1; \
		fi; \
		install -m 0644 "$(TARGET_OPENSSL_PREFIX)/lib/$$archive" \
			"$(TARGET_STATIC_LIBDIR)/$$archive"; \
	done
	@for archive in $(TSS_ARCHIVES); do \
		if test ! -f "$(TARGET_STATIC_LIBDIR)/$$archive"; then \
			printf 'shirudo: TPM2-TSS build did not produce %s\n' "$$archive" >&2; \
			exit 1; \
		fi; \
	done
	@touch "$@"

$(ENV_FILE): $(OPENSSL_STAMP) $(CURL_STAMP) $(STATIC_ARCHIVES_STAMP)
	@mkdir -p "$(TARGET_DEPS_DIR)"
	@{ \
		printf '%s\n' '# Source this file or use make target from the repository root.'; \
		printf 'export SHIRUDO_TARGET_PKG_CONFIG_PATH=%s\n' "$(TARGET_CURL_PREFIX)/lib/pkgconfig:$(OPENSSL_PKG_CONFIG_PATH):$(TARGET_STATIC_LIBDIR)/pkgconfig"; \
		printf 'export SHIRUDO_TARGET_STATIC_LIBDIR=%s\n' "$(TARGET_STATIC_LIBDIR)"; \
	} > "$@"

clean:
	@rm -rf "$(TARGET_DEPS_DIR)"