
ARG R_MAJOR=4.1

FROM ghcr.io/r-hub/r-minimal/r-minimal:${R_MAJOR} as build

WORKDIR /root

# system requirements -----------------------------------------------------

RUN apk add linux-headers bash gcc musl-dev g++ pkgconf

# zlib --------------------------------------------------------------------

RUN wget https://downloads.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
RUN tar xzf zlib-*.tar.gz && rm zlib-*.tar.gz
RUN cd zlib-* &&                                                     \
    CFLAGS=-fPIC ./configure --static &&                             \
    make &&                                                          \
    make install

# openssl -----------------------------------------------------------------

RUN wget https://www.openssl.org/source/openssl-1.1.1m.tar.gz
RUN tar xzf openssl-*.tar.gz && rm openssl-*.tar.gz
RUN apk add perl linux-headers
RUN cd openssl-* &&                                                  \
    CFLAGS="-fPIC -static -U__GNUC__" ./config -fPIC no-shared &&    \
    make &&                                                          \
    make install_sw &&                                               \
    rm -rf /usr/local/bin/openssl                                    \
       /usr/local/share/{man/doc}

# libcurl now -------------------------------------------------------------

RUN wget https://curl.haxx.se/download/curl-7.68.0.tar.gz
RUN tar xzf curl-*.tar.gz && rm curl-*.tar.gz
RUN apk add pkgconfig
RUN cd curl-* && \
    if [ "`uname -m`" = "aarch64" ]; then                            \
      ./configure --enable-static --disable-shared --enable-debug;   \
    else                                                             \
      ./configure --enable-static --disable-shared;                  \
    fi &&                                                            \
    make &&                                                          \
    make install &&                                                  \
    rm -rf /usr/local/bin/curl                                       \
       /usr/local/share/{man/doc}

# =========================================================================

FROM ghcr.io/r-hub/r-minimal/r-minimal:${R_MAJOR}

COPY --from=build /usr/local /usr/local

WORKDIR /root

# -------------------------------------------------------------------------
# set up static compilation
# -------------------------------------------------------------------------

RUN mkdir -p /root/.R
COPY tools/build/linux/Makevars-builder /root/.R/Makevars

# -------------------------------------------------------------------------
# Need a proper cp command for older R versions
# -------------------------------------------------------------------------

RUN apk add --no-cache coreutils findutils
