diff --git a/Makefile b/Makefile index f0d4dc415..de7915678 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,4 @@ - -ifeq ($(OS), Windows_NT) - SHELL := pwsh.exe - .SHELLFLAGS := -NoProfile -Command - CS_ROOT = $(shell (Get-Location).Path) - SYSTEM = windows - EXT = .exe -else - CS_ROOT ?= $(shell pwd) - SYSTEM ?= $(shell uname -s | tr '[A-Z]' '[a-z]') -endif - -ifneq ("$(wildcard $(CURDIR)/platform/$(SYSTEM).mk)", "") - include $(CURDIR)/platform/$(SYSTEM).mk -else - include $(CURDIR)/platform/linux.mk -endif - -ifneq ($(OS), Windows_NT) - include $(CS_ROOT)/platform/unix_common.mk -endif +include mk/platform.mk CROWDSEC_FOLDER = ./cmd/crowdsec CSCLI_FOLDER = ./cmd/crowdsec-cli/ @@ -44,14 +24,6 @@ CROWDSEC_BIN = crowdsec$(EXT) CSCLI_BIN = cscli$(EXT) BUILD_CMD = build -MINIMUM_SUPPORTED_GO_MAJOR_VERSION = 1 -MINIMUM_SUPPORTED_GO_MINOR_VERSION = 20 - -go_major_minor = $(subst ., ,$(BUILD_GOVERSION)) -GO_MAJOR_VERSION = $(word 1, $(go_major_minor)) -GO_MINOR_VERSION = $(word 2, $(go_major_minor)) - -GO_VERSION_VALIDATION_ERR_MSG = Golang version ($(BUILD_GOVERSION)) is not supported, please use at least $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION).$(MINIMUM_SUPPORTED_GO_MINOR_VERSION) GO_MODULE_NAME = github.com/crowdsecurity/crowdsec LD_OPTS_VARS= \ @@ -84,23 +56,6 @@ all: clean test build .PHONY: plugins plugins: http-plugin slack-plugin splunk-plugin email-plugin dummy-plugin -.PHONY: goversion -goversion: -ifneq ($(OS), Windows_NT) - @if [ $(GO_MAJOR_VERSION) -gt $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION) ]; then \ - exit 0 ;\ - elif [ $(GO_MAJOR_VERSION) -lt $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION) ]; then \ - echo '$(GO_VERSION_VALIDATION_ERR_MSG)';\ - exit 1; \ - elif [ $(GO_MINOR_VERSION) -lt $(MINIMUM_SUPPORTED_GO_MINOR_VERSION) ] ; then \ - echo '$(GO_VERSION_VALIDATION_ERR_MSG)';\ - exit 1; \ - fi -else - # This needs Set-ExecutionPolicy -Scope CurrentUser Unrestricted - @$(CS_ROOT)/scripts/check_go_version.ps1 $(MINIMUM_SUPPORTED_GO_MAJOR_VERSION) $(MINIMUM_SUPPORTED_GO_MINOR_VERSION) -endif - .PHONY: clean clean: testclean @$(MAKE) -C $(CROWDSEC_FOLDER) clean --no-print-directory RM="$(RM)" WIN_IGNORE_ERR="$(WIN_IGNORE_ERR)" CP="$(CP)" CPR="$(CPR)" MKDIR="$(MKDIR)" @@ -223,3 +178,5 @@ bats-clean: else include test/bats.mk endif + +include mk/goversion.mk diff --git a/scripts/check_go_version.ps1 b/mk/check_go_version.ps1 similarity index 97% rename from scripts/check_go_version.ps1 rename to mk/check_go_version.ps1 index ddc68ce9b..6060cb227 100644 --- a/scripts/check_go_version.ps1 +++ b/mk/check_go_version.ps1 @@ -15,5 +15,5 @@ elseif ($goversion_major -lt $min_major) { } elseif ($goversion_minor -lt $min_minor) { Write-Output $(GO_VERSION_VALIDATION_ERR_MSG); - exit 1; -} \ No newline at end of file + exit 1; +} diff --git a/mk/goversion.mk b/mk/goversion.mk new file mode 100644 index 000000000..48979ac8e --- /dev/null +++ b/mk/goversion.mk @@ -0,0 +1,28 @@ + +BUILD_REQUIRE_GO_MAJOR ?= 1 +BUILD_REQUIRE_GO_MINOR ?= 20 + +BUILD_GOVERSION = $(subst go,,$(shell go env GOVERSION)) + +go_major_minor = $(subst ., ,$(BUILD_GOVERSION)) +GO_MAJOR_VERSION = $(word 1, $(go_major_minor)) +GO_MINOR_VERSION = $(word 2, $(go_major_minor)) + +GO_VERSION_VALIDATION_ERR_MSG = Golang version ($(BUILD_GOVERSION)) is not supported, please use at least $(BUILD_REQUIRE_GO_MAJOR).$(BUILD_REQUIRE_GO_MINOR) + +.PHONY: goversion +goversion: +ifneq ($(OS), Windows_NT) + @if [ $(GO_MAJOR_VERSION) -gt $(BUILD_REQUIRE_GO_MAJOR) ]; then \ + exit 0; \ + elif [ $(GO_MAJOR_VERSION) -lt $(BUILD_REQUIRE_GO_MAJOR) ]; then \ + echo '$(GO_VERSION_VALIDATION_ERR_MSG)';\ + exit 1; \ + elif [ $(GO_MINOR_VERSION) -lt $(BUILD_REQUIRE_GO_MINOR) ] ; then \ + echo '$(GO_VERSION_VALIDATION_ERR_MSG)';\ + exit 1; \ + fi +else + # This needs Set-ExecutionPolicy -Scope CurrentUser Unrestricted + @$(CURDIR)/mk/check_go_version.ps1 $(BUILD_REQUIRE_GO_MAJOR) $(BUILD_REQUIRE_GO_MINOR) +endif diff --git a/mk/platform.mk b/mk/platform.mk new file mode 100644 index 000000000..67c94c07d --- /dev/null +++ b/mk/platform.mk @@ -0,0 +1,15 @@ +ifeq ($(OS), Windows_NT) + SHELL := pwsh.exe + .SHELLFLAGS := -NoProfile -Command + SYSTEM = windows + EXT = .exe +else + SYSTEM ?= $(shell uname -s | tr '[A-Z]' '[a-z]') + include mk/platform/unix_common.mk +endif + +ifneq ("$(wildcard mk/platform/$(SYSTEM).mk)", "") + include mk/platform/$(SYSTEM).mk +else + include mk/platform/linux.mk +endif diff --git a/mk/platform/freebsd.mk b/mk/platform/freebsd.mk new file mode 100644 index 000000000..c08c82d6e --- /dev/null +++ b/mk/platform/freebsd.mk @@ -0,0 +1,5 @@ +# FreeBSD specific + +MAKE=gmake + +$(info building for FreeBSD) diff --git a/platform/linux.mk b/mk/platform/linux.mk similarity index 100% rename from platform/linux.mk rename to mk/platform/linux.mk diff --git a/platform/openbsd.mk b/mk/platform/openbsd.mk similarity index 79% rename from platform/openbsd.mk rename to mk/platform/openbsd.mk index def3775e8..145b8257f 100644 --- a/platform/openbsd.mk +++ b/mk/platform/openbsd.mk @@ -1,6 +1,5 @@ # OpenBSD specific -# -Make=gmake +MAKE=gmake $(info building for OpenBSD) diff --git a/platform/unix_common.mk b/mk/platform/unix_common.mk similarity index 88% rename from platform/unix_common.mk rename to mk/platform/unix_common.mk index 7692fd14d..23f9d886b 100644 --- a/platform/unix_common.mk +++ b/mk/platform/unix_common.mk @@ -8,8 +8,6 @@ MKDIR=mkdir -p GOOS ?= $(shell go env GOOS) GOARCH ?= $(shell go env GOARCH) -BUILD_GOVERSION=$(shell go env GOVERSION | sed s/go//) - #Current versioning information from env BUILD_VERSION?=$(shell git describe --tags) BUILD_CODENAME="alphaga" diff --git a/platform/windows.mk b/mk/platform/windows.mk similarity index 93% rename from platform/windows.mk rename to mk/platform/windows.mk index a8fa4483b..33b92b828 100644 --- a/platform/windows.mk +++ b/mk/platform/windows.mk @@ -1,5 +1,4 @@ # Windows specific -# MAKE=make GOOS=windows @@ -12,7 +11,6 @@ GOARCH ?= $(shell go env GOARCH) #BUILD_VERSION?=$(shell (Invoke-WebRequest -UseBasicParsing -Uri https://api.github.com/repos/crowdsecurity/crowdsec/releases/latest).Content | jq -r '.tag_name') #hardcode it till i find a workaround BUILD_VERSION?=$(shell git describe --tags $$(git rev-list --tags --max-count=1)) -BUILD_GOVERSION?=$(shell (go env GOVERSION).replace("go","")) BUILD_CODENAME?=alphaga BUILD_TIMESTAMP?=$(shell Get-Date -Format "yyyy-MM-dd_HH:mm:ss") BUILD_TAG?=$(shell git rev-parse HEAD) @@ -26,5 +24,4 @@ CPR=Copy-Item -Recurse MKDIR=New-Item -ItemType directory WIN_IGNORE_ERR=; exit 0 - $(info Building for windows) diff --git a/platform/freebsd.mk b/platform/freebsd.mk deleted file mode 100644 index 48ccdc553..000000000 --- a/platform/freebsd.mk +++ /dev/null @@ -1,6 +0,0 @@ -# FreeBSD specific -# - -Make=gmake - -$(info building for FreeBSD) \ No newline at end of file