From f70615900c7c71ecd0e2cee02e75f81485dd1da7 Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Tue, 7 Dec 2021 01:10:43 +0530 Subject: [PATCH] fix potentially broken support for macos macOS support is broken for users who rely on the Makefile to install libgit2 for them. libgit2.1.1.dylib could not be dynamically linked at runtime because it couldn't be found. This patch makes the following changes to the Makefile: 1) Respects the user's PKG_CONFIG_PATH present in the env so that both libgit2.pc and openssl.pc are discoverable. 2) Embeds the required rpath in the binary at compile time, so that libgit2.1.1.dylib can be found at runtime. For more info see: https://github.com/fluxcd/source-controller/pull/515#discussion_r764245029 Signed-off-by: Sanskar Jaiswal Signed-off-by: pa250194 --- Makefile | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index fb4d99981..3053b281e 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,12 @@ endif ifeq ($(shell uname -s),Darwin) LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.$(LIBGIT2_VERSION).dylib + HAS_BREW := $(shell brew --version 2>/dev/null) +ifdef HAS_BREW + HAS_OPENSSL := $(shell brew --prefix openssl@1.1) endif +endif + # API (doc) generation utilities CONTROLLER_GEN_VERSION ?= v0.5.0 @@ -52,23 +57,53 @@ else GOBIN=$(shell go env GOBIN) endif +ifeq ($(strip ${PKG_CONFIG_PATH}),) + MAKE_PKG_CONFIG_PATH = $(LIBGIT2_LIB_PATH)/pkgconfig +else + MAKE_PKG_CONFIG_PATH = ${PKG_CONFIG_PATH}:$(LIBGIT2_LIB_PATH)/pkgconfig +endif + +ifdef HAS_OPENSSL + MAKE_PKG_CONFIG_PATH := $(MAKE_PKG_CONFIG_PATH):$(HAS_OPENSSL)/lib/pkgconfig +endif + all: build build: $(LIBGIT2) ## Build manager binary - PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig/ \ +ifeq ($(shell uname -s),Darwin) + PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \ + CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \ + go build -o bin/manager main.go +else + PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \ go build -o bin/manager main.go +endif test: $(LIBGIT2) test-api ## Run tests +ifeq ($(shell uname -s),Darwin) LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \ - PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig/ \ + PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \ + CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \ go test ./... -coverprofile cover.out +else + LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \ + PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \ + go test ./... -coverprofile cover.out +endif test-api: ## Run api tests cd api; go test ./... -coverprofile cover.out run: $(LIBGIT2) generate fmt vet manifests ## Run against the configured Kubernetes cluster in ~/.kube/config +ifeq ($(shell uname -s),Darwin) LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \ + CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \ go run ./main.go +else + LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \ + go run ./main.go +endif + install: manifests ## Install CRDs into a cluster kustomize build config/crd | kubectl apply -f - @@ -102,9 +137,16 @@ fmt: ## Run go fmt against code cd api; go fmt ./... vet: $(LIBGIT2) ## Run go vet against code - PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig \ +ifeq ($(shell uname -s),Darwin) + PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \ + CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \ + go vet ./... + cd api; go vet ./... +else + PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \ go vet ./... cd api; go vet ./... +endif generate: controller-gen ## Generate API code cd api; $(CONTROLLER_GEN) object:headerFile="../hack/boilerplate.go.txt" paths="./..."