From 5910baaf299f21ef4130f7f6429f9519d1eedf3e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=89rico=20Nogueira?= <erico.rolim@lnls.br>
Date: Mon, 27 Nov 2023 08:44:56 -0300
Subject: [PATCH] base: add musl build.

This is intended mainly for building fully static IOCs to be deployed on
older machines. We are using Alpine since it's the main musl-based
distro with LTS releases.

We also document it in README and CHANGES.
---
 CHANGES.md                   |  2 ++
 README.md                    | 10 +++++++
 base/.env                    |  1 +
 base/musl/.env               |  1 +
 base/musl/Dockerfile         | 55 ++++++++++++++++++++++++++++++++++++
 base/musl/docker-compose.yml | 24 ++++++++++++++++
 6 files changed, 93 insertions(+)
 create mode 120000 base/musl/.env
 create mode 100644 base/musl/Dockerfile
 create mode 100644 base/musl/docker-compose.yml

diff --git a/CHANGES.md b/CHANGES.md
index 8306458..9314956 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -11,6 +11,8 @@
   * This copies libraries built from source in the build image to the runtime
     image for all targets. This removes the need to use `RUNTIME_TAR_PACKAGES`
     for local libraries required during a module build.
+* base: add musl build. by @ericonr in
+  https://github.com/cnpem/epics-in-docker/pull/35
 
 ## v0.4.0
 
diff --git a/README.md b/README.md
index 04f67f5..9121d19 100644
--- a/README.md
+++ b/README.md
@@ -92,3 +92,13 @@ appropriate):
 ```
 $ docker exec -ti <container> nc -U ioc.sock
 ```
+
+## Alpine base image
+
+This alternative base image can be used to build fully static IOCs for
+scenarios where containerized deployment isn't an option or isn't desired for
+some reason. It should also be able to cope better than the Debian-based
+default image with older kernels.
+
+It can be obtained directly from the [GitHub
+registry](https://github.com/cnpem/epics-in-docker/pkgs/container/lnls-alpine-3.18-epics-7).
diff --git a/base/.env b/base/.env
index adf5fec..b7886e3 100644
--- a/base/.env
+++ b/base/.env
@@ -1,4 +1,5 @@
 DEBIAN_VERSION=11.7
+ALPINE_VERSION=3.18.4
 
 EPICS7_BASE_VERSION=7.0.7
 
diff --git a/base/musl/.env b/base/musl/.env
new file mode 120000
index 0000000..4a82335
--- /dev/null
+++ b/base/musl/.env
@@ -0,0 +1 @@
+../.env
\ No newline at end of file
diff --git a/base/musl/Dockerfile b/base/musl/Dockerfile
new file mode 100644
index 0000000..bb01a41
--- /dev/null
+++ b/base/musl/Dockerfile
@@ -0,0 +1,55 @@
+ARG ALPINE_VERSION
+
+FROM alpine:$ALPINE_VERSION
+
+ARG JOBS
+
+RUN apk add --no-cache \
+    bash \
+    g++ \
+    libevent-dev \
+    libevent-static \
+    libtirpc-dev \
+    libtirpc-static \
+    linux-headers \
+    make \
+    musl-dev \
+    ncurses-terminfo \
+    patch \
+    perl \
+    rpcsvc-proto \
+    gcc \
+    meson \
+    re2c \
+    readline-dev \
+    readline-static
+
+COPY lnls-get-n-unpack.sh /usr/local/bin/lnls-get-n-unpack
+COPY lnls-run.sh /usr/local/bin/lnls-run
+
+ARG EPICS_BASE_VERSION
+ENV EPICS_BASE_PATH /opt/epics/base
+ENV EPICS_MODULES_PATH /opt/epics/modules
+ENV EPICS_RELEASE_FILE /opt/epics/RELEASE
+
+WORKDIR /opt/epics
+COPY install-functions.sh .
+
+COPY backport-epics-base-musl.patch .
+COPY install_epics.sh .
+RUN ./install_epics.sh
+
+ARG SEQUENCER_VERSION
+ARG CALC_VERSION
+ARG ASYN_VERSION
+ARG STREAMDEVICE_VERSION
+ARG BUSY_VERSION
+ARG AUTOSAVE_VERSION
+ARG SSCAN_VERSION
+ARG RECCASTER_VERSION
+ARG IPAC_VERSION
+ARG CAPUTLOG_VERSION
+
+WORKDIR ${EPICS_MODULES_PATH}
+COPY install_modules.sh .
+RUN NEEDS_TIRPC=YES ./install_modules.sh
diff --git a/base/musl/docker-compose.yml b/base/musl/docker-compose.yml
new file mode 100644
index 0000000..1e41242
--- /dev/null
+++ b/base/musl/docker-compose.yml
@@ -0,0 +1,24 @@
+services:
+  epics-base:
+    image: ${REGISTRY:-ghcr.io/cnpem}/lnls-alpine-3.18-epics-7:$TAG
+    build:
+      context: ../
+      dockerfile: musl/Dockerfile
+      labels:
+        org.opencontainers.image.revision: ${TAG}
+        org.opencontainers.image.source: ${SOURCE:-https://github.com/cnpem/epics-in-docker}
+        org.opencontainers.image.description: "EPICS base and modules build image for fully static executables"
+      args:
+        JOBS: ${JOBS:-1}
+        ALPINE_VERSION: ${ALPINE_VERSION}
+        EPICS_BASE_VERSION: ${EPICS7_BASE_VERSION}
+        SEQUENCER_VERSION: ${SEQUENCER_VERSION}
+        CALC_VERSION: ${CALC_VERSION}
+        ASYN_VERSION: ${ASYN_VERSION}
+        STREAMDEVICE_VERSION: ${STREAMDEVICE_VERSION}
+        BUSY_VERSION: ${BUSY_VERSION}
+        AUTOSAVE_VERSION: ${AUTOSAVE_VERSION}
+        SSCAN_VERSION: ${SSCAN_VERSION}
+        RECCASTER_VERSION: ${RECCASTER_VERSION}
+        IPAC_VERSION: ${IPAC_VERSION}
+        CAPUTLOG_VERSION: ${CAPUTLOG_VERSION}