Skip to content

Instantly share code, notes, and snippets.

@rampageX
Last active March 23, 2022 06:24
Show Gist options
  • Save rampageX/27e6c2b4ce60fbe4e6427e04ca8ea3f7 to your computer and use it in GitHub Desktop.
Save rampageX/27e6c2b4ce60fbe4e6427e04ca8ea3f7 to your computer and use it in GitHub Desktop.

Revisions

  1. rampageX renamed this gist Aug 11, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. rampageX created this gist Aug 11, 2021.
    134 changes: 134 additions & 0 deletions sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,134 @@
    #!/bin/sh

    #Cross Build Static aarch64 Tor Binary on Alpine x86_x64

    install_dir="`pwd`/aarch64"
    include_dir="${install_dir}/include"
    lib_dir="${install_dir}/lib"

    custom_flags_set() {
    CPPFLAGS="--static -static -I${include_dir}"
    LDFLAGS="--static -static -Wl,--no-as-needed -L${lib_dir} -lpthread -pthread"
    }

    custom_flags_reset() {
    CPPFLAGS=""
    LDFLAGS=""
    }

    custom_flags_reset

    [ -n "$1" -a "$1" = "prepare" ] && {
    #Prerequirements
    # apt-get update
    # apt-get install -y build-essential curl
    #lzma, zstd
    # apt install -y liblzma-dev
    # apt install -y libzstd-dev

    #Dependecies
    rm -rf aarch64/
    rm -rf xz-5.2.5/
    rm -rf zstd-1.5.0/
    rm -rf zlib-1.2.11/
    rm -rf libevent-2.1.8-stable/
    rm -rf openssl-1.1.1k/

    #lzma
    curl -fsSL "https://tukaani.org/xz/xz-5.2.5.tar.gz" | tar zxvf -
    cd xz-5.2.5
    CC=aarch64-linux-musl-gcc ./configure --prefix=${install_dir} --host=aarch64-linux-musl
    make -j"$(nproc)" CXXFLAGS="${CXXFLAGS}" CPPFLAGS="${CPPFLAGS}" LDFLAGS="${LDFLAGS}"
    make install
    cd ..

    # exit

    #zstd
    curl -fsSL "https://github.com/facebook/zstd/releases/download/v1.5.0/zstd-1.5.0.tar.gz" | tar zxvf -
    cd zstd-1.5.0
    CC=aarch64-linux-musl-gcc make -j"$(nproc)" \
    CXXFLAGS="${CXXFLAGS}" CPPFLAGS="${CPPFLAGS}" LDFLAGS="${LDFLAGS}"
    make PREFIX=${install_dir} install
    cd ..

    # exit

    #zLib
    curl -fsSL "https://zlib.net/zlib-1.2.11.tar.gz" | tar zxvf -
    cd zlib-1.2.11
    custom_flags_set
    CC=aarch64-linux-musl-gcc ./configure --prefix=${install_dir} --static
    make -j"$(nproc)" CXXFLAGS="${CXXFLAGS}" CPPFLAGS="${CPPFLAGS}" LDFLAGS="${LDFLAGS}"
    make install
    cd ..

    #libssl
    curl -fsSL "https://www.openssl.org/source/openssl-1.1.1k.tar.gz" | tar zxvf -
    cd openssl-1.1.1k
    custom_flags_set
    ./Configure linux-aarch64 --cross-compile-prefix=aarch64-linux-musl- --prefix=${install_dir} threads no-shared no-dso no-comp CXXFLAGS="${CXXFLAGS}" CPPFLAGS="${CPPFLAGS}" LDFLAGS="${LDFLAGS}"
    make -j$(nproc)
    # make install
    make install_sw
    cd ..

    #libevent
    curl -fsSL "https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz" | tar zxvf -
    cd libevent-2.1.8-stable
    custom_flags_set
    ./configure --host=aarch64-linux-musl \
    --prefix=${install_dir} \
    --disable-shared \
    --enable-static \
    --with-pic \
    CC=aarch64-linux-musl-gcc CXX=aarch64-linux-musl-g++ \
    CPPFLAGS="${CPPFLAGS}" LDFLAGS="${LDFLAGS}"
    make -j$(nproc)
    make install
    cd ..

    }

    #Tor
    #rm -rf tor-0.4.6.6/
    #curl -fsSL "https://dist.torproject.org/tor-0.4.6.6.tar.gz" | tar zxvf -
    #cd tor-0.4.6.6
    cd tor
    git pull
    ./autogen.sh
    make distclean
    PWD="`pwd`"
    [ -n "$1" -a "$1" = "mini" ] && {
    ./configure --prefix=$PWD/install --host=aarch64-linux-musl --disable-tool-name-check \
    --enable-static-tor \
    --disable-zstd --disable-lzma \
    --disable-manpage --disable-html-manual --disable-asciidoc --disable-unittests \
    --disable-seccomp --disable-libscrypt \
    --with-libevent-dir=${install_dir} \
    --with-openssl-dir=${install_dir} \
    --with-zlib-dir=${install_dir} \
    --disable-module-dirauth --disable-module-relay --disable-tests --disable-bench \
    CC=aarch64-linux-musl-gcc CXX=aarch64-linux-musl-g++
    # CPPFLAGS="${CPPFLAGS}" LDFLAGS="${LDFLAGS}"
    #fix `read-only segment has dynamic relocations` error
    sed -i 's/-pie//g' Makefile
    } || {
    ./configure --prefix=$PWD/install --host=aarch64-linux-musl --disable-tool-name-check \
    --enable-static-tor --enable-static-libevent --enable-static-openssl --enable-static-zlib \
    --enable-zstd --enable-lzma --enable-pic \
    --disable-manpage --disable-html-manual --disable-asciidoc --disable-unittests \
    --disable-seccomp --disable-libscrypt \
    --with-libevent-dir=${install_dir} \
    --with-openssl-dir=${install_dir} \
    --with-zlib-dir=${install_dir} \
    CC=aarch64-linux-musl-gcc CXX=aarch64-linux-musl-g++
    # CPPFLAGS="${CPPFLAGS}" LDFLAGS="${LDFLAGS}"
    sed -i 's/-pie//g' Makefile
    }
    make -j$(nproc)
    make install
    cd install/bin
    aarch64-linux-musl-strip *
    ls -la
    file ./tor