Skip to content

Commit a066310

Browse files
committed
Refactoring a bit...
1 parent ef7bc9a commit a066310

10 files changed

Lines changed: 76 additions & 81 deletions

File tree

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use nix

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
.stack-work
2-
# JetBrains' IDEs
3-
.idea
1+
.direnv
File renamed without changes.

1_ghc/builder.sh renamed to ghc/builder.sh

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,32 @@ cd "$( git rev-parse --show-toplevel )"
1717
# cf. https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/
1818
################################################################################
1919

20-
container="builder-ghc"
21-
image="builder-ghc"
20+
alpine_ver="3.14"
21+
container="ghc"
22+
image="ghc"
2223
ghc_ver="8.10.7"
2324
# NOTE: The logic associated with this will have to change for GHC 9.x and up to
2425
# support the changes introduced with the switch to `ghc-bignum`.
2526
numeric="gmp"
2627

2728
usage="USAGE: $0
28-
-h show this help text
29-
-c CONTAINER override the default container name
30-
default: ${container}
31-
-g GHC_VER override the numeric library GHC is built against; either 'gmp' or 'simple'
32-
default: ${ghc_ver}
33-
-i IMAGE override the default image name
34-
default: ${image}
35-
-n NUMERIC override the numeric library GHC is built against; either 'gmp' or 'simple'
36-
default: ${numeric}"
37-
38-
while getopts "c:g:i:n:h" opt; do
29+
-h show this help text
30+
-a ALPINE_VER override the default Alpine version
31+
default: ${alpine_ver}
32+
-c CONTAINER override the default container name
33+
default: ${container}
34+
-g GHC_VER override the numeric library GHC is built against; either 'gmp' or 'simple'
35+
default: ${ghc_ver}
36+
-i IMAGE override the default image name
37+
default: ${image}
38+
-n NUMERIC override the numeric library GHC is built against; either 'gmp' or 'simple'
39+
default: ${numeric}"
40+
41+
while getopts "a:c:g:i:n:h" opt; do
3942
case ${opt} in
43+
a ) {
44+
alpine_ver="${OPTARG}"
45+
};;
4046
c ) {
4147
container="${OPTARG}"
4248
};;
@@ -76,7 +82,7 @@ container="${container}-${numeric}-${ghc_ver}"
7682
image="${image}-${numeric}:${ghc_ver}"
7783

7884
################################################################################
79-
# Container and dependencies.
85+
# Container.
8086
################################################################################
8187

8288
# Create the container that will be used to compile GHC from source.
@@ -89,35 +95,49 @@ image="${image}-${numeric}:${ghc_ver}"
8995
buildah \
9096
--signature-policy=./policy.json \
9197
--name "${container}" \
92-
from --pull "base-${numeric}" \
93-
|| true
98+
from --pull "docker.io/library/alpine:${alpine_ver}"
99+
100+
################################################################################
101+
# Copy `ghcup` from another container.
102+
################################################################################
103+
104+
# XXX: Maybe the `"ghcup"` image name should be fully qualified (i.e.
105+
# "localhost/ghcup" for an image built locally)...?
106+
buildah unshare ./ghc/copy_ghcup.sh "ghcup" "${container}"
107+
108+
################################################################################
109+
# Dependencies.
110+
################################################################################
94111

95112
buildah run "${container}" \
96113
apk add --no-cache \
97114
autoconf \
98115
automake \
99116
binutils-gold \
100117
build-base \
118+
curl \
101119
coreutils \
102120
cpio \
103121
ghc \
122+
git \
104123
linux-headers \
105124
libffi-dev \
106125
musl-dev \
107126
ncurses-dev \
108127
perl \
109128
python3 \
110129
py3-sphinx \
111-
zlib-dev
130+
zlib-dev \
131+
xz
112132

113133
# Copy the appropriate build file, depending on the chosen numeric library.
114134
if [ "${numeric}" = "gmp" ]; then
115135
buildah copy --chmod 444 "${container}" \
116-
./1_ghc/build-gmp.mk \
136+
./ghc/build-gmp.mk \
117137
/tmp/build.mk
118138
elif [ "${numeric}" = "simple" ]; then
119139
buildah copy --chmod 444 "${container}" \
120-
./1_ghc/build-simple.mk \
140+
./ghc/build-simple.mk \
121141
/tmp/build.mk
122142
else # Should be impossible...
123143
echo "This code path should be unreachable!" >&2
@@ -128,12 +148,12 @@ fi;
128148

129149
# Copy all patches that will be applied to the GHC source tree.
130150
buildah copy --chmod 444 "${container}" \
131-
./1_ghc/patches \
151+
./ghc/patches \
132152
/tmp/patches
133153

134154
# Copy wrapper script that will invoke `ghcup` to compile GHC.
135155
buildah copy --chmod 111 "${container}" \
136-
./1_ghc/compile_ghc.sh \
156+
./ghc/compile_ghc.sh \
137157
/tmp/compile_ghc.sh
138158

139159
################################################################################
@@ -156,16 +176,6 @@ buildah run "${container}" \
156176
# Generate the final image.
157177
################################################################################
158178

159-
# NOTE: Using images for all of these intermediate stages is sort of a
160-
# carry-over from multi-stage Docker builds.
161-
#
162-
# It might be preferable to keep this container around for future build steps
163-
# and just mount it to copy the files over manually.
164-
#
165-
# The tradeoff with this is that the image building process stops being
166-
# "declarative", since it depends on the intermediate state of a particular
167-
# container on the host builder's system.
168-
169179
buildah \
170180
--signature-policy=./policy.json \
171-
commit "${container}" "${image}"
181+
commit --rm "${container}" "${image}"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ SPHINXBUILD=/usr/bin/sphinx-build-3 \
1212
--config /tmp/build.mk \
1313
--patchdir /tmp/patches \
1414
--bootstrap-ghc /usr/bin/ghc \
15+
--set \
1516
--version "${ghc_ver}"

