Skip to content

Commit 60c98b5

Browse files
author
Paulo Gomes
committed
Update libgit2 to 1.1.1-6
Fix issues developing in amd64, arm64 and apple silicon Signed-off-by: Paulo Gomes <[email protected]>
1 parent 6292821 commit 60c98b5

File tree

5 files changed

+80
-49
lines changed

5 files changed

+80
-49
lines changed

.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,5 @@ bin/
1717
testbin/
1818
config/release/
1919

20-
# Exclude all libgit2 related files
21-
hack/libgit2/
22-
2320
# Exclude temporary build files
2421
build/

Dockerfile

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ ENV CGO_ENABLED=1
8484
RUN export $(cat build/musl/$(xx-info alpine-arch).env | xargs) && \
8585
export LIBRARY_PATH="/usr/local/$(xx-info triple):/usr/local/$(xx-info triple)/lib64" && \
8686
export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \
87-
export FLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2)" && \
88-
export CGO_LDFLAGS="${FLAGS} -static" && \
87+
export CGO_LDFLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2) -static" && \
8988
GOARCH=$TARGETARCH go build \
9089
-ldflags "-s -w" \
9190
-tags 'netgo,osusergo,static_build' \

Makefile

+42-24
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,60 @@ CRD_OPTIONS ?= crd:crdVersions=v1
1717

1818
# Repository root based on Git metadata
1919
REPOSITORY_ROOT := $(shell git rev-parse --show-toplevel)
20+
BUILD_DIR := $(REPOSITORY_ROOT)/build
2021

2122
# Other dependency versions
2223
ENVTEST_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)
2627
LIBGIT2_LIB_PATH := $(LIBGIT2_PATH)/lib
2728
LIBGIT2_LIB64_PATH := $(LIBGIT2_PATH)/lib64
2829
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.a
2930
MUSL-CC =
3031

3132
export 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)
3435
export CGO_CFLAGS=-I$(LIBGIT2_PATH)/include -I$(LIBGIT2_PATH)/include/openssl
3536

37+
3638
ifeq ($(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'
3841
else
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)
4045
endif
4146

4247

4348
ifeq ($(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
4961
endif
5062

5163
# API (doc) generation utilities
5264
CONTROLLER_GEN_VERSION ?= v0.7.0
5365
GEN_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.
5668
ifeq (,$(shell go env GOBIN))
57-
GOBIN=$(shell go env GOPATH)/bin
69+
export GOBIN=$(BUILD_DIR)/gobin
5870
else
59-
GOBIN=$(shell go env GOBIN)
71+
export GOBIN=$(shell go env GOBIN)
6072
endif
61-
73+
export PATH:=${GOBIN}:${PATH}
6274

6375
# Architecture to use envtest with
6476
ifeq ($(shell uname -m),x86_64)
@@ -67,24 +79,31 @@ else
6779
ENVTEST_ARCH ?= arm64
6880
endif
6981

82+
ifeq ($(shell uname -s),Darwin)
83+
# Envtest only supports darwin-amd64
84+
ENVTEST_ARCH=amd64
85+
endif
86+
7087
all: 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

7592
KUBEBUILDER_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

83102
test-api: ## Run api tests
84103
cd api; go test ./... -coverprofile cover.out
85104

86105
run: $(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

89108
install: 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
141160
controller-gen: ## Download controller-gen locally if necessary.
142161
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
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
147166
gen-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/[email protected])
149168

150-
ENVTEST = $(shell pwd)/bin/setup-envtest
169+
ENVTEST = $(GOBIN)/setup-envtest
151170
.PHONY: envtest
152171
setup-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
156175
ENVTEST_KUBERNETES_VERSION?=latest
157176
install-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))
188207
endif
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))))
192210
define go-install-tool
193211
@[ -f $(1) ] || { \
194212
set -e ;\
195213
TMP_DIR=$$(mktemp -d) ;\
196214
cd $$TMP_DIR ;\
197215
go mod init tmp ;\
198216
echo "Downloading $(2)" ;\
199-
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
217+
go install $(2) ;\
200218
rm -rf $$TMP_DIR ;\
201219
}
202220
endef

hack/download-musl.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ MUSL_DIR="${ROOT_DIR}/build/musl"
2626
if [ "${TARGET_ARCH}" = "$(uname -m)" ]; then
2727
MUSL_FILENAME="${MUSL_X86_64_FILENAME}"
2828
MUSL_SHA512="${MUSL_X86_64_SHA512}"
29-
MUSL_PREFIX=$(xx-info alpine-arch)-linux-musl-native/bin/$(xx-info alpine-arch)-linux-musl
29+
MUSL_PREFIX="${TARGET_ARCH}-linux-musl-native/bin/${TARGET_ARCH}-linux-musl"
3030
if [ "${TARGET_ARCH}" = "arm64" ] || [ "${TARGET_ARCH}" = "aarch64" ]; then
3131
MUSL_FILENAME="${MUSL_AARCH64_FILENAME}"
3232
MUSL_SHA512="${MUSL_AARCH64_SHA512}"
3333
fi
3434
else
3535
MUSL_FILENAME="${MUSL_XX86_64_FILENAME}"
3636
MUSL_SHA512="${MUSL_XX86_64_SHA512}"
37-
MUSL_PREFIX=$(xx-info alpine-arch)-linux-musl-cross/bin/$(xx-info alpine-arch)-linux-musl
37+
MUSL_PREFIX="${TARGET_ARCH}-linux-musl-cross/bin/${TARGET_ARCH}-linux-musl"
3838
if [ "${TARGET_ARCH}" = "arm64" ] || [ "${TARGET_ARCH}" = "aarch64" ]; then
3939
MUSL_FILENAME="${MUSL_XAARCH64_FILENAME}"
4040
MUSL_SHA512="${MUSL_XAARCH64_SHA512}"

hack/install-libraries.sh

+35-18
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,13 @@ function setup() {
2727
NEW_DIR="$(/bin/pwd)/build/libgit2/${TAG}"
2828
INSTALLED_DIR="/usr/local/${DIR}"
2929

30-
mkdir -p "./build/libgit2"
31-
3230
mv "local/${DIR}" "${TAG}"
3331
rm -rf "local"
3432
mv "${TAG}/" "./build/libgit2"
3533

3634
# Update the prefix paths included in the .pc files.
3735
# This will make it easier to update to the location in which they will be used.
38-
if [[ $OSTYPE == 'darwin'* ]]; then
39-
# sed has a sight different behaviour in MacOS
40-
find "${NEW_DIR}" -type f -name "*.pc" | xargs -I {} sed -i "" "s;${INSTALLED_DIR};${NEW_DIR};g" {}
41-
else
42-
find "${NEW_DIR}" -type f -name "*.pc" | xargs -I {} sed -i "s;${INSTALLED_DIR};${NEW_DIR};g" {}
43-
fi
36+
find "${NEW_DIR}" -type f -name "*.pc" | xargs -I {} sed -i "s;${INSTALLED_DIR};${NEW_DIR};g" {}
4437
}
4538

4639
function setup_current() {
@@ -49,18 +42,42 @@ function setup_current() {
4942
exit 0
5043
fi
5144

52-
DIR="x86_64-alpine-linux-musl"
53-
PLATFORM="linux/amd64"
45+
mkdir -p "./build/libgit2"
46+
if [[ $OSTYPE == 'darwin'* ]]; then
47+
# For MacOS development environments, download the amd64 static libraries released from from golang-with-libgit2.
48+
49+
#TODO: update URL with official URL + TAG:
50+
curl -o output.tar.gz -LO "https://github.com/pjbgf/golang-with-libgit2/releases/download/1.1.1-6/darwin-libs.tar.gz"
51+
52+
DIR=libgit2-darwin
53+
NEW_DIR="$(/bin/pwd)/build/libgit2/${TAG}"
54+
INSTALLED_DIR="/Users/runner/work/golang-with-libgit2/golang-with-libgit2/build/${DIR}-amd64"
55+
56+
tar -xf output.tar.gz
57+
mv "${DIR}" "${TAG}"
58+
mv "${TAG}/" "./build/libgit2"
59+
60+
sed -i "" "s;-L/Applications/Xcode_12.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/lib ;;g" "$(/bin/pwd)/build/libgit2/${TAG}/lib/pkgconfig/libgit2.pc"
61+
62+
# Update the prefix paths included in the .pc files.
63+
# This will make it easier to update to the location in which they will be used.
64+
# sed has a sight different behaviour in MacOS
65+
find "${NEW_DIR}" -type f -name "*.pc" | xargs -I {} sed -i "" "s;${INSTALLED_DIR};${NEW_DIR};g" {}
66+
else
67+
# for linux development environments, use the static libraries from the official container images.
68+
DIR="x86_64-alpine-linux-musl"
69+
PLATFORM="linux/amd64"
5470

55-
if [[ "$(uname -m)" == armv7* ]]; then
56-
DIR="armv7-alpine-linux-musleabihf"
57-
PLATFORM="linux/arm/v7"
58-
elif [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ]; then
59-
DIR="aarch64-alpine-linux-musl"
60-
PLATFORM="linux/arm64"
71+
if [[ "$(uname -m)" == armv7* ]]; then
72+
DIR="armv7-alpine-linux-musleabihf"
73+
PLATFORM="linux/arm/v7"
74+
elif [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ]; then
75+
DIR="aarch64-alpine-linux-musl"
76+
PLATFORM="linux/arm64"
77+
fi
78+
79+
setup "${PLATFORM}" "${DIR}"
6180
fi
62-
63-
setup "${PLATFORM}" "${DIR}"
6481
}
6582

6683
setup_current

0 commit comments

Comments
 (0)