From 1c8a9e91b7e0f81024735d76dde60468d1393e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 9 Mar 2023 16:53:25 +0800 Subject: [PATCH] Generate version during compilation --- .goreleaser.yaml | 4 +-- Dockerfile | 3 ++- Makefile | 3 ++- cmd/internal/build/main.go | 16 +----------- cmd/internal/build_libbox/main.go | 43 ++++++++++++++++++++++++------- cmd/internal/build_shared/tag.go | 18 +++++++++++++ cmd/internal/read_tag/main.go | 21 +++++++++++++++ constant/version.go | 2 +- go.mod | 2 +- go.sum | 4 +-- 10 files changed, 83 insertions(+), 33 deletions(-) create mode 100644 cmd/internal/build_shared/tag.go create mode 100644 cmd/internal/read_tag/main.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 57e2bff9..8904966e 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -10,7 +10,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 @@ -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 f546cb54..5873e232 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,8 @@ NAME = sing-box COMMIT = $(shell git rev-parse --short HEAD) 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 -PARAMS = -v -trimpath -tags "$(TAGS)" -ldflags "-s -w -buildid=" +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/cmd/internal/build/main.go b/cmd/internal/build/main.go index 47da4b1d..0bd11f98 100644 --- a/cmd/internal/build/main.go +++ b/cmd/internal/build/main.go @@ -3,32 +3,18 @@ package main import ( "os" "os/exec" - "strings" "github.com/sagernet/sing-box/cmd/internal/build_shared" - C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" - "github.com/sagernet/sing/common" ) func main() { build_shared.FindSDK() - currentTag, err := common.Exec("git", "describe", "--tags", "--abbrev=0").Read() - if err != nil { - log.Fatal(err) - } - - currentTag = strings.TrimSpace(currentTag) - - if "v"+C.Version != currentTag { - log.Fatal("version mismatch, update constant.Version (", C.Version, ")", " to ", currentTag[1:]) - } - command := exec.Command(os.Args[1], os.Args[2:]...) command.Stdout = os.Stdout command.Stderr = os.Stderr - err = command.Run() + err := command.Run() if err != nil { log.Fatal(err) } diff --git a/cmd/internal/build_libbox/main.go b/cmd/internal/build_libbox/main.go index 4a1ee074..700b7e48 100644 --- a/cmd/internal/build_libbox/main.go +++ b/cmd/internal/build_libbox/main.go @@ -35,6 +35,23 @@ func main() { } } +var ( + sharedFlags []string + debugFlags []string +) + +func init() { + sharedFlags = append(sharedFlags, "-trimpath") + sharedFlags = append(sharedFlags, "-ldflags") + + currentTag, err := build_shared.ReadTag() + if err != nil { + currentTag = "unknown" + } + sharedFlags = append(sharedFlags, "-X github.com/sagernet/sing-box/constant.Version="+currentTag+" -s -w -buildid=") + debugFlags = append(debugFlags, "-X github.com/sagernet/sing-box/constant.Version="+currentTag) +} + func buildAndroid() { build_shared.FindSDK() @@ -46,14 +63,17 @@ func buildAndroid() { "-libname=box", } if !debugEnabled { - args = append(args, - "-trimpath", "-ldflags=-s -w -buildid=", - "-tags", "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api", - ) + args = append(args, sharedFlags...) } else { - args = append(args, "-tags", "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api,debug") + args = append(args, debugFlags...) } + args = append(args, "-tags") + if !debugEnabled { + args = append(args, "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api") + } else { + args = append(args, "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api,debug") + } args = append(args, "./experimental/libbox") command := exec.Command(build_shared.GoBinPath+"/gomobile", args...) @@ -84,14 +104,17 @@ func buildiOS() { "-libname=box", } if !debugEnabled { - args = append( - args, "-trimpath", "-ldflags=-s -w -buildid=", - "-tags", "with_gvisor,with_utls,with_clash_api,with_conntrack", - ) + args = append(args, sharedFlags...) } else { - args = append(args, "-tags", "with_gvisor,with_utls,with_clash_api,with_conntrack,debug") + args = append(args, debugFlags...) } + args = append(args, "-tags") + if !debugEnabled { + args = append(args, "with_gvisor,with_utls,with_clash_api,with_conntrack") + } else { + args = append(args, "with_gvisor,with_utls,with_clash_api,with_conntrack,debug") + } args = append(args, "./experimental/libbox") command := exec.Command(build_shared.GoBinPath+"/gomobile", args...) diff --git a/cmd/internal/build_shared/tag.go b/cmd/internal/build_shared/tag.go new file mode 100644 index 00000000..15dabb36 --- /dev/null +++ b/cmd/internal/build_shared/tag.go @@ -0,0 +1,18 @@ +package build_shared + +import ( + "github.com/sagernet/sing/common" +) + +func ReadTag() (string, error) { + currentTag, err := common.Exec("git", "describe", "--tags").ReadOutput() + 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) + } +} diff --git a/constant/version.go b/constant/version.go index 8461d0dd..5a816a73 100644 --- a/constant/version.go +++ b/constant/version.go @@ -1,3 +1,3 @@ package constant -var Version = "1.2-beta7" +var Version = "unknown" diff --git a/go.mod b/go.mod index 32bd04c1..b12483a6 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,7 @@ require ( github.com/sagernet/gomobile v0.0.0-20221130124640-349ebaa752ca github.com/sagernet/quic-go v0.0.0-20230202071646-a8c8afb18b32 github.com/sagernet/reality v0.0.0-20230309024642-952cb58391a0 - github.com/sagernet/sing v0.1.8-0.20230307054559-0560a4da412b + github.com/sagernet/sing v0.1.8-0.20230309082535-3ccf42b7d589 github.com/sagernet/sing-dns v0.1.4 github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9 github.com/sagernet/sing-shadowtls v0.1.0 diff --git a/go.sum b/go.sum index d56be614..e2069250 100644 --- a/go.sum +++ b/go.sum @@ -113,8 +113,8 @@ github.com/sagernet/reality v0.0.0-20230309024642-952cb58391a0 h1:ffgI5Jo3imRx3A github.com/sagernet/reality v0.0.0-20230309024642-952cb58391a0/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU= github.com/sagernet/sing v0.0.0-20220812082120-05f9836bff8f/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= -github.com/sagernet/sing v0.1.8-0.20230307054559-0560a4da412b h1:wxqf3O+cLHm1ZWEQG1DRwApwLlTV/NLKGqF1kNCk3Ms= -github.com/sagernet/sing v0.1.8-0.20230307054559-0560a4da412b/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk= +github.com/sagernet/sing v0.1.8-0.20230309082535-3ccf42b7d589 h1:McKwXuB22ibRW+o0uP42xcJEPiVZxapOd9BgljcJhUw= +github.com/sagernet/sing v0.1.8-0.20230309082535-3ccf42b7d589/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk= github.com/sagernet/sing-dns v0.1.4 h1:7VxgeoSCiiazDSaXXQVcvrTBxFpOePPq/4XdgnUDN+0= github.com/sagernet/sing-dns v0.1.4/go.mod h1:1+6pCa48B1AI78lD+/i/dLgpw4MwfnsSpZo0Ds8wzzk= github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9 h1:qS39eA4C7x+zhEkySbASrtmb6ebdy5v0y2M6mgkmSO0=