## Emacs, make this -*- mode: sh; -*-

FROM rhub/fedora-clang:latest

MAINTAINER "r-hub admin" admin@r-hub.io

ENV CRAN http://cran.r-project.org

RUN cd /tmp \
    && svn co https://svn.r-project.org/R/trunk R-devel

ENV RPREFIX /opt/R-devel

ENV ROPTIONS --with-x --with-recommended-packages --enable-R-shlib --enable-R-static-lib

ENV CC /usr/bin/clang
ENV CXX /usr/bin/clang++
ENV F77 gfortran
ENV CPP cpp
ENV MAKEFLAGS = -j26

RUN yum -y install rsync
RUN dnf install -y libcxx-devel


RUN cd /tmp/R-devel \
    && ./tools/rsync-recommended \
    && R_PAPERSIZE=letter                                    \
    R_BATCHSAVE="--no-save --no-restore"                     \
    CC="clang -fsanitize=alignment,bool,bounds,builtin,enum,float-cast-overflow,float-divide-by-zero,function,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,return,returns-nonnull-attribute,shift,signed-integer-overflow,undefined,unreachable,unsigned-integer-overflow,unsigned-shift-base,vla-bound -fno-sanitize=float-divide-by-zero" \
    CXX="clang++ -stdlib=libc++ -fsanitize=address,alignment,bool,bounds,builtin,enum,float-cast-overflow,float-divide-by-zero,function,implicit-unsigned-integer-truncation,implicit-signed-integer-truncation,implicit-integer-sign-change,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,return,returns-nonnull-attribute,shift,signed-integer-overflow,undefined,unreachable,unsigned-integer-overflow,unsigned-shift-base,vla-bound -fno-sanitize=float-divide-by-zero -fno-omit-frame-pointer" \
    CFLAGS="-g -O3 -Wall -pedantic -mtune=native" \
    FFLAGS="-g -O2 -mtune=native" \
    FCFLAGS="-g -O2 -mtune=native" \
    CXXFLAGS="-g -O3 -Wall -pedantic -mtune=native" \
    MAIN_LD="clang++ -stdlib=libc++ -fsanitize=undefined,address" \
    R_OPENMP_CFLAGS=-fopenmp \
    ./configure --prefix=${RPREFIX} ${ROPTIONS}              \
    && make                                                  \
    && make install

COPY xvfb-run /usr/local/bin/xvfb-run

RUN chmod +x /usr/local/bin/xvfb-run && \
    rm -f /bin/xvfb-run /usr/bin/xvfb-run

ENV RHUB_PLATFORM linux-x86_64-fedora-clang


# More verbose UBSAN/SAN output (cf #3) -- this is still somewhat speculative
# Entry copied from Prof Ripley's setup described at http://www.stats.ox.ac.uk/pub/bdr/memtests/README.txt
ENV ASAN_OPTIONS 'alloc_dealloc_mismatch=0:detect_leaks=0:detect_odr_violation=0'

ENV PATH=${RPREFIX}/bin:$PATH

RUN cd $RPREFIX/bin \
	&& mv R Rdevel \
	&& cp Rscript Rscriptdevel \
	&& ln -s Rdevel RDsan \
	&& ln -s Rscriptdevel RDscriptsan
	
