Skip to content

Instantly share code, notes, and snippets.

@hugalafutro
Created March 7, 2026 21:58
Show Gist options
  • Select an option

  • Save hugalafutro/2f260ae25e7e4966f17428395fc7efd8 to your computer and use it in GitHub Desktop.

Select an option

Save hugalafutro/2f260ae25e7e4966f17428395fc7efd8 to your computer and use it in GitHub Desktop.
FROM alpine:latest AS build
ARG MC_TAG=4.8.33
# Install build tools + runtime libs (ensure .so files exist)
RUN apk add --no-cache \
build-base wget tar ncurses-dev ncurses-terminfo-base pkgconf \
glib glib-dev pcre pcre2 pcre-dev libstdc++ \
slang slang-dev ncurses-libs \
gettext gettext-dev intltool m4 autoconf automake libtool \
bash perl patchelf findutils
WORKDIR /src
# Download release tarball and extract
RUN wget -qO mc.tar.gz "https://github.com/MidnightCommander/mc/archive/refs/tags/${MC_TAG}.tar.gz" \
&& tar xzf mc.tar.gz --strip-components=1
# If configure missing, autoreconf (we have autotools installed)
RUN if [ ! -x ./configure ]; then \
if [ -x ./autogen.sh ]; then ./autogen.sh || true; fi; \
autoreconf --install --force || true; \
fi
# Compatibility shim for tinfo checks
RUN ln -sf /usr/lib/libncursesw.so /usr/lib/libtinfo.so || true
# Configure (do not pass -static to configure)
RUN ./configure --disable-nls --prefix=/usr --with-ncurses
# Build with static flags at make time (so libs are musl-linked)
ENV CFLAGS="-O2 -static"
ENV LDFLAGS="-static"
RUN make -j$(getconf _NPROCESSORS_ONLN)
# Prepare bundle dir and copy binary
RUN mkdir -p /out/bundle /out/bundle/lib
RUN cp src/mc /out/bundle/mc
# Copy the correct aarch64 musl loader into bundle (if present)
RUN if [ -e /lib/ld-musl-aarch64.so.1 ]; then cp /lib/ld-musl-aarch64.so.1 /out/bundle/ld-musl-aarch64.so.1; fi
# Collect NEEDED entries from the actual bundled binary (/out/bundle/mc)
# and copy the resolved .so files into bundle/lib
# inside the build stage, after copying /out/bundle/mc and the loader
RUN set -eux; \
cd /out/bundle; \
mkdir -p lib; \
# seed list: NEEDED of the bundled binary
readelf -d ./mc | awk '/NEEDED/ {print $5}' | tr -d '[]' > /tmp/todo.list; \
touch /tmp/copied.list; \
while [ -s /tmp/todo.list ]; do \
# pop one lib name
lib=$(sed -n '1p' /tmp/todo.list); \
# remove the first line from todo
sed -n '2,$p' /tmp/todo.list > /tmp/todo.next && mv /tmp/todo.next /tmp/todo.list; \
# skip if already copied
grep -xq "$lib" /tmp/copied.list && continue; \
echo "Resolving $lib"; \
# find the file path in common locations or via ldconfig
path=$(find /lib /usr/lib /usr/local/lib -name "$lib" -print -quit || true); \
if [ -z "$path" ]; then \
path=$(ldconfig -p 2>/dev/null | awk -v lib="$lib" '$1==lib {print $NF; exit}' || true); \
fi; \
if [ -z "$path" ]; then \
echo "Could not find $lib; aborting"; exit 1; \
fi; \
path=$(printf '%s' "$path" | tr -d '"'); \
echo "Copying $lib from $path"; cp -L "$path" lib/; \
# mark copied
echo "$lib" >> /tmp/copied.list; \
# read NEEDED of the copied file and append any new names to todo
readelf -d "lib/$lib" 2>/dev/null | awk '/NEEDED/ {print $5}' | tr -d '[]' | while read -r dep; do \
[ -z "$dep" ] && continue; \
grep -xq "$dep" /tmp/copied.list || grep -xq "$dep" /tmp/todo.list || echo "$dep" >> /tmp/todo.list; \
done; \
done; \
# ensure musl libc and libm are present if available
cp -L /lib/libc.musl-aarch64.so.1 lib/ 2>/dev/null || true; \
cp -L /lib/libm.so.6 lib/ 2>/dev/null || true; \
# replace symlinks with real files inside lib/
(cd lib && for f in *; do if [ -L "$f" ]; then target=$(readlink "$f"); rm -f "$f"; cp -L "$target" "$f"; fi; done) || true
# Patch interpreter and rpath to use the bundled aarch64 loader and libs
RUN set -eux; \
cd /out/bundle; \
if [ -e ./ld-musl-aarch64.so.1 ]; then \
patchelf --set-interpreter ./ld-musl-aarch64.so.1 mc; \
else \
echo "Warning: ld-musl-aarch64.so.1 not found in image; binary will keep its original interpreter"; \
fi; \
patchelf --set-rpath '$ORIGIN/lib' mc
# Create tarball artifact
RUN tar czf /out/mc-bundle.tar.gz -C /out bundle
# final artifact: /out/mc-bundle.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment