github.com/crowdsecurity/crowdsec@v1.6.1/Makefile (about)

     1  include mk/platform.mk
     2  include mk/gmsl
     3  
     4  # By default, this build requires the C++ re2 library to be installed.
     5  #
     6  # Debian/Ubuntu: apt install libre2-dev
     7  # Fedora/CentOS: dnf install re2-devel
     8  # FreeBSD:       pkg install re2
     9  # Alpine:        apk add re2-dev
    10  # Windows:       choco install re2
    11  # MacOS:         brew install re2
    12  
    13  # To build without re2, run "make BUILD_RE2_WASM=1"
    14  # The WASM version is slower and introduces a short delay when starting a process
    15  # (including cscli) so it is not recommended for production use.
    16  BUILD_RE2_WASM ?= 0
    17  
    18  # To build static binaries, run "make BUILD_STATIC=1".
    19  # On some platforms, this requires additional packages
    20  # (e.g. glibc-static and libstdc++-static on fedora, centos.. which are on the powertools/crb repository).
    21  # If the static build fails at the link stage, it might be because the static library is not provided
    22  # for your distribution (look for libre2.a). See the Dockerfile for an example of how to build it.
    23  BUILD_STATIC ?= 0
    24  
    25  # List of plugins to build
    26  PLUGINS ?= $(patsubst ./cmd/notification-%,%,$(wildcard ./cmd/notification-*))
    27  
    28  # Can be overriden, if you can deal with the consequences
    29  BUILD_REQUIRE_GO_MAJOR ?= 1
    30  BUILD_REQUIRE_GO_MINOR ?= 21
    31  
    32  #--------------------------------------
    33  
    34  GO = go
    35  GOTEST = $(GO) test
    36  
    37  BUILD_CODENAME ?= alphaga
    38  
    39  CROWDSEC_FOLDER = ./cmd/crowdsec
    40  CSCLI_FOLDER = ./cmd/crowdsec-cli/
    41  PLUGINS_DIR_PREFIX = ./cmd/notification-
    42  
    43  CROWDSEC_BIN = crowdsec$(EXT)
    44  CSCLI_BIN = cscli$(EXT)
    45  
    46  # semver comparison to select the hub branch requires the version to start with "v"
    47  ifneq ($(call substr,$(BUILD_VERSION),1,1),v)
    48      $(error BUILD_VERSION "$(BUILD_VERSION)" should start with "v")
    49  endif
    50  
    51  # Directory for the release files
    52  RELDIR = crowdsec-$(BUILD_VERSION)
    53  
    54  GO_MODULE_NAME = github.com/crowdsecurity/crowdsec
    55  
    56  # Check if a given value is considered truthy and returns "0" or "1".
    57  # A truthy value is one of the following: "1", "yes", or "true", case-insensitive.
    58  #
    59  # Usage:
    60  # ifeq ($(call bool,$(FOO)),1)
    61  # $(info Let's foo)
    62  # endif
    63  bool = $(if $(filter $(call lc, $1),1 yes true),1,0)
    64  
    65  #--------------------------------------
    66  #
    67  # Define MAKE_FLAGS and LD_OPTS for the sub-makefiles in cmd/
    68  #
    69  
    70  MAKE_FLAGS = --no-print-directory GOARCH=$(GOARCH) GOOS=$(GOOS) RM="$(RM)" WIN_IGNORE_ERR="$(WIN_IGNORE_ERR)" CP="$(CP)" CPR="$(CPR)" MKDIR="$(MKDIR)"
    71  
    72  LD_OPTS_VARS= \
    73  -X 'github.com/crowdsecurity/go-cs-lib/version.Version=$(BUILD_VERSION)' \
    74  -X 'github.com/crowdsecurity/go-cs-lib/version.BuildDate=$(BUILD_TIMESTAMP)' \
    75  -X 'github.com/crowdsecurity/go-cs-lib/version.Tag=$(BUILD_TAG)' \
    76  -X '$(GO_MODULE_NAME)/pkg/cwversion.Codename=$(BUILD_CODENAME)' \
    77  -X '$(GO_MODULE_NAME)/pkg/csconfig.defaultConfigDir=$(DEFAULT_CONFIGDIR)' \
    78  -X '$(GO_MODULE_NAME)/pkg/csconfig.defaultDataDir=$(DEFAULT_DATADIR)'
    79  
    80  ifneq (,$(DOCKER_BUILD))
    81  LD_OPTS_VARS += -X '$(GO_MODULE_NAME)/pkg/cwversion.System=docker'
    82  endif
    83  
    84  GO_TAGS := netgo,osusergo,sqlite_omit_load_extension
    85  
    86  # this will be used by Go in the make target, some distributions require it
    87  export PKG_CONFIG_PATH:=/usr/local/lib/pkgconfig:$(PKG_CONFIG_PATH)
    88  
    89  ifeq ($(call bool,$(BUILD_RE2_WASM)),0)
    90  ifeq ($(PKG_CONFIG),)
    91    $(error "pkg-config is not available. Please install pkg-config.")
    92  endif
    93  
    94  ifeq ($(RE2_CHECK),)
    95  RE2_FAIL := "libre2-dev is not installed, please install it or set BUILD_RE2_WASM=1 to use the WebAssembly version"
    96  else
    97  # += adds a space that we don't want
    98  GO_TAGS := $(GO_TAGS),re2_cgo
    99  LD_OPTS_VARS += -X '$(GO_MODULE_NAME)/pkg/cwversion.Libre2=C++'
   100  endif
   101  endif
   102  
   103  # Build static to avoid the runtime dependency on libre2.so
   104  ifeq ($(call bool,$(BUILD_STATIC)),1)
   105  BUILD_TYPE = static
   106  EXTLDFLAGS := -extldflags '-static'
   107  else
   108  BUILD_TYPE = dynamic
   109  EXTLDFLAGS :=
   110  endif
   111  
   112  # Build with debug symbols, and disable optimizations + inlining, to use Delve
   113  ifeq ($(call bool,$(DEBUG)),1)
   114  STRIP_SYMBOLS :=
   115  DISABLE_OPTIMIZATION := -gcflags "-N -l"
   116  else
   117  STRIP_SYMBOLS := -s -w
   118  DISABLE_OPTIMIZATION :=
   119  endif
   120  
   121  export LD_OPTS=-ldflags "$(STRIP_SYMBOLS) $(EXTLDFLAGS) $(LD_OPTS_VARS)" \
   122  	-trimpath -tags $(GO_TAGS) $(DISABLE_OPTIMIZATION)
   123  
   124  ifeq ($(call bool,$(TEST_COVERAGE)),1)
   125  LD_OPTS += -cover
   126  endif
   127  
   128  #--------------------------------------
   129  
   130  .PHONY: build
   131  build: pre-build goversion crowdsec cscli plugins  ## Build crowdsec, cscli and plugins
   132  
   133  .PHONY: pre-build
   134  pre-build:  ## Sanity checks and build information
   135  	$(info Building $(BUILD_VERSION) ($(BUILD_TAG)) $(BUILD_TYPE) for $(GOOS)/$(GOARCH))
   136  
   137  ifneq (,$(RE2_FAIL))
   138  	$(error $(RE2_FAIL))
   139  endif
   140  
   141  ifneq (,$(RE2_CHECK))
   142  	$(info Using C++ regexp library)
   143  else
   144  	$(info Fallback to WebAssembly regexp library. To use the C++ version, make sure you have installed libre2-dev and pkg-config.)
   145  endif
   146  
   147  ifeq ($(call bool,$(DEBUG)),1)
   148  	$(info Building with debug symbols and disabled optimizations)
   149  endif
   150  
   151  ifeq ($(call bool,$(TEST_COVERAGE)),1)
   152  	$(info Test coverage collection enabled)
   153  endif
   154  
   155  # intentional, empty line
   156  	$(info )
   157  
   158  .PHONY: all
   159  all: clean test build  ## Clean, test and build (requires localstack)
   160  
   161  .PHONY: plugins
   162  plugins:  ## Build notification plugins
   163  	@$(foreach plugin,$(PLUGINS), \
   164  		$(MAKE) -C $(PLUGINS_DIR_PREFIX)$(plugin) build $(MAKE_FLAGS); \
   165  	)
   166  
   167  # same as "$(MAKE) -f debian/rules clean" but without the dependency on debhelper
   168  .PHONY: clean-debian
   169  clean-debian:
   170  	@$(RM) -r debian/crowdsec
   171  	@$(RM) -r debian/crowdsec
   172  	@$(RM) -r debian/files
   173  	@$(RM) -r debian/.debhelper
   174  	@$(RM) -r debian/*.substvars
   175  	@$(RM) -r debian/*-stamp
   176  
   177  .PHONY: clean-rpm
   178  clean-rpm:
   179  	@$(RM) -r rpm/BUILD
   180  	@$(RM) -r rpm/BUILDROOT
   181  	@$(RM) -r rpm/RPMS
   182  	@$(RM) -r rpm/SOURCES/*.tar.gz
   183  	@$(RM) -r rpm/SRPMS
   184  
   185  .PHONY: clean
   186  clean: clean-debian clean-rpm testclean  ## Remove build artifacts
   187  	@$(MAKE) -C $(CROWDSEC_FOLDER) clean $(MAKE_FLAGS)
   188  	@$(MAKE) -C $(CSCLI_FOLDER) clean $(MAKE_FLAGS)
   189  	@$(RM) $(CROWDSEC_BIN) $(WIN_IGNORE_ERR)
   190  	@$(RM) $(CSCLI_BIN) $(WIN_IGNORE_ERR)
   191  	@$(RM) *.log $(WIN_IGNORE_ERR)
   192  	@$(RM) crowdsec-release.tgz $(WIN_IGNORE_ERR)
   193  	@$(foreach plugin,$(PLUGINS), \
   194  		$(MAKE) -C $(PLUGINS_DIR_PREFIX)$(plugin) clean $(MAKE_FLAGS); \
   195  	)
   196  
   197  .PHONY: cscli
   198  cscli: goversion  ## Build cscli
   199  	@$(MAKE) -C $(CSCLI_FOLDER) build $(MAKE_FLAGS)
   200  
   201  .PHONY: crowdsec
   202  crowdsec: goversion  ## Build crowdsec
   203  	@$(MAKE) -C $(CROWDSEC_FOLDER) build $(MAKE_FLAGS)
   204  
   205  .PHONY: generate
   206  generate:  ## Generate code for the database and APIs
   207  	$(GO) generate ./pkg/database/ent
   208  	$(GO) generate ./pkg/models
   209  
   210  .PHONY: testclean
   211  testclean: bats-clean  ## Remove test artifacts
   212  	@$(RM) pkg/apiserver/ent $(WIN_IGNORE_ERR)
   213  	@$(RM) pkg/cwhub/hubdir $(WIN_IGNORE_ERR)
   214  	@$(RM) pkg/cwhub/install $(WIN_IGNORE_ERR)
   215  	@$(RM) pkg/types/example.txt $(WIN_IGNORE_ERR)
   216  
   217  # for the tests with localstack
   218  export AWS_ENDPOINT_FORCE=http://localhost:4566
   219  export AWS_ACCESS_KEY_ID=test
   220  export AWS_SECRET_ACCESS_KEY=test
   221  
   222  testenv:
   223  	@echo 'NOTE: You need Docker, docker-compose and run "make localstack" in a separate shell ("make localstack-stop" to terminate it)'
   224  
   225  .PHONY: test
   226  test: testenv goversion  ## Run unit tests with localstack
   227  	$(GOTEST) $(LD_OPTS) ./...
   228  
   229  .PHONY: go-acc
   230  go-acc: testenv goversion  ## Run unit tests with localstack + coverage
   231  	go-acc ./... -o coverage.out --ignore database,notifications,protobufs,cwversion,cstest,models -- $(LD_OPTS)
   232  
   233  # mock AWS services
   234  .PHONY: localstack
   235  localstack:  ## Run localstack containers (required for unit testing)
   236  	docker-compose -f test/localstack/docker-compose.yml up
   237  
   238  .PHONY: localstack-stop
   239  localstack-stop:  ## Stop localstack containers
   240  	docker-compose -f test/localstack/docker-compose.yml down
   241  
   242  # build vendor.tgz to be distributed with the release
   243  .PHONY: vendor
   244  vendor: vendor-remove  ## CI only - vendor dependencies and archive them for packaging
   245  	$(GO) mod vendor
   246  	tar czf vendor.tgz vendor
   247  	tar --create --auto-compress --file=$(RELDIR)-vendor.tar.xz vendor
   248  
   249  # remove vendor directories and vendor.tgz
   250  .PHONY: vendor-remove
   251  vendor-remove:  ## Remove vendor dependencies and archives
   252  	$(RM) vendor vendor.tgz *-vendor.tar.xz
   253  
   254  .PHONY: package
   255  package:
   256  	@echo "Building Release to dir $(RELDIR)"
   257  	@$(MKDIR) $(RELDIR)/cmd/crowdsec
   258  	@$(MKDIR) $(RELDIR)/cmd/crowdsec-cli
   259  	@$(CP) $(CROWDSEC_FOLDER)/$(CROWDSEC_BIN) $(RELDIR)/cmd/crowdsec
   260  	@$(CP) $(CSCLI_FOLDER)/$(CSCLI_BIN) $(RELDIR)/cmd/crowdsec-cli
   261  
   262  	@$(foreach plugin,$(PLUGINS), \
   263  		$(MKDIR) $(RELDIR)/$(PLUGINS_DIR_PREFIX)$(plugin); \
   264  		$(CP) $(PLUGINS_DIR_PREFIX)$(plugin)/notification-$(plugin)$(EXT) $(RELDIR)/$(PLUGINS_DIR_PREFIX)$(plugin); \
   265  		$(CP) $(PLUGINS_DIR_PREFIX)$(plugin)/$(plugin).yaml $(RELDIR)/$(PLUGINS_DIR_PREFIX)$(plugin)/; \
   266  	)
   267  
   268  	@$(CPR) ./config $(RELDIR)
   269  	@$(CP) wizard.sh $(RELDIR)
   270  	@$(CP) scripts/test_env.sh $(RELDIR)
   271  	@$(CP) scripts/test_env.ps1 $(RELDIR)
   272  
   273  	@tar cvzf crowdsec-release.tgz $(RELDIR)
   274  
   275  .PHONY: check_release
   276  check_release:
   277  ifneq ($(OS), Windows_NT)
   278  	@if [ -d $(RELDIR) ]; then echo "$(RELDIR) already exists, abort" ;  exit 1 ; fi
   279  else
   280  	@if (Test-Path -Path $(RELDIR)) { echo "$(RELDIR) already exists, abort" ;  exit 1 ; }
   281  endif
   282  
   283  .PHONY: release
   284  release: check_release build package  ## Build a release tarball
   285  
   286  .PHONY: windows_installer
   287  windows_installer: build  ## Windows - build the installer
   288  	@.\make_installer.ps1 -version $(BUILD_VERSION)
   289  
   290  .PHONY: chocolatey
   291  chocolatey: windows_installer  ## Windows - build the chocolatey package
   292  	@.\make_chocolatey.ps1 -version $(BUILD_VERSION)
   293  
   294  # Include test/bats.mk only if it exists
   295  # to allow building without a test/ directory
   296  # (i.e. inside docker)
   297  ifeq (,$(wildcard test/bats.mk))
   298  bats-clean:
   299  else
   300  include test/bats.mk
   301  endif
   302  
   303  include mk/goversion.mk
   304  include mk/help.mk