# Dockerfile for SIMDe development

FROM debian:unstable-slim

ARG DEBIAN_FRONTEND=noninteractive

RUN \
  apt-get update -y && \
  apt-get upgrade -y && \
  apt-get install -y curl gpg && \
  curl -s "https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB" | gpg --dearmor > /etc/apt/trusted.gpg.d/intel.gpg && \
  echo "deb [arch=amd64] https://apt.repos.intel.com/oneapi all main" > /etc/apt/sources.list.d/oneAPI.list && \
  for arch in armhf arm64 ppc64el s390x i386 mips64el; do \
    dpkg --add-architecture "$arch"; \
  done

RUN \
  apt-get update -y && \
  apt-get install -y \
    git build-essential \
    meson cmake \
    '^clang-[0-9]+$' \
    '^g(cc|\+\+)-[0-9]+$' \
    intel-oneapi-dpcpp-cpp-compiler-pro \
    qemu binfmt-support qemu-user-static \
    creduce screen htop parallel nano rsync strace \
    npm libsleef-dev

RUN \
  apt-get update -y && \
  for arch in `dpkg --print-foreign-architectures`; do \
    echo libc6:"${arch}"; \
    for version in $(apt-cache pkgnames | grep -Po '(?<=gcc\-)([0-9]+)$'); do \
      echo libstdc++-${version}-dev:"${arch}"; \
    done; \
  done | xargs apt-get install -y

RUN \
  apt-get update -y && \
  for arch in $(dpkg --print-foreign-architectures); do \
    triplet="$(dpkg -L libc6:${arch} | grep -Po '(?<=/usr/lib/)[^/]+$' | sed -e 's/i386/i686/')"; \
    apt-get install -y "^g(cc|\+\+)(\-[0-9]+)-${triplet}$"; \
  done

# Install emscripten
RUN \
  git clone https://github.com/emscripten-core/emsdk.git /opt/emsdk && \
  cd /opt/emsdk && ./emsdk update-tags && ./emsdk install tot && ./emsdk activate tot && \
  ln -s /opt/emsdk/upstream/bin/wasm-ld /usr/bin/wasm-ld && \
  npm install jsvu -g && jsvu --os=linux64 --engines=v8 && ln -s "/root/.jsvu/v8" "/usr/bin/v8"

# # xlc -- Install fails.
# # Once IBM releases a version for Ubuntu Focal (20.04) I hope I can
# # get this working.
# RUN \
#   curl -s 'https://public.dhe.ibm.com/software/server/POWER/Linux/xl-compiler/eval/ppc64le/ubuntu/public.gpg' | apt-key add - && \
#   echo "deb [arch=ppc64el] https://public.dhe.ibm.com/software/server/POWER/Linux/xl-compiler/eval/ppc64le/ubuntu/ bionic main" > /etc/apt/sources.list.d/xlc.list && \
#   apt-get update && \
#   XLC_VERSION="$(apt-cache search '^xlc\.[0-9]+\.[0-9]+\.[0-9]+$' | awk '{ print substr($1, 5) }')" && \
#   apt-get install "xlc.${XLC_VERSION}:ppc64el" "xlc-license-community.${XLC_VERSION}:ppc64el" && \
#   /opt/ibm/xlC/${XLC_VERSION}/bin/xlc_configure <<< 1 >/dev/null

# Wrappers for ICC
RUN \
    for exe in icc icpc; do \
      printf '#!/bin/bash\nARGS="$@"\nsource /opt/intel/oneapi/compiler/latest/env/vars.sh >/dev/null\n%s ${ARGS}\n' "${exe}" > /usr/bin/"${exe}" && \
      chmod 0755 /usr/bin/"${exe}" ; \
    done

# Install Intel SDE
COPY test/download-sde.sh /usr/local/bin/download-sde.sh
RUN \
  "/usr/local/bin/download-sde.sh" "/opt/intel/sde" && \
  for executable in sde sde64; do \
    ln -s "/opt/intel/sde/${executable}" "/usr/bin/${executable}"; \
  done

# Copy Meson cross files
# COPY docker/cross-files/ /usr/local/share/meson/cross/simde
RUN mkdir -p "/usr/local/share/meson/cross" && ln -s /usr/local/src/simde/docker/cross-files /usr/local/share/meson/cross/simde

# Copy helper scripts
RUN \
  for script in simde-reset-build.sh; do \
    ln -s /usr/local/src/simde/docker/bin/"${script}" /usr/bin/"${script}"; \
  done

RUN mkdir -p /opt/simde
WORKDIR /opt/simde
