Skip to content

Commit

Permalink
build static link crt binary (#192)
Browse files Browse the repository at this point in the history
* musl

* some progress

* install musl tools

* install

* static crt

* musl and static

* ver
  • Loading branch information
ibigbug authored Nov 28, 2023
1 parent 68bec17 commit 40366d7
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 12 deletions.
1 change: 0 additions & 1 deletion .bazelversion

This file was deleted.

6 changes: 6 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -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"
14 changes: 12 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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},
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion clash_lib/src/proxy/shadowsocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
132 changes: 128 additions & 4 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 40366d7

Please sign in to comment.