# Dockerfile to create Ubuntu 24.04 (Noble) sysroot for cross-compilation
#
# This creates a minimal sysroot containing headers and libraries needed
# to cross-compile C++ code targeting Linux x86_64.
#
# Key requirement: Clang needs to find GCC's installation to locate libstdc++
# headers. It looks for /usr/lib/gcc/<triple>/<version>/ in the sysroot.

FROM ubuntu:24.04

# Install the packages we need in the sysroot
# These provide headers and libraries for linking
RUN apt-get update && apt-get install -y --no-install-recommends \
    # C++ standard library with C++23 support (GCC 13)
    libstdc++-13-dev \
    # GCC 13 (needed for Clang to find C++ headers via GCC detection)
    gcc-13 \
    g++-13 \
    # C library
    libc6-dev \
    # Linux kernel headers
    linux-libc-dev \
    # Common dependencies
    zlib1g-dev \
    libssl-dev \
    # Clean up
    && rm -rf /var/lib/apt/lists/*

# Create sysroot directory
RUN mkdir -p /sysroot

# Copy the essential directories for cross-compilation
# Note: We preserve the exact directory structure that clang expects
RUN cp -a /usr/include /sysroot/ && \
    cp -a /usr/lib/x86_64-linux-gnu /sysroot/usr_lib && \
    mkdir -p /sysroot/lib && \
    cp -a /lib/x86_64-linux-gnu /sysroot/lib/ && \
    # Copy GCC installation directory - Clang uses this to find libstdc++ headers
    # Copy everything except .so files (Bazel can't handle them as inputs)
    mkdir -p /sysroot/usr/lib/gcc && \
    cp -a /usr/lib/gcc/x86_64-linux-gnu /sysroot/usr/lib/gcc/ && \
    find /sysroot/usr/lib/gcc -name "*.so" -delete && \
    find /sysroot/usr/lib/gcc -name "*.so.*" -delete && \
    # Create symlinks for clang's target triple (x86_64-unknown-linux-gnu -> x86_64-linux-gnu)
    # Clang uses --target=x86_64-unknown-linux-gnu but Ubuntu uses x86_64-linux-gnu
    ln -s x86_64-linux-gnu /sysroot/usr/lib/gcc/x86_64-unknown-linux-gnu && \
    ln -s x86_64-linux-gnu /sysroot/lib/x86_64-unknown-linux-gnu && \
    ln -s x86_64-linux-gnu /sysroot/include/x86_64-unknown-linux-gnu