ghc/copy_ghcup.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env sh
2+
3+
ghcup_img="$1"
4+
builder_cnt="$2"
5+
6+
ghcup_cnt=$(buildah from --signature-policy=./policy.json "${ghcup_img}")
7+
ghcup_mnt=$(buildah mount "${ghcup_cnt}")
8+
builder_mnt=$(buildah mount "${builder_cnt}")
9+
10+
cp "${ghcup_mnt}/usr/bin/ghcup" "${builder_mnt}/usr/bin/ghcup"
11+
12+
# Clean up intermediate container.
13+
buildah rm "${ghcup_cnt}"
File renamed without changes.
Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,30 @@ cd "$( git rev-parse --show-toplevel )"
1717
# cf. https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/
1818
################################################################################
1919

20-
container="base"
21-
image="base"
22-
# NOTE: The logic associated with this will have to change for GHC 9.x and up to
23-
# support the changes introduced with the switch to `ghc-bignum`.
24-
numeric="gmp"
20+
alpine_ver="3.14"
21+
container="ghcup"
22+
image="ghcup"
2523

2624
usage="USAGE: $0
2725
-h show this help text
26+
-a ALPINE_VER override the default Alpine version
27+
default: ${alpine_ver}
2828
-c CONTAINER override the default container name
2929
default: ${container}
30-
-i IMAGE override the default image name
31-
default: ${image}
32-
-n NUMERIC override the numeric library GHC is built against; either 'gmp' or 'simple'
33-
default: ${numeric}"
30+
-i IMAGE override the default image name
31+
default: ${image}"
3432

35-
while getopts "c:i:n:h" opt; do
33+
while getopts "a:c:i:h" opt; do
3634
case ${opt} in
35+
a ) {
36+
alpine_ver="${OPTARG}"
37+
};;
3738
c ) {
3839
container="${OPTARG}"
3940
};;
4041
i ) {
4142
image="${OPTARG}"
4243
};;
43-
n ) {
44-
if [ "${OPTARG}" = "gmp" ] || [ "${OPTARG}" = "simple" ]; then
45-
numeric="${OPTARG}"
46-
else
47-
echo "Invalid NUMERIC argument (i.e. '-n')." >&2
48-
echo "Expected either 'gmp' or 'simple', got '${OPTARG}'" >&2
49-
exit 1
50-
fi;
51-
};;
5244
h ) {
5345
echo "${usage}"
5446
exit 0
@@ -65,26 +57,14 @@ if [ "$#" -ne 0 ]; then
6557
exit 1
6658
fi
6759

68-
# Add the numeric library to container and image names.
69-
container="${container}-${numeric}"
70-
image="${image}-${numeric}"
71-
7260
################################################################################
7361
# Container and basic dependencies.
7462
################################################################################
7563

76-
# Create the "base" container that will be used elsewhere in the project.
77-
#
78-
# NOTE: Alternatively: `buildah commit --rm` (at the end of the script) removes
79-
# the working container.
80-
#
81-
# XXX: Reusing the container by name if it exists seems like not the best idea
82-
# but it's convenient for development.
8364
buildah \
8465
--signature-policy=./policy.json \
8566
--name "${container}" \
86-
from --pull docker.io/library/alpine:3.14 \
87-
|| true
67+
from --pull docker.io/library/alpine:3.14
8868

8969
# Update index files and upgrade the currently installed packages.
9070
#
@@ -96,23 +76,14 @@ buildah run "${container}" \
9676
buildah run "${container}" \
9777
apk add \
9878
curl \
99-
gcc \
100-
git \
101-
libc-dev \
10279
xz
10380

104-
if [ "${numeric}" = "gmp" ]; then
105-
echo "Installing 'libgmp'."
106-
buildah run "${container}" \
107-
apk add gmp-dev
108-
fi;
109-
11081
################################################################################
11182
# Download and install `ghcup`.
11283
################################################################################
11384

114-
ghcup_version="0.1.16.2"
115-
ghcup_expected_checksum="d5e43b95ce1d42263376e414f7eb7c5dd440271c7c6cd9bad446fdeff3823893"
85+
ghcup_version="0.1.17.2"
86+
ghcup_expected_checksum="e9adb022b9bcfe501caca39e76ae7241af0f30fbb466a2202837a7a578607daf"
11687

11788
# Fetch `ghcup`.
11889
buildah run "${container}" \
@@ -122,7 +93,7 @@ buildah run "${container}" \
12293

12394
# Copy the checksum validation script into the container...
12495
buildah copy --chmod 111 "${container}" \
125-
./0_base/validate_checksum.sh \
96+
./ghcup/validate_checksum.sh \
12697
/tmp/validate_checksum.sh
12798

12899
# ...and verify that the expected and actual actual `ghcup` checksums match.
@@ -147,6 +118,7 @@ buildah run "${container}" \
147118
# Generate the final image.
148119
################################################################################
149120

121+
# NOTE: Tagging the image with the `ghcup_version` for convenience.
150122
buildah \
151123
--signature-policy=./policy.json \
152-
commit "${container}" "${image}"
124+
commit --rm "${container}" "${image}:${ghcup_version}"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env sh
22
set -eu
33

4-
ghcup_path=$1
4+
ghcup_path="$1"
55
ghcup_expected_checksum="$2"
66

77
if ! echo "${ghcup_expected_checksum} ${ghcup_path}" | sha256sum -c -; then

0 commit comments

Comments
 (0)