Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit 94c6239

Browse files
author
Paulo Gomes
committed
Add smoke tests for development environment
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
1 parent f79adb7 commit 94c6239

File tree

5 files changed

+196
-107
lines changed

5 files changed

+196
-107
lines changed

Dockerfile.test

Lines changed: 10 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,28 @@
1-
# This Dockerfile tests the hack/Makefile output against git2go.
21
ARG BASE_VARIANT=alpine
3-
ARG GO_VERSION=1.17.6
4-
ARG XX_VERSION=1.1.0
52

6-
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
3+
ARG LIBGIT2_IMG
4+
ARG LIBGIT2_TAG
75

8-
FROM golang:${GO_VERSION}-${BASE_VARIANT} as gostable
6+
FROM --platform=linux/amd64 ${LIBGIT2_IMG}:${LIBGIT2_TAG} as build-amd64
7+
FROM --platform=linux/arm64 ${LIBGIT2_IMG}:${LIBGIT2_TAG} as build-arm64
8+
FROM --platform=linux/arm/v7 ${LIBGIT2_IMG}:${LIBGIT2_TAG} as build-armv7
99

10-
FROM gostable AS go-linux
10+
FROM --platform=$BUILDPLATFORM build-$TARGETARCH$TARGETVARIANT AS build
1111

12-
FROM --platform=$BUILDPLATFORM ${BASE_VARIANT} AS build-deps
13-
14-
RUN apk add --no-cache \
15-
bash \
16-
curl \
17-
build-base \
18-
linux-headers \
19-
perl \
20-
cmake \
21-
pkgconfig \
22-
gcc \
23-
musl-dev \
24-
clang \
25-
lld
26-
27-
COPY --from=xx / /
12+
RUN apk add clang lld pkgconfig ca-certificates
2813

2914
ARG TARGETPLATFORM
3015

16+
# Some dependencies have to installed
17+
# for the target platform: https://github.com/tonistiigi/xx#go--cgo
3118
RUN xx-apk add --no-cache \
32-
xx-c-essentials
33-
34-
RUN xx-apk add --no-cache \
35-
xx-cxx-essentials
36-
37-
ARG TARGETPLATFORM
38-
RUN xx-apk add --no-cache \
39-
build-base \
40-
pkgconfig \
41-
gcc \
42-
musl-dev \
43-
clang \
44-
lld \
45-
llvm \
46-
linux-headers
47-
48-
WORKDIR /build
49-
COPY hack/static.sh .
50-
51-
ARG TARGETPLATFORM
52-
ENV CC=xx-clang
53-
ENV CXX=xx-clang++
54-
55-
RUN CHOST=$(xx-clang --print-target-triple) \
56-
./static.sh build_libz
57-
58-
RUN CHOST=$(xx-clang --print-target-triple) \
59-
./static.sh build_openssl
60-
61-
RUN export LIBRARY_PATH="/usr/local/$(xx-info triple)/lib:/usr/local/$(xx-info triple)/lib64:${LIBRARY_PATH}" && \
62-
export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \
63-
export OPENSSL_ROOT_DIR="/usr/local/$(xx-info triple)" && \
64-
export OPENSSL_CRYPTO_LIBRARY="/usr/local/$(xx-info triple)/lib64" && \
65-
export OPENSSL_INCLUDE_DIR="/usr/local/$(xx-info triple)/include/openssl"
66-
67-
RUN ./static.sh build_libssh2
68-
RUN ./static.sh build_libgit2
69-
70-
71-
FROM go-${TARGETOS} AS build
72-
73-
# Copy cross-compilation tools
74-
COPY --from=xx / /
75-
# Copy compiled libraries
76-
COPY --from=build-deps /usr/local/ /usr/local/
77-
78-
RUN apk add clang lld pkgconfig
19+
musl-dev gcc lld binutils-gold
7920

8021
WORKDIR /root/smoketest
8122
COPY tests/smoketest/go.mod .
8223
COPY tests/smoketest/go.sum .
8324
RUN go mod download
8425

85-
ARG TARGETPLATFORM
86-
87-
# Some dependencies have to installed
88-
# for the target platform: https://github.com/tonistiigi/xx#go--cgo
89-
RUN xx-apk add --no-cache \
90-
musl-dev \
91-
gcc
92-
93-
9426
COPY tests/smoketest/main.go .
9527

9628
ENV CGO_ENABLED=1
@@ -119,11 +51,4 @@ COPY --from=build \
11951

12052
ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
12153

122-
# To do docker run instead, replace the RUN statement with:
123-
# ENTRYPOINT [ "/root/smoketest/static-test-runner" ]
124-
125-
# The approach below was preferred as it provides a way to
126-
# assert the functionality across the supported architectures
127-
# without any extra steps.
128-
12954
RUN /root/smoketest/static-test-runner

Makefile

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
1-
IMG ?= hiddeco/golang-with-libgit2
1+
IMG ?= ghcr.io/fluxcd/golang-with-libgit2
22
TAG ?= latest
3-
STATIC_TEST_TAG := test
43

54
PLATFORMS ?= linux/amd64,linux/arm/v7,linux/arm64
65
BUILD_ARGS ?=
76

7+
REPOSITORY_ROOT := $(shell git rev-parse --show-toplevel)
8+
TARGET_DIR ?= $(REPOSITORY_ROOT)/build/libgit2
9+
BUILD_ROOT_DIR ?= $(REPOSITORY_ROOT)/build/libgit2-src
10+
11+
LIBGIT2_PATH := $(TARGET_DIR)
12+
LIBGIT2_LIB_PATH := $(LIBGIT2_PATH)/lib
13+
LIBGIT2_LIB64_PATH := $(LIBGIT2_PATH)/lib64
14+
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.a
15+
16+
export CGO_ENABLED=1
17+
export LIBRARY_PATH=$(LIBGIT2_LIB_PATH):$(LIBGIT2_LIB64_PATH)
18+
export PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig:$(LIBGIT2_LIB64_PATH)/pkgconfig
19+
export CGO_CFLAGS=-I$(LIBGIT2_PATH)/include
20+
21+
22+
ifeq ($(shell uname -s),Linux)
23+
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libssh2 openssl libgit2) -static
24+
else
25+
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libssh2 openssl libgit2) -Wl,--unresolved-symbols=ignore-in-object-files -Wl,-allow-shlib-undefined -static
26+
endif
27+
28+
ifeq ($(shell uname -s),Linux)
29+
ifneq ($(shell uname -m),x86_64)
30+
export CC=$(shell uname -m)-linux-musl-gcc
31+
export CXX=$(shell uname -m)-linux-musl-g++
32+
export AR=$(shell uname -m)-linux-musl-ar
33+
endif
34+
endif
35+
36+
GO_STATIC_FLAGS=-tags 'netgo,osusergo,static_build'
37+
38+
839
.PHONY: build
940
build:
1041
docker buildx build \
@@ -18,6 +49,8 @@ test:
1849
docker buildx build \
1950
--platform=$(PLATFORMS) \
2051
--tag $(IMG):$(TAG) \
52+
--build-arg LIBGIT2_IMG=$(IMG) \
53+
--build-arg LIBGIT2_TAG=$(TAG) \
2154
--file Dockerfile.test \
2255
$(BUILD_ARGS) .
2356

@@ -33,3 +66,17 @@ builder:
3366
--use
3467
# install qemu emulators
3568
docker run -it --rm --privileged tonistiigi/binfmt --install all
69+
70+
$(LIBGIT2):
71+
ifeq ($(shell uname -s),Darwin)
72+
TARGET_DIR=$(TARGET_DIR) BUILD_ROOT_DIR=$(BUILD_ROOT_DIR) \
73+
./hack/static.sh all
74+
else
75+
IMG_TAG=$(IMG):$(TAG) ./hack/extract-libraries.sh
76+
endif
77+
78+
# dev-test is a smoke test for development environment
79+
# consuming the libraries generated by this project.
80+
dev-test: $(LIBGIT2)
81+
cd tests/smoketest; go vet $(GO_STATIC_FLAGS) ./...
82+
cd tests/smoketest; go run $(GO_STATIC_FLAGS) main.go

hack/extract-libraries.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
3+
set -euxo pipefail
4+
5+
IMG_TAG="${IMG_TAG:-.}"
6+
7+
function extract(){
8+
PLATFORM=$1
9+
DIR=$2
10+
11+
id=$(docker create --platform="${PLATFORM}" "${IMG_TAG}")
12+
docker cp "${id}":/usr/local - > output.tar.gz
13+
docker rm -v "${id}"
14+
15+
tar -xf output.tar.gz "local/${DIR}"
16+
rm output.tar.gz
17+
}
18+
19+
function setup() {
20+
PLATFORM=$1
21+
DIR=$2
22+
23+
extract "${PLATFORM}" "${DIR}"
24+
25+
NEW_DIR="$(/bin/pwd)/build/libgit2"
26+
INSTALLED_DIR="/usr/local/${DIR}"
27+
28+
mkdir -p "./build"
29+
30+
mv "local/${DIR}/" "libgit2"
31+
rm -rf "local"
32+
mv "libgit2/" "./build/"
33+
34+
# Update the prefix paths included in the .pc files.
35+
# This will make it easier to update to the location in which they will be used.
36+
if [[ $OSTYPE == 'darwin'* ]]; then
37+
# sed has a sight different behaviour in MacOS
38+
find "${NEW_DIR}" -type f -name "*.pc" | xargs -I {} sed -i "" "s;${INSTALLED_DIR};${NEW_DIR};g" {}
39+
else
40+
find "${NEW_DIR}" -type f -name "*.pc" | xargs -I {} sed -i "s;${INSTALLED_DIR};${NEW_DIR};g" {}
41+
fi
42+
}
43+
44+
function setup_current() {
45+
if [ -d "./build/libgit2" ]; then
46+
echo "Skipping libgit2 setup as it already exists"
47+
exit 0
48+
fi
49+
50+
DIR="x86_64-alpine-linux-musl"
51+
PLATFORM="linux/amd64"
52+
53+
if [[ "$(uname -m)" == armv7* ]]; then
54+
DIR="armv7-alpine-linux-musleabihf"
55+
PLATFORM="linux/arm/v7"
56+
elif [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ]; then
57+
DIR="aarch64-alpine-linux-musl"
58+
PLATFORM="linux/arm64"
59+
fi
60+
61+
setup "${PLATFORM}" "${DIR}"
62+
}
63+
64+
setup_current

hack/static.sh

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ TARGET_DIR="${TARGET_DIR:-/usr/local/$(xx-info triple)}"
1414
BUILD_ROOT_DIR="${BUILD_ROOT_DIR:-/build}"
1515
SRC_DIR="${BUILD_ROOT_DIR}/src"
1616

17-
1817
TARGET_ARCH="$(uname -m)"
1918
if command -v xx-info; then
2019
TARGET_ARCH="$(xx-info march)"
2120
fi
2221

23-
C_COMPILER="/usr/bin/gcc"
22+
C_COMPILER="${CC:-/usr/bin/gcc}"
2423
CMAKE_PARAMS=""
2524
if command -v xx-clang; then
2625
C_COMPILER="/usr/bin/xx-clang"
@@ -40,6 +39,7 @@ function build_libz(){
4039
download_source "${LIBZ_URL}" "${SRC_DIR}/libz"
4140
pushd "${SRC_DIR}/libz"
4241

42+
# if target architecture is the same as current, no cross compiling is required
4343
if [ "${TARGET_ARCH}" = "$(uname -m)" ]; then
4444
./configure --static --prefix="${TARGET_DIR}"
4545
else
@@ -56,14 +56,24 @@ function build_openssl(){
5656
download_source "${OPENSSL_URL}" "${SRC_DIR}/openssl"
5757
pushd "${SRC_DIR}/openssl"
5858

59-
target_name="${TARGET_ARCH}"
60-
if [ "${target_name}" = "armv7l" ]; then
59+
export OPENSSL_ROOT_DIR="${TARGET_DIR}"
60+
export OPENSSL_LIBRARIES="${TARGET_DIR}/lib"
61+
62+
target_arch=""
63+
if [ "${TARGET_ARCH}" = "armv7l" ]; then
6164
# openssl does not have a specific armv7l
6265
# using generic32 instead.
63-
target_name=generic32
66+
target_arch="linux-generic32"
67+
elif [ "${TARGET_ARCH}" = "arm64" ] || [ "${TARGET_ARCH}" = "aarch64" ]; then
68+
target_arch="linux-aarch64"
69+
elif [ "${TARGET_ARCH}" = "x86_64" ]; then
70+
target_arch="linux-x86_64"
71+
else
72+
echo "Architecture currently not supported: ${TARGET_ARCH}"
73+
exit 1
6474
fi
6575

66-
./Configure "linux-${target_name}" threads no-shared zlib -fPIC -DOPENSSL_PIC \
76+
./Configure "${target_arch}" threads no-shared zlib -fPIC -DOPENSSL_PIC \
6777
--prefix="${TARGET_DIR}" \
6878
--with-zlib-include="${TARGET_DIR}/include" \
6979
--with-zlib-lib="${TARGET_DIR}/lib" \
@@ -83,14 +93,23 @@ function build_libssh2(){
8393
mkdir -p build
8494
pushd build
8595

96+
OPENSSL_LIBRARIES="${TARGET_DIR}/lib"
97+
if [ "${TARGET_ARCH}" = "x86_64" ]; then
98+
OPENSSL_LIBRARIES="${TARGET_DIR}/lib64"
99+
fi
100+
101+
86102
cmake "${CMAKE_PARAMS}" \
87103
-DCMAKE_C_COMPILER="${C_COMPILER}" \
88104
-DCMAKE_INSTALL_PREFIX="${TARGET_DIR}" \
89105
-DBUILD_SHARED_LIBS=OFF \
106+
-DLINT=OFF \
90107
-DCMAKE_C_FLAGS=-fPIC \
91108
-DCRYPTO_BACKEND=OpenSSL \
92109
-DENABLE_ZLIB_COMPRESSION=ON \
93110
-DCMAKE_BUILD_TYPE="RelWithDebInfo" \
111+
-DOPENSSL_CRYPTO_LIBRARY="${OPENSSL_LIBRARIES}/libcrypto.a" \
112+
-DOPENSSL_SSL_LIBRARY="${OPENSSL_LIBRARIES}/libssl.a" \
94113
..
95114

96115
cmake --build . --target install
@@ -108,11 +127,19 @@ function build_libgit2(){
108127

109128
pushd build
110129

130+
SSL_LIBRARY="${TARGET_DIR}/lib/libssl.a"
131+
CRYPTO_LIBRARY="${TARGET_DIR}/lib/libcrypto.a"
132+
if [ "${TARGET_ARCH}" = "x86_64" ]; then
133+
SSL_LIBRARY="${TARGET_DIR}/lib64/libssl.a"
134+
CRYPTO_LIBRARY="${TARGET_DIR}/lib64/libcrypto.a"
135+
fi
136+
111137
cmake "${CMAKE_PARAMS}" \
112138
-DCMAKE_C_COMPILER="${C_COMPILER}" \
113139
-DCMAKE_INSTALL_PREFIX="${TARGET_DIR}" \
114140
-DTHREADSAFE:BOOL=ON \
115-
-DBUILD_CLAR:BOOL:BOOL=OFF \
141+
-DBUILD_CLAR:BOOL=OFF \
142+
-DBUILD_TESTS:BOOL=OFF \
116143
-DBUILD_SHARED_LIBS=OFF \
117144
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON \
118145
-DCMAKE_C_FLAGS=-fPIC \
@@ -122,6 +149,9 @@ function build_libgit2(){
122149
-DUSE_BUNDLED_ZLIB:BOOL=ON \
123150
-DUSE_HTTPS:STRING=OpenSSL \
124151
-DREGEX_BACKEND:STRING=builtin \
152+
-DOPENSSL_SSL_LIBRARY="${SSL_LIBRARY}" \
153+
-DOPENSSL_CRYPTO_LIBRARY="${CRYPTO_LIBRARY}" \
154+
-DZLIB_LIBRARY="${TARGET_DIR}/lib/libz.a" \
125155
-DCMAKE_INCLUDE_PATH="${TARGET_DIR}/include" \
126156
-DCMAKE_LIBRARY_PATH="${TARGET_DIR}/lib" \
127157
-DCMAKE_BUILD_TYPE="RelWithDebInfo" \

0 commit comments

Comments
 (0)