Build Argentum compiler on Ubuntu

Preparations

Install GCC and friends:

sudo apt-get update
sudo apt-get install build-essential

Install Ninja build system and CMake

sudo apt-get -y install ninja-build
sudo apt-get install cmake

LLVM

Get LLVM sources:

mkdir ~/cpp/
cd ~/cpp/
wget -qO llvm.tgz \
https://github.com/llvm/llvm-project/archive/refs/tags/llvmorg-17.0.4.tar.gz

tar -xzf llvm.tgz
rm llvm.tgz
mv llvm-project-llvmorg-17.0.4/ llvm-17.0.4-src

Prepare build files:

cd ~/cpp/llvm-17.0.4-src
mkdir build-rel

cmake -S llvm -B build-rel -G Ninja \
-DLLVM_BUILD_RUNTIME=OFF -DLLVM_BUILD_EXAMPLES=OFF -DLLVM_BUILD_TESTS=OFF -DLLVM_BUILD_UTILS=OFF \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DLLVM_TARGETS_TO_BUILD=X86 \
-DCMAKE_INSTALL_PREFIX=$(pwd)/../llvm17.0.4-rel

Build LLVM (this will take ~~hour)

cmake --build build-rel

Install LLVM to out dir

cmake --build build-rel --target install

Now ~/cpp/llvm17.0.4-rel contains the newly built release version of LLVM

SDL

Install dev versions SDL and SDL_image:

sudo apt install libsdl2-dev
sudo apt install libjpeg-dev libwebp-dev libtiff5-dev libsdl2-image-dev

Some versions of libsdl2-image-dev have no sdl2_image-config.cmake file.

In this case it should be added manually:

Create /usr/lib/x86_64-linux-gnu/cmake/sdl2_image/sdl2_image-config.cmake file with this content:

set(prefix "/usr")
set(exec_prefix "${prefix}")
set(libdir "${prefix}/lib/x86_64-linux-gnu")
set(SDL2_IMAGE_PREFIX "/usr")
set(SDL2_IMAGE_EXEC_PREFIX "/usr")
set(SDL2_IMAGE_INCLUDE_DIRS "${prefix}/include/SDL2_image")
set(SDL2_IMAGE_LIBRARIES "-lSDL2_image")

Build Argentum compiler

Get sources

cd ~/cpp
git clone https://github.com/karol11/argentum.git

Build

cd ~/cpp/argentum
cmake -S . -B build -G Ninja -DLLVM_DIR=../llvm17.0.4-rel/lib/cmake/llvm/
cmake --build build

Build and run examples

cd build
./agc --help

Compile helloWorld to asm file out.a

./agc -src  ../demo -start helloWorld -S -o out.a

Build and run helloWorld

# compile
./agc -src  ../demo -start helloWorld -o out.o

# link
gcc -no-pie out.o libag_runtime.a -o hw

# run
./hw

Build and run SDL demo application

# compile and link
./agc -src  ../demo -start demo -o demo.o

gcc -no-pie demo.o libag_runtime.a \
-L/usr/lib/x86_64-linux-gnu \
-lSDL2 -lSDL2_image \
-o demo

# copy image resources
cp ../demo/*.jpg .
cp ../demo/*.png .

# run
./demo

Leave a Reply

Your email address will not be published. Required fields are marked *