From 60740f627958aea71910f361f5ace8730859bac4 Mon Sep 17 00:00:00 2001 From: AlteredCoder <64792091+AlteredCoder@users.noreply.github.com> Date: Mon, 26 Apr 2021 14:45:52 +0200 Subject: [PATCH] fix makefile for static binaries (#764) * Fix LD_OPTS for static build Co-authored-by: AlteredCoder Co-authored-by: Sebastien Blot --- Makefile | 9 +++++++++ cmd/crowdsec-cli/Makefile | 2 +- cmd/crowdsec-cli/utils.go | 2 ++ cmd/crowdsec/Makefile | 2 +- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 0d6ebed5f..e16ebd5c7 100644 --- a/Makefile +++ b/Makefile @@ -33,12 +33,21 @@ BUILD_GOVERSION="$(shell go version | cut -d " " -f3 | sed -r 's/[go]+//g')" BUILD_CODENAME=$(shell cat RELEASE.json | jq -r .CodeName) BUILD_TIMESTAMP=$(shell date +%F"_"%T) BUILD_TAG="$(shell git rev-parse HEAD)" + export LD_OPTS=-ldflags "-s -w -X github.com/crowdsecurity/crowdsec/pkg/cwversion.Version=$(BUILD_VERSION) \ -X github.com/crowdsecurity/crowdsec/pkg/cwversion.System=$(SYSTEM) \ -X github.com/crowdsecurity/crowdsec/pkg/cwversion.BuildDate=$(BUILD_TIMESTAMP) \ -X github.com/crowdsecurity/crowdsec/pkg/cwversion.Codename=$(BUILD_CODENAME) \ -X github.com/crowdsecurity/crowdsec/pkg/cwversion.Tag=$(BUILD_TAG) \ -X github.com/crowdsecurity/crowdsec/pkg/cwversion.GoVersion=$(BUILD_GOVERSION)" + +export LD_OPTS_STATIC=-ldflags "-s -w -X github.com/crowdsecurity/crowdsec/pkg/cwversion.Version=$(BUILD_VERSION) \ +-X github.com/crowdsecurity/crowdsec/pkg/cwversion.BuildDate=$(BUILD_TIMESTAMP) \ +-X github.com/crowdsecurity/crowdsec/pkg/cwversion.Codename=$(BUILD_CODENAME) \ +-X github.com/crowdsecurity/crowdsec/pkg/cwversion.Tag=$(BUILD_TAG) \ +-X github.com/crowdsecurity/crowdsec/pkg/cwversion.GoVersion=$(BUILD_GOVERSION) \ +-extldflags '-static'" + RELDIR = crowdsec-$(BUILD_VERSION) all: clean test build diff --git a/cmd/crowdsec-cli/Makefile b/cmd/crowdsec-cli/Makefile index 442aa2a42..6ec33b99a 100644 --- a/cmd/crowdsec-cli/Makefile +++ b/cmd/crowdsec-cli/Makefile @@ -14,7 +14,7 @@ build: clean @$(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) -v static: clean - @$(GOBUILD) -o $(BINARY_NAME) -v -a -tags netgo -ldflags '-w -extldflags "-static"' + @$(GOBUILD) $(LD_OPTS_STATIC) -o $(BINARY_NAME) -v -a -tags netgo install: install-conf install-bin diff --git a/cmd/crowdsec-cli/utils.go b/cmd/crowdsec-cli/utils.go index a946af0a7..81cd2168b 100644 --- a/cmd/crowdsec-cli/utils.go +++ b/cmd/crowdsec-cli/utils.go @@ -83,6 +83,8 @@ func setHubBranch() error { } else if semver.Compare(csVersion, latest) == 1 { // if current version is greater than the latest we are in pre-release log.Debugf("Your current crowdsec version seems to be a pre-release (%s)", csVersion) cwhub.HubBranch = "master" + } else if csVersion == "" { + log.Warningf("Crowdsec version is '', using master branch for the hub") } else { log.Warnf("Crowdsec is not the latest version. Current version is '%s' and the latest stable version is '%s'. Please update it!", csVersion, latest) log.Warnf("As a result, you will not be able to use parsers/scenarios/collections added to Crowdsec Hub after CrowdSec %s", latest) diff --git a/cmd/crowdsec/Makefile b/cmd/crowdsec/Makefile index 724e41f60..9980b9004 100644 --- a/cmd/crowdsec/Makefile +++ b/cmd/crowdsec/Makefile @@ -20,7 +20,7 @@ build: clean $(GOBUILD) $(LD_OPTS) -o $(CROWDSEC_BIN) -v static: clean - $(GOBUILD) -o $(CROWDSEC_BIN) -v -a -tags netgo -ldflags '-w -extldflags "-static"' + $(GOBUILD) $(LD_OPTS_STATIC) -o $(CROWDSEC_BIN) -v -a -tags netgo test: $(GOTEST) -v ./...