Skip to content

Instantly share code, notes, and snippets.

@milisarge
Last active February 1, 2022 19:45
Show Gist options
  • Save milisarge/b9c03e9ad2b2c138e3e1dcc130ada141 to your computer and use it in GitHub Desktop.
Save milisarge/b9c03e9ad2b2c138e3e1dcc130ada141 to your computer and use it in GitHub Desktop.
GNU tar static build
#!/bin/bash
#
# build static tar because we need exercises in minimalism
# MIT licentar: google it or see robxu9.mit-license.org.
# https://github.com/luciusmagn/tar-static/
# For Linux, also builds musl for truly static linking.
tar_version="1.34"
musl_version="1.2.2"
platform=$(uname -s)
if [ -d build ]; then
echo "= removing previous build directory"
rm -rf build
fi
mkdir build # make build directory
pushd build
# download tarballs
echo "= downloading tar"
curl -LO http://ftp.gnu.org/gnu/tar/tar-${tar_version}.tar.xz
echo "= extracting tar"
tar xJf tar-${tar_version}.tar.xz
if [ "$platform" = "Linux" ]; then
echo "= downloading musl"
curl -LO http://www.musl-libc.org/releases/musl-${musl_version}.tar.gz
echo "= extracting musl"
tar -xf musl-${musl_version}.tar.gz
echo "= building musl"
working_dir=$(pwd)
install_dir=${working_dir}/musl-install
pushd musl-${musl_version}
env CFLAGS="$CFLAGS -Os -ffunction-sections -fdata-sections" LDFLAGS='-Wl,--gc-sections' ./configure --prefix=${install_dir}
make install
popd # musl-${musl-version}
echo "= setting CC to musl-gcc"
export CC=${working_dir}/musl-install/bin/musl-gcc
export CFLAGS="-static"
else
echo "= WARNING: your platform does not support static binaries."
echo "= (This is mainly due to non-static libc availability.)"
fi
echo "= building tar"
pushd tar-${tar_version}
env FORCE_UNSAFE_CONFIGURE=1 CFLAGS="$CFLAGS -Os -ffunction-sections -fdata-sections" LDFLAGS='-Wl,--gc-sections' ./configure
make
popd # tar-${tar_version}
popd # build
if [ ! -d releases ]; then
mkdir releases
fi
echo "= striptease"
strip -s -R .comment -R .gnu.version --strip-unneeded build/tar-${tar_version}/src/tar
#echo "= compressing"
#upx --ultra-brute build/tar-${tar_version}/src/tar
echo "= extracting tar binary"
cp build/tar-${tar_version}/src/tar releases
echo "= done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment