FROM dockcross/base:latest

LABEL maintainer="Matt McCormick matt.mccormick@kitware.com"

# Crosstool-ng version
# We use a git commit while waiting for 1.25 to release (1.24 is several years old)
# crosstool-ng master 2021-09-20
ENV CT_VERSION 5f5a22f7a7307790ec571d0c5acf8790416964e6

ARG QEMU_VERSION=6.0.0

#include "common.crosstool"

ENV CROSS_TRIPLE powerpc64le-unknown-linux-gnu

WORKDIR /usr/src

RUN apt-get install -y libglib2.0-dev zlib1g-dev libpixman-1-dev && \
  curl -L http://wiki.qemu-project.org/download/qemu-${QEMU_VERSION}.tar.bz2 | tar xj && \
  cd qemu-${QEMU_VERSION} && \
  ./configure --target-list=ppc64le-linux-user --prefix=/usr && \
  make -j$(nproc) && \
  make install && \
  cd .. && rm -rf qemu-${QEMU_VERSION}

ENV CROSS_ROOT ${XCC_PREFIX}/${CROSS_TRIPLE}
ENV AS=${CROSS_ROOT}/bin/${CROSS_TRIPLE}-as \
    AR=${CROSS_ROOT}/bin/${CROSS_TRIPLE}-ar \
    CC=${CROSS_ROOT}/bin/${CROSS_TRIPLE}-gcc \
    CPP=${CROSS_ROOT}/bin/${CROSS_TRIPLE}-cpp \
    CXX=${CROSS_ROOT}/bin/${CROSS_TRIPLE}-g++ \
    LD=${CROSS_ROOT}/bin/${CROSS_TRIPLE}-ld \
    FC=${CROSS_ROOT}/bin/${CROSS_TRIPLE}-gfortran

ENV QEMU_LD_PREFIX "${CROSS_ROOT}/${CROSS_TRIPLE}/sysroot"
ENV QEMU_SET_ENV "LD_LIBRARY_PATH=${CROSS_ROOT}/lib:${QEMU_LD_PREFIX}"

WORKDIR /work

COPY Toolchain.cmake /usr/lib/${CROSS_TRIPLE}/
ENV CMAKE_TOOLCHAIN_FILE /usr/lib/${CROSS_TRIPLE}/Toolchain.cmake

ENV PKG_CONFIG_PATH /usr/lib/powerpc64le-unknown-linux-gnu/pkgconfig

# Linux kernel cross compilation variables
ENV PATH ${PATH}:${CROSS_ROOT}/bin
ENV CROSS_COMPILE ${CROSS_TRIPLE}-
ENV ARCH powerpc

#include "common.label-and-env"