Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
静态编译ss
#!/bin/bash
# Update: 2021-03-17
# Modified by lixingcong
# Fork from https://github.com/necan/shadowsocks-libev-static-build
# Attention: modified from https://github.com/shadowsocks/shadowsocks-libev/blob/master/docker/mingw/build.sh
# The script does not need to run as root
set -e
BASEDIR=/mydata/temp/static-build
#SOCKS5_PROXY=127.0.0.1:1080
DEST_ARCH=x86_64
# check if missed the bins
# autoreconf is a part of autoconf, aclocal is a part of automake
REQUIRE_BINS=("gcc" "g++" "curl" "git" "autoreconf" "aclocal" "libtoolize" "tar" "gzip")
MISSING_BINS=
for b in "${REQUIRE_BINS[@]}"; do
type $b >/dev/null 2>&1 || MISSING_BINS+="$b "
done
if [ ! "x" = "x$MISSING_BINS" ]; then
echo >&2 "Error: the following binaries were not found:"
echo >&2 $MISSING_BINS
exit 1
fi
unzip_file()
{
local filename=$1
echo "unzipping ${filename}..."
tar -xf ${filename} >/dev/null 2>&1 && return 0
gzip -d ${filename} >/dev/null 2>&1 && return 0
echo "error when unzip ${filename}"
exit 1
}
CURL_CMD=curl
if [ ! "x$SOCKS5_PROXY" = "x" ]; then
echo "Use socks5 proxy for curl: $SOCKS5_PROXY"
CURL_CMD="curl -x socks5://$SOCKS5_PROXY"
# git proxy: https://gist.github.com/evantoli/f8c23a37eb3558ab8765
git config --global http.proxy socks5://$SOCKS5_PROXY
git config --global https.proxy socks5://$SOCKS5_PROXY
fi
PREFIX="$BASEDIR/stage"
SRC="$BASEDIR/src"
DIST="$BASEDIR/dist"
CORES=`grep -c ^processor /proc/cpuinfo`
mkdir -p $BASEDIR
mkdir -p $PREFIX
mkdir -p $SRC
mkdir -p $DIST
cd $BASEDIR
# libev
LIBEV_VER=4.33
LIBEV_NAME=libev-${LIBEV_VER}
LIBEV_URL=http://dist.schmorp.de/libev/Attic/${LIBEV_NAME}.tar.gz
# mbedTLS
MBEDTLS_VER=2.25.0
MBEDTLS_NAME=mbedtls-${MBEDTLS_VER}
MBEDTLS_URL=https://codeload.github.com/ARMmbed/mbedtls/tar.gz/${MBEDTLS_NAME}
# Sodium
SODIUM_VER=1.0.18
SODIUM_NAME=libsodium-${SODIUM_VER}
SODIUM_URL=https://download.libsodium.org/libsodium/releases/${SODIUM_NAME}.tar.gz
# PCRE
PCRE_VER=8.44
PCRE_NAME=pcre-${PCRE_VER}
PCRE_URL=https://ftp.pcre.org/pub/pcre/${PCRE_NAME}.tar.gz
# c-ares
CARES_VER=1.17.1
CARES_NAME=c-ares-${CARES_VER}
CARES_URL=https://c-ares.haxx.se/download/${CARES_NAME}.tar.gz
# 下载源码
echo "Download and overwrite deps source now? (y/n)"
read choose
if [ "xy" = "x$choose" ]; then
rm -rf "${SRC}/*"
pushd "${SRC}"
for pkg in LIBEV SODIUM PCRE CARES; do
#for pkg in LIBEV SODIUM MBEDTLS PCRE CARES; do
name=${pkg}_NAME
url=${pkg}_URL
filename="${!name}".tar.gz
$CURL_CMD ${!url} --output "${filename}"
unzip_file ${filename}
done
popd
fi
# compile deps
build_deps() {
# 静态编译参数
arch=$1
host=$arch-linux-gnu
prefix=${PREFIX}/$arch
config_args="--host=${host} --prefix=${prefix} --disable-shared --enable-static"
CORES=`grep -c ^processor /proc/cpuinfo`
# libev
cd "$SRC/$LIBEV_NAME"
./configure $config_args
make clean
make install -j $CORES
# mbedtls
#cd "$SRC/mbedtls-$MBEDTLS_NAME"
#make clean
#make no_test DESTDIR="${prefix}" CC="${host}-gcc" AR="${host}-ar" LD="${host}-ld" LDFLAGS=-static install -j $CORES
#unset DESTDIR
# sodium
cd "$SRC/$SODIUM_NAME"
./configure $config_args
make clean
make install -j $CORES
# pcre
cd "$SRC/$PCRE_NAME"
./configure $config_args \
--enable-unicode-properties --enable-utf8 --disable-cpp
make clean
make install -j $CORES
# c-ares
cd "$SRC/$CARES_NAME"
./configure $config_args --disable-debug
make clean
make install -j $CORES
}
echo "force rebuild the deps now? (y/n)"
read choose
if [ "xy" = "x$choose" ]; then
build_deps $DEST_ARCH
fi
# ss source
cd $BASEDIR
SHADOWSOCKS_DIR=shadowsocks-libev
if [ ! -d $SHADOWSOCKS_DIR ]; then
git clone https://github.com/shadowsocks/shadowsocks-libev $SHADOWSOCKS_DIR
pushd $SHADOWSOCKS_DIR
git submodule update --init --recursive
popd #SHADOWSOCKS_DIR
else
echo "Git pull shadowsocks-libev, update submodules now? (y/n)"
read choose
if [ "xy" = "x$choose" ]; then
pushd $SHADOWSOCKS_DIR
git pull
git submodule update --init --recursive
popd #SHADOWSOCKS_DIR
fi
fi
if [ ! "x$SOCKS5_PROXY" = "x" ]; then
echo "Disable proxy and write global gitconfig for git? (y/n)"
read choose
if [ "xy" = "x$choose" ]; then
git config --global --unset http.proxy
git config --global --unset https.proxy
fi
fi
echo "Execute shadowsocks-libev/autogen.sh now? (y/n)"
read choose
if [ "xy" = "x$choose" ]; then
pushd $SHADOWSOCKS_DIR
./autogen.sh
popd #SHADOWSOCKS_DIR
fi
build_ss() {
arch=$1
host=$arch-linux-gnu
prefix=${DIST}/$arch
dep=${PREFIX}/$arch
pushd $SHADOWSOCKS_DIR
# --with-mbedtls="$dep"
./configure LIBS="-lpthread -lm" \
CFLAGS="-DCARES_STATICLIB -DPCRE_STATIC -I$dep/include" \
--host=${host} \
--prefix=${prefix} \
--disable-ssp \
--disable-documentation \
--with-pcre="$dep" \
--with-sodium="$dep" \
--with-cares="$dep"
make clean
make install LDFLAGS="-all-static -L${dep}/lib" -j $CORES
popd #SHADOWSOCKS_DIR
}
build_ss $DEST_ARCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment