Generate version during compilation

This commit is contained in:
世界 2023-03-09 16:53:25 +08:00
parent 325f6c71ff
commit 1c8a9e91b7
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
10 changed files with 83 additions and 33 deletions

View file

@ -10,7 +10,7 @@ builds:
gcflags: gcflags:
- all=-trimpath={{.Env.GOPATH}} - all=-trimpath={{.Env.GOPATH}}
ldflags: ldflags:
- -s -w -buildid= - -X github.com/sagernet/sing-box/constant.Version={{ .Version }} -s -w -buildid=
tags: tags:
- with_gvisor - with_gvisor
- with_quic - with_quic
@ -43,7 +43,7 @@ builds:
gcflags: gcflags:
- all=-trimpath={{.Env.GOPATH}} - all=-trimpath={{.Env.GOPATH}}
ldflags: ldflags:
- -s -w -buildid= - -X github.com/sagernet/sing-box/constant.Version={{ .Version }} -s -w -buildid=
tags: tags:
- with_gvisor - with_gvisor
- with_quic - with_quic

View file

@ -8,9 +8,10 @@ ENV CGO_ENABLED=0
RUN set -ex \ RUN set -ex \
&& apk add git build-base \ && apk add git build-base \
&& export COMMIT=$(git rev-parse --short HEAD) \ && 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 \ && go build -v -trimpath -tags with_quic,with_wireguard,with_acme \
-o /go/bin/sing-box \ -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 ./cmd/sing-box
FROM alpine AS dist FROM alpine AS dist
LABEL maintainer="nekohasekai <contact-git@sekai.icu>" LABEL maintainer="nekohasekai <contact-git@sekai.icu>"

View file

@ -2,7 +2,8 @@ NAME = sing-box
COMMIT = $(shell git rev-parse --short HEAD) COMMIT = $(shell git rev-parse --short HEAD)
TAGS ?= with_gvisor,with_quic,with_wireguard,with_utls,with_reality_server,with_clash_api 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 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 MAIN = ./cmd/sing-box
.PHONY: test release .PHONY: test release

View file

@ -3,32 +3,18 @@ package main
import ( import (
"os" "os"
"os/exec" "os/exec"
"strings"
"github.com/sagernet/sing-box/cmd/internal/build_shared" "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-box/log"
"github.com/sagernet/sing/common"
) )
func main() { func main() {
build_shared.FindSDK() 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 := exec.Command(os.Args[1], os.Args[2:]...)
command.Stdout = os.Stdout command.Stdout = os.Stdout
command.Stderr = os.Stderr command.Stderr = os.Stderr
err = command.Run() err := command.Run()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View file

@ -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() { func buildAndroid() {
build_shared.FindSDK() build_shared.FindSDK()
@ -46,14 +63,17 @@ func buildAndroid() {
"-libname=box", "-libname=box",
} }
if !debugEnabled { if !debugEnabled {
args = append(args, args = append(args, sharedFlags...)
"-trimpath", "-ldflags=-s -w -buildid=",
"-tags", "with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api",
)
} else { } 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") args = append(args, "./experimental/libbox")
command := exec.Command(build_shared.GoBinPath+"/gomobile", args...) command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)
@ -84,14 +104,17 @@ func buildiOS() {
"-libname=box", "-libname=box",
} }
if !debugEnabled { if !debugEnabled {
args = append( args = append(args, sharedFlags...)
args, "-trimpath", "-ldflags=-s -w -buildid=",
"-tags", "with_gvisor,with_utls,with_clash_api,with_conntrack",
)
} else { } 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") args = append(args, "./experimental/libbox")
command := exec.Command(build_shared.GoBinPath+"/gomobile", args...) command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)

View file

@ -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
}

View file

@ -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)
}
}

View file

@ -1,3 +1,3 @@
package constant package constant
var Version = "1.2-beta7" var Version = "unknown"

2
go.mod
View file

@ -26,7 +26,7 @@ require (
github.com/sagernet/gomobile v0.0.0-20221130124640-349ebaa752ca github.com/sagernet/gomobile v0.0.0-20221130124640-349ebaa752ca
github.com/sagernet/quic-go v0.0.0-20230202071646-a8c8afb18b32 github.com/sagernet/quic-go v0.0.0-20230202071646-a8c8afb18b32
github.com/sagernet/reality v0.0.0-20230309024642-952cb58391a0 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-dns v0.1.4
github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9 github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9
github.com/sagernet/sing-shadowtls v0.1.0 github.com/sagernet/sing-shadowtls v0.1.0

4
go.sum
View file

@ -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/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-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.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.20230309082535-3ccf42b7d589 h1:McKwXuB22ibRW+o0uP42xcJEPiVZxapOd9BgljcJhUw=
github.com/sagernet/sing v0.1.8-0.20230307054559-0560a4da412b/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk= 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 h1:7VxgeoSCiiazDSaXXQVcvrTBxFpOePPq/4XdgnUDN+0=
github.com/sagernet/sing-dns v0.1.4/go.mod h1:1+6pCa48B1AI78lD+/i/dLgpw4MwfnsSpZo0Ds8wzzk= 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= github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9 h1:qS39eA4C7x+zhEkySbASrtmb6ebdy5v0y2M6mgkmSO0=