diff --git a/Makefile b/Makefile index 63edcd06a..50e05339d 100644 --- a/Makefile +++ b/Makefile @@ -64,7 +64,7 @@ bool = $(if $(filter $(call lc, $1),1 yes true),1,0) #-------------------------------------- # -# Define MAKE_FLAGS and LD_OPTS for the sub-makefiles in cmd/ and plugins/ +# Define MAKE_FLAGS and LD_OPTS for the sub-makefiles in cmd/ # MAKE_FLAGS = --no-print-directory GOARCH=$(GOARCH) GOOS=$(GOOS) RM="$(RM)" WIN_IGNORE_ERR="$(WIN_IGNORE_ERR)" CP="$(CP)" CPR="$(CPR)" MKDIR="$(MKDIR)" @@ -92,7 +92,6 @@ ifeq ($(PKG_CONFIG),) endif ifeq ($(RE2_CHECK),) -# we could detect the platform and suggest the command to install RE2_FAIL := "libre2-dev is not installed, please install it or set BUILD_RE2_WASM=1 to use the WebAssembly version" else # += adds a space that we don't want @@ -101,6 +100,7 @@ LD_OPTS_VARS += -X '$(GO_MODULE_NAME)/pkg/cwversion.Libre2=C++' endif endif +# Build static to avoid the runtime dependency on libre2.so ifeq ($(call bool,$(BUILD_STATIC)),1) BUILD_TYPE = static EXTLDFLAGS := -extldflags '-static' @@ -109,10 +109,19 @@ BUILD_TYPE = dynamic EXTLDFLAGS := endif -export LD_OPTS=-ldflags "-s -w $(EXTLDFLAGS) $(LD_OPTS_VARS)" \ - -trimpath -tags $(GO_TAGS) +# Build with debug symbols, and disable optimizations + inlining, to use Delve +ifeq ($(call bool,$(DEBUG)),1) +STRIP_SYMBOLS := +DISABLE_OPTIMIZATION := -gcflags "-N -l" +else +STRIP_SYMBOLS := -s -w +DISABLE_OPTIMIZATION := +endif -ifneq (,$(TEST_COVERAGE)) +export LD_OPTS=-ldflags "$(STRIP_SYMBOLS) $(EXTLDFLAGS) $(LD_OPTS_VARS)" \ + -trimpath -tags $(GO_TAGS) $(DISABLE_OPTIMIZATION) + +ifeq ($(call bool,$(TEST_COVERAGE)),1) LD_OPTS += -cover endif @@ -135,8 +144,18 @@ ifneq (,$(RE2_CHECK)) else $(info Fallback to WebAssembly regexp library. To use the C++ version, make sure you have installed libre2-dev and pkg-config.) endif + +ifeq ($(call bool,$(DEBUG)),1) + $(info Building with debug symbols and disabled optimizations) +endif + +ifeq ($(call bool,$(TEST_COVERAGE)),1) + $(info Test coverage collection enabled) +endif + $(info ) + .PHONY: all all: clean test build diff --git a/cmd/crowdsec-cli/Makefile b/cmd/crowdsec-cli/Makefile index 95acf358a..392361ef8 100644 --- a/cmd/crowdsec-cli/Makefile +++ b/cmd/crowdsec-cli/Makefile @@ -15,7 +15,7 @@ BIN_PREFIX = $(PREFIX)"/usr/local/bin/" all: clean build build: clean - $(GOBUILD) $(LD_OPTS) $(BUILD_VENDOR_FLAGS) -o $(BINARY_NAME) + $(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) .PHONY: install install: install-conf install-bin diff --git a/cmd/crowdsec/Makefile b/cmd/crowdsec/Makefile index 66757d7ad..7425d970a 100644 --- a/cmd/crowdsec/Makefile +++ b/cmd/crowdsec/Makefile @@ -22,7 +22,7 @@ SYSTEMD_PATH_FILE = "/etc/systemd/system/crowdsec.service" all: clean test build build: clean - $(GOBUILD) $(LD_OPTS) $(BUILD_VENDOR_FLAGS) -o $(CROWDSEC_BIN) + $(GOBUILD) $(LD_OPTS) -o $(CROWDSEC_BIN) test: $(GOTEST) $(LD_OPTS) -v ./... diff --git a/cmd/notification-dummy/Makefile b/cmd/notification-dummy/Makefile index 7650e480b..251abe19d 100644 --- a/cmd/notification-dummy/Makefile +++ b/cmd/notification-dummy/Makefile @@ -10,7 +10,7 @@ GOBUILD = $(GO) build BINARY_NAME = notification-dummy$(EXT) build: clean - $(GOBUILD) $(LD_OPTS) $(BUILD_VENDOR_FLAGS) -o $(BINARY_NAME) + $(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) .PHONY: clean clean: diff --git a/cmd/notification-email/Makefile b/cmd/notification-email/Makefile index cc933ffd4..7a782cc9d 100644 --- a/cmd/notification-email/Makefile +++ b/cmd/notification-email/Makefile @@ -10,7 +10,7 @@ GOBUILD = $(GO) build BINARY_NAME = notification-email$(EXT) build: clean - $(GOBUILD) $(LD_OPTS) $(BUILD_VENDOR_FLAGS) -o $(BINARY_NAME) + $(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) .PHONY: clean clean: diff --git a/cmd/notification-http/Makefile b/cmd/notification-http/Makefile index b3e219266..30ed43a69 100644 --- a/cmd/notification-http/Makefile +++ b/cmd/notification-http/Makefile @@ -10,7 +10,7 @@ GOBUILD = $(GO) build BINARY_NAME = notification-http$(EXT) build: clean - $(GOBUILD) $(LD_OPTS) $(BUILD_VENDOR_FLAGS) -o $(BINARY_NAME) + $(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) .PHONY: clean clean: diff --git a/cmd/notification-sentinel/Makefile b/cmd/notification-sentinel/Makefile index b60b6612a..21d176a90 100644 --- a/cmd/notification-sentinel/Makefile +++ b/cmd/notification-sentinel/Makefile @@ -10,7 +10,7 @@ GOBUILD = $(GO) build BINARY_NAME = notification-sentinel$(EXT) build: clean - $(GOBUILD) $(LD_OPTS) $(BUILD_VENDOR_FLAGS) -o $(BINARY_NAME) + $(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) .PHONY: clean clean: diff --git a/cmd/notification-slack/Makefile b/cmd/notification-slack/Makefile index 7fd5ec898..06c9ccc3f 100644 --- a/cmd/notification-slack/Makefile +++ b/cmd/notification-slack/Makefile @@ -10,7 +10,7 @@ GOBUILD = $(GO) build BINARY_NAME = notification-slack$(EXT) build: clean - $(GOBUILD) $(LD_OPTS) $(BUILD_VENDOR_FLAGS) -o $(BINARY_NAME) + $(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) .PHONY: clean clean: diff --git a/cmd/notification-splunk/Makefile b/cmd/notification-splunk/Makefile index a5d990aa9..aa15ecac9 100644 --- a/cmd/notification-splunk/Makefile +++ b/cmd/notification-splunk/Makefile @@ -10,7 +10,7 @@ GOBUILD = $(GO) build BINARY_NAME = notification-splunk$(EXT) build: clean - $(GOBUILD) $(LD_OPTS) $(BUILD_VENDOR_FLAGS) -o $(BINARY_NAME) + $(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME) .PHONY: clean clean: diff --git a/test/bats.mk b/test/bats.mk index bbd275701..35241b03e 100644 --- a/test/bats.mk +++ b/test/bats.mk @@ -77,7 +77,7 @@ bats-update-tools: # Build and installs crowdsec in a local directory. Rebuilds if already exists. bats-build: bats-environment @$(MKDIR) $(BIN_DIR) $(LOG_DIR) $(PID_DIR) $(BATS_PLUGIN_DIR) - @TEST_COVERAGE=$(TEST_COVERAGE) DEFAULT_CONFIGDIR=$(CONFIG_DIR) DEFAULT_DATADIR=$(DATA_DIR) $(MAKE) build + @$(MAKE) build DEBUG=1 TEST_COVERAGE=$(TEST_COVERAGE) DEFAULT_CONFIGDIR=$(CONFIG_DIR) DEFAULT_DATADIR=$(DATA_DIR) @install -m 0755 cmd/crowdsec/crowdsec cmd/crowdsec-cli/cscli $(BIN_DIR)/ @install -m 0755 cmd/notification-*/notification-* $(BATS_PLUGIN_DIR)/