feat: embed build version info in binaries

This commit is contained in:
2026-03-02 19:16:53 +08:00
parent 489c2d191c
commit 676a6e659a
4 changed files with 36 additions and 4 deletions

24
Makefile Normal file
View File

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

View File

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

View File

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

View File

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