@@ -17,48 +17,60 @@ CRD_OPTIONS ?= crd:crdVersions=v1
1717
1818# Repository root based on Git metadata
1919REPOSITORY_ROOT := $(shell git rev-parse --show-toplevel)
20+ BUILD_DIR := $(REPOSITORY_ROOT ) /build
2021
2122# Other dependency versions
2223ENVTEST_BIN_VERSION ?= 1.19.2
2324
2425# Caches libgit2 versions per tag, "forcing" rebuild only when needed.
25- LIBGIT2_PATH := $(REPOSITORY_ROOT ) /build /libgit2/$(LIBGIT2_TAG )
26+ LIBGIT2_PATH := $(BUILD_DIR ) /libgit2/$(LIBGIT2_TAG )
2627LIBGIT2_LIB_PATH := $(LIBGIT2_PATH ) /lib
2728LIBGIT2_LIB64_PATH := $(LIBGIT2_PATH ) /lib64
2829LIBGIT2 := $(LIBGIT2_LIB_PATH ) /libgit2.a
2930MUSL-CC =
3031
3132export CGO_ENABLED =1
32- export PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH ) /pkgconfig : $( LIBGIT2_LIB64_PATH ) /pkgconfig
33- export LD_LIBRARY_PATH =$(LIBGIT2_LIB_PATH ) : $( LIBGIT2_LIB64_PATH )
33+ export PKG_CONFIG_PATH =$(LIBGIT2_LIB_PATH ) /pkgconfig
34+ export LIBRARY_PATH =$(LIBGIT2_LIB_PATH )
3435export CGO_CFLAGS =-I$(LIBGIT2_PATH ) /include -I$(LIBGIT2_PATH ) /include/openssl
3536
37+
3638ifeq ($(shell uname -s) ,Darwin)
37- export CGO_LDFLAGS=-L$(LIBGIT2_LIB_PATH) -lssh2 -lssl -lcrypto -lgit2
39+ export CGO_LDFLAGS =$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH ) pkg-config --libs --static --cflags libssh2 openssl libgit2)
40+ GO_STATIC_FLAGS =-ldflags "-s -w" -tags 'netgo,osusergo,static_build'
3841else
39- export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libssh2 openssl libgit2)
42+ export PKG_CONFIG_PATH: =$(PKG_CONFIG_PATH ) :$(LIBGIT2_LIB64_PATH ) /pkgconfig
43+ export LIBRARY_PATH: =$(LIBRARY_PATH ) :$(LIBGIT2_LIB64_PATH )
44+ export CGO_LDFLAGS =$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH ) pkg-config --libs --static --cflags libssh2 openssl libgit2)
4045endif
4146
4247
4348ifeq ($(shell uname -s) ,Linux)
44- MUSL-PREFIX=$(REPOSITORY_ROOT)/build/musl/$(shell uname -m)-linux-musl-native/bin/$(shell uname -m)-linux-musl
49+ ifeq ($(shell uname -m) ,x86_64)
50+ # Linux x86_64 seem to be able to cope with the static libraries
51+ # by having only musl-dev installed, without the need of using musl toolchain.
52+ GO_STATIC_FLAGS=-ldflags "-s -w" -tags 'netgo,osusergo,static_build'
53+ else
54+ MUSL-PREFIX=$(BUILD_DIR)/musl/$(shell uname -m)-linux-musl-native/bin/$(shell uname -m)-linux-musl
4555 MUSL-CC=$(MUSL-PREFIX)-gcc
4656 export CC=$(MUSL-PREFIX)-gcc
4757 export CXX=$(MUSL-PREFIX)-g++
4858 export AR=$(MUSL-PREFIX)-ar
59+ GO_STATIC_FLAGS=-ldflags "-s -w -extldflags \"-static\"" -tags 'netgo,osusergo,static_build'
60+ endif
4961endif
5062
5163# API (doc) generation utilities
5264CONTROLLER_GEN_VERSION ?= v0.7.0
5365GEN_API_REF_DOCS_VERSION ?= v0.3.0
5466
55- # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
67+ # If gobin not set, create one on ./build and add to path.
5668ifeq (,$(shell go env GOBIN) )
57- GOBIN =$(shell go env GOPATH) /bin
69+ export GOBIN =$(BUILD_DIR ) /gobin
5870else
59- GOBIN =$(shell go env GOBIN)
71+ export GOBIN =$(shell go env GOBIN)
6072endif
61-
73+ export PATH: =${GOBIN}:${PATH}
6274
6375# Architecture to use envtest with
6476ifeq ($(shell uname -m) ,x86_64)
6779ENVTEST_ARCH ?= arm64
6880endif
6981
82+ ifeq ($(shell uname -s) ,Darwin)
83+ # Envtest only supports darwin-amd64
84+ ENVTEST_ARCH =amd64
85+ endif
86+
7087all : build
7188
72- build : $(LIBGIT2 ) # # Build manager binary
73- go build -o bin/manager main.go
89+ build : check-deps $(LIBGIT2 ) # # Build manager binary
90+ go build $( GO_STATIC_FLAGS ) -o $( BUILD_DIR ) / bin/manager main.go
7491
7592KUBEBUILDER_ASSETS? ="$(shell $(ENVTEST ) --arch=$(ENVTEST_ARCH ) use -i $(ENVTEST_KUBERNETES_VERSION ) --bin-dir=$(ENVTEST_ASSETS_DIR ) -p path) "
76- test : $(LIBGIT2 ) install-envtest test-api # # Run tests
93+ test : $(LIBGIT2 ) install-envtest test-api check-deps # # Run tests
7794 KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS ) \
78- go test ./... \
79- -ldflags " -s -w" \
80- -coverprofile cover.out \
81- -tags ' netgo,osusergo,static_build'
95+ go test $(GO_STATIC_FLAGS ) ./... -coverprofile cover.out
96+
97+ check-deps :
98+ ifeq ($(shell uname -s) ,Darwin)
99+ if ! command -v pkg-config &> /dev/null; then echo "pkg-config is required"; exit 1; fi
100+ endif
82101
83102test-api : # # Run api tests
84103 cd api; go test ./... -coverprofile cover.out
85104
86105run : $(LIBGIT2 ) generate fmt vet manifests # # Run against the configured Kubernetes cluster in ~/.kube/config
87- go run ./main.go
106+ go run $( GO_STATIC_FLAGS ) ./main.go
88107
89108install : manifests # # Install CRDs into a cluster
90109 kustomize build config/crd | kubectl apply -f -
@@ -136,23 +155,23 @@ docker-push: ## Push Docker image
136155 docker push $(IMG ) :$(TAG )
137156
138157# Find or download controller-gen
139- CONTROLLER_GEN = $(shell pwd) /bin /controller-gen
158+ CONTROLLER_GEN = $(GOBIN ) /controller-gen
140159.PHONY : controller-gen
141160controller-gen : # # Download controller-gen locally if necessary.
142161 $(call go-install-tool,$(CONTROLLER_GEN ) ,sigs.k8s.io/controller-tools/cmd/controller-gen@v0.7.0)
143162
144163# Find or download gen-crd-api-reference-docs
145- GEN_CRD_API_REFERENCE_DOCS = $(shell pwd) /bin /gen-crd-api-reference-docs
164+ GEN_CRD_API_REFERENCE_DOCS = $(GOBIN ) /gen-crd-api-reference-docs
146165.PHONY : gen-crd-api-reference-docs
147166gen-crd-api-reference-docs : # # Download gen-crd-api-reference-docs locally if necessary
148167 $(call go-install-tool,$(GEN_CRD_API_REFERENCE_DOCS ) ,github.com/ahmetb/gen-crd-api-reference-docs@v0.3.0)
149168
150- ENVTEST = $(shell pwd) /bin /setup-envtest
169+ ENVTEST = $(GOBIN ) /setup-envtest
151170.PHONY : envtest
152171setup-envtest : # # Download setup-envtest locally if necessary.
153172 $(call go-install-tool,$(ENVTEST ) ,sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
154173
155- ENVTEST_ASSETS_DIR =$(shell pwd ) /testbin
174+ ENVTEST_ASSETS_DIR =$(BUILD_DIR ) /testbin
156175ENVTEST_KUBERNETES_VERSION? =latest
157176install-envtest : setup-envtest # # Download envtest binaries locally.
158177 mkdir -p ${ENVTEST_ASSETS_DIR}
@@ -188,15 +207,14 @@ ifneq (, $(shell git status --porcelain --untracked-files=no))
188207endif
189208
190209# go-install-tool will 'go install' any package $2 and install it to $1.
191- PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST ) ) ) )
192210define go-install-tool
193211@[ -f $(1 ) ] || { \
194212set -e ;\
195213TMP_DIR=$$(mktemp -d ) ;\
196214cd $$TMP_DIR ;\
197215go mod init tmp ;\
198216echo "Downloading $(2 ) " ;\
199- GOBIN= $( PROJECT_DIR ) /bin go install $(2 ) ;\
217+ go install $(2 ) ;\
200218rm -rf $$TMP_DIR ;\
201219}
202220endef
0 commit comments