# Note: there are docker-compose .yml files that handle the 3
# build steps automatically for you; see the comments in the
# respective docker-compose files for details.
# However, if you prefer to run individual docker commands rather
# than using docker-compose, you can do so by following the
# instructions below.
#
# Example usage of this Dockerfile:
# (the --build-arg arguments are all optional)
#
# 1. cd to Code/MinimalLib/docker
# cd Code/MinimalLib/docker
#
# 2. build the MinimalLib rdkit-minimallib-deps image:
# docker build --target deps-stage -t rdkit-minimallib-deps \
#   --build-arg http_proxy=$HTTP_PROXY \
#   --build-arg https_proxy=$HTTP_PROXY \
#   --network=host --build-arg "EXCEPTION_HANDLING=-fwasm-exceptions" \
#   -f Dockerfile_1_deps .
#
# 3. build the MinimalLib rdkit-minimallib-rdkit-src image:
#   3a. from a git clone:
#   docker build --target clone-stage -t rdkit-minimallib-rdkit-src \
#     --build-arg http_proxy=$HTTP_PROXY \
#     --build-arg https_proxy=$HTTP_PROXY \
#     --build-arg "RDKIT_GIT_URL=https://github.com/myfork/rdkit.git" \
#     --build-arg "RDKIT_BRANCH=mybranch" \
#     --network=host -f Dockerfile_rdkit_clone_from_github .
#   or
#   3b. from an existing local source tree:
#   docker build --target local-src-stage -t rdkit-minimallib-rdkit-src \
#     --build-arg http_proxy=$HTTP_PROXY \
#     --build-arg https_proxy=$HTTP_PROXY \
#     --network=host -f Dockerfile_2_rdkit_copy_from_local ../../..
#
# 4. build the MinimalLib rdkit-minimallib image:
#    (the build-arg arguments are all optional; in the following
#     example we select the more performant, though still experimental,
#     native WASM exception handling):
# docker build -t rdkit-minimallib --network=host \
#   --build-arg http_proxy=$HTTP_PROXY \
#   --build-arg https_proxy=$HTTP_PROXY \
#   --build-arg "EXCEPTION_HANDLING=-fwasm-exceptions" \
#   -f Dockerfile_3_rdkit_build .
#
# 5. create a temporary container and copy built libraries
#    from the container to your local filesystem, then destroy
#    the temporary container
# docker create --name=rdkit-minimallib-container rdkit-minimallib:latest --entrypoint /
# docker cp rdkit-minimallib-container:/RDKit_minimal.js ../demo
# docker cp rdkit-minimallib-container:/RDKit_minimal.wasm ../demo
# docker rm rdkit-minimallib-container


ARG EXCEPTION_HANDLING="-fexceptions -sNO_DISABLE_EXCEPTION_CATCHING"

FROM rdkit-minimallib-rdkit-src AS build-stage
ARG EXCEPTION_HANDLING

LABEL maintainer="Greg Landrum <greg.landrum@t5informatics.com>"

WORKDIR /src
ENV RDBASE=/src/rdkit
RUN mkdir build
WORKDIR $RDBASE/build
RUN emcmake cmake -DRDK_BUILD_FREETYPE_SUPPORT=ON -DRDK_BUILD_MINIMAL_LIB=ON \
  -DRDK_BUILD_PYTHON_WRAPPERS=OFF -DRDK_BUILD_CPP_TESTS=OFF -DRDK_BUILD_INCHI_SUPPORT=ON \
  -DRDK_USE_BOOST_SERIALIZATION=OFF -DRDK_OPTIMIZE_POPCNT=OFF -DRDK_BUILD_THREADSAFE_SSS=OFF \
  -DRDK_BUILD_DESCRIPTORS3D=OFF -DRDK_TEST_MULTITHREADED=OFF \
  -DRDK_BUILD_CHEMDRAW_SUPPORT=OFF -DRDK_BUILD_MAEPARSER_SUPPORT=OFF -DRDK_BUILD_COORDGEN_SUPPORT=ON \
  -DRDK_BUILD_MINIMAL_LIB_MCS=ON -DRDK_BUILD_MINIMAL_LIB_MOLZIP=ON \
  -DRDK_BUILD_MINIMAL_LIB_MMPA=ON \
  -DBoost_DIR=/opt/boost/lib/cmake/Boost-${BOOST_DOT_VERSION} \
  -Dboost_headers_DIR=/opt/boost/lib/cmake/boost_headers-${BOOST_DOT_VERSION} \
  -DRDK_BUILD_SLN_SUPPORT=OFF -DRDK_USE_BOOST_IOSTREAMS=OFF \
  -DFREETYPE_INCLUDE_DIRS=/opt/freetype/include/freetype2 \
  -DFREETYPE_LIBRARY=/opt/freetype/lib/libfreetype.a \
  -DZLIB_INCLUDE_DIR=/opt/zlib/include \
  -DZLIB_LIBRARY=/opt/zlib/lib/libz.a \
  -DCMAKE_CXX_FLAGS="${EXCEPTION_HANDLING} -O3 -DNDEBUG" \
  -DCMAKE_C_FLAGS="${EXCEPTION_HANDLING} -O3 -DNDEBUG -DCOMPILE_ANSI_ONLY" \
  -DCMAKE_EXE_LINKER_FLAGS="${EXCEPTION_HANDLING} -s STACK_OVERFLOW_CHECK=1 -s USE_PTHREADS=0 -s ALLOW_MEMORY_GROWTH=1 -s MAXIMUM_MEMORY=4GB -s MODULARIZE=1 -s EXPORT_NAME=\"'initRDKitModule'\"" ..

# "patch" to make the InChI code work with emscripten:
RUN cp /src/rdkit/External/INCHI-API/src/INCHI_BASE/src/util.c /src/rdkit/External/INCHI-API/src/INCHI_BASE/src/util.c.bak && \
  sed 's/&& defined(__APPLE__)//' /src/rdkit/External/INCHI-API/src/INCHI_BASE/src/util.c.bak > /src/rdkit/External/INCHI-API/src/INCHI_BASE/src/util.c

# build and "install"
RUN make -j2 RDKit_minimal && \
  cp Code/MinimalLib/RDKit_minimal.* ../Code/MinimalLib/demo/

# run the tests
WORKDIR /src/rdkit/Code/MinimalLib/tests
RUN /opt/emsdk/node/*/bin/node tests.js

# Copy js and wasm rdkit files to use in browser
# This feature requires the BuildKit backend
# https://docs.docker.com/engine/reference/commandline/build/#custom-build-outputs
FROM scratch as export-stage
COPY --from=build-stage /src/rdkit/Code/MinimalLib/demo /
COPY --from=build-stage /src/rdkit/Code/MinimalLib/docs /
