Skip to content

Instantly share code, notes, and snippets.

@kwilczynski
Last active December 2, 2021 06:43
Show Gist options
  • Save kwilczynski/9270161 to your computer and use it in GitHub Desktop.
Save kwilczynski/9270161 to your computer and use it in GitHub Desktop.
Build "file" and "libmagic" for use in Travis CI.
#!/bin/bash
# Build "file" binary and "libmagic" library for use in Travis CI.
# Note: All relevant environment variables are set during the Travis CI run,
# see the following for details: https://github.com/kwilczynski/ruby-magic
set -e
set -o pipefail
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:${PATH}
# Avoid the configuration file questions ...
cat <<EOF | tee /etc/apt/apt.conf.d/99apt &>/dev/null
DPkg::options { "--force-confdef"; "--force-confnew"; }
EOF
# Disable need for user interaction ...
export DEBIAN_FRONTEND=noninteractive
export DEBCONF_NONINTERACTIVE_SEEN=true
# Clean up before building ...
rm -Rf file-${VERSION} file-${VERSION}.tar.gz &> /dev/null
# Update package repositories ...
apt-get update &> /dev/null
# Install needed build time dependencies ...
apt-get install --force-yes -y \
curl \
git \
mercurial \
make \
bison \
flex \
build-essential \
autoconf \
automake \
autotools-dev \
libltdl-dev \
libtool \
libtool-doc &> /dev/null
# Remove existing "libmagic-dev" package.
apt-get remove --purge --force-yes -y libmagic-dev &> /dev/null
# Clean up unneeded packages ...
for action in autoremove autoclean clean; do
apt-get --force-yes -y $action &>/dev/null
done
# Download source code.
if ! curl -s ftp://ftp.astron.com/pub/file/file-${VERSION}.tar.gz > file-${VERSION}.tar.gz; then
# Try download from a mirror.
curl -s http://ftp.clfs.org/pub/clfs/conglomeration/file/file-${VERSION}.tar.gz > file-${VERSION}.tar.gz
fi
# Validate SHA1 checksum ...
echo "$SHA1 *file-${VERSION}.tar.gz" | sha1sum -c
# Unpack source code.
tar -zxf file-${VERSION}.tar.gz
pushd file-${VERSION} &> /dev/null
# Remove surplus file, often left behind. But version "5.09" will not re-generate ...
if [[ $VERSION != "5.09" ]]; then
rm -f src/magic.h
fi
# Version "5.14" of "libmagic" is very buggy, download patch ...
if [[ $VERSION == "5.14" ]]; then
PATCH_URL="https://gist.githubusercontent.com/kwilczynski/6583179/raw/8c5fbac07472a0a4b68d4028fc9031647ba50876/file-5.14.diff"
PATCH_LEVEL=1
fi
# Another buggy release, apply patch.
if [[ $VERSION == "5.18" ]]; then
PATCH_URL="https://gist.githubusercontent.com/kwilczynski/9925996/raw/483a877749c925a8dcc068de8e3ce5b92f5ea826/338-341-342.diff"
PATCH_LEVEL=1
fi
if [[ -n $PATCH_URL ]]; then
curl -L -s $PATCH_URL > patch.diff
for option in '--dry-run -s -i' '-i'; do
if ! patch -l -t -p${PATCH_LEVEL} $option patch.diff; then
break
fi
done
fi
# Build "file" and "libmagic" ...
for action in clean distclean; do
make $action || true
done
libtoolize
aclocal
autoheader
automake --add-missing
autoconf
./configure --prefix=/usr --enable-fsect-man5
if [[ $? != 0 ]]; then
$CC --version
cat config.log
declare -p
exit $?
fi
make -j 4
make install
# Make sure to update dynamic libraries cache.
ldconfig
popd &> /dev/null
# Clean up after building ...
rm -Rf file-${VERSION} file-${VERSION}.tar.gz &> /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment