From 676a6e659aa920ec8051b51cad5958be4b80e6aa Mon Sep 17 00:00:00 2001 From: openclaw Date: Mon, 2 Mar 2026 19:16:53 +0800 Subject: [PATCH] feat: embed build version info in binaries --- Makefile | 24 ++++++++++++++++++++++++ cmd/inp2pc/main.go | 3 ++- cmd/inp2ps/main.go | 3 ++- pkg/config/config.go | 10 ++++++++-- 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5df4bc9 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +APP=inps + +VERSION ?= 0.1.0 +GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown) +BUILD_TIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ') +GO_VERSION := $(shell go env GOVERSION 2>/dev/null || echo unknown) + +LDFLAGS := -s -w \ + -X 'github.com/openp2p-cn/inp2p/pkg/config.Version=$(VERSION)' \ + -X 'github.com/openp2p-cn/inp2p/pkg/config.GitCommit=$(GIT_COMMIT)' \ + -X 'github.com/openp2p-cn/inp2p/pkg/config.BuildTime=$(BUILD_TIME)' \ + -X 'github.com/openp2p-cn/inp2p/pkg/config.GoVersion=$(GO_VERSION)' + +.PHONY: build build-client build-server + +build: build-client build-server + +build-client: + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \ + go build -ldflags "$(LDFLAGS)" -o bin/inp2pc ./cmd/inp2pc + +build-server: + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \ + go build -ldflags "$(LDFLAGS)" -o bin/inp2ps ./cmd/inp2ps diff --git a/cmd/inp2pc/main.go b/cmd/inp2pc/main.go index ca3d0bc..3174c15 100644 --- a/cmd/inp2pc/main.go +++ b/cmd/inp2pc/main.go @@ -41,7 +41,8 @@ func main() { flag.Parse() if *version { - fmt.Printf("inp2pc version %s\n", config.Version) + fmt.Printf("inp2pc version %s\ncommit: %s\nbuild: %s\ngo: %s\n", + config.Version, config.GitCommit, config.BuildTime, config.GoVersion) os.Exit(0) } diff --git a/cmd/inp2ps/main.go b/cmd/inp2ps/main.go index 396755f..c0cedfd 100644 --- a/cmd/inp2ps/main.go +++ b/cmd/inp2ps/main.go @@ -40,7 +40,8 @@ func main() { flag.Parse() if *version { - fmt.Printf("inp2ps version %s\n", config.Version) + fmt.Printf("inp2ps version %s\ncommit: %s\nbuild: %s\ngo: %s\n", + config.Version, config.GitCommit, config.BuildTime, config.GoVersion) os.Exit(0) } diff --git a/pkg/config/config.go b/pkg/config/config.go index bbd4ff2..09ad439 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -9,9 +9,15 @@ import ( "strconv" ) -const ( - Version = "0.1.0" +// Version info (set via -ldflags) +var ( + Version = "0.1.0" + GitCommit = "unknown" + BuildTime = "unknown" + GoVersion = "unknown" +) +const ( DefaultWSPort = 27183 // WSS signaling DefaultSTUNUDP1 = 27182 // UDP STUN port 1 DefaultSTUNUDP2 = 27183 // UDP STUN port 2