Merge pull request #67 from infosiftr/musl

Convert from glibc-based binaries to musl-based binaries
This commit is contained in:
yosifkit 2019-12-30 17:16:10 -07:00 committed by GitHub
commit e6a1422dd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 62 additions and 33 deletions

View File

@ -1,30 +1,50 @@
# explicitly use Debian for maximum cross-architecture compatibility # explicitly use Debian for maximum cross-architecture compatibility
FROM debian:stretch-slim FROM debian:buster-slim
RUN dpkg --add-architecture i386 RUN set -eux; \
apt-get update; \
RUN apt-get update && apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
ca-certificates \
gnupg dirmngr \
wget \
\
gcc \ gcc \
libc6-dev \ libc6-dev \
make \ make \
\ \
libc6-dev:i386 \
libgcc-6-dev:i386 \
\
libc6-dev-arm64-cross \ libc6-dev-arm64-cross \
libc6-dev-armel-cross \ libc6-dev-armel-cross \
libc6-dev-armhf-cross \ libc6-dev-armhf-cross \
libc6-dev-i386-cross \
libc6-dev-ppc64el-cross \ libc6-dev-ppc64el-cross \
libc6-dev-s390x-cross \ libc6-dev-s390x-cross \
\ \
gcc-aarch64-linux-gnu \ gcc-aarch64-linux-gnu \
gcc-arm-linux-gnueabi \ gcc-arm-linux-gnueabi \
gcc-arm-linux-gnueabihf \ gcc-arm-linux-gnueabihf \
gcc-i686-linux-gnu \
gcc-powerpc64le-linux-gnu \ gcc-powerpc64le-linux-gnu \
gcc-s390x-linux-gnu \ gcc-s390x-linux-gnu \
\ \
file \ file \
&& rm -rf /var/lib/apt/lists/* ; \
rm -rf /var/lib/apt/lists/*
# https://www.musl-libc.org/download.html
ENV MUSL_VERSION 1.1.24
RUN set -eux; \
wget -O musl.tgz.asc "https://www.musl-libc.org/releases/musl-$MUSL_VERSION.tar.gz.asc"; \
wget -O musl.tgz "https://www.musl-libc.org/releases/musl-$MUSL_VERSION.tar.gz"; \
\
export GNUPGHOME="$(mktemp -d)"; \
gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys '836489290BB6B70F99FFDA0556BCDB593020450F'; \
gpg --batch --verify musl.tgz.asc musl.tgz; \
gpgconf --kill all; \
rm -rf "$GNUPGHOME" musl.tgz.asc; \
\
mkdir /usr/local/src/musl; \
tar --extract --file musl.tgz --directory /usr/local/src/musl --strip-components 1; \
rm musl.tgz
WORKDIR /usr/src/hello WORKDIR /usr/src/hello
COPY . . COPY . .
@ -32,44 +52,38 @@ COPY . .
RUN set -ex; \ RUN set -ex; \
make clean all test \ make clean all test \
TARGET_ARCH='amd64' \ TARGET_ARCH='amd64' \
CC='x86_64-linux-gnu-gcc' \ CROSS_COMPILE='x86_64-linux-gnu-'
STRIP='x86_64-linux-gnu-strip'
RUN set -ex; \ RUN set -ex; \
make clean all \ make clean all \
TARGET_ARCH='arm32v5' \ TARGET_ARCH='arm32v5' \
CC='arm-linux-gnueabi-gcc' \ CROSS_COMPILE='arm-linux-gnueabi-'
STRIP='arm-linux-gnueabi-strip'
RUN set -ex; \ RUN set -ex; \
make clean all \ make clean all \
TARGET_ARCH='arm32v7' \ TARGET_ARCH='arm32v7' \
CC='arm-linux-gnueabihf-gcc' \ CROSS_COMPILE='arm-linux-gnueabihf-'
STRIP='arm-linux-gnueabihf-strip'
RUN set -ex; \ RUN set -ex; \
make clean all \ make clean all \
TARGET_ARCH='arm64v8' \ TARGET_ARCH='arm64v8' \
CC='aarch64-linux-gnu-gcc' \ CROSS_COMPILE='aarch64-linux-gnu-'
STRIP='aarch64-linux-gnu-strip'
RUN set -ex; \ RUN set -ex; \
make clean all test \ make clean all test \
TARGET_ARCH='i386' \ TARGET_ARCH='i386' \
CC='gcc -m32 -L/usr/lib/gcc/i686-linux-gnu/6' \ CROSS_COMPILE='i686-linux-gnu-'
STRIP='x86_64-linux-gnu-strip'
RUN set -ex; \ RUN set -ex; \
make clean all \ make clean all \
TARGET_ARCH='ppc64le' \ TARGET_ARCH='ppc64le' \
CC='powerpc64le-linux-gnu-gcc' \ CROSS_COMPILE='powerpc64le-linux-gnu-' \
STRIP='powerpc64le-linux-gnu-strip' CFLAGS+='-mlong-double-64'
RUN set -ex; \ RUN set -ex; \
make clean all \ make clean all \
TARGET_ARCH='s390x' \ TARGET_ARCH='s390x' \
CC='s390x-linux-gnu-gcc' \ CROSS_COMPILE='s390x-linux-gnu-'
STRIP='s390x-linux-gnu-strip'
RUN find \( -name 'hello' -or -name 'hello.txt' \) -exec file '{}' + -exec ls -lh '{}' + RUN find \( -name 'hello' -or -name 'hello.txt' \) -exec file '{}' + -exec ls -lh '{}' +

View File

@ -1,16 +1,30 @@
TARGET_ARCH := amd64 TARGET_ARCH := amd64
C_TARGETS := $(addsuffix hello, $(wildcard $(TARGET_ARCH)/*/)) C_TARGETS := $(addsuffix hello, $(wildcard $(TARGET_ARCH)/*/))
CC := gcc export CFLAGS := -Os -fdata-sections -ffunction-sections -s
CFLAGS := -static -Os -nostartfiles -fno-asynchronous-unwind-tables STRIP := $(CROSS_COMPILE)strip
STRIP := strip
.PHONY: all .PHONY: all
all: $(C_TARGETS) all: $(C_TARGETS)
$(C_TARGETS): hello.c MUSL_SRC := /usr/local/src/musl
$(CC) $(CFLAGS) -o '$@' -D DOCKER_IMAGE='"$(notdir $(@D))"' -D DOCKER_GREETING="\"$$(cat 'greetings/$(notdir $(@D)).txt')\"" -D DOCKER_ARCH='"$(TARGET_ARCH)"' '$<' MUSL_DIR := $(CURDIR)/musl/$(TARGET_ARCH)
$(STRIP) -R .comment -s '$@' MUSL_PREFIX := $(MUSL_DIR)/prefix
MUSL_GCC := $(MUSL_PREFIX)/bin/musl-gcc
$(MUSL_GCC):
mkdir -p '$(MUSL_DIR)'
cd '$(MUSL_DIR)' && '$(MUSL_SRC)/configure' --disable-shared --prefix='$(MUSL_PREFIX)' > /dev/null
$(MAKE) -C '$(MUSL_DIR)' -j '$(shell nproc)' install > /dev/null
$(C_TARGETS): hello.c $(MUSL_GCC)
$(MUSL_GCC) $(CFLAGS) -Wl,--gc-sections -static \
-o '$@' \
-D DOCKER_IMAGE='"$(notdir $(@D))"' \
-D DOCKER_GREETING="\"$$(cat 'greetings/$(notdir $(@D)).txt')\"" \
-D DOCKER_ARCH='"$(TARGET_ARCH)"' \
'$<'
$(STRIP) --strip-all --remove-section=.comment '$@'
@if [ '$(TARGET_ARCH)' = 'amd64' ]; then \ @if [ '$(TARGET_ARCH)' = 'amd64' ]; then \
for winVariant in \ for winVariant in \
nanoserver-1809 \ nanoserver-1809 \
@ -27,7 +41,7 @@ $(C_TARGETS): hello.c
.PHONY: clean .PHONY: clean
clean: clean:
-rm -vrf $(C_TARGETS) -rm -vrf $(C_TARGETS) $(MUSL_DIR)
.PHONY: test .PHONY: test
test: $(C_TARGETS) test: $(C_TARGETS)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +1,5 @@
//#include <unistd.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h>
#ifndef DOCKER_IMAGE #ifndef DOCKER_IMAGE
#define DOCKER_IMAGE "hello-world" #define DOCKER_IMAGE "hello-world"
@ -37,10 +37,11 @@ const char message[] =
" https://docs.docker.com/get-started/\n" " https://docs.docker.com/get-started/\n"
"\n"; "\n";
void _start() { int main() {
//write(1, message, sizeof(message) - 1); //write(1, message, sizeof(message) - 1);
syscall(SYS_write, 1, message, sizeof(message) - 1); syscall(SYS_write, 1, message, sizeof(message) - 1);
//_exit(0); //_exit(0);
syscall(SYS_exit, 0); //syscall(SYS_exit, 0);
return 0;
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -7,7 +7,7 @@ set -x
docker build -f Dockerfile.build -t hello-world:build . docker build -f Dockerfile.build -t hello-world:build .
rm -rf */hello */nanoserver*/hello.txt find */ \( -name hello -or -name hello.txt \) -delete
docker run --rm hello-world:build sh -c 'find \( -name hello -or -name hello.txt \) -print0 | xargs -0 tar --create' | tar --extract --verbose docker run --rm hello-world:build sh -c 'find \( -name hello -or -name hello.txt \) -print0 | xargs -0 tar --create' | tar --extract --verbose
find -name hello -type f -exec dirname '{}' ';' | xargs -n1 -i'{}' cp Dockerfile-linux.template '{}/Dockerfile' find -name hello -type f -exec dirname '{}' ';' | xargs -n1 -i'{}' cp Dockerfile-linux.template '{}/Dockerfile'