toolchain-gcc/Dockerfile
Kevin Hamacher 59f3c95a18 Compile rust-mega-drive using rustc_codegen_gcc
This still generates binaries for 68020 + stdlib functions are stubbed
out.
2025-01-14 19:04:41 +01:00

65 lines
3.3 KiB
Docker

FROM ubuntu:22.04
RUN apt-get -y update && apt-get install -y --no-install-recommends git curl ca-certificates build-essential sudo
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
# Not sure why/if this is needed
ENV LIBRARY_PATH=/usr/lib
# Make sure we compile to the right target
ENV OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu
# Clone source repo
RUN git clone https://github.com/rust-lang/rustc_codegen_gcc.git
# Setup rustup
RUN cd rustc_codegen_gcc && rustup show
# Install gcc
RUN curl -LO https://github.com/cross-cg-gcc-tools/cross-gcc/releases/latest/download/gcc-m68k-13.deb
RUN dpkg -i gcc-m68k-13.deb
# Set gcc path in the rustc_codegen project
RUN echo 'gcc-path = "/usr/lib"' > rustc_codegen_gcc/config.toml
# Git config is needed for ./y.sh prepare
RUN git config --global user.email "user@example.com"
RUN git config --global user.name "User"
# Apply hacky patch
COPY 0001-Hackery.patch /tmp/0001-Hackery.patch
RUN cd rustc_codegen_gcc && patch -p1 < /tmp/0001-Hackery.patch && rm /tmp/0001-Hackery.patch
# Build project
# See https://github.com/rust-lang/rustc_codegen_gcc/blob/master/doc/tips.md#how-to-build-a-cross-compiling-libgccjit
RUN cd rustc_codegen_gcc && ./y.sh prepare --cross
RUN cd rustc_codegen_gcc && ./y.sh build --sysroot --features compiler_builtins/no-f16-f128 --target-triple m68k-unknown-linux-gnu --target /rustc_codegen_gcc/target_specs/m68k-unknown-linux-gnu.json
# Build "megapong" PoC
RUN git clone https://github.com/ricky26/rust-mega-drive.git
COPY 0001-Patches-to-run-with-gcc.patch /tmp/rust-mega-drive.patch
RUN cd rust-mega-drive && patch -p1 </tmp/rust-mega-drive.patch && rm /tmp/rust-mega-drive.patch
# rust-mega-drive requires python3 as part of the build script.
RUN apt-get install -y python3
# Compile rust code
RUN cd rustc_codegen_gcc && ./y.sh cargo build --target ./target_specs/m68k-unknown-linux-gnu.json --manifest-path /rust-mega-drive/examples/megapong/Cargo.toml --release
# Compile entrypoint + stdlib stub (should not be strictly necessary to reproduce)
RUN cd rust-mega-drive/examples/megapong && m68k-unknown-linux-gnu-gcc -c entry.S -o entry.o -fno-exceptions -mcpu=68000
RUN cd rust-mega-drive/examples/megapong && m68k-unknown-linux-gnu-gcc -c stdlib.S -o stdlib.o -fno-exceptions -mcpu=68000
# Link everything
RUN cd rust-mega-drive/examples/megapong && m68k-unknown-linux-gnu-ld -o out.elf -T /rust-mega-drive/share/ldscripts/megadrive.x entry.o stdlib.o ../../target/m68k-unknown-linux-gnu/release/libmegapong.a -static -z nocopyreloc --no-eh-frame-hdr -nostdlib
# Create cartridge
RUN cd rust-mega-drive/examples/megapong && m68k-unknown-linux-gnu-objcopy -O binary out.elf out.md
# Installing utility file
RUN apt install -y file
# XXX: Artifacts have been created, the following commands can be used to verify if things are okay or not.
# 1) This shows that the object files were created for 68020 "Motorola m68k, 68020".
# cd /tmp
# ar x /rust-mega-drive/target/m68k-unknown-linux-gnu/release/libmegapong.a
# file *.o
#
# 2) The offending instruction will be inserted at the beginning of _init_runtime, if the following command finds something, the code is invalid for 68000
# cd rust-mega-drive/examples/megapong && m68k-unknown-linux-gnu-objdump -d out.elf | grep -A3 "_init_runtime" | grep "4bfb 0170"