From 3bdc01ab0a05c1e2ec4a60fc366569a459152a97 Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Mon, 21 Oct 2024 05:49:23 -0600 Subject: [PATCH] Pact with the devil: LLVM --- .github/workflows/ci.yml | 32 ++++++++++++++++++++------------ CMakeLists.txt | 12 ++++++++++-- fetch-llvm.ps1 | 14 ++++++++++++++ project.ps1 | 18 +++++++++++++----- project.sh | 14 +++++++++++++- 5 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 fetch-llvm.ps1 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f5067e2..b35c496 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: - name: Checkout uses: actions/checkout@v4 - name: Install dependencies - run: sudo apt install -y ninja-build + run: sudo apt install -y ninja-build mold - name: System information run: | uname -a @@ -32,7 +32,7 @@ jobs: - name: Checkout uses: actions/checkout@v4 - name: Install dependencies - run: sudo apt install -y ninja-build + run: sudo apt install -y ninja-build mold - name: System information run: | uname -a @@ -48,7 +48,7 @@ jobs: - name: Checkout uses: actions/checkout@v4 - name: Install dependencies - run: sudo apt install -y ninja-build + run: sudo apt install -y ninja-build mold - name: System information run: | uname -a @@ -64,7 +64,7 @@ jobs: - name: Checkout uses: actions/checkout@v4 - name: Install dependencies - run: sudo apt install -y ninja-build + run: sudo apt install -y ninja-build mold - name: System information run: | uname -a @@ -144,13 +144,15 @@ jobs: - name: Checkout uses: actions/checkout@v4 - name: Install dependencies - run: choco install ninja + run: | + choco install ninja + pwsh ./fetch-llvm.ps1 # - name: System information # run: | # systeminfo # clang -v - name: Build and test - run: pwsh ./project.ps1 "build_type=Debug" test all + run: pwsh ./project.ps1 "-DCMAKE_PREFIX_PATH=clang+llvm-19.1.2-x86_64-pc-windows-msvc" "build_type=Debug" test all env: LANG: en_US.UTF-8 # Ensure UTF-8 encoding windows_MinSizeRel: @@ -160,13 +162,15 @@ jobs: - name: Checkout uses: actions/checkout@v4 - name: Install dependencies - run: choco install ninja + run: | + choco install ninja + pwsh ./fetch-llvm.ps1 # - name: System information # run: | # systeminfo # clang -v - name: Build and test - run: pwsh ./project.ps1 "build_type=MinSizeRel" test all + run: pwsh ./project.ps1 "-DCMAKE_PREFIX_PATH=clang+llvm-19.1.2-x86_64-pc-windows-msvc" "build_type=MinSizeRel" test all env: LANG: en_US.UTF-8 # Ensure UTF-8 encoding windows_RelWithDebInfo: @@ -176,13 +180,15 @@ jobs: - name: Checkout uses: actions/checkout@v4 - name: Install dependencies - run: choco install ninja + run: | + choco install ninja + pwsh ./fetch-llvm.ps1 # - name: System information # run: | # systeminfo # clang -v - name: Build and test - run: pwsh ./project.ps1 "build_type=RelWithDebInfo" test all + run: pwsh ./project.ps1 "-DCMAKE_PREFIX_PATH=clang+llvm-19.1.2-x86_64-pc-windows-msvc" "build_type=RelWithDebInfo" test all env: LANG: en_US.UTF-8 # Ensure UTF-8 encoding windows_Release: @@ -192,12 +198,14 @@ jobs: - name: Checkout uses: actions/checkout@v4 - name: Install dependencies - run: choco install ninja + run: | + choco install ninja + pwsh ./fetch-llvm.ps1 # - name: System information # run: | # systeminfo # clang -v - name: Build and test - run: pwsh ./project.ps1 "build_type=Release" test all + run: pwsh ./project.ps1 "-DCMAKE_PREFIX_PATH=clang+llvm-19.1.2-x86_64-pc-windows-msvc" "build_type=Release" test all env: LANG: en_US.UTF-8 # Ensure UTF-8 encoding diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c0d7fb..cf1bf4d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,9 +14,12 @@ add_compile_options( ) include_directories("bootstrap/include") +find_package(LLVM REQUIRED CONFIG) + set(LIBRARY_NAME "std") set(RUNNER_NAME "runner") set(COMPILER_NAME "nest") + add_library("${LIBRARY_NAME}" "bootstrap/std/base.c" "bootstrap/std/string.c" @@ -27,10 +30,15 @@ add_library("${LIBRARY_NAME}" "bootstrap/std/sha1.c" ) add_executable("${RUNNER_NAME}" "bootstrap/runner/runner.c") -target_link_libraries(${RUNNER_NAME} ${LIBRARY_NAME}) +target_link_libraries(${RUNNER_NAME} PRIVATE ${LIBRARY_NAME}) add_executable("${COMPILER_NAME}" "bootstrap/src/main.c" "bootstrap/src/pdb_image.c" ) -target_link_libraries(${COMPILER_NAME} ${LIBRARY_NAME}) + +target_compile_definitions(${COMPILER_NAME} PRIVATE ${LLVM_DEFINITIONS}) +target_include_directories(${COMPILER_NAME} PRIVATE ${LLVM_INCLUDE_DIRS}) +target_link_directories(${COMPILER_NAME} PRIVATE ${LLVM_LIBRARY_DIRS}) +target_link_libraries(${COMPILER_NAME} PRIVATE ${LIBRARY_NAME} ${LLVM_LIBRARIES}) + diff --git a/fetch-llvm.ps1 b/fetch-llvm.ps1 new file mode 100644 index 0000000..f760428 --- /dev/null +++ b/fetch-llvm.ps1 @@ -0,0 +1,14 @@ +Set-StrictMode -Version Latest +Set-PSDebug -Trace 2 + +$LLVM_VERSION="19.1.2" +$BASE_DOWNLOAD_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-$LLVM_VERSION" + +$LLVM_DOWNLOAD_FILE_BASENAME="clang+llvm-$LLVM_VERSION-x86_64-pc-windows-msvc" +$LLVM_DOWNLOAD_FILE="$LLVM_DOWNLOAD_FILE_BASENAME.tar.xz" +$LLVM_DOWNLOAD_URL="$BASE_DOWNLOAD_URL/$LLVM_DOWNLOAD_FILE" + +Invoke-WebRequest -Uri "$LLVM_DOWNLOAD_URL" -OutFile "$LLVM_DOWNLOAD_FILE" +7z x $LLVM_DOWNLOAD_FILE +7z x "$LLVM_DOWNLOAD_FILE_BASENAME.tar" +dir diff --git a/project.ps1 b/project.ps1 index 2ceb5cf..6aaa0d4 100644 --- a/project.ps1 +++ b/project.ps1 @@ -1,25 +1,33 @@ Set-StrictMode -Version Latest Set-PSDebug -Trace 2 $previous_error_action_preference = $global:ErrorActionPreference +$global:ErrorActionPreference = 'Stop' $myargs=$args $build_dir="build" $release_mode="Debug" $build_type_prefix="build_type=" +$cmake_prefix_path_prefix="-DCMAKE_PREFIX_PATH=" +$cmake_prefix_path="" & { try { - $global:ErrorActionPreference = 'Stop' - - if ($($myargs.Length) -ne 0 -and $myargs[0].StartsWith($build_type_prefix)) + foreach ($arg in $myargs) { - $release_mode = $myargs[0].Substring($build_type_prefix.Length) + if ($arg.StartsWith($build_type_prefix)) + { + $release_mode = $arg.Substring($build_type_prefix.Length) + } + if ($arg.StartsWith($cmake_prefix_path_prefix)) + { + $cmake_prefix_path = $arg.Substring($cmake_prefix_path_prefix.Length); + } } Write-Output "Build type: $release_mode" New-Item -Path $build_dir -ItemType Directory -Force - cmake . "-B$build_dir" -G Ninja "-DCMAKE_BUILD_TYPE=$release_mode" -DCMAKE_C_COMPILER="clang" -DCMAKE_CXX_COMPILER="clang++" + cmake . "-B$build_dir" -G Ninja "-DCMAKE_BUILD_TYPE=$release_mode" -DCMAKE_C_COMPILER="clang" -DCMAKE_CXX_COMPILER="clang++" "-DCMAKE_PREFIX_PATH=$cmake_prefix_path" pushd $build_dir ninja popd diff --git a/project.sh b/project.sh index 9c43f7e..02f7c27 100755 --- a/project.sh +++ b/project.sh @@ -7,7 +7,19 @@ fi echo "Build type: $release_mode" build_dir=build mkdir -p $build_dir -cmake . -B$build_dir -G Ninja -DCMAKE_BUILD_TYPE="$release_mode" -DCMAKE_C_COMPILER="clang" -DCMAKE_CXX_COMPILER="clang++" + +case "$OSTYPE" in + linux*) CLANG_PREFIX="/usr/bin" ;; + darwin*) CLANG_PREFIX="/opt/homebrew/opt/llvm/bin" ;; + *) exit 1 ;; +esac + +case "$OSTYPE" in + linux*) cmake . -B$build_dir -G Ninja -DCMAKE_BUILD_TYPE="$release_mode" -DCMAKE_C_COMPILER="$CLANG_PREFIX/clang" -DCMAKE_CXX_COMPILER="$CLANG_PREFIX/clang++" -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=mold" -DCMAKE_SHARED_LINKER_FLAGS="-fuse-ld=mold" ;; + darwin*) cmake . -B$build_dir -G Ninja -DCMAKE_BUILD_TYPE="$release_mode" -DCMAKE_C_COMPILER="$CLANG_PREFIX/clang" -DCMAKE_CXX_COMPILER="$CLANG_PREFIX/clang++" -DCMAKE_PREFIX_PATH=$(brew --prefix llvm) ;; + *) exit 1 ;; +esac + original_dir=$PWD cd $build_dir ninja