# This will build R-devel with LTO, debug symbols but no compiler
# optimizations.  It would run R CMD check for a given package.  Created for
# debugging issues with accesses to elements of empty R vectors.
#
# Supplied by Tomas Kalibera 2024-12-05
#
#
# Arguments intended for modification:
# 
# PKG: CRAN package to check
# RVER: svn version of R-devel to use
# JOBS: number of parallel jobs in builds
#
#
# Example: to check CRAN package timeplyr
#
#     docker build -t fflto -f debian_testing --build-arg PKG=ff .
#
#     the output ends with these lines from a failed R CMD check:
#
#  51: test_check("ff")
#  An irrecoverable exception occurred. R is aborting now ...
#  Segmentation fault (core dumped)
# * checking PDF version of manual ... OK
# * DONE
#
# Status: 1 ERROR
# See
#  '/test/ff.Rcheck/00check.log'
# for details.
#
#     to explore, enter the container via
#
#        docker run -it fflto bash
#
#     this opens a command prompt inside the debian testing installation
#     where the test failed. To see the full output, run
#
#        apt-get update
#        apt-get install less
#        less test/ff.Rcheck/00check.log
#        less test/ff.Rcheck/tests/testthat.Rout.fail
#
#     to run the failing test in a debugger, do the following
#
#        apt-get install gdb
#        cd test/ff.Rcheck/tests
#        ../../../build/bin/R -d gdb 
#          r
#          library(testthat)
#          test_check("ff")
#
#     this will end up in a segfault, in gdb
#
# To install additional dependencies (debian packages) already as part of
# docker build, modify this docker file accordingly.  See e.g.  how rsync is
# installed.  It is better to add the docker command as much below as
# possible to re-use the previous steps in the docker file from the cache.
#

FROM debian:testing
LABEL maintainer tomas.kalibera@gmail.com

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
  apt-get -y upgrade && \
  apt-get install -yq --no-install-recommends apt-utils && \
  rm -rf /var/lib/apt/lists/* && \
  apt autoremove -y

ENV TZ=Europe/Prague

# Install R build dependencies
RUN apt-get update && \
  sed -i 's/^Types: deb/\0 deb-src/g' /etc/apt/sources.list.d/debian.sources && \
  apt-get update && \
  apt-get -y build-dep r-base && \
  apt-get -yq install rsync subversion && \
  apt-get -yq install libpcre2-dev locales-all && \
  rm -rf /var/lib/apt/lists/* && \
  apt autoremove -y

# Checkout R
RUN svn checkout https://svn.r-project.org/R/trunk && \
  cd trunk && \
  ./tools/rsync-recommended && \
  cd ..

ARG RVER=87417
ARG JOBS=20

RUN cd trunk && \
  svn up -r $RVER && \
  ./tools/rsync-recommended && \
  cd ..

ENV RGL_USE_NULL=true
ENV R_DONT_USE_TK=true

# Configure R
RUN  mkdir build && \
  cd build && \
  ../trunk/configure --enable-lto \
  CFLAGS="-ggdb -O0" \
  CXXFLAGS="-ggdb -O0" \
  FFLAGS="-ggdb -O0" \
  2>&1 | tee configure.out && \
  cd ..

# Build R
RUN cd build && \
  make -j ${JOBS} 2>&1 | tee make.out && \
  echo 'options(repos = c(CRAN="https://cran.r-project.org"))' > etc/Rprofile.site && \
  cd ..

ARG PKG=ff

# Install package and dependencies
#   intentionally made to pass even when R CMD check fails, so that
#   the container can be easily re-entered
RUN mkdir -p test && \
  cd test && \
  ../build/bin/Rscript -e "install.packages(\"$PKG\", dependencies=T, Ncpus=$JOBS)" && \
  ../build/bin/Rscript -e "download.packages(\"$PKG\", destdir=\".\")" && \
  ../build/bin/R CMD check $PKG_*gz || true && \
  cd ..
