Skip to content

Commit

Permalink
Pass build info using ldflags
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles-Antoine Mathieu committed Sep 28, 2020
1 parent afe2a8c commit 046f403
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 127 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ documentation
release
releases
server/plikd
server/common/version.go
testing
webapp/node_modules
webapp/bower_components
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ webapp/dist
clients
client/client
servers
server/common/version.go
release
releases
debs
Expand Down
34 changes: 17 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ GOHOSTARCH=$(shell go env GOHOSTARCH)
DEBROOT_SERVER=debs/server
DEBROOT_CLIENT=debs/client

race_detector = GORACE="halt_on_error=1" go build -race
BUILD_INFO=$(shell server/gen_build_info.sh $(RELEASE_VERSION) base64)
BUILD_FLAG=-ldflags="-X github.com/root-gg/plik/server/common.buildInfoString=$(BUILD_INFO)"

GO_BUILD=go build $(BUILD_FLAG)
GO_TEST=GORACE="halt_on_error=1" go test $(BUILD_FLAG) -race -cover -p 1

ifdef ENABLE_RACE_DETECTOR
build = $(race_detector)
else
build = go build
GO_BUILD:=GORACE="halt_on_error=1" $(GO_BUILD) -race
endif
test: build = $(race_detector)

all: clean clean-frontend frontend clients server

Expand All @@ -34,15 +36,15 @@ frontend:
# Build plik server for the current architecture
###
server:
@server/gen_build_info.sh $(RELEASE_VERSION)
@server/gen_build_info.sh $(RELEASE_VERSION) info
@echo "Building Plik server"
@cd server && $(build) -o plikd ./
@cd server && $(GO_BUILD) -o plikd

###
# Build plik server for all architectures
###
servers: frontend
@server/gen_build_info.sh $(RELEASE_VERSION)
@server/gen_build_info.sh $(RELEASE_VERSION) info
@cd server && for target in $(RELEASE_TARGETS) ; do \
SERVER_DIR=../servers/$$target; \
SERVER_PATH=$$SERVER_DIR/plikd; \
Expand All @@ -52,23 +54,23 @@ servers: frontend
if [ $$GOOS = "windows" ] ; then SERVER_PATH=$$SERVER_DIR/plikd.exe ; fi ; \
if [ -e $$SERVER_PATH ] ; then continue ; fi ; \
echo "Building Plik server for $$target to $$SERVER_PATH"; \
$(build) -o $$SERVER_PATH ; \
$(GO_BUILD) -o $$SERVER_PATH ; \
done

###
# Build plik client for the current architecture
###
client:
@server/gen_build_info.sh $(RELEASE_VERSION)
@server/gen_build_info.sh $(RELEASE_VERSION) info
@echo "Building Plik client"
@cd client && $(build) -o plik ./
@cd client && $(GO_BUILD) -o plik ./


###
# Build plik client for all architectures
###
clients:
@server/gen_build_info.sh $(RELEASE_VERSION)
@server/gen_build_info.sh $(RELEASE_VERSION) info
@cd client && for target in $(RELEASE_TARGETS) ; do \
CLIENT_DIR=../clients/$$target; \
CLIENT_PATH=$$CLIENT_DIR/plik; \
Expand All @@ -79,7 +81,7 @@ clients:
if [ $$GOOS = "windows" ] ; then CLIENT_PATH=$$CLIENT_DIR/plik.exe ; fi ; \
if [ -e $$CLIENT_PATH ] ; then continue ; fi ; \
echo "Building Plik client for $$target to $$CLIENT_PATH"; \
$(build) -o $$CLIENT_PATH ; \
$(GO_BUILD) -o $$CLIENT_PATH ; \
md5sum $$CLIENT_PATH | awk '{print $$1}' > $$CLIENT_MD5; \
done
@mkdir -p clients/bash && cp client/plik.sh clients/bash
Expand Down Expand Up @@ -185,7 +187,7 @@ releases: release-template servers
# Generate build info
###
build-info:
@server/gen_build_info.sh $(RELEASE_VERSION)
@server/gen_build_info.sh $(RELEASE_VERSION) info

###
# Run linters
Expand All @@ -210,8 +212,7 @@ fmt:
###
test:
@if curl -s 127.0.0.1:8080 > /dev/null ; then echo "Plik server probably already running" && exit 1 ; fi
@server/gen_build_info.sh $(RELEASE_VERSION)
@GORACE="halt_on_error=1" go test -race -cover -p 1 ./... 2>&1 | grep -v "no test files"; test $${PIPESTATUS[0]} -eq 0
@$(GO_TEST) ./... 2>&1 | grep -v "no test files"; test $${PIPESTATUS[0]} -eq 0
@echo "cli client integration tests :" && cd client && ./test.sh

###
Expand All @@ -230,7 +231,6 @@ docker:
# Remove server build files
###
clean:
@rm -rf server/common/version.go
@rm -rf server/plikd
@rm -rf client/plik
@rm -rf clients
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ To compile plik from sources, you'll need golang and npm installed on your syste
First, get the project and libs via go get :
```sh
$ go get github.com/root-gg/plik/server
go/src/github.com/root-gg/plik/server/handlers/misc.go:51: undefined: common.GetBuildInfo <== ignore this warning
$ cd $GOPATH/src/github.com/root-gg/plik/
```

Expand Down
94 changes: 94 additions & 0 deletions server/common/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package common

//
// This file is generated automatically by gen_build_info.sh
//

import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"time"
)

var buildInfoString string
var buildInfo *BuildInfo

func init() {
buildInfo = &BuildInfo{}

if buildInfoString != "" {
jsonString, err := base64.StdEncoding.DecodeString(buildInfoString)
if err != nil {
panic(fmt.Errorf("Unable to parse build info base64 string : %s", err))
}

err = json.Unmarshal(jsonString, buildInfo)
if err != nil {
panic(fmt.Errorf("Unable to parse build info json string : %s", err))
}
}
}

// BuildInfo export build related variables
type BuildInfo struct {
Version string `json:"version"`
Date int64 `json:"date"`

User string `json:"user"`
Host string `json:"host"`

GitShortRevision string `json:"gitShortRevision"`
GitFullRevision string `json:"gitFullRevision"`

IsRelease bool `json:"isRelease"`
IsMint bool `json:"isMint"`

GoVersion string `json:"goVersion"`

Clients []*Client `json:"clients"`
Releases []*Release `json:"releases"`
}

// Client export client build related variables
type Client struct {
Name string `json:"name"`
Md5 string `json:"md5"`
Path string `json:"path"`
OS string `json:"os"`
ARCH string `json:"arch"`
}

// Release export releases related variables
type Release struct {
Name string `json:"name"`
Date int64 `json:"date"`
}

// GetBuildInfo get build info
func GetBuildInfo() *BuildInfo {
return buildInfo
}

func (bi *BuildInfo) String() string {

v := fmt.Sprintf("v%s (built from git rev %s", bi.Version, bi.GitShortRevision)

// Compute flags
var flags []string
if buildInfo.IsMint {
flags = append(flags, "mint")
}
if buildInfo.IsRelease {
flags = append(flags, "release")
}

if len(flags) > 0 {
v += fmt.Sprintf(" [%s]", strings.Join(flags, ","))
}

v += fmt.Sprintf(" at %s with %s)", time.Unix(bi.Date, 0), bi.GoVersion)

return v
}
Loading

0 comments on commit 046f403

Please sign in to comment.