Skip to content

Instantly share code, notes, and snippets.

@mlund
Last active January 1, 2023 02:46
Show Gist options
  • Save mlund/ab2a99e441b7fdbb8c10c9b200f25680 to your computer and use it in GitHub Desktop.
Save mlund/ab2a99e441b7fdbb8c10c9b200f25680 to your computer and use it in GitHub Desktop.
How to build rust-mos for aarch64 linux based on Docker files from rust-mos

Build rust-mos docker image for aarch64

Based on repositories on 29 Dec. 2022.

Start in a fresh directory

mkdir docker-build
cd docker-build

Download steps

# get the right branch of llvm-mos
wget https://github.com/mrk-its/llvm-mos/archive/refs/heads/lldb-mos.zip
unzip llvm-mos-lldb-mos.zip
mv llvm-mos-lldb-mos llvm-mos # -> llvm-mos/

# get the right branch of llvm-mos-sdk
git clone https://github.com:mrk-its/llvm-mos-sdk.git
cd rust-mos
git checkout rust-mos # -> llvm-mos-sdk/
cd ..

# get rust-mos; this *must* be through git clone due to submodules
git clone https://github.com/mrk-its/rust-mos # -> rust-mos/

You should now have three new directories: llvm-mos, llvm-mos-sdk, and rust-mos.

Config

  1. In Dockerfile-llvm you may tweek what is build. Importantly we include the AArch64 target, and have further omitted lldb from LLVM_ENABLE_PROJECTS (you may want to add it, but so far untested)

  2. The config.toml file must contain [target.XXX] that you want to build. Here it's [target.aarch64-unknown-linux-gnu]. It also specifies where llvm-mos is installed which will be in /usr/local.

Build Docker Images

In the docker-build/ directory do:

  1. docker build -t llvm-mos -f $HOME/docker/Dockerfile-llvm . This is a slow process that requires plenty of disk space and memory.

  2. docker build -t rust-mos -f $HOME/docker/Dockerfile-rust . The Dockerfile also contains architectural information and is currently set to aarch64 linux.

  3. Verify that you have the rust-mos image with docker image ls.

changelog-seen = 2
[build]
# Use this directory to store build artifacts.
# You can use "$ROOT" to indicate the root of the git repository.
build-dir = "/tmp/rust-mos-build"
docs = false
[target.aarch64-unknown-linux-gnu]
# Path to the `llvm-config` binary of the installation of a custom LLVM to link
# against. Note that if this is specified we don't compile LLVM at all for this
# target.
llvm-config = "/usr/local/bin/llvm-config"
FROM ubuntu:22.04 as llvm_mos_base
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
git curl ca-certificates ninja-build cmake build-essential \
libstdc++-10-dev libxml2-dev libssl-dev pkg-config python3-pip \
swig python3-dev libedit-dev libncurses5-dev liblzma-dev
FROM llvm_mos_base as build
WORKDIR /tmp
COPY llvm-mos llvm-mos
WORKDIR llvm-mos
RUN ls -la /usr/lib/
RUN uname -m
RUN cmake -C clang/cmake/caches/MOS.cmake -G "Ninja" -S llvm -B build \
-DLLVM_INSTALL_TOOLCHAIN_ONLY=OFF \
-DLLVM_BUILD_LLVM_DYLIB=ON -DLLVM_LINK_LLVM_DYLIB=ON \
-DLLVM_INSTALL_UTILS=ON -DLLVM_BUILD_UTILS=ON -DLLVM_TOOLCHAIN_UTILITIES=FileCheck \
-DLLVM_TOOLCHAIN_TOOLS="llvm-addr2line;llvm-ar;llvm-cxxfilt;llvm-dwarfdump;llvm-mc;llvm-nm;llvm-objcopy;llvm-objdump;llvm-ranlib;llvm-readelf;llvm-readobj;llvm-size;llvm-strings;llvm-strip;llvm-symbolizer;llvm-config;llc" \
-DLIBXML2_LIBRARY=/usr/lib/$(uname -m)-linux-gnu/libxml2.so \
-DLLVM_TARGETS_TO_BUILD="MOS;AArch64" \
-DLLVM_ENABLE_PROJECTS="clang;lld"
RUN cmake --build build -t install
WORKDIR /tmp
COPY llvm-mos-sdk llvm-mos-sdk
WORKDIR llvm-mos-sdk
RUN cmake -G "Ninja" -B build
RUN cmake --build build -t install
FROM llvm_mos_base
COPY --from=build /usr/local /usr/local
WORKDIR /
RUN apt install -y vim vim-runtime less gawk
WORKDIR /usr/local/src
RUN git clone https://github.com/jhallen/atari-tools
WORKDIR /usr/local/src/atari-tools
RUN make; cp /usr/local/src/atari-tools/atr /usr/local/bin/atr
ARG USERNAME=mos
ARG USER_UID=1000
ARG USER_GID=$USER_UID
# Create the user
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME -s /bin/bash \
#
# [Optional] Add sudo support. Omit if you don't need to install software after connecting.
&& apt-get update \
&& apt-get install -y sudo \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME
USER $USERNAME
WORKDIR /home/$USERNAME
ARG LLVM_MOS_SDK_IMAGE=llvm-mos
FROM ${LLVM_MOS_SDK_IMAGE} as build
# RUN apt-get update
# RUN DEBIAN_FRONTEND=noninteractive apt-get install -y python3 libssl-dev libxml2-dev pkg-config
USER root
WORKDIR /usr/local/src
COPY rust-mos rust-mos
COPY config.toml rust-mos/config.toml
WORKDIR rust-mos
RUN python3 x.py build -i --stage 0 src/tools/cargo
RUN python3 x.py build -i
RUN cp /tmp/rust-mos-build/aarch64-unknown-linux-gnu/stage0-tools-bin/cargo /tmp/rust-mos-build/aarch64-unknown-linux-gnu/stage1/bin && \
mv /tmp/rust-mos-build/aarch64-unknown-linux-gnu/stage1 /usr/local/rust-mos && \
rm -fr .git
FROM ${LLVM_MOS_SDK_IMAGE}
COPY --from=build /usr/local/src/rust-mos /usr/local/src/rust-mos
COPY --from=build /usr/local/rust-mos /usr/local/rust-mos
USER mos
WORKDIR /home/mos
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH=/home/mos/.cargo/bin:${PATH}
RUN rustup toolchain link mos /usr/local/rust-mos
RUN rustup default mos
ENV RUST_TARGET_PATH=/home/mos/rust-mos-target
RUN mkdir $RUST_TARGET_PATH
RUN python3 /usr/local/src/rust-mos/create_mos_targets.py $RUST_TARGET_PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment