# ghcr.io/r-lib/pak-libs-glibc:latest

# Compile a static libcurl (and its dependencies: OpenSSL and zlib) and
# install them into /opt/r-lib/. Dockerfiles can then build on this container
# by copying over /opt/r-lib/.
#
# We build on Ubuntu 18.04 (bionic), so the resulting pak binary is portable
# to any glibc >= 2.27. We use newer OpenSSL and libcurl than bionic ships,
# following the r-glibc Dockerfile:
# https://github.com/r-hub/r-glibc/blob/main/dockerfiles/bionic/Dockerfile
#
# This builds natively, so it works on both x86_64 and aarch64, just build
# it on the appropriate machine.

FROM ubuntu:18.04 AS build

USER root
WORKDIR /root
ENV TZ=UTC

RUN apt-get update && \
    apt-get install -y \
      build-essential g++ curl perl pkg-config

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

RUN curl -LO https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz
RUN tar xzf zlib-*.tar.gz && rm zlib-*.tar.gz
RUN cd zlib-* &&                                                     \
    CFLAGS=-fPIC ./configure --static --prefix=/opt/r-lib &&         \
    make &&                                                          \
    make install

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

RUN curl -LO https://github.com/openssl/openssl/releases/download/openssl-3.5.7/openssl-3.5.7.tar.gz
RUN tar xzf openssl-3.5.7.tar.gz && rm openssl-3.5.7.tar.gz
RUN cd openssl-* &&                                                  \
    CFLAGS="-fPIC" ./config -fPIC no-shared --prefix=/opt/r-lib &&   \
    make &&                                                          \
    make install_sw &&                                              \
    rm -rf /opt/r-lib/bin/openssl

# OpenSSL installs its libraries and pkg-config files into lib64 on 64-bit
# platforms; move them next to zlib and libcurl so a single
# PKG_CONFIG_PATH=/opt/r-lib/lib/pkgconfig finds everything.

RUN if [ -d /opt/r-lib/lib64 ]; then                                 \
      cp -a /opt/r-lib/lib64/*.a /opt/r-lib/lib/ &&                   \
      mkdir -p /opt/r-lib/lib/pkgconfig &&                           \
      cp -a /opt/r-lib/lib64/pkgconfig/* /opt/r-lib/lib/pkgconfig/;   \
    fi

# libcurl -----------------------------------------------------------------

RUN curl -LO https://github.com/curl/curl/releases/download/curl-8_21_0/curl-8.21.0.tar.gz
RUN tar xzf curl-*.tar.gz && rm curl-*.tar.gz
RUN cd curl-* &&                                                     \
    CFLAGS="-fPIC" CPPFLAGS="-I/opt/r-lib/include"                    \
    LDFLAGS="-L/opt/r-lib/lib"                                        \
    ./configure --prefix=/opt/r-lib                                  \
      --enable-static --disable-shared --with-openssl=/opt/r-lib      \
      --with-zlib=/opt/r-lib --without-libpsl --without-brotli &&    \
    make &&                                                          \
    make install &&                                                  \
    rm -rf /opt/r-lib/bin/curl

# =========================================================================
# We don't need to keep the compilation artifacts, so copy the results
# to a clean image

FROM ubuntu:18.04
COPY --from=build /opt/r-lib /opt/r-lib/

# Some of this info is shown on the GH packages pages

LABEL org.opencontainers.image.source="https://github.com/r-lib/pak"
LABEL org.opencontainers.image.description="This image builds and contains \
    static glibc libraries for libcurl and its dependencies. See \
    https://github.com/r-lib/pak/blob/main/tools/build/linux/Dockerfile-libs-glibc \
    for details."
LABEL org.opencontainers.image.authors="https://github.com/gaborcsardi"
