Skip to content

Instantly share code, notes, and snippets.

@harv
Last active February 18, 2024 12:05
Show Gist options
  • Star 53 You must be signed in to star a gist
  • Fork 44 You must be signed in to fork a gist
  • Save harv/dd0bcd5eba533cbc267f6a0aaf6bee62 to your computer and use it in GitHub Desktop.
Save harv/dd0bcd5eba533cbc267f6a0aaf6bee62 to your computer and use it in GitHub Desktop.
cross & static compile shadowsocks-libev
#!/bin/sh
# cross & static compile shadowsocks-libev
PCRE_VER=8.41
PCRE_FILE="http://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-$PCRE_VER.tar.gz"
MBEDTLS_VER=2.6.0
MBEDTLS_FILE="https://tls.mbed.org/download/mbedtls-$MBEDTLS_VER-gpl.tgz"
LIBSODIUM_VER=1.0.15
LIBSODIUM_FILE="https://download.libsodium.org/libsodium/releases/libsodium-$LIBSODIUM_VER.tar.gz"
LIBEV_VER=4.24
LIBEV_FILE="http://dist.schmorp.de/libev/libev-$LIBEV_VER.tar.gz"
LIBC_ARES_VER=1.13.0
LIBC_ARES_FILE="https://c-ares.haxx.se/download/c-ares-$LIBC_ARES_VER.tar.gz"
SHADOWSOCKS_LIBEV_VER=3.1.0
SHADOWSOCKS_LIBEV_FILE="https://github.com/shadowsocks/shadowsocks-libev"
SIMPLE_OBFS_VER=0.0.4
SIMPLE_OBFS_FILE="https://github.com/shadowsocks/simple-obfs"
cur_dir=$(pwd)
prepare() {
rm -rf $cur_dir/build && mkdir $cur_dir/build
}
compile_pcre() {
[ -d $prefix/pcre ] && return
cd $cur_dir/build
wget --no-check-certificate $PCRE_FILE
tar xvf pcre-$PCRE_VER.tar.gz
cd pcre-$PCRE_VER
CPPFLAGS="-DNEED_PRINTF" ./configure --prefix=$prefix/pcre --host=$host --enable-jit --enable-utf8 --enable-unicode-properties --disable-shared
make -j$(getconf _NPROCESSORS_ONLN) && make install
}
compile_mbedtls() {
[ -d $prefix/mbedtls ] && return
cd $cur_dir/build
wget --no-check-certificate $MBEDTLS_FILE
tar xvf mbedtls-$MBEDTLS_VER-gpl.tgz
cd mbedtls-$MBEDTLS_VER
prefix_reg=$(echo $prefix | sed "s/\//\\\\\//g")
sed -i "s/DESTDIR=\/usr\/local/DESTDIR=$prefix_reg\/mbedtls/g" Makefile
[ -z $host ] && make install -j$(getconf _NPROCESSORS_ONLN) || CC=$host-gcc AR=$host-ar LD=$host-ld make install -j$(getconf _NPROCESSORS_ONLN)
}
compile_libsodium() {
[ -d $prefix/libsodium ] && return
cd $cur_dir/build
wget --no-check-certificate $LIBSODIUM_FILE
tar xvf libsodium-$LIBSODIUM_VER.tar.gz
cd libsodium-$LIBSODIUM_VER
./configure --prefix=$prefix/libsodium --host=$host --disable-ssp --disable-shared
make -j$(getconf _NPROCESSORS_ONLN) && make install
}
compile_libev() {
[ -d $prefix/libev ] && return
cd $cur_dir/build
wget --no-check-certificate $LIBEV_FILE
tar xvf libev-$LIBEV_VER.tar.gz
cd libev-$LIBEV_VER
./configure --prefix=$prefix/libev --host=$host --disable-shared
make -j$(getconf _NPROCESSORS_ONLN) && make install
}
compile_libc_ares() {
[ -d $prefix/libc-ares ] && return
cd $cur_dir/build
wget --no-check-certificate $LIBC_ARES_FILE
tar xvf c-ares-$LIBC_ARES_VER.tar.gz
cd c-ares-$LIBC_ARES_VER
./configure --prefix=$prefix/libc-ares --host=$host --disable-shared
make -j$(getconf _NPROCESSORS_ONLN) && make install
}
compile_shadowsocks_libev() {
[ -f $prefix/shadowsocks-libev/bin/ss-local ] && return
cd $cur_dir/build
git clone --branch v$SHADOWSOCKS_LIBEV_VER --single-branch --depth 1 $SHADOWSOCKS_LIBEV_FILE
cd shadowsocks-libev
git submodule update --init --recursive
./autogen.sh
LIBS="-lpthread -lm" LDFLAGS="-Wl,-static -static-libgcc -L$prefix/libc-ares/lib -L$prefix/libev/lib" CFLAGS="-I$prefix/libc-ares/include -I$prefix/libev/include" ./configure --prefix=$prefix/shadowsocks-libev --host=$host --disable-ssp --disable-documentation --with-mbedtls=$prefix/mbedtls --with-pcre=$prefix/pcre --with-sodium=$prefix/libsodium
make -j$(getconf _NPROCESSORS_ONLN) && make install
}
compile_simple_obfs() {
[ -f $prefix/shadowsocks-libev/bin/obfs-local ] && return
cd $cur_dir/build
git clone --branch v$SIMPLE_OBFS_VER --single-branch --depth 1 $SIMPLE_OBFS_FILE
cd simple-obfs
git submodule update --init --recursive
./autogen.sh
LIBS="-lpthread -lm" LDFLAGS="-Wl,-static -static-libgcc -L$prefix/libc-ares/lib -L$prefix/libev/lib -L$prefix/libsodium/lib" CFLAGS="-I$prefix/libc-ares/include -I$prefix/libev/include -I$prefix/libsodium/include" ./configure --prefix=$prefix/shadowsocks-libev --host=$host --disable-ssp --disable-documentation
make -j$(getconf _NPROCESSORS_ONLN) && make install
}
clean() {
cd $cur_dir
rm -rf $cur_dir/build
}
while [ ! -z $1 ]; do
case $1 in
-h | --help)
echo "Useage: sh $0 [--host=<host>] [--prefix=<path>]"
echo ""
echo "Options:"
echo " --host=<host> the machine that you are building for"
echo " --prefix=<path> install architecture-independent files in prefix[$cur_dir/dists]"
exit 0
;;
--host)
shift
host=$1
;;
--host=*)
arr=(${1//=/ })
host=${arr[1]}
;;
--prefix)
shift
prefix=$1
;;
--prefix=*)
arr=(${1//=/ })
prefix=${arr[1]}
;;
esac
shift
done
red="\033[0;31m"
green="\033[0;32m"
plain="\033[0m"
[ -z $host ] && compiler=gcc || compiler=$host-gcc
if [ -f "$(which $compiler)" ]; then
echo -e "found cross compiler ${green}$(which ${compiler})${plain}"
else
echo -e "${red}Error:${plain} not found cross compiler ${green}${compiler}${plain}"
exit -1
fi
[ -z $prefix ] && prefix=$cur_dir/dists
echo -e "binaries will be installed in ${green}${prefix}${plain}"
prepare
compile_pcre
compile_mbedtls
compile_libsodium
compile_libev
compile_libc_ares
compile_shadowsocks_libev
compile_simple_obfs
clean
@wobrea
Copy link

wobrea commented Jul 28, 2017

I was trying to build for ddwrt following your steps, everything (mbedtls, libsodium, libev, libudns) builds well until pcre won't build and the error is quite odd as below. I don't have much experience in cross build or even cpp build, do you have any hint what's going on here? Thanks a lot

make  all-am
make[1]: Entering directory '/home/build/pcre-8.40'
  CC       libpcre_la-pcre_byte_order.lo
  CC       libpcre_la-pcre_compile.lo
  CC       libpcre_la-pcre_config.lo
  CC       libpcre_la-pcre_dfa_exec.lo
  CC       libpcre_la-pcre_exec.lo
  CC       libpcre_la-pcre_fullinfo.lo
  CC       libpcre_la-pcre_get.lo
  CC       libpcre_la-pcre_globals.lo
  CC       libpcre_la-pcre_jit_compile.lo
  CC       libpcre_la-pcre_maketables.lo
  CC       libpcre_la-pcre_newline.lo
  CC       libpcre_la-pcre_ord2utf8.lo
  CC       libpcre_la-pcre_refcount.lo
  CC       libpcre_la-pcre_string_utils.lo
  CC       libpcre_la-pcre_study.lo
  CC       libpcre_la-pcre_tables.lo
  CC       libpcre_la-pcre_ucd.lo
  CC       libpcre_la-pcre_valid_utf8.lo
  CC       libpcre_la-pcre_version.lo
  CC       libpcre_la-pcre_xclass.lo
  CC       libpcre_la-pcre_chartables.lo
  CCLD     libpcre.la
arm-linux-ar: `u' modifier ignored since `D' is the default (see `U')
  CC       libpcreposix_la-pcreposix.lo
  CCLD     libpcreposix.la
arm-linux-ar: `u' modifier ignored since `D' is the default (see `U')
  CXX      libpcrecpp_la-pcrecpp.lo
In file included from pcrecpp.cc:37:0:
/home/build/toolchain-arm_cortex-a9_gcc-6.1.0_musl-1.1.15_eabi/arm-openwrt-linux-muslgnueabi/include/c++/6.1.0/cstdio:127:11: error: '::printf' has not been declared
   using ::printf;
           ^~~~~~
Makefile:2255: recipe for target 'libpcrecpp_la-pcrecpp.lo' failed
make[1]: *** [libpcrecpp_la-pcrecpp.lo] Error 1
make[1]: Leaving directory '/home/build/pcre-8.40'
Makefile:1322: recipe for target 'all' failed
make: *** [all] Error 2

the code around line 127 in the toolchain cstdio is like

#if __cplusplus <= 201103L
  // LWG 2249             
  using ::gets;           
#endif                    
  using ::perror;         
  using ::printf;         # 127
  using ::putc;           
  using ::putchar;        
  using ::puts;           
  using ::remove;         
  using ::rename;         
  using ::rewind;         
  using ::scanf;          

@harv
Copy link
Author

harv commented Aug 8, 2017

@buggysoul
Copy link

buggysoul commented Aug 18, 2017

@wobrea
To resolve the problem, insert the following line before "./configure" command if you use the toolchains from ddwrt.
echo "#define NEED_PRINTF 1" >> config.h.in

