# dev/Dockerfile.ppc.musl
#
# 1. Add to ~/.cargo/config.toml:
#[target.powerpc-unknown-linux-musl]
#linker = "powerpc-linux-musl-gcc"
# ar = "powerpc-linux-gnu-ar"
# 2. Build the image
# TMPDIR=$HOME/tmp podman build -f dev/Dockerfile.ppc.musl -t local/cross-ppc:trixie .

FROM debian:trixie-slim

RUN dpkg --add-architecture powerpc \
 && apt-get update \
 && apt-get install -y --no-install-recommends \
      build-essential gperf pkg-config \
      rsync wget ca-certificates \
      binutils-powerpc-linux-gnu \
      gcc-powerpc-linux-gnu \
      linux-libc-dev-powerpc-cross \
      cmake ninja-build xz-utils \
      g++-powerpc-linux-gnu \
 && rm -rf /var/lib/apt/lists/*

# Copy over linux-headers to the new SYSROOT.
RUN mkdir -m755 -p /usr/local/powerpc-linux-musl/include && \
  rsync -a /usr/powerpc-linux-gnu/include/ /usr/local/powerpc-linux-musl/include/

# Build musl.
ARG MUSL_TC=powerpc-linux-musl
ARG MUSL_TC_URL=https://musl.cc/${MUSL_TC}-cross.tgz

RUN set -ex; \
    wget -O /tmp/musl-cross.tgz "$MUSL_TC_URL"; \
    mkdir -p /opt/${MUSL_TC}-cross; \
    tar -xvpf /tmp/musl-cross.tgz -C /opt; \
    rm -f /tmp/musl-cross.tgz; \
    ln -s /opt/${MUSL_TC}-cross/bin/${MUSL_TC}-gcc /usr/bin/${MUSL_TC}-gcc; \
    ln -s /opt/${MUSL_TC}-cross/bin/${MUSL_TC}-g++ /usr/bin/${MUSL_TC}-g++; \
    ln -s /opt/${MUSL_TC}-cross/bin/${MUSL_TC}-ar  /usr/bin/${MUSL_TC}-ar; \
    ln -s /opt/${MUSL_TC}-cross/bin/${MUSL_TC}-as  /usr/bin/${MUSL_TC}-as; \
    ln -s /opt/${MUSL_TC}-cross/bin/${MUSL_TC}-pkg-config /usr/bin/${MUSL_TC}-pkg-config

# Build libunwind.
ARG LLVM_VER=21.1.0
ARG LLVM_TAG=llvmorg-${LLVM_VER}
ARG LLVM_TARBALL=llvm-project-${LLVM_VER}.src.tar.xz

RUN wget -O /tmp/${LLVM_TARBALL} \
      https://github.com/llvm/llvm-project/releases/download/${LLVM_TAG}/${LLVM_TARBALL} \
 && mkdir -p /opt/src && tar -C /opt/src -xJf /tmp/${LLVM_TARBALL} \
 && printf 'set(CMAKE_SYSTEM_NAME Linux)\n\
set(CMAKE_SYSTEM_PROCESSOR powerpc)\n\
set(CMAKE_SYSROOT /opt/powerpc-linux-musl-cross/powerpc-linux-musl)\n\
set(CMAKE_C_COMPILER powerpc-linux-musl-gcc)\n\
set(CMAKE_CXX_COMPILER powerpc-linux-musl-g++)\n\
set(CMAKE_ASM_COMPILER powerpc-linux-musl-gcc)\n\
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)\n\
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)\n\
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)\n' > /tmp/ppc-toolchain.cmake \
 && cmake -S /opt/src/llvm-project-${LLVM_VER}.src/libunwind -B /opt/build/libunwind \
      -G Ninja \
      -DCMAKE_TOOLCHAIN_FILE=/tmp/ppc-toolchain.cmake \
      -DCMAKE_C_FLAGS="-g -O2" \
      -DCMAKE_CXX_FLAGS="-g -O2" \
      -DCMAKE_ASM_FLAGS="-g" \
      -DCMAKE_INSTALL_PREFIX=/opt/powerpc-linux-musl-cross \
      -DLIBUNWIND_ENABLE_SHARED=OFF \
      -DLIBUNWIND_ENABLE_STATIC=ON \
      -DLIBUNWIND_STANDALONE_BUILD=ON \
      -DLIBUNWIND_USE_COMPILER_RT=OFF \
      -DCMAKE_BUILD_TYPE=Release \
 && ninja -C /opt/build/libunwind -j"$(nproc)" install \
 && cd /opt/src/llvm-project-${LLVM_VER}.src/libunwind/src \
 && powerpc-linux-musl-gcc -g -O2 -I../include -c UnwindRegistersSave.S -o /tmp/UnwindRegistersSave.o \
 && powerpc-linux-musl-gcc -g -O2 -I../include -c UnwindRegistersRestore.S -o /tmp/UnwindRegistersRestore.o \
 && powerpc-linux-musl-ar rcs /opt/powerpc-linux-musl-cross/lib/libunwind.a /tmp/UnwindRegistersSave.o /tmp/UnwindRegistersRestore.o \
 && rm -rf /opt/build/libunwind /opt/src/llvm-project-* /tmp/${LLVM_TARBALL} /tmp/ppc-toolchain.cmake /tmp/Unwind*.o

# Build libseccomp with musl toolchain.
RUN wget https://github.com/seccomp/libseccomp/releases/download/v2.6.0/libseccomp-2.6.0.tar.gz && \
 tar -xvpf libseccomp-2.6.0.tar.gz && \
 cd libseccomp-2.6.0 && \
 env CC=powerpc-linux-musl-gcc \
   LD=powerpc-linux-musl-gcc \
   AR=powerpc-linux-musl-ar \
   CFLAGS='-m32 -g -O2 -mlong-double-64' \
   ./configure \
     --enable-static \
     --disable-shared \
     --host=powerpc-linux-musl \
     --prefix=/opt/powerpc-linux-musl-cross >&2 && \
   make -j -C src libseccomp.la >&2 && \
   make -j -C src install >&2 && \
   rm -rf /libseccomp-2.6.0 /libseccomp-2.6.0.tar.gz

#RUN ln -sf /usr/lib/gcc-cross/powerpc-linux-gnu/14/crtbeginT.o /opt/powerpc-linux-musl-cross/lib/crtbegin.o && \
#  ln -sf /usr/lib/gcc-cross/powerpc-linux-gnu/14/crtend.o /opt/powerpc-linux-musl-cross/lib/crtend.o
