Compiling Static Binaries
Static binaries of user space applications have many advantages, it can be used on locked down Linux appliances like firewalls, VMware ESXi hosts, etc. Static binaries can run on other architectures using qemu and binfmt-support emulation, eg. running x86 binaries on ARM64.
Binaries of Ubuntu/Debian are built from source .deb packages using apt-get build --source. This script overrides the build command to create static binaries.
For clean builds, a ephemeral docker container can be used to install the dev tool chain and perform a clean build, eg. docker run -ti --rm -v $PWD:/build ubuntu:jammy bash /build/build.sh rsync.
#!/bin/bash
# Filename: build.sh
# Uses the Ubuntu "apt-get source --build" command to compile static binaries.
# Script to be run inside a Ubuntu docker image to build static binaries
# The current dir will be used as the /build directory and binaries will be
#
# docker run -ti --rm -v $PWD:/build ubuntu:jammy bash /build/build.sh rsync
[ $# -ne 1 ] && echo -e "Usage: $0 app-name\n\tEg. $0 rsync" && exit 1
app=$1
echo "Building ${app} in the current directory."
# do not continue script on errors
set -euo pipefail
# Verbose
set -x
cd /build
sed -i 's/^#.deb-src/deb-src/g' /etc/apt/sources.list
apt-get update
apt-get -y upgrade
apt-get -y install dpkg-dev
export CFLAGS='-static'
export LDFLAGS='-static'
export CPPFLAGS='-static'
export CFLAGS_APPEND='-static'
export LDFLAGS_APPEND='-static'
export CPPFLAGS_APPEND='-static'
apt-get -y build-dep ${app}
apt-get -y source --build ${app}