From 80ed5bf8fb48d3e232f7ed2d20ea46959c9dc0f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 11 Dec 2022 14:38:01 +0800 Subject: [PATCH] Fix android package --- .goreleaser.yaml | 54 +++++++++++++++++++++++-- Makefile | 4 +- cmd/internal/build/main.go | 20 ++++++++++ cmd/internal/build/sdk.go | 80 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 cmd/internal/build/main.go create mode 100644 cmd/internal/build/sdk.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 70e0dc46..547854d0 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,6 +1,7 @@ project_name: sing-box builds: - - main: ./cmd/sing-box + - id: main + main: ./cmd/sing-box flags: - -v - -trimpath @@ -19,9 +20,6 @@ builds: env: - CGO_ENABLED=0 targets: - - android_arm64 - - android_amd64 - - android_amd64_v3 - linux_amd64_v1 - linux_amd64_v3 - linux_arm64 @@ -35,6 +33,54 @@ builds: - darwin_amd64_v3 - darwin_arm64 mod_timestamp: '{{ .CommitTimestamp }}' + - id: android + main: ./cmd/sing-box + flags: + - -v + - -trimpath + asmflags: + - all=-trimpath={{.Env.GOPATH}} + gcflags: + - all=-trimpath={{.Env.GOPATH}} + ldflags: + - -s -w -buildid= + tags: + - with_gvisor + - with_quic + - with_wireguard + - with_utls + - with_clash_api + env: + - CGO_ENABLED=1 + overrides: + - goos: android + goarch: arm + goarm: 7 + env: + - CC=armv7a-linux-androideabi19-clang + - CXX=armv7a-linux-androideabi19-clang++ + - goos: android + goarch: arm64 + env: + - CC=aarch64-linux-android21-clang + - CXX=aarch64-linux-android21-clang++ + - goos: android + goarch: 386 + env: + - CC=i686-linux-android21-clang + - CXX=i686-linux-android21-clang++ + - goos: android + goarch: amd64 + goamd64: v1 + env: + - CC=x86_64-linux-android21-clang + - CXX=x86_64-linux-android21-clang++ + targets: + - android_arm_7 + - android_arm64 + - android_386 + - android_amd64 + mod_timestamp: '{{ .CommitTimestamp }}' snapshot: name_template: "{{ .Version }}.{{ .ShortCommit }}" archives: diff --git a/Makefile b/Makefile index e98c8a65..01d63781 100644 --- a/Makefile +++ b/Makefile @@ -42,14 +42,14 @@ proto_install: go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest snapshot: - goreleaser release --rm-dist --snapshot + go run ./cmd/internal/build goreleaser release --rm-dist --snapshot || exit 1 mkdir dist/release mv dist/*.tar.gz dist/*.zip dist/*.deb dist/*.rpm dist/release ghr --delete --draft --prerelease -p 1 nightly dist/release rm -r dist release: - goreleaser release --rm-dist --skip-publish + go run ./cmd/internal/build goreleaser release --rm-dist --skip-publish || exit 1 mkdir dist/release mv dist/*.tar.gz dist/*.zip dist/*.deb dist/*.rpm dist/release ghr --delete --draft --prerelease -p 3 $(shell git describe --tags) dist/release diff --git a/cmd/internal/build/main.go b/cmd/internal/build/main.go new file mode 100644 index 00000000..b6adaf63 --- /dev/null +++ b/cmd/internal/build/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "os" + "os/exec" + + "github.com/sagernet/sing-box/log" +) + +func main() { + findSDK() + + command := exec.Command(os.Args[1], os.Args[2:]...) + command.Stdout = os.Stdout + command.Stderr = os.Stderr + err := command.Run() + if err != nil { + log.Fatal(err) + } +} diff --git a/cmd/internal/build/sdk.go b/cmd/internal/build/sdk.go new file mode 100644 index 00000000..aaa5cb8a --- /dev/null +++ b/cmd/internal/build/sdk.go @@ -0,0 +1,80 @@ +package main + +import ( + "os" + "path/filepath" + "sort" + "strconv" + "strings" + + "github.com/sagernet/sing-box/log" + "github.com/sagernet/sing/common" + "github.com/sagernet/sing/common/rw" +) + +var ( + androidSDKPath string + androidNDKPath string +) + +func findSDK() { + searchPath := []string{ + "$ANDROID_HOME", + "$HOME/Android/Sdk", + "$HOME/.local/lib/android/sdk", + "$HOME/Library/Android/sdk", + } + for _, path := range searchPath { + path = os.ExpandEnv(path) + if rw.FileExists(path + "/licenses/android-sdk-license") { + androidSDKPath = path + break + } + } + if androidSDKPath == "" { + log.Fatal("android SDK not found") + } + if !findNDK() { + log.Fatal("android NDK not found") + } + + os.Setenv("ANDROID_HOME", androidSDKPath) + os.Setenv("ANDROID_SDK_HOME", androidSDKPath) + os.Setenv("ANDROID_NDK_HOME", androidNDKPath) + os.Setenv("NDK", androidNDKPath) + os.Setenv("PATH", os.Getenv("PATH")+":"+filepath.Join(androidNDKPath, "toolchains", "llvm", "prebuilt", "linux-x86_64", "bin")) +} + +func findNDK() bool { + if rw.FileExists(androidSDKPath + "/ndk/25.1.8937393") { + androidNDKPath = androidSDKPath + "/ndk/25.1.8937393" + return true + } + ndkVersions, err := os.ReadDir(androidSDKPath + "/ndk") + if err != nil { + return false + } + versionNames := common.Map(ndkVersions, os.DirEntry.Name) + if len(versionNames) == 0 { + return false + } + sort.Slice(versionNames, func(i, j int) bool { + iVersions := strings.Split(versionNames[i], ".") + jVersions := strings.Split(versionNames[j], ".") + for k := 0; k < len(iVersions) && k < len(jVersions); k++ { + iVersion, _ := strconv.Atoi(iVersions[k]) + jVersion, _ := strconv.Atoi(jVersions[k]) + if iVersion != jVersion { + return iVersion > jVersion + } + } + return true + }) + for _, versionName := range versionNames { + if rw.FileExists(androidSDKPath + "/ndk/" + versionName) { + androidNDKPath = androidSDKPath + "/ndk/" + versionName + return true + } + } + return false +}