Skip to content

Commit 4c945db

Browse files
committed
fix(build): let a builder stage inherit its base stage's platform
The cross-compile check read only the literal FROM line, so a stage built FROM an earlier --platform=$BUILDPLATFORM stage counted as emulated even though it runs natively. The shadowbox webpack stage is such a stage, and it failed the check on main. The check now follows FROM <stage> to its base and inherits its platform. ARCH_NEUTRAL also accepts "<file>:<stage>", so the webpack stage can be marked as producing no architecture without excusing the Go stage in the same Dockerfile. A lookup failure now fails the stage instead of passing it. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 6d759cc commit 4c945db

1 file changed

Lines changed: 55 additions & 19 deletions

File tree

‎hack/image-builders-cross-compile.bats‎

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
# A stage compiles when one of its RUN commands is go build/test/install, make,
1212
# gradle, dpkg-buildpackage, a node package manager or a *build.sh script, at
1313
# command position (after RUN and its --flag options, `&&`, `;` or `||`, past
14-
# any VAR=value prefixes). Such a stage must sit on a BUILDPLATFORM FROM and
15-
# use TARGETARCH or TARGETPLATFORM on a RUN or ENV line, unless its output
14+
# any VAR=value prefixes). Such a stage must run on the build platform (see
15+
# stages() below) and use TARGETARCH or TARGETPLATFORM on a RUN or ENV line, unless its output
1616
# carries no architecture at all (ARCH_NEUTRAL below). A bare
1717
# `ARG TARGETARCH` does not count: it declares the variable, and a `go build`
1818
# that never reads it still builds for the host. The check is per stage, not
@@ -25,19 +25,40 @@
2525

2626
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
2727

28-
# Outputs with no architecture: the console builds static JS; piraeus-server
29-
# builds Java .debs that linstor-server's debian/control declares
30-
# `Architecture: all`.
31-
ARCH_NEUTRAL="packages/system/dashboard/images/console/Containerfile
28+
# Outputs with no architecture, one entry per line: a whole file, or
29+
# "<file>:<stage>" when only that stage is neutral and the file's other
30+
# stages still have to target TARGETARCH. The console and shadowbox's webpack
31+
# stage build static JS; piraeus-server builds Java .debs that
32+
# linstor-server's debian/control declares `Architecture: all`.
33+
ARCH_NEUTRAL="packages/apps/vpn/images/shadowbox/Dockerfile:app
34+
packages/system/dashboard/images/console/Containerfile
3235
packages/system/linstor/images/piraeus-server/Dockerfile"
3336

