FROM linaro/ci-#{ARCH}-tcwg-base-ubuntu:#{DISTRO}

RUN echo "deb https://apt.buildkite.com/buildkite-agent stable main" | tee /etc/apt/sources.list.d/buildkite-agent.list \
 && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 32A37959C2FA5C3C99EFBC32A79206696452D198

RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive apt-get install -y \
 binutils-gold \
 buildkite-agent \
 cpio \
 gfortran \
 htop \
 iotop \
# Add locales used for testing libcxx
 language-pack-en \
 language-pack-fr \
 language-pack-ja \
 language-pack-ru \
 language-pack-zh-hans \
 libjson-perl \
 libtinfo-dev \
#if DISTRO_focal || DISTRO_jammy
# We build clang+llvm releases on Ubuntu Bionic, which has libtinfo.so.5,
# and we link against it.
# Ubuntu Focal by default installs libtinfo6 (aka libtinfo.so.6), and
# our clang+llvm releases don't work on Ubuntu Focal unless we install
# legacy libtinfo5 (aka libtinfo.so.5).
# We should no longer need libtinfo5 when we switch building releases to
# Ubuntu Focal or later.
 libtinfo5 \
#endif
 python3-buildbot-worker \
 tmux \
# uuidgen is used by the flang driver script. Not installed by default on focal.
 uuid-runtime \
# Although the base image has python3-venv installed, this does not give
# you 'virtualenv'. Instead you must do 'python3 -m venv'. However the test
# suite calls virtualenv with --python=python3, so we're ok to use the Python2
# version here.
 virtualenv \
 && apt-get clean \
 && rm -rf \
 /var/lib/apt/lists/* \
 /tmp/* \
 /var/tmp/*

# Libcxx needs some locales that are not generated by default
RUN printf "fr_CA ISO-8859-1\ncs_CZ ISO-8859-2" >> /etc/locale.gen \
  && mkdir /usr/local/share/i1en/ \
  && printf "fr_CA ISO-8859-1\ncs_CZ ISO-8859-2" >> /usr/local/share/i1en/SUPPORTED \
  && locale-gen

# Add ninja with support for matching concurrency to available CPU allowance.
RUN git clone -b limit-on-cpu https://github.com/maxim-kuvyrkov/ninja.git \
 && cd ninja \
 && python3 ./configure.py --bootstrap && ./ninja all && ./ninja_test \
 && mv ninja /usr/local/bin/ninja.limit-on-cpu \
 && cd .. \
 && rm -rf ninja \
 && git clone -b master https://github.com/maxim-kuvyrkov/ninja.git \
 && cd ninja \
 && python3 ./configure.py --bootstrap && ./ninja all && ./ninja_test \
 && mv ninja /usr/local/bin/ninja.limit-on-psi \
 && cd .. \
 && rm -rf ninja

#if ARCH_arm64
# AArch64 bots run MLIR tests for SVE and SME using qemu-user-static.
# Armv7-M libcxx bots use qemu-system-arm for emulation.
# We use a newer version than apt becuase it includes fixes for
# Arm and AArch64 targets.
RUN qemu_ver=9.0.2 \
 && wget --progress=dot:giga https://download.qemu.org/qemu-${qemu_ver}.tar.xz \
 && tar -xvf qemu-${qemu_ver}.tar.xz \
 && cd qemu-${qemu_ver} \
 && ./configure --prefix=/usr/local --target-list=aarch64-linux-user,arm-softmmu \
 && make -j$(nproc) \
 && make install \
 && rm -rf qemu-${qemu_ver}.tar.xz qemu-${qemu_ver}/
#endif

RUN new-user.sh --user tcwg-buildbot \
 # Initialize .ccache dir, so that we mount ccache docker volumes with
 # correct permissions.
 && sudo -i -u tcwg-buildbot mkdir -p /home/tcwg-buildbot/.ccache \
 # Allow non root users to access the buildkite config files
 && chmod -R a+rw /etc/buildkite-agent/

VOLUME /home

COPY run.sh .
COPY start.sh .

#if ARCH_amd64 || ARCH_arm64
ENTRYPOINT ["/run.sh"]
#else
ENTRYPOINT ["linux32", "/run.sh"]
#endif
CMD ["start.sh"]
