2023-03-07 10:46:52 +00:00
i n c l u d e m k / p l a t f o r m . m k
2023-06-23 12:25:29 +00:00
i n c l u d e m k / g m s l
2022-01-06 10:20:59 +00:00
2023-06-23 12:25:29 +00:00
# By default, this build requires the C++ re2 library to be installed.
#
# Debian/Ubuntu: apt install libre2-dev
# Fedora/CentOS: dnf install re2-devel
# FreeBSD: pkg install re2
# Alpine: apk add re2-dev
# Windows: choco install re2
# MacOS: brew install re2
# To build without re2, run "make BUILD_RE2_WASM=1"
2023-07-04 10:26:32 +00:00
# The WASM version is slower and introduces a short delay when starting a process
# (including cscli) so it is not recommended for production use.
2023-06-23 12:25:29 +00:00
BUILD_RE2_WASM ?= 0
# To build static binaries, run "make BUILD_STATIC=1".
2023-07-04 10:26:32 +00:00
# On some platforms, this requires additional packages
# (e.g. glibc-static and libstdc++-static on fedora, centos.. which are on the powertools/crb repository).
2023-06-23 12:25:29 +00:00
# If the static build fails at the link stage, it might be because the static library is not provided
# for your distribution (look for libre2.a). See the Dockerfile for an example of how to build it.
BUILD_STATIC ?= 0
# List of plugins to build
2023-08-24 07:46:25 +00:00
PLUGINS ?= $( patsubst ./cmd/notification-%,%,$( wildcard ./cmd/notification-*) )
2023-06-23 12:25:29 +00:00
# Can be overriden, if you can deal with the consequences
2023-06-05 11:02:37 +00:00
BUILD_REQUIRE_GO_MAJOR ?= 1
2023-10-04 11:01:57 +00:00
BUILD_REQUIRE_GO_MINOR ?= 21
2023-06-05 11:02:37 +00:00
2023-06-23 12:25:29 +00:00
#--------------------------------------
2023-07-26 08:24:37 +00:00
GO = go
GOTEST = $( GO) test
2023-06-06 13:46:25 +00:00
2023-05-25 08:32:05 +00:00
BUILD_CODENAME ?= alphaga
2022-05-17 10:14:59 +00:00
CROWDSEC_FOLDER = ./cmd/crowdsec
CSCLI_FOLDER = ./cmd/crowdsec-cli/
2024-02-06 19:11:53 +00:00
CSCTI_FOLDER = ./cmd/cscti/
2023-08-24 07:46:25 +00:00
PLUGINS_DIR_PREFIX = ./cmd/notification-
2022-05-17 10:14:59 +00:00
CROWDSEC_BIN = crowdsec$( EXT)
CSCLI_BIN = cscli$( EXT)
2020-05-15 09:39:16 +00:00
2023-07-18 07:31:04 +00:00
# semver comparison to select the hub branch requires the version to start with "v"
i f n e q ( $( call substr ,$ ( BUILD_VERSION ) ,1,1) , v )
$( error BUILD_VERSION " $( BUILD_VERSION) " should start with "v" )
e n d i f
2023-06-06 13:46:25 +00:00
# Directory for the release files
RELDIR = crowdsec-$( BUILD_VERSION)
2023-01-19 21:24:45 +00:00
GO_MODULE_NAME = github.com/crowdsecurity/crowdsec
2022-02-04 12:02:45 +00:00
2023-06-23 12:25:29 +00:00
# Check if a given value is considered truthy and returns "0" or "1".
# A truthy value is one of the following: "1", "yes", or "true", case-insensitive.
#
# Usage:
# ifeq ($(call bool,$(FOO)),1)
# $(info Let's foo)
# endif
bool = $( if $( filter $( call lc, $1 ) ,1 yes true ) ,1,0)
2023-06-06 13:46:25 +00:00
#--------------------------------------
#
2023-08-28 13:58:26 +00:00
# Define MAKE_FLAGS and LD_OPTS for the sub-makefiles in cmd/
2023-06-06 13:46:25 +00:00
#
MAKE_FLAGS = --no-print-directory GOARCH = $( GOARCH) GOOS = $( GOOS) RM = " $( RM) " WIN_IGNORE_ERR = " $( WIN_IGNORE_ERR) " CP = " $( CP) " CPR = " $( CPR) " MKDIR = " $( MKDIR) "
2022-02-04 12:02:45 +00:00
LD_OPTS_VARS = \
2023-07-28 14:35:08 +00:00
- X 'github.com/crowdsecurity/go-cs-lib/version.Version=$(BUILD_VERSION)' \
- X 'github.com/crowdsecurity/go-cs-lib/version.BuildDate=$(BUILD_TIMESTAMP)' \
- X 'github.com/crowdsecurity/go-cs-lib/version.Tag=$(BUILD_TAG)' \
2023-01-19 21:24:45 +00:00
- X '$(GO_MODULE_NAME)/pkg/cwversion.Codename=$(BUILD_CODENAME)' \
- X '$(GO_MODULE_NAME)/pkg/csconfig.defaultConfigDir=$(DEFAULT_CONFIGDIR)' \
- X '$(GO_MODULE_NAME)/pkg/csconfig.defaultDataDir=$(DEFAULT_DATADIR)'
2021-04-26 12:45:52 +00:00
2023-03-22 10:57:29 +00:00
i f n e q ( , $( DOCKER_BUILD ) )
LD_OPTS_VARS += -X '$(GO_MODULE_NAME)/pkg/cwversion.System=docker'
e n d i f
2023-06-06 13:46:25 +00:00
GO_TAGS := netgo,osusergo,sqlite_omit_load_extension
2023-07-05 15:45:31 +00:00
# this will be used by Go in the make target, some distributions require it
export PKG_CONFIG_PATH := /usr/local/lib/pkgconfig:$( PKG_CONFIG_PATH)
2023-06-23 12:25:29 +00:00
i f e q ( $( call bool ,$ ( BUILD_RE 2_WASM ) ) , 0 )
2023-06-29 14:35:19 +00:00
i f e q ( $( PKG_CONFIG ) , )
$( error "pkg-config is not available. Please install pkg-config." )
e n d i f
2023-06-23 12:25:29 +00:00
i f e q ( $( RE 2_CHECK ) , )
RE2_FAIL := "libre2-dev is not installed, please install it or set BUILD_RE2_WASM=1 to use the WebAssembly version"
e l s e
2023-06-06 13:46:25 +00:00
# += adds a space that we don't want
GO_TAGS := $( GO_TAGS) ,re2_cgo
LD_OPTS_VARS += -X '$(GO_MODULE_NAME)/pkg/cwversion.Libre2=C++'
2023-03-30 13:05:09 +00:00
e n d i f
2023-06-23 12:25:29 +00:00
e n d i f
2023-03-30 13:05:09 +00:00
2023-08-28 13:58:26 +00:00
# Build static to avoid the runtime dependency on libre2.so
2023-06-23 12:25:29 +00:00
i f e q ( $( call bool ,$ ( BUILD_STATIC ) ) , 1 )
BUILD_TYPE = static
EXTLDFLAGS := -extldflags '-static'
e l s e
BUILD_TYPE = dynamic
EXTLDFLAGS :=
e n d i f
2023-08-28 13:58:26 +00:00
# Build with debug symbols, and disable optimizations + inlining, to use Delve
i f e q ( $( call bool ,$ ( DEBUG ) ) , 1 )
STRIP_SYMBOLS :=
DISABLE_OPTIMIZATION := -gcflags "-N -l"
e l s e
STRIP_SYMBOLS := -s -w
DISABLE_OPTIMIZATION :=
e n d i f
export LD_OPTS = -ldflags " $( STRIP_SYMBOLS) $( EXTLDFLAGS) $( LD_OPTS_VARS) " \
-trimpath -tags $( GO_TAGS) $( DISABLE_OPTIMIZATION)
2023-03-30 13:05:09 +00:00
2023-08-28 13:58:26 +00:00
i f e q ( $( call bool ,$ ( TEST_COVERAGE ) ) , 1 )
2023-03-30 13:05:09 +00:00
LD_OPTS += -cover
2022-09-14 12:22:57 +00:00
e n d i f
2021-04-26 12:45:52 +00:00
2023-06-06 13:46:25 +00:00
#--------------------------------------
2023-05-25 08:32:05 +00:00
2022-02-01 08:22:47 +00:00
.PHONY : build
2023-12-18 09:13:08 +00:00
build : pre -build goversion crowdsec cscli plugins ## Build crowdsec, cscli and plugins
2023-06-05 21:15:18 +00:00
2023-06-06 13:46:25 +00:00
.PHONY : pre -build
2023-12-18 09:13:08 +00:00
pre-build : ## Sanity checks and build information
2023-06-23 12:25:29 +00:00
$( info Building $( BUILD_VERSION) ( $( BUILD_TAG) ) $( BUILD_TYPE) for $( GOOS) /$( GOARCH) )
i f n e q ( , $( RE 2_FAIL ) )
$( error $( RE2_FAIL) )
2023-06-06 13:46:25 +00:00
e n d i f
2023-06-23 12:25:29 +00:00
2023-06-06 13:46:25 +00:00
i f n e q ( , $( RE 2_CHECK ) )
$( info Using C++ regexp library)
e l s e
$( info Fallback to WebAssembly regexp library. To use the C++ version, make sure you have installed libre2-dev and pkg-config.)
e n d i f
2023-08-28 13:58:26 +00:00
i f e q ( $( call bool ,$ ( DEBUG ) ) , 1 )
$( info Building with debug symbols and disabled optimizations)
e n d i f
i f e q ( $( call bool ,$ ( TEST_COVERAGE ) ) , 1 )
$( info Test coverage collection enabled)
e n d i f
2023-12-18 09:13:08 +00:00
# intentional, empty line
2023-06-05 21:15:18 +00:00
$( info )
2020-05-15 09:39:16 +00:00
2024-02-01 16:20:50 +00:00
# To update cti_openapi.yaml:
# curl https://crowdsecurity.github.io/cti-api/v2/swagger.yaml | npx swagger2openapi -o cti_openapi.yaml /dev/stdin
.PHONY : gen -cti
gen-cti : ## Generate CTI client code from the specs
@which oapi-codegen > /dev/null 2>& 1 || ( echo "oapi-codegen is not installed. You can install it with 'go install github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@latest'" && exit 1)
@echo "Generating Go client from Swagger spec..."
oapi-codegen -package cti -generate client -o ./pkg/cti/client.go ./pkg/cti/cti_openapi.yaml
oapi-codegen -package cti -generate types -o ./pkg/cti/types.go ./pkg/cti/cti_openapi.yaml
@echo "Client generation complete."
2022-02-18 17:09:19 +00:00
.PHONY : all
2023-12-18 09:13:08 +00:00
all : clean test build ## Clean, test and build (requires localstack)
2022-02-18 17:09:19 +00:00
2022-02-01 08:22:47 +00:00
.PHONY : plugins
2023-12-18 09:13:08 +00:00
plugins : ## Build notification plugins
2023-06-05 21:15:18 +00:00
@$( foreach plugin,$( PLUGINS) , \
2023-08-24 07:46:25 +00:00
$( MAKE) -C $( PLUGINS_DIR_PREFIX) $( plugin) build $( MAKE_FLAGS) ; \
2023-06-05 21:15:18 +00:00
)
2021-08-25 09:43:29 +00:00
2023-10-04 11:01:57 +00:00
# same as "$(MAKE) -f debian/rules clean" but without the dependency on debhelper
.PHONY : clean -debian
clean-debian :
@$( RM) -r debian/crowdsec
@$( RM) -r debian/crowdsec
@$( RM) -r debian/files
@$( RM) -r debian/.debhelper
@$( RM) -r debian/*.substvars
@$( RM) -r debian/*-stamp
.PHONY : clean -rpm
clean-rpm :
@$( RM) -r rpm/BUILD
@$( RM) -r rpm/BUILDROOT
@$( RM) -r rpm/RPMS
@$( RM) -r rpm/SOURCES/*.tar.gz
@$( RM) -r rpm/SRPMS
2022-02-01 08:22:47 +00:00
.PHONY : clean
2023-12-18 09:13:08 +00:00
clean : clean -debian clean -rpm testclean ## Remove build artifacts
2023-05-25 08:32:05 +00:00
@$( MAKE) -C $( CROWDSEC_FOLDER) clean $( MAKE_FLAGS)
@$( MAKE) -C $( CSCLI_FOLDER) clean $( MAKE_FLAGS)
2022-05-17 10:14:59 +00:00
@$( RM) $( CROWDSEC_BIN) $( WIN_IGNORE_ERR)
@$( RM) $( CSCLI_BIN) $( WIN_IGNORE_ERR)
@$( RM) *.log $( WIN_IGNORE_ERR)
@$( RM) crowdsec-release.tgz $( WIN_IGNORE_ERR)
2023-06-05 21:15:18 +00:00
@$( foreach plugin,$( PLUGINS) , \
2023-08-24 07:46:25 +00:00
$( MAKE) -C $( PLUGINS_DIR_PREFIX) $( plugin) clean $( MAKE_FLAGS) ; \
2023-06-05 21:15:18 +00:00
)
2020-05-15 09:39:16 +00:00
2023-06-06 13:46:25 +00:00
.PHONY : cscli
2023-12-18 09:13:08 +00:00
cscli : goversion ## Build cscli
2023-05-25 08:32:05 +00:00
@$( MAKE) -C $( CSCLI_FOLDER) build $( MAKE_FLAGS)
2020-05-15 09:39:16 +00:00
2024-02-06 19:11:53 +00:00
.PHONY : cscti
cscti : goversion ## Build cscli
@$( MAKE) -C $( CSCTI_FOLDER) build $( MAKE_FLAGS)
2023-06-06 13:46:25 +00:00
.PHONY : crowdsec
2023-12-18 09:13:08 +00:00
crowdsec : goversion ## Build crowdsec
2023-05-25 08:32:05 +00:00
@$( MAKE) -C $( CROWDSEC_FOLDER) build $( MAKE_FLAGS)
2020-05-15 09:39:16 +00:00
2024-03-07 13:25:25 +00:00
.PHONY : generate
generate : ## Generate code for the database and APIs
$( GO) generate ./pkg/database/ent
$( GO) generate ./pkg/models
2023-08-24 07:46:25 +00:00
2022-02-14 21:21:19 +00:00
.PHONY : testclean
2023-12-18 09:13:08 +00:00
testclean : bats -clean ## Remove test artifacts
2022-05-17 10:14:59 +00:00
@$( RM) pkg/apiserver/ent $( WIN_IGNORE_ERR)
@$( RM) pkg/cwhub/hubdir $( WIN_IGNORE_ERR)
2022-05-17 13:54:52 +00:00
@$( RM) pkg/cwhub/install $( WIN_IGNORE_ERR)
@$( RM) pkg/types/example.txt $( WIN_IGNORE_ERR)
2022-02-14 21:21:19 +00:00
2023-07-04 10:26:32 +00:00
# for the tests with localstack
2023-03-08 13:41:16 +00:00
export AWS_ENDPOINT_FORCE = http://localhost:4566
2023-12-19 14:27:04 +00:00
export AWS_ACCESS_KEY_ID = test
export AWS_SECRET_ACCESS_KEY = test
2023-03-08 13:41:16 +00:00
testenv :
2022-06-16 14:13:48 +00:00
@echo 'NOTE: You need Docker, docker-compose and run "make localstack" in a separate shell ("make localstack-stop" to terminate it)'
2023-03-08 13:41:16 +00:00
.PHONY : test
2023-12-18 09:13:08 +00:00
test : testenv goversion ## Run unit tests with localstack
2022-02-14 21:21:19 +00:00
$( GOTEST) $( LD_OPTS) ./...
2021-08-25 09:43:29 +00:00
2023-03-08 13:41:16 +00:00
.PHONY : go -acc
2023-12-18 09:13:08 +00:00
go-acc : testenv goversion ## Run unit tests with localstack + coverage
2023-11-28 16:10:44 +00:00
go-acc ./... -o coverage.out --ignore database,notifications,protobufs,cwversion,cstest,models -- $( LD_OPTS)
2023-03-08 13:41:16 +00:00
2023-07-04 10:26:32 +00:00
# mock AWS services
2022-05-17 13:54:52 +00:00
.PHONY : localstack
2023-12-18 09:13:08 +00:00
localstack : ## Run localstack containers (required for unit testing)
2023-03-03 14:54:49 +00:00
docker-compose -f test/localstack/docker-compose.yml up
2022-05-17 13:54:52 +00:00
2022-06-16 14:13:48 +00:00
.PHONY : localstack -stop
2023-12-18 09:13:08 +00:00
localstack-stop : ## Stop localstack containers
2023-03-03 14:54:49 +00:00
docker-compose -f test/localstack/docker-compose.yml down
2022-06-16 14:13:48 +00:00
2023-07-04 10:26:32 +00:00
# build vendor.tgz to be distributed with the release
2023-06-05 21:15:18 +00:00
.PHONY : vendor
2023-12-18 09:13:08 +00:00
vendor : vendor -remove ## CI only - vendor dependencies and archive them for packaging
2023-07-26 08:24:37 +00:00
$( GO) mod vendor
2023-08-25 15:02:00 +00:00
tar czf vendor.tgz vendor
tar --create --auto-compress --file= $( RELDIR) -vendor.tar.xz vendor
2023-07-26 08:24:37 +00:00
2023-07-04 10:26:32 +00:00
# remove vendor directories and vendor.tgz
2023-06-21 07:52:33 +00:00
.PHONY : vendor -remove
2023-12-18 09:13:08 +00:00
vendor-remove : ## Remove vendor dependencies and archives
2023-08-25 15:02:00 +00:00
$( RM) vendor vendor.tgz *-vendor.tar.xz
2023-06-05 21:15:18 +00:00
.PHONY : package
package :
2022-05-17 10:14:59 +00:00
@echo " Building Release to dir $( RELDIR) "
@$( MKDIR) $( RELDIR) /cmd/crowdsec
@$( MKDIR) $( RELDIR) /cmd/crowdsec-cli
@$( CP) $( CROWDSEC_FOLDER) /$( CROWDSEC_BIN) $( RELDIR) /cmd/crowdsec
@$( CP) $( CSCLI_FOLDER) /$( CSCLI_BIN) $( RELDIR) /cmd/crowdsec-cli
2023-06-05 21:15:18 +00:00
@$( foreach plugin,$( PLUGINS) , \
2023-08-24 07:46:25 +00:00
$( MKDIR) $( RELDIR) /$( PLUGINS_DIR_PREFIX) $( plugin) ; \
$( CP) $( PLUGINS_DIR_PREFIX) $( plugin) /notification-$( plugin) $( EXT) $( RELDIR) /$( PLUGINS_DIR_PREFIX) $( plugin) ; \
$( CP) $( PLUGINS_DIR_PREFIX) $( plugin) /$( plugin) .yaml $( RELDIR) /$( PLUGINS_DIR_PREFIX) $( plugin) /; \
2023-06-05 21:15:18 +00:00
)
2022-05-17 10:14:59 +00:00
@$( CPR) ./config $( RELDIR)
@$( CP) wizard.sh $( RELDIR)
@$( CP) scripts/test_env.sh $( RELDIR)
@$( CP) scripts/test_env.ps1 $( RELDIR)
2021-09-14 13:32:35 +00:00
2022-03-15 10:19:35 +00:00
@tar cvzf crowdsec-release.tgz $( RELDIR)
2022-01-06 10:20:59 +00:00
2021-08-25 09:43:29 +00:00
.PHONY : check_release
check_release :
2022-09-14 12:22:57 +00:00
i f n e q ( $( OS ) , W i n d o w s _ N T )
2021-08-25 09:43:29 +00:00
@if [ -d $( RELDIR) ] ; then echo " $( RELDIR) already exists, abort " ; exit 1 ; fi
2022-05-17 10:14:59 +00:00
e l s e
@if ( Test-Path -Path $( RELDIR) ) { echo " $( RELDIR) already exists, abort " ; exit 1 ; }
e n d i f
2021-08-25 09:43:29 +00:00
2022-02-01 08:22:47 +00:00
.PHONY : release
2023-12-18 09:13:08 +00:00
release : check_release build package ## Build a release tarball
2021-08-25 09:43:29 +00:00
2022-05-17 10:14:59 +00:00
.PHONY : windows_installer
2023-12-18 09:13:08 +00:00
windows_installer : build ## Windows - build the installer
2022-05-17 10:14:59 +00:00
@.\m ake_installer.ps1 -version $( BUILD_VERSION)
.PHONY : chocolatey
2023-12-18 09:13:08 +00:00
chocolatey : windows_installer ## Windows - build the chocolatey package
2022-05-17 10:14:59 +00:00
@.\m ake_chocolatey.ps1 -version $( BUILD_VERSION)
2022-03-09 13:45:36 +00:00
2023-03-03 14:54:49 +00:00
# Include test/bats.mk only if it exists
2023-01-25 09:39:23 +00:00
# to allow building without a test/ directory
# (i.e. inside docker)
2023-03-03 14:54:49 +00:00
i f e q ( , $( wildcard test /bats .mk ) )
2023-01-25 09:39:23 +00:00
bats-clean :
e l s e
2023-03-03 14:54:49 +00:00
i n c l u d e t e s t / b a t s . m k
2023-01-25 09:39:23 +00:00
e n d i f
2023-03-07 10:46:52 +00:00
i n c l u d e m k / g o v e r s i o n . m k
2023-12-18 09:13:08 +00:00
i n c l u d e m k / h e l p . m k