#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

ARG DOCKER_TARGET_IMAGE_BUILDENV_WITH_OS_IMAGE

FROM $DOCKER_TARGET_IMAGE_BUILDENV_WITH_OS_IMAGE AS gluten-build
MAINTAINER Hongze Zhang<hongze.zhang@intel.com>

# Whether debug build is enabled
ARG JDK_DEBUG_BUILD
ARG GLUTEN_DEBUG_BUILD
RUN echo "JDK debug build is [$JDK_DEBUG_BUILD]!"
RUN echo "Gluten debug build is [$GLUTEN_DEBUG_BUILD]!"

# If JDK debug is on
RUN if [ "$JDK_DEBUG_BUILD" == "ON" ]; \
    then \
      apt-get update; \
      DEBIAN_FRONTEND=noninteractive apt-get uninstall -y openjdk-8-jdk; \
      DEBIAN_FRONTEND=noninteractive apt-get uninstall -y maven; \
      mkdir -p /opt/jdk/ \
      && mkdir -p /opt/maven/ \
      && cd /opt/jdk/ \
      && wget https://builds.shipilev.net/openjdk-jdk8/openjdk-jdk8-linux-x86_64-server-fastdebug-gcc8-glibc2.28.tar.xz \
      && tar -xvf openjdk-jdk8-linux-x86_64-server-fastdebug-gcc8-glibc2.28.tar.xz \
      && rm -f openjdk-jdk8-linux-x86_64-server-fastdebug-gcc8-glibc2.28.tar.xz \
      && cd /opt/maven/ \
      && wget https://dlcdn.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz \
      && tar -xvf apache-maven-3.6.3-bin.tar.gz \
      && rm -f apache-maven-3.6.3-bin.tar.gz \
      && cp -rs /opt/jdk/j2sdk-image/bin/* /usr/local/bin/ \
      && cp -rs /opt/maven/apache-maven-3.6.3/bin/mvn /usr/local/bin/ \
      && echo "JAVA_HOME=/opt/jdk/j2sdk-image" > ~/.mavenrc; \
    fi

# These branches are mainly for pre-downloading dependencies to speed-up builds. 
# Thus it should not be required to change these values every time when the build branch
# is changed.
ARG CACHE_GLUTEN_REPO
ARG CACHE_GLUTEN_COMMIT

RUN test -n "$CACHE_GLUTEN_REPO" || (echo "CACHE_GLUTEN_REPO not set" && false)
RUN test -n "$CACHE_GLUTEN_COMMIT" || (echo "CACHE_GLUTEN_COMMIT not set" && false)

RUN cd /opt \
    && git clone $CACHE_GLUTEN_REPO gluten \
    && cd gluten \
    && git fetch $CACHE_GLUTEN_REPO $CACHE_GLUTEN_COMMIT:build_$CACHE_GLUTEN_COMMIT \
    && git checkout build_$CACHE_GLUTEN_COMMIT

# Set ccache size
RUN ccache -M 128G
RUN ccache -s

# Default Gluten Maven build options (empty as of now)
ENV GLUTEN_MAVEN_OPTIONS=
RUN set-login-env "GLUTEN_MAVEN_OPTIONS="

ARG BUILD_BACKEND_TYPE

RUN test -n "$BUILD_BACKEND_TYPE" || (echo "BUILD_BACKEND_TYPE not set" && false)

RUN if [ "$BUILD_BACKEND_TYPE" == "velox" ]; \
    then \
      if [ "$GLUTEN_DEBUG_BUILD" == "ON" ]; then GLUTEN_BUILD_TYPE="Debug"; else GLUTEN_BUILD_TYPE="Release"; fi; \
      DEPS_INSTALL_SCRIPT="bash /opt/gluten/dev/builddeps-veloxbe.sh \
                           --enable_hdfs=ON --enable_s3=ON --enable_gcs=ON --enable_abfs=ON \
                           --build_type=$GLUTEN_BUILD_TYPE"; \
      EXTRA_MAVEN_OPTIONS="-Pspark-3.2 \
                           -Pbackends-velox \
                           -Pceleborn \
                           -Puniffle \
                           -Piceberg \
                           -Pdelta \
                           -DskipTests \
                           -Dscalastyle.skip=true \
                           -Dcheckstyle.skip=true"; \
    else \
      echo "Unrecognizable backend type: $BUILD_BACKEND_TYPE"; \
      exit 1; \
    fi \
    && echo $EXTRA_MAVEN_OPTIONS > ~/.gluten-mvn-options \
    && echo $DEPS_INSTALL_SCRIPT > ~/.gluten-deps-install-script

# Prebuild Gluten
RUN EXTRA_MAVEN_OPTIONS=$(cat ~/.gluten-mvn-options) \
    DEPS_INSTALL_SCRIPT=$(cat ~/.gluten-deps-install-script) \
    && cd /opt/gluten \
    && bash -c "$DEPS_INSTALL_SCRIPT" \
    && bash -c "mvn clean install $GLUTEN_MAVEN_OPTIONS $EXTRA_MAVEN_OPTIONS"

# EOF
