-
Notifications
You must be signed in to change notification settings - Fork 17
/
Dockerfile
207 lines (194 loc) · 8.24 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# ---
# Special thanks to luislavena for this approach to cross-compiling Crystal apps with Zig.
# Source: https://forum.crystal-lang.org/t/cross-compiling-crystal-applications-part-1/6956
# Repo: https://github.com/luislavena/crystal-xbuild-container
# Background Crystal Lang Docs on:
# - Cross-Compilation: https://crystal-lang.org/reference/1.13/syntax_and_semantics/cross-compilation.html
# - Static Linking: https://crystal-lang.org/reference/1.13/guides/static_linking.html
#
# NOTE:
# We've modified the original approach here as necessary for coverage-reporter:
# - Installed additional dependencies for our target architectures
# - Made a few tweaks to address recurring issues with our target architectures
# - Populated our Makefile with convenience targets for our use case
# - Leveraging those targets to extended this approach to our CI/CD for releases
# ---
# Base image from luislavena's hydrofoil-crystal image
FROM ghcr.io/luislavena/hydrofoil-crystal:1.13 AS base
# install cross-compiler (Zig) with dependencies and utilities
RUN --mount=type=cache,sharing=private,target=/var/cache/apk \
--mount=type=tmpfs,target=/tmp \
set -eux -o pipefail; \
# Tools to extract Zig
{ \
apk add \
file \
tar \
xz \
; \
}; \
# Zig
{ \
cd /tmp; \
mkdir -p /opt/zig; \
export ZIG_VERSION=0.13.0; \
case "$(arch)" in \
x86_64) \
export \
ZIG_ARCH=x86_64 \
ZIG_SHA256=d45312e61ebcc48032b77bc4cf7fd6915c11fa16e4aad116b66c9468211230ea \
; \
;; \
aarch64) \
export \
ZIG_ARCH=aarch64 \
ZIG_SHA256=041ac42323837eb5624068acd8b00cd5777dac4cf91179e8dad7a7e90dd0c556 \
; \
;; \
esac; \
wget -q -O zig.tar.xz https://ziglang.org/download/${ZIG_VERSION}/zig-linux-${ZIG_ARCH}-${ZIG_VERSION}.tar.xz; \
echo "${ZIG_SHA256} *zig.tar.xz" | sha256sum -c - >/dev/null 2>&1; \
tar -C /opt/zig --strip-components=1 -xf zig.tar.xz; \
rm zig.tar.xz; \
# symlink executable
ln -nfs /opt/zig/zig /usr/local/bin; \
}; \
# smoke check
[ "$(command -v zig)" = '/usr/local/bin/zig' ]; \
zig version; \
zig cc --version
# ---
# Alpine Linux libraries for multi-arch cross-compilation
#
# NOTE:
# We are cross-compiling for `x86_64` and `aarch64` at this time.
# ---
# Set the library path to include both /lib and /opt/multiarch-libs
ENV LIBRARY_PATH="/lib:/opt/multiarch-libs/aarch64-linux-musl/lib:/opt/multiarch-libs/x86_64-linux-musl/lib"
# Create the target directories for both aarch64 and x86_64
RUN mkdir -p /opt/multiarch-libs/aarch64-linux-musl/lib && \
mkdir -p /opt/multiarch-libs/x86_64-linux-musl/lib
# Set the include paths for header files for both architectures
ENV CFLAGS="-I/opt/multiarch-libs/aarch64-linux-musl/include -I/opt/multiarch-libs/x86_64-linux-musl/include"
ENV LDFLAGS="-L/opt/multiarch-libs/aarch64-linux-musl/lib -L/opt/multiarch-libs/x86_64-linux-musl/lib"
# Install multi-arch libraries
RUN --mount=type=cache,sharing=private,target=/var/cache/apk \
--mount=type=tmpfs,target=/tmp \
set -eux -o pipefail; \
# Alpine Linux: download and extract packages for each arch
{ \
supported_arch="aarch64 x86_64"; \
target_alpine=3.20; \
cd /tmp; \
for target_arch in $supported_arch; do \
target_path="/tmp/$target_arch-apk-chroot"; \
mkdir -p $target_path/etc/apk; \
# patch apk repositories to $target_alpine version
sed -E "s/v\d\.\d+/v$target_alpine/g" /etc/apk/repositories | tee $target_path/etc/apk/repositories; \
# use apk to download the specific packages
apk add --root $target_path --arch $target_arch --initdb --no-cache --no-scripts --allow-untrusted \
gc-dev \
gmp-dev \
libevent-dev \
libevent-static \
libsodium-dev \
libsodium-static \
libxml2-dev \
libxml2-static \
openssl-dev \
openssl-libs-static \
pcre2-dev \
sqlite-dev \
sqlite-static \
yaml-dev \
yaml-static \
zlib-dev \
zlib-static \
xz-dev \
xz-static \
; \
# Copy the correct libz.a for each architecture \
# This is required because `libz.a` does not otherwise \
# get installed for `aarh64` and `x86_64` \
# just for `aarch64-apple-darwin`
if [ "$target_arch" = "aarch64" ]; then \
cp $target_path/lib/libz.a /opt/multiarch-libs/aarch64-linux-musl/lib/; \
elif [ "$target_arch" = "x86_64" ]; then \
cp $target_path/lib/libz.a /opt/multiarch-libs/x86_64-linux-musl/lib/; \
fi; \
# Verify the installed libz.a for each $target_arch
# echo "DEBUG: Checking installed libz.a for $target_arch"; \
ls -al /tmp/$target_arch-apk-chroot/lib/libz.a; \
# Debug: List the contents of $target_path/usr/lib/
# echo "DEBUG: Listing contents of $target_path/usr/lib/"; \
pkg_path="/opt/multiarch-libs/$target_arch-linux-musl"; \
ls -al $target_path/usr/lib/; \
mkdir -p $pkg_path/lib/pkgconfig; \
# copy the static libs & .pc files
cp $target_path/usr/lib/*.a $pkg_path/lib/; \
cp $target_path/usr/lib/pkgconfig/*.pc $pkg_path/lib/pkgconfig/; \
# Debug: List the contents of /opt/multiarch-libs/$target_arch-linux-musl/
# echo "DEBUG: Installed libraries for $target_arch"; \
# echo "DEBUG: Listing contents of $pkg_path"; \
ls -al $pkg_path/lib/; \
done; \
}
# ---
# MacOS
#
# NOTE:
# We are not using this script to cross-compile for MacOS at this time
# (you'll notice we have no accompanying target in our Makefile for a MacOS build).
# Instead, we're continuing to leverage our Homebrew formula ("bottle") here: https://github.com/coverallsapp/homebrew-coveralls
# (which our `build.yml` workflow updates using the `homebrew-bump-formula` GitHub Action).
#
# That said, we're continuing to build the MacOS dependencies here in case we decide to
# switch to this method for cross-compiling MacOS binaries in the future.
# ---
# MacOS dependencies are installed in separate target
FROM base AS macos-packages
COPY ./scripts/homebrew-downloader.cr /homebrew-downloader.cr
RUN --mount=type=cache,sharing=private,target=/var/cache/apk \
--mount=type=tmpfs,target=/tmp \
set -eux -o pipefail; \
# macOS (Monterey), supports only Apple Silicon (aarch64/arm64)
{ \
pkg_path="/opt/multiarch-libs/aarch64-apple-darwin"; \
crystal run /homebrew-downloader.cr -- \
$pkg_path \
gmp \
libevent \
libgc \
libiconv \
libsodium \
libxml2 \
libyaml \
openssl@3 \
pcre2 \
sqlite \
zlib \
; \
}
# Copy macOS dependencies back into `base`
FROM base
COPY --from=macos-packages /opt/multiarch-libs/aarch64-apple-darwin /opt/multiarch-libs/aarch64-apple-darwin
# Install macOS SDK
RUN --mount=type=cache,sharing=private,target=/var/cache/apk \
--mount=type=tmpfs,target=/tmp \
set -eux -o pipefail; \
{ \
cd /tmp; \
export \
MACOS_SDK_VERSION=12.3 \
MACOS_SDK_MAJOR_VERSION=12 \
MACOS_SDK_SHA256=3abd261ceb483c44295a6623fdffe5d44fc4ac2c872526576ec5ab5ad0f6e26c \
; \
wget -q -O sdk.tar.xz https://github.com/joseluisq/macosx-sdks/releases/download/${MACOS_SDK_VERSION}/MacOSX${MACOS_SDK_VERSION}.sdk.tar.xz; \
echo "${MACOS_SDK_SHA256} *sdk.tar.xz" | sha256sum -c - >/dev/null 2>&1; \
tar -C /opt/multiarch-libs -xf sdk.tar.xz --no-same-owner; \
rm sdk.tar.xz; \
# symlink to latest version
ln -nfs /opt/multiarch-libs/MacOSX${MACOS_SDK_VERSION}.sdk /opt/multiarch-libs/MacOSX${MACOS_SDK_MAJOR_VERSION}.sdk; \
}
# Copy xbuild helper
COPY ./scripts/xbuild.sh /usr/local/bin/xbuild