From cace12e8291be115f12847d4764f4ea2c5edaa01 Mon Sep 17 00:00:00 2001 From: HystericalDragon Date: Sun, 1 Dec 2024 14:05:26 +0800 Subject: [PATCH] build(libcore): use Makefile to build core Now you can build with `debug` tag. (Although sing-box pprof should be enabled by options) --- .github/workflows/nightly.yml | 2 +- .github/workflows/release.yml | 2 +- Makefile | 13 ++++++++---- buildScript/lib/core.sh | 4 ---- buildScript/lib/core/build.sh | 8 -------- buildScript/lib/core/init.sh | 8 -------- libcore/Makefile | 37 +++++++++++++++++++++++++++++++++++ libcore/build.sh | 35 --------------------------------- libcore/init.sh | 11 ----------- 9 files changed, 48 insertions(+), 72 deletions(-) delete mode 100755 buildScript/lib/core.sh delete mode 100755 buildScript/lib/core/build.sh delete mode 100755 buildScript/lib/core/init.sh create mode 100644 libcore/Makefile delete mode 100755 libcore/build.sh delete mode 100755 libcore/init.sh diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 7b616128..d40e5ea0 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -56,7 +56,7 @@ jobs: - name: Native Build if: steps.cache.outputs.cache-hit != 'true' run: | - ./run lib core + make libcore - name: Upload libcore uses: actions/upload-artifact@v4 with: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e17d840e..a98e8705 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -53,7 +53,7 @@ jobs: - name: Native Build if: steps.cache.outputs.cache-hit != 'true' run: | - ./run lib core + make libcore - name: Upload libcore uses: actions/upload-artifact@v4 with: diff --git a/Makefile b/Makefile index a8627ee0..a8a055dd 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,14 @@ -.PHONY: update libcore apk apk_debug assets lint_go test_go plugin generate_option +LIBCORE_DIR := ./libcore/ + +.PHONY: libcore libcore_debug apk apk_debug assets lint_go test_go plugin generate_option + +build: libcore assets apk libcore: - ./run lib core + $(MAKE) -C $(LIBCORE_DIR) all + +libcore_debug: + $(MAKE) -C $(LIBCORE_DIR) debug apk: BUILD_PLUGIN=none ./gradlew app:assembleFossRelease @@ -12,8 +19,6 @@ apk_debug: assets: ./run lib assets -build: libcore assets apk - update: ./run lib update diff --git a/buildScript/lib/core.sh b/buildScript/lib/core.sh deleted file mode 100755 index ee56e230..00000000 --- a/buildScript/lib/core.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -buildScript/lib/core/init.sh -buildScript/lib/core/build.sh \ No newline at end of file diff --git a/buildScript/lib/core/build.sh b/buildScript/lib/core/build.sh deleted file mode 100755 index 8e17d4be..00000000 --- a/buildScript/lib/core/build.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -source "buildScript/init/env.sh" -export CGO_ENABLED=1 -export GO386=softfloat - -cd libcore -rel=1 ./build.sh || exit 1 diff --git a/buildScript/lib/core/init.sh b/buildScript/lib/core/init.sh deleted file mode 100755 index 21abc759..00000000 --- a/buildScript/lib/core/init.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -source "buildScript/init/env.sh" - -[ -f libcore/go.mod ] || exit 1 -cd libcore - -./init.sh || exit 1 diff --git a/libcore/Makefile b/libcore/Makefile new file mode 100644 index 00000000..53382db2 --- /dev/null +++ b/libcore/Makefile @@ -0,0 +1,37 @@ +NDK_ENV := ../buildScript/init/env_ndk.sh +BOX_VERSION = $(shell go run ./cmd/boxversion/) +LIB_DIR = $(shell realpath ../app/libs) +TAGS := with_conntrack,with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api,with_ech + +# -buildvcs require: https://github.com/SagerNet/gomobile/commit/6bc27c2027e816ac1779bf80058b1a7710dad260 +# CGO_FLAGS require Go 1.23: https://go.dev/doc/go1.23#cgo +# max-page-size: https://developer.android.com/guide/practices/page-sizes +CGO_LDFLAGS := -Wl,-z,max-page-size=16384 +LDFLAGS := -X github.com/sagernet/sing-box/constant.Version=$(BOX_VERSION) -s -w -buildid= +ANDROID_API := 21 +INIT_DONE := inited + +.PHONY: all build copy + +all: $(INIT_DONE) build copy + +build: + . $(NDK_ENV) && \ + export CGO_LDFLAGS="$(CGO_LDFLAGS)" && export CGO_ENABLED=1 && export GO386=softfloat && \ + gomobile bind -v -androidapi $(ANDROID_API) -trimpath -buildvcs=false -ldflags="$(LDFLAGS)" -tags="$(TAGS)" . + + +$(INIT_DONE): + CGO_ENABLED=0 go install -v -trimpath -ldflags="-w -s" github.com/sagernet/gomobile/cmd/gobind@v0.1.4 + CGO_ENABLED=0 go install -v -trimpath -ldflags="-w -s" github.com/sagernet/gomobile/cmd/gomobile@v0.1.4 + gomobile init + touch $(INIT_DONE) + +debug: + $(MAKE) TAGS="$(TAGS),debug" all + +copy: + @rm -f libcore-sources.jar + @cp -f libcore.aar $(LIB_DIR) + echo ">> install $(realpath $(LIB_DIR))/libcore.aar" + sha256sum libcore.aar diff --git a/libcore/build.sh b/libcore/build.sh deleted file mode 100755 index 66b58960..00000000 --- a/libcore/build.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash - -source ../buildScript/init/env_ndk.sh - -set -e -# set -x - -box_version=$(go run ./cmd/boxversion/) - -TAGS=( - "with_conntrack" - "with_gvisor" - "with_quic" - "with_wireguard" - "with_utls" - "with_clash_api" - "with_ech" -) - -IFS="," BUILD_TAGS="${TAGS[*]}" - -# -buildvcs require: https://github.com/SagerNet/gomobile/commit/6bc27c2027e816ac1779bf80058b1a7710dad260 -# CGO_FLAGS require Go 1.23: https://go.dev/doc/go1.23#cgo -# max-page-size: https://developer.android.com/guide/practices/page-sizes -CGO_LDFLAGS="-Wl,-z,max-page-size=16384" gomobile bind -v -androidapi 21 -trimpath -buildvcs=false \ - -ldflags="-X github.com/sagernet/sing-box/constant.Version=${box_version} -s -w -buildid=" \ - -tags="$BUILD_TAGS" . || exit 1 - -rm -r libcore-sources.jar - -proj=../app/libs -mkdir -p $proj -cp -f libcore.aar $proj -echo ">> install $(realpath $proj)/libcore.aar" -sha256sum libcore.aar diff --git a/libcore/init.sh b/libcore/init.sh deleted file mode 100755 index 575d539e..00000000 --- a/libcore/init.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -set -e - -[[ -e "inited" ]] && exit 0 - -# Install gomobile -CGO_ENABLED=0 go install -v -trimpath -ldflags="-w -s" github.com/sagernet/gomobile/cmd/gobind@v0.1.4 -CGO_ENABLED=0 go install -v -trimpath -ldflags="-w -s" github.com/sagernet/gomobile/cmd/gomobile@v0.1.4 - -gomobile init -touch inited