Skip to content

Commit 370f98e

Browse files
committed
Build libgit2 using golang-with-libgit2 helper
- Detection of the system version of `libgit2` is attempted using `pkg-config`. If this fails, or does not match the version as defined in the `Makefile` (or configured using `LIBGIT2_VER=1.x.y`), the library is compiled using the instructions from the `BASE_IMG`, and installed to `REPOSITORY_ROOT/hack/libgit2`. - Where `libgit2` is required as a dependency, `LD_LIBRARY_PATH` and/or `PKG_CONFIG_PATH` instructions are added as a prefix to the command. This works for system libraries as well, because the paths are ignored if they do not exist. Signed-off-by: Hidde Beydals <[email protected]>
1 parent a959ee7 commit 370f98e

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

Makefile

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ IMG ?= fluxcd/image-automation-controller:latest
33
# Produce CRDs that work back to Kubernetes 1.16
44
CRD_OPTIONS ?= crd:crdVersions=v1
55

6+
# Base image used to build the Go binary
7+
BASE_IMG ?= ghcr.io/hiddeco/golang-with-libgit2
8+
BASE_TAG ?= dev
9+
610
# Directory with versioned, downloaded things
7-
CACHE:=cache
11+
CACHE := cache
812

913
# Version of the source-controller from which to get the GitRepository CRD.
1014
# Change this if you bump the source-controller/api version in go.mod.
@@ -14,17 +18,31 @@ SOURCE_VER ?= v0.15.4
1418
# Change this if you bump the image-reflector-controller/api version in go.mod.
1519
REFLECTOR_VER ?= v0.11.1
1620

21+
# Version of libgit2 the controller should depend on.
22+
LIBGIT2_VER ?= 1.1.1
23+
24+
# Repository root based on Git metadata.
25+
REPOSITORY_ROOT := $(shell git rev-parse --show-toplevel)
26+
27+
# libgit2 related magical paths.
28+
# These are used to determine if the target libgit2 version is already available on
29+
# the system, or where they should be installed to.
30+
SYSTEM_LIBGIT2_VER := $(shell pkg-config --modversion libgit2 2>/dev/null)
31+
LIBGIT2_PATH := $(REPOSITORY_ROOT)/hack/libgit2
32+
LIBGIT2_LIB_PATH := $(LIBGIT2_PATH)/lib
33+
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.so.$(LIBGIT2_VER)
34+
1735
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
1836
ifeq (,$(shell go env GOBIN))
1937
GOBIN=$(shell go env GOPATH)/bin
2038
else
2139
GOBIN=$(shell go env GOBIN)
2240
endif
2341

24-
TEST_CRDS:=controllers/testdata/crds
42+
TEST_CRDS := controllers/testdata/crds
2543

2644
# Log level for `make run`
27-
LOG_LEVEL?=info
45+
LOG_LEVEL ?= info
2846

2947
all: manager
3048

@@ -52,14 +70,19 @@ ${CACHE}/imagepolicies_${REFLECTOR_VER}.yaml:
5270
curl -s --fail https://raw.githubusercontent.com/fluxcd/image-reflector-controller/${REFLECTOR_VER}/config/crd/bases/image.toolkit.fluxcd.io_imagepolicies.yaml \
5371
-o ${CACHE}/imagepolicies_${REFLECTOR_VER}.yaml
5472

55-
test: test_deps generate fmt vet manifests api-docs ## Run tests
73+
test: $(LIBGIT2) test-api test_deps generate fmt vet manifests api-docs ## Run tests
74+
LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
75+
PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig/ \
5676
go test ./... -coverprofile cover.out
77+
78+
test-api: ## Run api tests
5779
cd api; go test ./... -coverprofile cover.out
5880

59-
manager: generate fmt vet ## Build manager binary
81+
manager: $(LIBGIT2) generate fmt vet ## Build manager binary
82+
PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig/ \
6083
go build -o bin/manager main.go
6184

62-
run: generate fmt vet manifests # Run against the configured Kubernetes cluster in ~/.kube/config
85+
run: $(LIBGIT2) generate fmt vet manifests # Run against the configured Kubernetes cluster in ~/.kube/config
6386
go run ./main.go --log-level=${LOG_LEVEL} --log-encoding=console
6487

6588
install: manifests ## Install CRDs into a cluster
@@ -92,11 +115,11 @@ fmt: ## Run go fmt against code
92115
go fmt ./...
93116
cd api; go fmt ./...
94117

95-
vet: ## Run go vet against code
118+
vet: $(LIBGIT2) ## Run go vet against code
119+
PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig \
96120
go vet ./...
97121
cd api; go vet ./...
98122

99-
100123
generate: controller-gen ## Generate code
101124
cd api; $(CONTROLLER_GEN) object:headerFile="../hack/boilerplate.go.txt" paths="./..."
102125

@@ -139,6 +162,19 @@ else
139162
API_REF_GEN=$(shell which gen-crd-api-reference-docs)
140163
endif
141164

165+
libgit2: $(LIBGIT2) ## Detect or download libgit2 library
166+
167+
$(LIBGIT2):
168+
ifeq ($(LIBGIT2_VER),$(SYSTEM_LIBGIT2_VER))
169+
else
170+
@{ \
171+
set -e; \
172+
mkdir -p $(LIBGIT2_PATH); \
173+
docker cp $(shell docker create --rm $(BASE_IMG):$(BASE_TAG)):/libgit2/Makefile $(LIBGIT2_PATH); \
174+
INSTALL_PREFIX=$(LIBGIT2_PATH) LIGBIT2_VERSION=$(LIBGIT2_VER) LIBGIT2_REVISION= make -C $(LIBGIT2_PATH); \
175+
}
176+
endif
177+
142178
.PHONY: help
143179
help: ## Display this help menu
144180
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

0 commit comments

Comments
 (0)