From d012a9091d38d4bc0f9457ad87d6da49c7132517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 6 Mar 2023 16:32:16 +0800 Subject: [PATCH] Update build configuration --- .github/workflows/debug.yml | 5 ++--- .goreleaser.yaml | 2 +- Dockerfile | 3 ++- Makefile | 7 +++--- box.go | 25 ++++++++++----------- cmd/internal/build/main.go | 3 ++- cmd/internal/{build => build_shared}/sdk.go | 15 +++++++++++-- cmd/internal/build_shared/tag.go | 16 +++++++++++++ cmd/internal/read_tag/main.go | 21 +++++++++++++++++ 9 files changed, 73 insertions(+), 24 deletions(-) rename cmd/internal/{build => build_shared}/sdk.go (88%) create mode 100644 cmd/internal/build_shared/tag.go create mode 100644 cmd/internal/read_tag/main.go diff --git a/.github/workflows/debug.yml b/.github/workflows/debug.yml index 495edddc..f6f6195f 100644 --- a/.github/workflows/debug.yml +++ b/.github/workflows/debug.yml @@ -60,7 +60,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v3 with: - go-version: 1.18.7 + go-version: 1.18.10 - name: Cache go module uses: actions/cache@v3 with: @@ -68,8 +68,7 @@ jobs: ~/go/pkg/mod key: go118-${{ hashFiles('**/go.sum') }} - name: Run Test - run: | - go test -v ./... + run: make cross: strategy: matrix: diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 57e2bff9..ca31165c 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -43,7 +43,7 @@ builds: gcflags: - all=-trimpath={{.Env.GOPATH}} ldflags: - - -s -w -buildid= + - -X "github.com/sagernet/sing-box/constant.Version={{ .Version }}" -s -w -buildid= tags: - with_gvisor - with_quic diff --git a/Dockerfile b/Dockerfile index 23cd8f92..0867a067 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,9 +8,10 @@ ENV CGO_ENABLED=0 RUN set -ex \ && apk add git build-base \ && export COMMIT=$(git rev-parse --short HEAD) \ + && export VERSION=$(go run ./cmd/internal/read_tag) \ && go build -v -trimpath -tags with_quic,with_wireguard,with_acme \ -o /go/bin/sing-box \ - -ldflags "-s -w -buildid=" \ + -ldflags "-X \"github.com/sagernet/sing-box/constant.Version=$VERSION\" -s -w -buildid=" \ ./cmd/sing-box FROM alpine AS dist LABEL maintainer="nekohasekai " diff --git a/Makefile b/Makefile index 19a53276..643bab99 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,9 @@ NAME = sing-box COMMIT = $(shell git rev-parse --short HEAD) -TAGS ?= with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api -TAGS_TEST ?= with_gvisor,with_quic,with_wireguard,with_grpc,with_ech,with_utls,with_shadowsocksr -PARAMS = -v -trimpath -tags "$(TAGS)" -ldflags "-s -w -buildid=" +TAGS ?= with_gvisor,with_quic,with_wireguard,with_utls,with_reality_server,with_clash_api +TAGS_TEST ?= with_gvisor,with_quic,with_wireguard,with_grpc,with_ech,with_utls,with_reality_server,with_shadowsocksr +VERSION=$(shell go run ./cmd/internal/read_tag) +PARAMS = -v -trimpath -tags "$(TAGS)" -ldflags "-X \"github.com/sagernet/sing-box/constant.Version=$(VERSION)\" -s -w -buildid=" MAIN = ./cmd/sing-box .PHONY: test release diff --git a/box.go b/box.go index f70e5de7..01bb7701 100644 --- a/box.go +++ b/box.go @@ -202,18 +202,6 @@ func (s *Box) Start() error { } func (s *Box) start() error { - if s.clashServer != nil { - err := s.clashServer.Start() - if err != nil { - return E.Cause(err, "start clash api server") - } - } - if s.v2rayServer != nil { - err := s.v2rayServer.Start() - if err != nil { - return E.Cause(err, "start v2ray api server") - } - } for i, out := range s.outbounds { if starter, isStarter := out.(common.Starter); isStarter { err := starter.Start() @@ -244,7 +232,18 @@ func (s *Box) start() error { return E.Cause(err, "initialize inbound/", in.Type(), "[", tag, "]") } } - + if s.clashServer != nil { + err = s.clashServer.Start() + if err != nil { + return E.Cause(err, "start clash api server") + } + } + if s.v2rayServer != nil { + err = s.v2rayServer.Start() + if err != nil { + return E.Cause(err, "start v2ray api server") + } + } s.logger.Info("sing-box started (", F.Seconds(time.Since(s.createdAt).Seconds()), "s)") return nil } diff --git a/cmd/internal/build/main.go b/cmd/internal/build/main.go index b6adaf63..0bd11f98 100644 --- a/cmd/internal/build/main.go +++ b/cmd/internal/build/main.go @@ -4,11 +4,12 @@ import ( "os" "os/exec" + "github.com/sagernet/sing-box/cmd/internal/build_shared" "github.com/sagernet/sing-box/log" ) func main() { - findSDK() + build_shared.FindSDK() command := exec.Command(os.Args[1], os.Args[2:]...) command.Stdout = os.Stdout diff --git a/cmd/internal/build/sdk.go b/cmd/internal/build_shared/sdk.go similarity index 88% rename from cmd/internal/build/sdk.go rename to cmd/internal/build_shared/sdk.go index e5468c98..35607196 100644 --- a/cmd/internal/build/sdk.go +++ b/cmd/internal/build_shared/sdk.go @@ -1,6 +1,7 @@ -package main +package build_shared import ( + "go/build" "os" "path/filepath" "runtime" @@ -18,7 +19,7 @@ var ( androidNDKPath string ) -func findSDK() { +func FindSDK() { searchPath := []string{ "$ANDROID_HOME", "$HOME/Android/Sdk", @@ -79,3 +80,13 @@ func findNDK() bool { } return false } + +var GoBinPath string + +func FindMobile() { + goBin := filepath.Join(build.Default.GOPATH, "bin") + if !rw.FileExists(goBin + "/" + "gobind") { + log.Fatal("missing gomobile installation") + } + GoBinPath = goBin +} diff --git a/cmd/internal/build_shared/tag.go b/cmd/internal/build_shared/tag.go new file mode 100644 index 00000000..5fb21453 --- /dev/null +++ b/cmd/internal/build_shared/tag.go @@ -0,0 +1,16 @@ +package build_shared + +import "github.com/sagernet/sing/common" + +func ReadTag() (string, error) { + currentTag, err := common.Exec("git", "describe", "--tags").Read() + if err != nil { + return currentTag, err + } + currentTagRev, _ := common.Exec("git", "describe", "--tags", "--abbrev=0").ReadOutput() + if currentTagRev == currentTag { + return currentTag[1:], nil + } + shortCommit, _ := common.Exec("git", "rev-parse", "--short", "HEAD").ReadOutput() + return currentTagRev[1:] + "-" + shortCommit, nil +} diff --git a/cmd/internal/read_tag/main.go b/cmd/internal/read_tag/main.go new file mode 100644 index 00000000..d319f771 --- /dev/null +++ b/cmd/internal/read_tag/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "os" + + "github.com/sagernet/sing-box/cmd/internal/build_shared" + "github.com/sagernet/sing-box/log" +) + +func main() { + currentTag, err := build_shared.ReadTag() + if err != nil { + log.Error(err) + _, err = os.Stdout.WriteString("unknown\n") + } else { + _, err = os.Stdout.WriteString(currentTag + "\n") + } + if err != nil { + log.Error(err) + } +}