From 40366d7fb235883d03a716d4cd12be63b8e69ddb Mon Sep 17 00:00:00 2001 From: Yuwei Ba Date: Tue, 28 Nov 2023 12:41:24 +1100 Subject: [PATCH] build static link crt binary (#192) * musl * some progress * install musl tools * install * static crt * musl and static * ver --- .bazelversion | 1 - .cargo/config | 6 ++ .github/workflows/ci.yml | 14 ++- Cargo.lock | 6 +- Cargo.toml | 2 +- clash_lib/src/proxy/shadowsocks/mod.rs | 2 +- scripts/build.sh | 132 ++++++++++++++++++++++++- 7 files changed, 151 insertions(+), 12 deletions(-) delete mode 100644 .bazelversion diff --git a/.bazelversion b/.bazelversion deleted file mode 100644 index f9da12e11..000000000 --- a/.bazelversion +++ /dev/null @@ -1 +0,0 @@ -6.3.2 \ No newline at end of file diff --git a/.cargo/config b/.cargo/config index f2aca2991..619aac0cb 100644 --- a/.cargo/config +++ b/.cargo/config @@ -14,3 +14,9 @@ linker = "/usr/bin/arm-linux-gnueabihf-gcc" [target.armv7-unknown-linux-gnueabi] linker = "/usr/bin/arm-linux-gnueabi-gcc" + +[target.arm-unknown-linux-gnueabihf] +linker = "/usr/bin/arm-linux-gnueabihf-gcc" + +[target.arm-unknown-linux-gnueabi] +linker = "/usr/bin/arm-linux-gnueabi-gcc" \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 20a385121..739c5a34b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,12 @@ jobs: with: toolchain: stable - uses: Swatinem/rust-cache@v2 - - run: cargo test --all --all-features + - name: Run cargo check + run: cargo check --all --all-features +# - name Run cargo clippy +# run: cargo clippy --all --all-features + - name: Run cargo test + run: cargo test --all --all-features publish: name: publish ${{ matrix.platforms.target }} on ${{ matrix.platforms.os }} @@ -37,11 +42,16 @@ jobs: runs-on: ${{ matrix.platforms.os }} strategy: matrix: + static: [true, false] platforms: [ {os: ubuntu-22.04, target: x86_64-unknown-linux-gnu}, + {os: ubuntu-22.04, target: x86_64-unknown-linux-musl}, {os: ubuntu-22.04, target: aarch64-unknown-linux-gnu}, + {os: ubuntu-22.04, target: aarch64-unknown-linux-musl}, {os: ubuntu-22.04, target: armv7-unknown-linux-gnueabihf}, + {os: ubuntu-22.04, target: armv7-unknown-linux-musleabihf}, {os: ubuntu-22.04, target: armv7-unknown-linux-gnueabi}, + {os: ubuntu-22.04, target: armv7-unknown-linux-musleabi}, {os: macos-13, target: aarch64-apple-darwin}, {os: macos-13, target: x86_64-apple-darwin}, {os: windows-2022, target: x86_64-pc-windows-msvc}, @@ -59,7 +69,7 @@ jobs: toolchain: stable - uses: Swatinem/rust-cache@v2 - name: Build - run: sh ./scripts/build.sh "${{ matrix.platforms.target }}" + run: sh ./scripts/build.sh "${{ matrix.platforms.target }}" "${{ matrix.static }}" - name: Create Release id: create_release uses: softprops/action-gh-release@v1 diff --git a/Cargo.lock b/Cargo.lock index 084df9fb6..720956fa5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -744,7 +744,7 @@ checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" [[package]] name = "clash" -version = "0.1.8" +version = "0.1.9" dependencies = [ "clap 4.4.8", "clash_lib", @@ -752,14 +752,14 @@ dependencies = [ [[package]] name = "clash_doc" -version = "0.1.8" +version = "0.1.9" dependencies = [ "clash_lib", ] [[package]] name = "clash_lib" -version = "0.1.8" +version = "0.1.9" dependencies = [ "aes-gcm", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index b242335b9..f7ea7fd11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ members = [ [workspace.package] -version = "0.1.8" +version = "0.1.9" repository = "https://github.com/Watfaq/clash-rs.git" edition = "2021" diff --git a/clash_lib/src/proxy/shadowsocks/mod.rs b/clash_lib/src/proxy/shadowsocks/mod.rs index 9853c7ad8..49a95c433 100644 --- a/clash_lib/src/proxy/shadowsocks/mod.rs +++ b/clash_lib/src/proxy/shadowsocks/mod.rs @@ -4,7 +4,7 @@ mod stream; mod v2ray; use async_trait::async_trait; -use futures::{Stream, TryFutureExt}; +use futures::TryFutureExt; use shadowsocks::{ config::ServerType, context::Context, crypto::CipherKind, relay::udprelay::proxy_socket::UdpSocketType, ProxyClientStream, ProxySocket, ServerConfig, diff --git a/scripts/build.sh b/scripts/build.sh index 6a918f297..a78ab5e17 100644 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -8,35 +8,159 @@ os=`uname` case $os in Linux) sudo apt update - sudo apt install -y gcc-multilib gcc make + sudo apt install -y gcc-multilib gcc make musl-dev musl-tools ;; esac +llvm_version=16 + +install_llvm() { + if ! command -v -- "clang-$llvm_version" &> /dev/null + then + wget https://apt.llvm.org/llvm.sh -O /tmp/llvm.sh + chmod +x /tmp/llvm.sh + sudo /tmp/llvm.sh $llvm_version + fi +} + +ROOT_DIR=`git rev-parse --show-toplevel` + for TARGET in $1; do TARGET=`echo "$TARGET" | tr -d '[:space:]' | tr -d '\n' | tr -d '\r'` - echo "building for $TARGET" + echo "building for $TARGET(static: $2)" rustup target add $TARGET case $TARGET in + x86_64-unknown-linux-musl) + install_llvm + export CC=clang-$llvm_version + export CXX=clang++-$llvm_version + export CMAKE_TOOLCHAIN_FILE=$ROOT_DIR/scripts/cmake/x86_64-musl.cmake + ;; aarch64-unknown-linux-gnu) sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu binutils-aarch64-linux-gnu libc6-dev-arm64-cross export CC=aarch64-linux-gnu-gcc export CXX=aarch64-linux-gnu-g++ export BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/aarch64-linux-gnu" ;; + aarch64-unknown-linux-musl) + sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu binutils-aarch64-linux-gnu libc6-dev-arm64-cross + install_llvm + export CC=clang-$llvm_version + export CXX=clang++-$llvm_version + export LDFLAGS="-fuse-ld=lld" + export BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/aarch64-linux-gnu" + export CMAKE_TOOLCHAIN_FILE=$ROOT_DIR/scripts/cmake/aarch64-musl.cmake + ;; arm-unknown-linux-gnueabi | armv7-unknown-linux-gnueabi) sudo apt install -y gcc-arm-linux-gnueabi g++-arm-linux-gnueabi binutils-arm-linux-gnueabi libc6-dev-armel-cross export CC=arm-linux-gnueabi-gcc export CXX=arm-linux-gnueabi-g++ ;; - armv7-unknown-linux-gnueabihf) + arm-unknown-linux-gnueabihf | armv7-unknown-linux-gnueabihf) sudo apt install -y gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf binutils-arm-linux-gnueabihf libc6-dev-armhf-cross export CC=arm-linux-gnueabihf-gcc export CXX=arm-linux-gnueabihf-g++ ;; + arm-unknown-linux-musleabi) + sudo apt install -y gcc-arm-linux-gnueabi g++-arm-linux-gnueabi binutils-arm-linux-gnueabi libc6-dev-armel-cross + install_llvm + export CC=clang-$llvm_version + export CXX=clang++-$llvm_version + export LDFLAGS="-fuse-ld=lld" + export CMAKE_TOOLCHAIN_FILE=$ROOT_DIR/scripts/cmake/arm-musl.cmake + ;; + arm-unknown-linux-musleabihf) + sudo apt install -y gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf binutils-arm-linux-gnueabihf libc6-dev-armhf-cross + install_llvm + export CC=clang-$llvm_version + export CXX=clang++-$llvm_version + export LDFLAGS="-fuse-ld=lld" + export CMAKE_TOOLCHAIN_FILE=$ROOT_DIR/scripts/cmake/armhf-musl.cmake + ;; + armv7-unknown-linux-musleabi) + sudo apt install -y gcc-arm-linux-gnueabi g++-arm-linux-gnueabi binutils-arm-linux-gnueabi libc6-dev-armel-cross + install_llvm + export CC=clang-$llvm_version + export CXX=clang++-$llvm_version + export LDFLAGS="-fuse-ld=lld" + export CMAKE_TOOLCHAIN_FILE=$ROOT_DIR/scripts/cmake/armv7-musl.cmake + ;; + armv7-unknown-linux-musleabihf) + sudo apt install -y gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf binutils-arm-linux-gnueabihf libc6-dev-armhf-cross + install_llvm + export CC=clang-$llvm_version + export CXX=clang++-$llvm_version + export LDFLAGS="-fuse-ld=lld" + export CMAKE_TOOLCHAIN_FILE=$ROOT_DIR/scripts/cmake/armv7hf-musl.cmake + ;; + i686-unknown-linux-gnu) + sudo apt install -y libc6-dev-i386 + ;; + mips-unknown-linux-gnu) + sudo apt install -y gcc-mips-linux-gnu g++-mips-linux-gnu binutils-mips-linux-gnu libc6-dev-mips-cross + export CC=mips-linux-gnu-gcc + export CXX=mips-linux-gnu-g++ + ;; + mipsel-unknown-linux-gnu) + sudo apt install -y gcc-mipsel-linux-gnu g++-mipsel-linux-gnu binutils-mipsel-linux-gnu libc6-dev-mipsel-cross + export CC=mipsel-linux-gnu-gcc + export CXX=mipsel-linux-gnu-g++ + ;; + mips64-unknown-linux-gnuabi64) + sudo apt install -y gcc-mips64-linux-gnuabi64 g++-mips64-linux-gnuabi64 binutils-mips64-linux-gnuabi64 libc6-dev-mips64-cross + export CC=mips64-linux-gnuabi64-gcc + export CXX=mips64-linux-gnuabi64-g++ + ;; + mips64el-unknown-linux-gnuabi64) + sudo apt install -y gcc-mips64el-linux-gnuabi64 g++-mips64el-linux-gnuabi64 binutils-mips64el-linux-gnuabi64 libc6-dev-mips64el-cross + export CC=mips64el-linux-gnuabi64-gcc + export CXX=mips64el-linux-gnuabi64-g++ + ;; + powerpc-unknown-linux-gnu) + sudo apt install -y gcc-powerpc-linux-gnu g++-powerpc-linux-gnu binutils-powerpc-linux-gnu libc6-dev-powerpc-cross + export CC=powerpc-linux-gnu-gcc + export CXX=powerpc-linux-gnu-g++ + ;; + powerpc64-unknown-linux-gnu) + sudo apt install -y gcc-powerpc64-linux-gnu g++-powerpc64-linux-gnu binutils-powerpc64-linux-gnu libc6-dev-powerpc64-cross + export CC=powerpc64-linux-gnu-gcc + export CXX=powerpc64-linux-gnu-g++ + ;; + powerpc64le-unknown-linux-gnu) + sudo apt install -y gcc-powerpc64le-linux-gnu g++-powerpc64le-linux-gnu binutils-powerpc64le-linux-gnu libc6-dev-powerpc64le-cross + export CC=powerpc64le-linux-gnu-gcc + export CXX=powerpc64le-linux-gnu-g++ + ;; + s390x-unknown-linux-gnu) + sudo apt install -y gcc-s390x-linux-gnu g++-s390x-linux-gnu binutils-s390x-linux-gnu libc6-dev-s390x-cross + export CC=s390x-linux-gnu-gcc + export CXX=s390x-linux-gnu-g++ + ;; + riscv64gc-unknown-linux-gnu) + sudo apt install -y gcc-riscv64-linux-gnu g++-riscv64-linux-gnu binutils-riscv64-linux-gnu libc6-dev-riscv64-cross + export CC=riscv64-linux-gnu-gcc + export CXX=riscv64-linux-gnu-g++ + ;; esac + if [[ "$TARGET" == *"musl"* ]]; then + if [[ $2 == "true" ]]; then + export RUSTFLAGS="-Clinker=rust-lld -Clink-self-contained=yes -Ctarget-feature=+crt-static" + else + export RUSTFLAGS="-Clinker=rust-lld" + fi + else + if [[ $2 == "true" ]]; then + export RUSTFLAGS="-Clink-self-contained=yes -Ctarget-feature=+crt-static" + fi + fi + OUTPUT_BIN=./target/artifacts/clash-$TARGET + if [[ "$2" == "true" ]]; then + OUTPUT_BIN=$OUTPUT_BIN-static + fi + cargo build -p clash --target $TARGET --release ls -l ./target/$TARGET/release/ - mv ./target/$TARGET/release/clash ./target/artifacts/clash-$TARGET + mv ./target/$TARGET/release/clash $OUTPUT_BIN done