34-
# One line per stage: "<compiles 0|1> <targets 0|1> <FROM line>".
37+
# is_neutral <file> <stage> <list>: 1 when the list names the file or that stage of it.
38+
is_neutral() {
39+
printf '%s\n' "$3" | awk -v f="$1" -v s="$1:$2" '$0 == f || $0 == s { hit = 1 } END { print hit + 0 }'
40+
}
41+
42+
# One line per stage: "<compiles 0|1> <targets 0|1> <native 0|1> <name> <FROM line>".
43+
# A stage is native when its FROM carries --platform=$BUILDPLATFORM, or when it
44+
# builds FROM an earlier native stage: a stage inherits the platform of the
45+
# stage it starts from. An unnamed stage is reported as "-".
3546
# Continuation lines are joined first so a command after `&& \` is seen at
3647
# command position.
3748
stages() {
3849
sed -e ':a' -e '/\\$/N; s/\\\n//; ta' "$1" | awk '
39-
function flush() { if (from != "") printf "%d %d %s\n", compiles, targets, from }
40-
/^FROM[[:space:]]/ { flush(); from = $0; compiles = 0; targets = 0; next }
50+
function flush() { if (from != "") printf "%d %d %d %s %s\n", compiles, targets, native, name, from }
51+
/^FROM[[:space:]]/ {
52+
flush(); from = $0; compiles = 0; targets = 0; native = 0; name = "-"; base = ""
53+
for (i = 2; i <= NF; i++) {
54+
if ($i ~ /^--platform=(\$BUILDPLATFORM|\$\{BUILDPLATFORM\})$/) native = 1
55+
else if ($i !~ /^--/ && base == "") base = $i
56+
else if (tolower($i) == "as" && i < NF) name = $(i + 1)
57+
}
58+
if (base in nativestage) native = 1
59+
if (native && name != "-") nativestage[name] = 1
60+
next
61+
}
4162
/^RUN[[:space:]]/ && /(^RUN([[:space:]]+--[^[:space:]]+)*|&&|;|[|][|])[[:space:]]+([A-Za-z_][A-Za-z0-9_]*=[^[:space:]]*[[:space:]]+)*(go[[:space:]]+(build|test|install)|make|gradle|[.]\/gradlew|dpkg-buildpackage|pnpm|npm|yarn|[^[:space:]]*build[.]sh)([[:space:]]|$)/ { compiles = 1 }
4263
/^(RUN|ENV)[[:space:]]/ && /TARGETARCH|TARGETPLATFORM/ { targets = 1 }
4364
END { flush() }
@@ -52,18 +73,16 @@ stages() {
5273
# makes), so word-splitting find's output is safe.
5374
for f in $(find "$REPO_ROOT"/packages -path '*/charts' -prune -o \( -name Dockerfile -o -name Containerfile \) -type f -print | sort); do
5475
rel="${f#"$REPO_ROOT"/}"
55-
neutral=0
56-
case "$ARCH_NEUTRAL" in *"$rel"*) neutral=1 ;; esac
5776
stages "$f" >"$stagelist"
58-
while read -r compiles targets from; do
77+
while read -r compiles targets native name from; do
5978
[ "$compiles" = 1 ] || continue
6079
checked=$((checked + 1))
61-
case "$from" in
62-
*'--platform=$BUILDPLATFORM'*|*'--platform=${BUILDPLATFORM}'*) ;;
63-
*) failed="$failed
64-
$rel: $from (not on BUILDPLATFORM)"; continue ;;
65-
esac
66-
if [ "$neutral" = 0 ] && [ "$targets" = 0 ]; then
80+
if [ "$native" = 0 ]; then
81+
failed="$failed
82+
$rel: $from (not on BUILDPLATFORM)"
83+
continue
84+
fi
85+
if [ "$targets" = 0 ] && [ "$(is_neutral "$rel" "$name" "$ARCH_NEUTRAL")" != 1 ]; then
6786
failed="$failed
6887
$rel: $from (compiles without TARGETARCH)"
6988
fi
@@ -83,5 +102,22 @@ stages() {
83102
printf '%s\n' 'FROM golang AS builder' 'RUN --mount=type=cache,target=/root/.cache go build .' >"$fixture"
84103
out="$(stages "$fixture")"
85104
rm -f "$fixture"
86-
[ "$out" = "1 0 FROM golang AS builder" ]
105+
[ "$out" = "1 0 0 builder FROM golang AS builder" ]
106+
}
107+
108+
@test "a stage built FROM an earlier BUILDPLATFORM stage runs on the build platform too" {
109+
fixture="$(mktemp)"
110+
printf '%s\n' 'FROM --platform=$BUILDPLATFORM node AS source' 'RUN true' 'FROM source AS app' 'RUN npm ci' 'FROM node AS emulated' 'RUN npm ci' 'FROM emulated AS child' 'RUN go build .' >"$fixture"
111+
out="$(stages "$fixture")"
112+
rm -f "$fixture"
113+
[ "$(printf '%s\n' "$out" | sed -n 2p)" = "1 0 1 app FROM source AS app" ]
114+
[ "$(printf '%s\n' "$out" | sed -n 3p)" = "1 0 0 emulated FROM node AS emulated" ]
115+
# A stage built from an emulated stage stays emulated.
116+
[ "$(printf '%s\n' "$out" | sed -n 4p)" = "1 0 0 child FROM emulated AS child" ]
117+
}
118+
119+
@test "an arch-neutral stage entry excuses only that stage" {
120+
[ "$(is_neutral packages/x/Dockerfile app 'packages/x/Dockerfile:app')" = 1 ]
121+
[ "$(is_neutral packages/x/Dockerfile ss 'packages/x/Dockerfile:app')" = 0 ]
122+
[ "$(is_neutral packages/y/Dockerfile any 'packages/y/Dockerfile')" = 1 ]
87123
}

0 commit comments

Comments
 (0)