#
# 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_BUILD_WITH_OS_IMAGE

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

# Incrementally build Gluten against target commit based on previous build
ARG TARGET_GLUTEN_REPO
ARG TARGET_GLUTEN_COMMIT

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

RUN cd /opt/gluten \
    && git fetch $TARGET_GLUTEN_REPO $TARGET_GLUTEN_COMMIT:build_$TARGET_GLUTEN_COMMIT \
    && git checkout build_$TARGET_GLUTEN_COMMIT

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" \
    && bash -c "ln -sf /opt/gluten/ep/build-velox/build/velox_ep /opt/velox"

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends ssh net-tools vim unzip libjemalloc-dev

ARG BUILD_BACKEND_TYPE

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

RUN GLUTEN_IT_MAVEN_OPTIONS="-Pspark-3.2" \
    && echo $GLUTEN_IT_MAVEN_OPTIONS > ~/.gluten-it-mvn-options;

RUN GLUTEN_IT_MAVEN_OPTIONS=$(cat ~/.gluten-it-mvn-options) \
    && cd /opt/gluten/tools/gluten-it \
    && bash -c "mvn clean install $GLUTEN_IT_MAVEN_OPTIONS"

FROM gluten-tpc AS gluten-tpc-gdb

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends gdb

FROM gluten-tpc-gdb AS gluten-tpc-gdb-server

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends gdbserver

# EOF
