Skip to content

Instantly share code, notes, and snippets.

@robinvanemden
Last active April 27, 2021 15:01
Show Gist options
  • Save robinvanemden/821adc2c84ab7b34b78aa599ec478bf2 to your computer and use it in GitHub Desktop.
Save robinvanemden/821adc2c84ab7b34b78aa599ec478bf2 to your computer and use it in GitHub Desktop.
Cross-compile fully static curl library.
#!/bin/bash
# Static libcurl crosscompiler #########################################################################################
# This is script to cross-compiles a **fully static** curl library.
# It builds WolfSSL and C-Ares, then LibCurl - therein supporting most SSL protocols including TSL 1.3.
# Helper variables, leave as is
export BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export BUILDROOT=$BASEDIR/build
export SYSROOT=${BUILDROOT}/sysroot
# Configure (cross) compiler root and prefix - adapt these to your crosscompiler
export INSTALL_PACKAGES=false
export CROSS_ROOT=/opt/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu
export COMPILER_PREFIX=aarch64-none-linux-gnu-
export BUILD=x86_64-linux-gnu
export ARCH=aarch64
export HOST=aarch64-linux
#export CROSS_FLAGS="-mcpu=cortex-a8 -mfloat-abi=hard -mfpu=neon -mtune=cortex-a8"
export CROSS_FLAGS="-march=armv8-a -mtune=cortex-a53 -mcpu=cortex-a53"
# Do not edit past this line ###########################################################################################
export PATH=$CROSS_ROOT/bin:/bin:/usr/bin
export BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export CPPFLAGS="-I${CROSS_ROOT}/include"
export AR=${CROSS_ROOT}/bin/${COMPILER_PREFIX}ar
export AS=${CROSS_ROOT}/bin/${COMPILER_PREFIX}as
export LD=${CROSS_ROOT}/bin/${COMPILER_PREFIX}ld
export RANLIB=${CROSS_ROOT}/bin/${COMPILER_PREFIX}ranlib
export CC=${CROSS_ROOT}/bin/${COMPILER_PREFIX}gcc
export NM=${CROSS_ROOT}/bin/${COMPILER_PREFIX}nm
set -eu
# Install
install_packages() {
apt-get update && apt-get upgrade -yy
local packages=(
# Required build tools
make
cmake
autoconf
automake
pkg-config
libtool
flex
bison
gettext
# Utilities required
curl
ca-certificates
xz-utils
# For debugging
less
silversearcher-ag
tree
vim
)
apt-get install -yy --no-install-recommends "${packages[@]}"
}
# Download helper function
fetch() {
local url fileopt
if [[ $# -eq 2 ]]; then
fileopt="-o $1"
url="$2"
elif [[ $# -eq 1 ]]; then
fileopt="-O"
url="$1"
else
echo "Bad args"
exit 1
fi
/usr/local/bin/curl -L '-#' $fileopt $url
}
fetch_deps() {
cd $BUILDROOT/srcs
echo "Fetching sources..."
fetch https://github.com/wolfSSL/wolfssl/archive/v4.6.0-stable.tar.gz
fetch https://c-ares.haxx.se/download/c-ares-1.17.1.tar.gz
fetch https://curl.se/download/curl-7.75.0.tar.xz
}
build_wolfssl() {
cd $BUILDROOT/build
tar -xf $BUILDROOT/srcs/v4.6.0* && cd wolf*
./autogen.sh
./configure \
--prefix=$SYSROOT \
--enable-tls13 \
--exec-prefix=$SYSROOT \
--host=$HOST \
--disable-shared \
CFLAGS="-fPIC ${CROSS_FLAGS}"
make -j4 AR='${COMPILER_PREFIX}ar r ' RANLIB=${COMPILER_PREFIX}ranlib
make install
}
build_cares() {
cd $BUILDROOT/build
tar -xf $BUILDROOT/srcs/c-ares* && cd *c-ares*
./configure CFLAGS="-fPIC ${CROSS_FLAGS}" --host=$HOST --prefix=$SYSROOT --with-pic --enable-static --disable-shared
make -j4
make install
}
build_curl() {
cd $BUILDROOT/build
tar -xf $BUILDROOT/srcs/curl-*
cd curl-*
./configure --host=$HOST --prefix=$SYSROOT \
--disable-shared \
--enable-static \
--disable-netrc \
--disable-ipv6 \
--disable-verbose \
--disable-versioned-symbols \
--disable-ntlm-wb \
--disable-proxy \
--disable-manual \
--disable-crypto-auth \
--disable-ldap --disable-sspi --without-librtmp --disable-ftp --disable-file --disable-dict \
--disable-telnet --disable-tftp --disable-pop3 --disable-imap --disable-smtp --disable-rtsp \
--disable-ldap --disable-ipv6 --disable-unix-sockets \
--disable-gopher --disable-smb --without-libidn \
--with-wolfssl=$SYSROOT \
--without-zlib \
--enable-ares=$SYSROOT \
CFLAGS="-fPIC ${CROSS_FLAGS}" \
LIBS="-ldl -lm" \
PKG_CONFIG_PATH="$SYSROOT/lib/pkgconfig"
LIBS="-ldl"
make -j4 curl_LDFLAGS=-all-static -ldl
make install
}
main() {
# download and install packages
if [ "INSTALL_PACKAGES" = true ]; then
install_packages
fi
# clean start
rm -rf $BASEDIR/build
mkdir $BASEDIR/build
mkdir -p $BUILDROOT/srcs $BUILDROOT/build $SYSROOT
# install_packages
fetch_deps
build_cares
build_wolfssl
if [ -f $SYSROOT/lib/pkgconfig/wolfssl.pc ]; then
mv $SYSROOT/lib/pkgconfig/wolfssl.pc $SYSROOT/lib/pkgconfig/wolfssl.pc_
fi
if [ -f $SYSROOT/lib/pkgconfig/libcares.pc ]; then
mv $SYSROOT/lib/pkgconfig/libcares.pc $SYSROOT/lib/pkgconfig/libcares.pc_
fi
build_curl
# clean
rm -rf $BASEDIR/build/build
rm -rf $BASEDIR/build/srcs
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment