From 778cda58abc61711f054d89b09d8bc016763e774 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Wed, 20 Dec 2023 00:47:51 +0100 Subject: llvm-orc-jit: add build-llvm.sh, enforce find_package success, and print used llvm/clang cmake config --- content/2022-07-07-llvm-orc-jit/.gitignore | 2 ++ content/2022-07-07-llvm-orc-jit/CMakeLists.txt | 7 +++- content/2022-07-07-llvm-orc-jit/build-llvm.sh | 50 ++++++++++++++++++++++++++ content/2022-07-07-llvm-orc-jit/index.md | 6 +++- 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 content/2022-07-07-llvm-orc-jit/build-llvm.sh diff --git a/content/2022-07-07-llvm-orc-jit/.gitignore b/content/2022-07-07-llvm-orc-jit/.gitignore index 50f1b95..facd05e 100644 --- a/content/2022-07-07-llvm-orc-jit/.gitignore +++ b/content/2022-07-07-llvm-orc-jit/.gitignore @@ -1,3 +1,5 @@ BUILD/ compile_commands.json .cache +install-llvm* +src-llvm* diff --git a/content/2022-07-07-llvm-orc-jit/CMakeLists.txt b/content/2022-07-07-llvm-orc-jit/CMakeLists.txt index 04fe617..51989e9 100644 --- a/content/2022-07-07-llvm-orc-jit/CMakeLists.txt +++ b/content/2022-07-07-llvm-orc-jit/CMakeLists.txt @@ -8,7 +8,7 @@ target_compile_options(main PRIVATE -Wall -Wextra -Werror) # -- LLVM/CLANG ---------------------------------------------------------------- -find_package(Clang HINTS "${CLANG_INSTALL_PREFIX}/lib/cmake/clang") +find_package(Clang REQUIRED CONFIG HINTS "${CLANG_INSTALL_PREFIX}/lib/cmake/clang") if (NOT ${CLANG_INSTALL_PREFIX} STREQUAL "/") # Treat custom LLVM/CLANG include path as system include path, such that @@ -18,6 +18,11 @@ endif() target_link_libraries(main clang-cpp) +message(STATUS "Using LLVMConfig.cmake: ${LLVM_CONFIG}") +message(STATUS "LLVM version: ${LLVM_VERSION}") +message(STATUS "Using ClangConfig.cmake: ${Clang_CONFIG}") +message(STATUS "Clang version: ${Clang_VERSION}") + # -- SANITIZER ----------------------------------------------------------------- option(SANITIZER "Enable ASAN/LSAN/UBSAN" ON) diff --git a/content/2022-07-07-llvm-orc-jit/build-llvm.sh b/content/2022-07-07-llvm-orc-jit/build-llvm.sh new file mode 100644 index 0000000..df9d6b8 --- /dev/null +++ b/content/2022-07-07-llvm-orc-jit/build-llvm.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# +# A small utility script to help me build specific llvm versions. +# +# ## General cmake build info +# +# See [1] for an overview of the llvm cmake build and a discussion of the most +# frequently used cmake options. +# +# ## Building on low end machines +# +# Builds with debug information can use a lot of RAM and disk space and is +# usually slower to run. You can improve RAM usage by using lld, see the +# LLVM_USE_LINKER option. +# +# LLVM_PARALLEL_{COMPILE,LINK}_JOBS:STRING [2] +# Building the llvm toolchain can use a lot of resources, particularly +# linking. These options, when you use the Ninja generator, allow you to +# restrict the parallelism. For example, to avoid OOMs or going into swap, +# permit only one link job per 15GB of RAM available on a 32GB machine, +# specify -G Ninja -DLLVM_PARALLEL_LINK_JOBS=2. +# +# [1]: https://llvm.org/docs/CMake.html +# [2]: https://llvm.org/docs/CMake.html#frequently-used-llvm-related-variables + +#TAG=llvmorg-14.0.6 +#TAG=llvmorg-15.0.0 +#TAG=llvmorg-15.0.7 +TAG=llvmorg-17.0.5 + +SRCDIR=src-$TAG + +if [[ ! -d $SRCDIR ]]; then + git clone --depth 1 --branch $TAG https://github.com/llvm/llvm-project.git $SRCDIR +fi + +cmake -S $SRCDIR/llvm -B $SRCDIR/BUILD -G Ninja \ + -DCMAKE_C_COMPILER=clang \ + -DCMAKE_CXX_COMPILER=clang++ \ + -DCMAKE_INSTALL_PREFIX=$PWD/install-$TAG \ + -DCMAKE_BUILD_TYPE=Debug \ + -DLLVM_ENABLE_PROJECTS='clang' \ + -DLLVM_BUILD_LLVM_DYLIB=1 \ + -DLLVM_ENABLE_ASSERTIONS=ON \ + -DLLVM_INCLUDE_TESTS=OFF \ + -DLLVM_TARGETS_TO_BUILD='X86' \ + -DLLVM_USE_LINKER=lld \ + -DLLVM_PARALLEL_LINK_JOBS=1 + +cmake --build $SRCDIR/BUILD --target install diff --git a/content/2022-07-07-llvm-orc-jit/index.md b/content/2022-07-07-llvm-orc-jit/index.md index e453f0c..0fc2f27 100644 --- a/content/2022-07-07-llvm-orc-jit/index.md +++ b/content/2022-07-07-llvm-orc-jit/index.md @@ -6,7 +6,7 @@ tags = ["llvm", "clang", "c++"] +++ **EDIT**: -- 2023-12-19: Migrate example to `llvm17`. +- 2023-12-19: Migrate example to `LLVM-17`. Based on the in-memory compiler shared in the last post ([C to LLVM IR in memory using libclang](@/2022-06-18-libclang-c-to-llvm-ir/index.md)), this post @@ -41,8 +41,12 @@ The sources are available under [llvm-orc-jit][post-src]. The following [Makefile][post-makefile] provides a convenience wrapper to configure, build, and run the example with a single `make` invocation. +Additionally, the [build-llvm.sh][post-build-llvm] script is provided to build +specific LLVM versions. + [post-src]: https://git.memzero.de/blog/tree/content/2022-07-07-llvm-orc-jit?h=main [post-makefile]: https://git.memzero.de/blog/tree/content/2022-07-07-llvm-orc-jit/Makefile?h=main +[post-build-llvm]: https://git.memzero.de/blog/tree/content/2022-07-07-llvm-orc-jit/build-llvm.sh?h=main [src-clang]: https://github.com/llvm/llvm-project/tree/main/clang [blog-clang-in-memory]: https://blog.audio-tk.com/2018/09/18/compiling-c-code-in-memory-with-clang/ [llvm-jit-tut]: https://www.llvm.org/docs/tutorial/BuildingAJIT1.html -- cgit v1.2.3