or explicitly pass the CPPFLAGS to gcc, which like this:

./configure				\
	--prefix="${prefix}/pcre"	\
	--host="${host}"		\
	--enable-pcre16			\
	--enable-pcre32			\
	--enable-utf			\
	--enable-unicode-properties	\
	--enable-jit			\
	--disable-shared		\
	CPPFLAGS="-DNEED_PRINTF"

@qoli
Copy link

qoli commented Aug 23, 2017

#!/bin/sh to #!/bin/bash

work for zsh.

@Blueplanet20120
Copy link

请您指导一下,谢谢您!
autoreconf: aclocal failed with exit status: 1
./11.sh: 行 95: ./configure: 没有那个文件或目录
make: *** 没有指明目标并且找不到 makefile。 停止。
正克隆到 'simple-obfs'...
remote: Counting objects: 75, done.
remote: Compressing objects: 100% (66/66), done.
remote: Total 75 (delta 7), reused 46 (delta 3), pack-reused 0
展开对象中: 100% (75/75), 完成.
检查连接... 完成。
子模组 'libcork' (https://github.com/shadowsocks/libcork.git) 未对路径 'libcork' 注册
正克隆到 'libcork'...
remote: Counting objects: 3741, done.
remote: Total 3741 (delta 0), reused 0 (delta 0), pack-reused 3741
接收对象中: 100% (3741/3741), 1.42 MiB | 897.00 KiB/s, 完成.
处理 delta 中: 100% (1969/1969), 完成.
检查连接... 完成。
子模组路径 'libcork':检出 '3bcb8324431d3bd4be5e4ff2a4323b455c8d5409'
main::scan_file() called too early to check prototype at /projects/hnd/tools/linux/hndtools-arm-linux-2.6.36-uclibc-4.5.3/bin/aclocal line 618.
autom4te: need GNU m4 1.4 or later: /projects/hnd/tools/linux/hndtools-arm-linux-2.6.36-uclibc-4.5.3/bin/m4
aclocal: /projects/hnd/tools/linux/hndtools-arm-linux-2.6.36-uclibc-4.5.3/bin/autom4te failed with exit status: 1
autoreconf: aclocal failed with exit status: 1
./11.sh: 行 108: ./configure: 没有那个文件或目录
make: *** 没有指明目标并且找不到 makefile。 停止。

@harv
Copy link
Author

harv commented Oct 25, 2017

把 hndtools 放到 PATH 的最后试试

@Blueplanet20120
Copy link

非常感谢您的指导,本人勤奋菜鸟一只,反复研究,通过您的手动编译教程成功编译3.0.8,但是最新的3.1.0编译不成功,官方改libudns为libc-ares,我就不会弄了。。。期盼您的再次指导或者更新哪个帖子。。谢谢您的帖子。

@LawrenceHan
Copy link

@qswx2007 怎么编译成功的

@LawrenceHan
Copy link

@harv 能帮忙看看嘛,我也是用Netgear R6400的

configure.ac:13: installing 'auto/ar-lib'
configure.ac:10: installing 'auto/compile'
configure.ac:21: installing 'auto/config.guess'
configure.ac:21: installing 'auto/config.sub'
configure.ac:12: installing 'auto/install-sh'
configure.ac:12: installing 'auto/missing'
libcork/Makefile.am: installing 'auto/depcomp'
checking for gcc... gcc
checking whether the C compiler works... no
configure: error: in `/root/cross/build/simple-obfs':
configure: error: C compiler cannot create executables
See `config.log' for more details
make: *** No targets specified and no makefile found.  Stop.

@Blueplanet20120
Copy link

@ LawrenceHan加入电报讨论之:https://t.me/joinchat/FWY7DA_CQ7HWhy9AyaOekA

@0x6d3f9c
Copy link

0x6d3f9c commented Nov 3, 2017

It works.

NOTE:

  1. change /bin/sh to /bin/bash if neccessary.
  2. set env properly like this

export host=arm-brcm-linux-uclibcgnueabi

@qoli
Copy link

qoli commented Aug 2, 2019

PCRE_VER=8.41
PCRE_FILE="http://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-$PCRE_VER.tar.gz"

update to

PCRE_VER=8.41
PCRE_FILE="ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-$PCRE_VER.tar.gz"

@0neday
Copy link

0neday commented Feb 10, 2021

适当优化,添加 cross toolchains,https://gist.github.com/0neday/20b95dc30157af177cb10c38f936d3ef

@rampageX
Copy link

rampageX commented Sep 7, 2021

On Alpine:

compile_shadowsocks_libev() {
    [ -f $prefix/shadowsocks-libev/bin/ss-local ] && return

    cd $cur_dir/build
    git clone --branch v$SHADOWSOCKS_LIBEV_VER --single-branch --depth 1 $SHADOWSOCKS_LIBEV_FILE
    cd shadowsocks-libev
    git submodule update --init --recursive
    ./autogen.sh
    LIBS="-lpthread -lm" LDFLAGS="-L$prefix/libc-ares/lib -L$prefix/libev/lib" CFLAGS="-I$prefix/libc-ares/include -I$prefix/libev/include" ./configure --prefix=$prefix/shadowsocks-libev --host=$host --disable-ssp --disable-documentation --with-mbedtls=$prefix/mbedtls --with-pcre=$prefix/pcre --with-sodium=$prefix/libsodium
    ld_line=$(grep "^LDFLAGS" src/Makefile | sed 's/LDFLAGS = //g')
    sed -i "s#$ld_line#-all-static -s $ld_line#g" src/Makefile
    make -j$(getconf _NPROCESSORS_ONLN) && make install
}

@artenax
Copy link

artenax commented Feb 15, 2024

All the links are out of date. None of them work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment