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

Commit 17b6b59

Browse files
authored
Merge pull request #14 from pjbgf/testdarwin
Generate MacOS universal static libraries
2 parents a05dea1 + d44e3c6 commit 17b6b59

File tree

6 files changed

+226
-52
lines changed

6 files changed

+226
-52
lines changed

.github/workflows/build.yaml

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,99 @@ on:
44
push:
55
branches:
66
- main
7+
- testdarwin2
78
tags:
89
- '**'
9-
paths-ignore:
10-
- README.md
11-
pull_request:
12-
paths-ignore:
13-
- README.md
10+
# paths-ignore:
11+
# - README.md
12+
# pull_request:
13+
# paths-ignore:
14+
# - README.md
15+
16+
permissions:
17+
contents: write # needed to write releases
18+
packages: write # needed for ghcr access
19+
1420
jobs:
21+
22+
mac-build:
23+
# This job builds and releases "universal libraries" that are
24+
# supported by both darwin-amd64 and darwin-arm64.
25+
#
26+
# First builds in amd64, then cross-compile in arm64. Later combining
27+
# both outcomes onto a single binary for each static library.
28+
#
29+
# `macos-11` has been picked as support for arm64 was only added on Xcode 12.
30+
# Although some minor versions of Catalina 10.15 can support it, at the time
31+
# of testing, GitHub's macos-10.15 did not seem to.
32+
# Cross-compiling to arm64 on that runner consistently failed.
33+
runs-on: macos-11
34+
steps:
35+
- name: Checkout
36+
uses: actions/checkout@v2
37+
- name: Build universal static libraries for Darwin
38+
run: |
39+
TARGET_DIR=${GITHUB_WORKSPACE}/build/libgit2-darwin-amd64 \
40+
BUILD_ROOT_DIR=${GITHUB_WORKSPACE}/libgit2/build/amd \
41+
./hack/static.sh all
42+
43+
TARGET_DIR=${GITHUB_WORKSPACE}/build/libgit2-darwin-arm64 \
44+
BUILD_ROOT_DIR=${GITHUB_WORKSPACE}/libgit2/build/arm \
45+
TARGET_ARCH=arm64 \
46+
CMAKE_APPLE_SILICON_PROCESSOR=arm64 \
47+
./hack/static.sh all
48+
49+
mkdir -p ./libgit2-darwin/lib
50+
mv ${GITHUB_WORKSPACE}/build/libgit2-darwin-amd64/include ./libgit2-darwin/
51+
mv ${GITHUB_WORKSPACE}/build/libgit2-darwin-amd64/share ./libgit2-darwin/
52+
mv ${GITHUB_WORKSPACE}/build/libgit2-darwin-amd64/lib/cmake ./libgit2-darwin/lib/
53+
mv ${GITHUB_WORKSPACE}/build/libgit2-darwin-amd64/lib/engines-3 ./libgit2-darwin/lib/
54+
mv ${GITHUB_WORKSPACE}/build/libgit2-darwin-amd64/lib/ossl-modules ./libgit2-darwin/lib/
55+
mv ${GITHUB_WORKSPACE}/build/libgit2-darwin-amd64/lib/pkgconfig ./libgit2-darwin/lib/
56+
57+
libtool -static -o ./libgit2-darwin/lib/libcrypto.a \
58+
${GITHUB_WORKSPACE}/build/libgit2-darwin-amd64/lib/libcrypto.a \
59+
${GITHUB_WORKSPACE}/build/libgit2-darwin-arm64/lib/libcrypto.a
60+
libtool -static -o ./libgit2-darwin/lib/libgit2.a \
61+
${GITHUB_WORKSPACE}/build/libgit2-darwin-amd64/lib/libgit2.a \
62+
${GITHUB_WORKSPACE}/build/libgit2-darwin-arm64/lib/libgit2.a
63+
libtool -static -o ./libgit2-darwin/lib/libssh2.a \
64+
${GITHUB_WORKSPACE}/build/libgit2-darwin-amd64/lib/libssh2.a \
65+
${GITHUB_WORKSPACE}/build/libgit2-darwin-arm64/lib/libssh2.a
66+
libtool -static -o ./libgit2-darwin/lib/libssl.a \
67+
${GITHUB_WORKSPACE}/build/libgit2-darwin-amd64/lib/libssl.a \
68+
${GITHUB_WORKSPACE}/build/libgit2-darwin-arm64/lib/libssl.a
69+
libtool -static -o ./libgit2-darwin/lib/libz.a \
70+
${GITHUB_WORKSPACE}/build/libgit2-darwin-amd64/lib/libz.a \
71+
${GITHUB_WORKSPACE}/build/libgit2-darwin-arm64/lib/libz.a
72+
73+
tar -zcvf darwin-libs.tar.gz ./libgit2-darwin
74+
env:
75+
MACOSX_DEPLOYMENT_TARGET: 10.15
76+
77+
- name: Create Release
78+
if: github.event_name != 'pull_request'
79+
id: create_release
80+
uses: actions/create-release@v1
81+
env:
82+
GITHUB_TOKEN: ${{ github.token }}
83+
with:
84+
release_name: ${{ github.ref }}
85+
tag_name: ${{ github.ref }}
86+
draft: false
87+
prerelease: true
88+
- name: Upload Release Asset
89+
if: github.event_name != 'pull_request'
90+
id: upload-release-asset
91+
uses: actions/upload-release-asset@v1
92+
env:
93+
GITHUB_TOKEN: ${{ github.token }}
94+
with:
95+
upload_url: ${{ steps.create_release.outputs.upload_url }}
96+
asset_path: ./darwin-libs.tar.gz
97+
asset_name: darwin-libs.tar.gz
98+
asset_content_type: application/gzip
99+
15100
build:
16101
runs-on: ubuntu-latest
17102
env:
@@ -55,7 +140,7 @@ jobs:
55140
key: ${{ runner.os }}-buildx-ghcache-${{ github.sha }}
56141
restore-keys: |
57142
${{ runner.os }}-buildx-ghcache-
58-
- run: cat ./hack/Makefile
143+
- run: cat ./hack/static.sh
59144
- name: Build candidate image
60145
id: build_candidate
61146
uses: docker/build-push-action@v2

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This Dockerfile tests the hack/Makefile output against git2go.
22
ARG BASE_VARIANT=alpine
3-
ARG GO_VERSION=1.17.6
3+
ARG GO_VERSION=1.17
44
ARG XX_VERSION=1.1.0
55

66
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx

Dockerfile.test

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,102 @@
1+
# This Dockerfile tests the hack/Makefile output against git2go.
12
ARG BASE_VARIANT=alpine
23
ARG GO_VERSION=1.17
34
ARG XX_VERSION=1.1.0
45

5-
ARG LIBGIT2_IMG
6-
ARG LIBGIT2_TAG
6+
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
77

8-
FROM ${LIBGIT2_IMG}:${LIBGIT2_TAG} AS build-deps
8+
FROM --platform=$BUILDPLATFORM ${BASE_VARIANT} AS build-base
9+
10+
RUN apk add --no-cache \
11+
bash \
12+
curl \
13+
build-base \
14+
linux-headers \
15+
perl \
16+
cmake \
17+
pkgconfig \
18+
gcc \
19+
musl-dev \
20+
clang \
21+
lld
922

10-
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
23+
COPY --from=xx / /
24+
25+
FROM build-base AS build-cross
26+
27+
ARG TARGETPLATFORM
28+
29+
RUN xx-apk add --no-cache \
30+
build-base \
31+
pkgconfig \
32+
gcc \
33+
musl-dev \
34+
clang \
35+
lld \
36+
llvm \
37+
linux-headers
38+
39+
WORKDIR /build
40+
COPY hack/static.sh .
41+
42+
ENV CC=xx-clang
43+
ENV CXX=xx-clang++
44+
45+
RUN CHOST=$(xx-clang --print-target-triple) \
46+
./static.sh build_libz
47+
48+
RUN CHOST=$(xx-clang --print-target-triple) \
49+
./static.sh build_openssl
50+
51+
RUN export LIBRARY_PATH="/usr/local/$(xx-info triple)/lib:/usr/local/$(xx-info triple)/lib64:${LIBRARY_PATH}" && \
52+
export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \
53+
export OPENSSL_ROOT_DIR="/usr/local/$(xx-info triple)" && \
54+
export OPENSSL_CRYPTO_LIBRARY="/usr/local/$(xx-info triple)/lib64" && \
55+
export OPENSSL_INCLUDE_DIR="/usr/local/$(xx-info triple)/include/openssl"
56+
57+
RUN ./static.sh build_libssh2
58+
RUN ./static.sh build_libgit2
59+
60+
61+
# trimmed removes all non necessary files (i.e. openssl binary).
62+
FROM build-cross AS trimmed
63+
64+
ARG TARGETPLATFORM
65+
RUN mkdir -p /trimmed/usr/local/$(xx-info triple)/ && \
66+
mkdir -p /trimmed/usr/local/$(xx-info triple)/share
67+
68+
RUN cp -r /usr/local/$(xx-info triple)/lib/ /trimmed/usr/local/$(xx-info triple)/ && \
69+
cp -r /usr/local/$(xx-info triple)/lib64/ /trimmed/usr/local/$(xx-info triple)/ | true && \
70+
cp -r /usr/local/$(xx-info triple)/include/ /trimmed/usr/local/$(xx-info triple)/ && \
71+
cp -r /usr/local/$(xx-info triple)/share/doc/ /trimmed/usr/local/$(xx-info triple)/share/
72+
73+
FROM scratch as libs-arm64
74+
COPY --from=trimmed /trimmed/ /
75+
76+
FROM scratch as libs-amd64
77+
COPY --from=trimmed /trimmed/ /
78+
79+
FROM scratch as libs-armv7
80+
COPY --from=trimmed /trimmed/ /
81+
82+
FROM libs-$TARGETARCH$TARGETVARIANT as libs
83+
84+
# Everything above this line is a copy from Dockefile.
1185

1286
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} as gostable
1387

1488
FROM gostable AS go-linux
1589

1690
# Build-base consists of build platform dependencies and xx.
1791
# These will be used at current arch to yield execute the cross compilations.
18-
FROM go-${TARGETOS} AS build-base
92+
FROM go-${TARGETOS} AS go-base
1993

2094
RUN apk add clang lld pkgconfig
2195

2296
COPY --from=xx / /
2397

2498
# build-go-mod can still be cached at build platform architecture.
25-
FROM build-base as build-go-mod
99+
FROM go-base as build-go-mod
26100

27101
WORKDIR /root/smoketest
28102
COPY tests/smoketest/go.mod .
@@ -42,7 +116,7 @@ RUN xx-apk add musl-dev gcc clang lld
42116
WORKDIR /root/smoketest
43117

44118
COPY tests/smoketest/main.go .
45-
COPY --from=build-deps /usr/local/ /usr/local/
119+
COPY --from=libs /usr/local/ /usr/local/
46120

47121
ENV CGO_ENABLED=1
48122
RUN export LIBRARY_PATH="/usr/local/$(xx-info triple):/usr/local/$(xx-info triple)/lib64" && \

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# golang-with-libgit2
22

3-
This repository contains a `Dockerfile` with two files: `Makefile` and `static.sh`.
3+
This repository contains a `Dockerfile` with the statically built libgit2 and its dependency chain.
4+
5+
The `hack` directory contains two main files: `Makefile` and `static.sh`.
46
Both of which can be used to build the [libgit2][] dependency chain for **AMD64, ARM64 and ARMv7** binaries
57
of Go projects that depend on [git2go][].
68

79
The `Makefile` is useful for development environments and will leverage OS specific packages to build `libgit2`.
810
The `static.sh` will build all `libgit2` dependencies from source using `musl` toolchain. This enables for a full
911
static binary with the freedom of configuring each of the dependencies in chain.
1012

13+
Alternatively, the statically built libraries can be pulling from the produced images for Linux or from the github release artifacts for MacOS.
14+
1115
### :warning: **Public usage discouraged**
1216

1317
The set of dependencies was handpicked for the Flux project, based on the issue list documented below. While this setup
@@ -47,17 +51,14 @@ while testing these against the git2go code before releasing the image.
4751
- [ ] [libgit2/git2go#836](https://github.com/libgit2/git2go/issues/836)
4852
- [ ] [libgit2/git2go#837](https://github.com/libgit2/git2go/issues/837)
4953

50-
---
51-
**NOTE**
5254

53-
The issues above do not affect libgit2 built with `static.sh` as all its
55+
> **NOTE:** The issues above do not affect libgit2 built with `static.sh` as all its
5456
dependencies have been configured to be optimal for its use, as the first supported version of libgit2 is `1.3.0`.
5557

56-
---
5758

5859
## Usage
5960

60-
The [Dockerfile.test](./Dockerfile.test) file provides a working example on how to statically build a golang application that has a dependency to libgit2 and git2go.
61+
The [Dockerfile.test](./Dockerfile.test) file provides a working example on how to statically build a golang application that has a dependency on libgit2 and git2go.
6162

6263
The example will statically build all dependencies based on the versions specified on `static.sh`.
6364
Then statically build the golang application and deploy it into an image based off `gcr.io/distroless/static`.

0 commit comments

Comments
 (0)