FROM quay.io/pypa/manylinux2014_x86_64:latest

ENV PATH /opt/rh/devtoolset-10/root/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ADD scripts /tmp/scripts
RUN cd /tmp/scripts && /tmp/scripts/manylinux/install_centos.sh && /tmp/scripts/manylinux/install_deps.sh && rm -rf /tmp/scripts

ARG PYTHON_VER_PATH="cp38-cp38"
ARG PYTHON_VERSION="3.8"
ARG BUILD_UID=1001
ARG BUILD_USER=onnxruntimedev
ARG OV_DEVICE_PRECISION="CPU_FP32"
ARG ENABLE_TRAINING=true
ARG ORT_BRANCH="rel-1.13.1"
ARG OV_VERSION="2022.2.0"
RUN adduser --uid $BUILD_UID $BUILD_USER
WORKDIR /home/$BUILD_USER
ENV PYTHON_EXE="/opt/python/$PYTHON_VER_PATH/bin/python$PYTHON_VERSION"

RUN yum -y install wget git

# libusb1.0.22
RUN cd /home/ && wget https://github.com/libusb/libusb/archive/v1.0.22.zip && \
    unzip v1.0.22.zip && rm -rf v1.0.22.zip && cd  /home/libusb-1.0.22 && \
# bootstrap steps
    ./bootstrap.sh && \
    ./configure --disable-udev --enable-shared && \
    make -j4 && \
# configure libusb1.0.22
    cd /home/libusb-1.0.22/libusb && \
    /bin/mkdir -p '/usr/local/lib' && \
    /bin/bash ../libtool   --mode=install /usr/bin/install -c   libusb-1.0.la '/usr/local/lib' && \
    /bin/mkdir -p '/usr/local/include/libusb-1.0' && \
    /usr/bin/install -c -m 644 libusb.h '/usr/local/include/libusb-1.0' && \
    /bin/mkdir -p '/usr/local/lib/pkgconfig'

RUN ${PYTHON_EXE} -m pip install onnx numpy wheel
USER $BUILD_USER
RUN cd $WORKDIR && git clone https://github.com/openvinotoolkit/openvino.git && \
    cd openvino && \
    git checkout $OV_VERSION && \
    git submodule init && \
    git submodule update --recursive

RUN cd $WORKDIR && cd openvino && mkdir build && cd build && \
    cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI=0 -DENABLE_PYTHON=ON -DPYTHON_EXECUTABLE=$PYTHON_EXE -DCMAKE_INSTALL_PREFIX=/home/onnxruntimedev/openvino_$OV_VERSION && \
    make -j8 && make install

ENV INTEL_OPENVINO_DIR /home/onnxruntimedev/openvino_$OV_VERSION
ENV LD_LIBRARY_PATH $INTEL_OPENVINO_DIR/runtime/lib/intel64:$INTEL_OPENVINO_DIR/runtime/3rdparty/tbb/lib:/usr/local/openblas/lib:$LD_LIBRARY_PATH
ENV TBB_LIBS $INTEL_OPENVINO_DIR/runtime/3rdparty/tbb/lib
ENV InferenceEngine_DIR $INTEL_OPENVINO_DIR/runtime/cmake
ENV ngraph_DIR $INTEL_OPENVINO_DIR/runtime/cmake
ENV IE_PLUGINS_PATH $INTEL_OPENVINO_DIR/runtime/lib/intel64
ENV OPENVINO_MANYLINUX 1

RUN cd $WORKDIR && \
    git clone --recursive -b $ORT_BRANCH https://github.com/intel/onnxruntime.git
RUN cd onnxruntime/onnxruntime/core/providers/openvino && mkdir scripts

RUN cp ${IE_PLUGINS_PATH}/libopenvino.so /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${IE_PLUGINS_PATH}/libopenvino_c.so /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${IE_PLUGINS_PATH}/libopenvino_onnx_frontend.so /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${IE_PLUGINS_PATH}/libopenvino_intel_cpu_plugin.so /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${IE_PLUGINS_PATH}/libopenvino_intel_gpu_plugin.so /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${IE_PLUGINS_PATH}/libopenvino_intel_myriad_plugin.so /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${IE_PLUGINS_PATH}/libopenvino_hetero_plugin.so /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${IE_PLUGINS_PATH}/libopenvino_auto_plugin.so /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${IE_PLUGINS_PATH}/plugins.xml /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${IE_PLUGINS_PATH}/usb-ma2x8x.mvcmd /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${TBB_LIBS}/libtbb.so /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${TBB_LIBS}/libtbb.so.2 /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${TBB_LIBS}/libtbbmalloc.so /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cp ${TBB_LIBS}/libtbbmalloc.so.2 /home/onnxruntimedev/onnxruntime/onnxruntime/core/providers/openvino/scripts/
RUN cd /home/onnxruntimedev/onnxruntime && git pull
RUN if $ENABLE_TRAINING; then \
        ${PYTHON_EXE} ./onnxruntime/tools/ci_build/build.py \
        --build_dir ./onnxruntime/build --use_openvino $(OV_DEVICE_PRECISION) --build_shared_lib \
        --config Release --build_wheel --skip_tests --enable_training ; \
    else \
        ${PYTHON_EXE} ./onnxruntime/tools/ci_build/build.py \
        --build_dir ./onnxruntime/build --use_openvino $(OV_DEVICE_PRECISION) --build_shared_lib \
        --config Release --build_wheel --skip_tests ;\
    fi
