FROM ubuntu:22.04

LABEL maintainer="Alice D. <alice@starwitch.productions>"

ENV LANG C.UTF-8
ENV DEBIAN_FRONTEND=noninteractive
ENV TOOLCHAIN_PREFIX=/opt/llvm-mingw
ENV TOOLCHAIN_VERSION="20230919"

ARG TOOLCHAIN_BUILD_ARGS="--disable-lldb --disable-clang-tools-extra"
ARG TOOLCHAIN_ARCHS="i686 x86_64"
ARG MESON_VERSION="0.63.3"

RUN apt-get update

RUN apt-get dist-upgrade -y

RUN apt-get install -y --no-install-recommends \
	automake \
	autopoint \
	build-essential \
	bzip2 \
	ca-certificates \
	cmake \
	curl \
	file \
	gettext \
	git \
	gnupg \
	less \
	libtool \
	ninja-build \
	nsis \
	pkg-config \
	python3 \
	python3-distutils \
	python3-docutils \
	python3-gnupg \
	python3-pip \
	unzip \
	wget \
	yasm \
	zip

RUN mkdir -p -m 755 /etc/apt/keyrings

RUN wget https://cli.github.com/packages/githubcli-archive-keyring.gpg -O /etc/apt/keyrings/githubcli-archive-keyring.gpg

RUN chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg

RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" > /etc/apt/sources.list.d/github-cli.list

RUN apt-get update

RUN apt-get install -y --no-install-recommends gh

RUN apt-get clean -y

RUN rm -rf /var/lib/apt/lists/*

RUN pip3 install \
    meson==$MESON_VERSION \
    zstandard

WORKDIR /build

# clone a specific tag
RUN git clone --branch $TOOLCHAIN_VERSION \
	--single-branch --depth 1 \
	https://github.com/mstorsjo/llvm-mingw

WORKDIR /build/llvm-mingw

RUN CHECKOUT_ONLY=1 CHECKOUT=1 ./build-llvm.sh

RUN ./build-all.sh $TOOLCHAIN_BUILD_ARGS $TOOLCHAIN_PREFIX

ENV PATH=$TOOLCHAIN_PREFIX/bin:$PATH

WORKDIR /build

RUN rm -rfv /build/llvm-mingw

RUN find $TOOLCHAIN_PREFIX/{bin,lib*} -type f | \
	while read line; do llvm-strip -g "$line"; done
