Dependency errors when building as subdirectory

I’m attempting to build llvm with clang and openmp as a subdirectory of my project.

My project structure is as follows, with the llvm-project subdirectory being a recent clone of trunk.

. myproject/
|--- CMakeLists.txt
|--- src/
|--- lib/
|    |--- CMakeLists.txt
|    |--- llvm-project/
|    |    |--- clang/
|    |    |--- openmp/
|    |    |--- llvm/
|    |    |--- .....
....

The file at lib/CMakeLists.txt looks as follows:

cmake_minimum_required(VERSION 3.21 FATAL_ERROR)

add_subdirectory(llvm-project/llvm)

Basically, my problem is this: If I direct my build command at -S lib/llvm-project/llvm it will work, but if I direct it at -S lib using my own CMakeLists.txt as above, it will fail with clang dependency errors.

This is my full build command:

cd '/data/myproject/build/build_libs' && env CC="clang" CXX="clang++" \
  cmake \
    -B '/data/myproject/build/build_libs' \
    -S '/data/myproject/lib' \
    -DCMAKE_INSTALL_PREFIX="/data/myproject/build/libs" \
    -DCMAKE_BUILD_TYPE="Release" \
    -DLLVM_TARGETS_TO_BUILD="X86;ARM;AArch64;WebAssembly;RISCV;NVPTX" \
    -DLLVM_ENABLE_PROJECTS="clang" \
    -DLLVM_ENABLE_RUNTIMES="openmp" \
    -DCMAKE_CXX_FLAGS=-fPIC
cd '/data/myproject/build/build_libs' && env CC="clang" CXX="clang++" \
  cmake \
    --build '/data/myproject/build/build_libs' \
    --target install \
    --config Release \
    -- -j8

However, the build will succeed when changing the respective line to -S '/data/myproject/lib/llvm-project/llvm' in order to target llvm’s own CMakeLists.txt rather than mine.

Here is the build output I’m getting:

[100%] Building CXX object llvm-project/llvm/tools/llvm-reduce/CMakeFiles/llvm-reduce.dir/deltas/ReduceDIMetadata.cpp.o
-- Performing Test LIBOMP_HAVE_RTM_INTRINSICS - Success
-- Found Perl: /usr/bin/perl (found version "5.34.0") 
-- Performing Test LIBOMP_HAVE_VERSION_SYMBOLS
-- Performing Test LIBOMP_HAVE_VERSION_SYMBOLS - Success
-- Performing Test LIBOMP_HAVE___BUILTIN_FRAME_ADDRESS
-- Performing Test LIBOMP_HAVE___BUILTIN_FRAME_ADDRESS - Success
-- Performing Test LIBOMP_HAVE_WEAK_ATTRIBUTE
-- Performing Test LIBOMP_HAVE_WEAK_ATTRIBUTE - Success
-- Performing Test LIBOMP_HAVE_PSAPI
-- Performing Test LIBOMP_HAVE_PSAPI - Failed
-- Looking for sqrt in m
-- Looking for sqrt in m - found
-- Looking for __atomic_load_1
-- Looking for __atomic_load_1 - not found
-- Looking for __atomic_load_1 in atomic
-- Looking for __atomic_load_1 in atomic - found
-- Using LLVM include directories: /data/myproject/lib/llvm-project/llvm/include;/data/myproject/build/build_libs/llvm-project/llvm/include
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.2") 
-- Found LIBOMPTARGET_DEP_LIBFFI: /usr/lib/x86_64-linux-gnu/libffi.so  
-- OMPT target enabled
-- OpenMP tools dir in libomptarget: /data/myproject/build/build_libs/llvm-project/llvm/runtimes/runtimes-bins/openmp/runtime/src
-- LIBOMPTARGET: Building offloading runtime library libomptarget.
[100%] Building CXX object llvm-project/llvm/tools/llvm-reduce/CMakeFiles/llvm-reduce.dir/deltas/ReduceFunctionBodies.cpp.o
-- LIBOMPTARGET: Not building aarch64 NextGen offloading plugin: machine not found in the system.
-- LIBOMPTARGET: Building AMDGPU NextGen plugin for dlopened libhsa
-- LIBOMPTARGET: Not generating AMDGPU tests, no supported devices detected.
-- LIBOMPTARGET: Building CUDA NextGen offloading plugin.
-- LIBOMPTARGET: Building CUDA plugin linked against libcuda
-- LIBOMPTARGET: Not generating NVIDIA tests, no supported devices detected.
-- LIBOMPTARGET: Not building PPC64 NextGen offloading plugin: machine not found in the system.
-- LIBOMPTARGET: Not building PPC64le NextGen offloading plugin: machine not found in the system.
-- LIBOMPTARGET: Building x86_64 NextGen offloading plugin.
-- LIBOMPTARGET: Not building DeviceRTL. Missing clang: /data/myproject/build/build_libs/llvm-project/llvm/bin/clang, llvm-link: /data/myproject/build/build_libs/llvm-project/llvm/bin/llvm-link, opt: OPT_TOOL-NOTFOUND, or clang-offload-packager: /data/myproject/build/build_libs/llvm-project/llvm/bin/clang-offload-packager
-- LIBOMPTARGET: Building the llvm-omp-device-info tool
-- LIBOMPTARGET: Building the llvm-omp-kernel-replay tool
-- Looking for __atomic_load_1
[100%] Building CXX object llvm-project/llvm/tools/llvm-reduce/CMakeFiles/llvm-reduce.dir/deltas/ReduceFunctions.cpp.o
-- Looking for __atomic_load_1 - not found
-- Looking for __atomic_load_1 in atomic
-- Looking for __atomic_load_1 in atomic - found
-- Could NOT find Python3 (missing: Python3_INCLUDE_DIRS Development Development.Module Development.Embed) (found version "3.10.12")
-- Configuring done
CMake Error at CMakeLists.txt:248 (add_dependencies):
  The dependency target "clang" of target "runtimes-test-depends" does not
  exist.


CMake Error at /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:1940 (add_dependencies):
  The dependency target "clang" of target "check-runtimes" does not exist.
Call Stack (most recent call first):
  /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:1980 (add_lit_target)
  CMakeLists.txt:252 (umbrella_lit_testsuite_end)


CMake Error at /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:1940 (add_dependencies):
  The dependency target "clang" of target "check-openmp" does not exist.
Call Stack (most recent call first):
  /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:2007 (add_lit_target)
  /data/myproject/lib/llvm-project/openmp/cmake/OpenMPTesting.cmake:217 (add_lit_testsuite)
  /data/myproject/lib/llvm-project/openmp/cmake/OpenMPTesting.cmake:240 (add_openmp_testsuite)
  /data/myproject/lib/llvm-project/openmp/CMakeLists.txt:135 (construct_check_openmp_target)


CMake Error at /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:1940 (add_dependencies):
  The dependency target "clang" of target "check-libomp" does not exist.
Call Stack (most recent call first):
  /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:2007 (add_lit_target)
  /data/myproject/lib/llvm-project/openmp/cmake/OpenMPTesting.cmake:225 (add_lit_testsuite)
  /data/myproject/lib/llvm-project/openmp/runtime/test/CMakeLists.txt:44 (add_openmp_testsuite)


CMake Error at /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:1940 (add_dependencies):
  The dependency target "clang" of target "check-ompt" does not exist.
Call Stack (most recent call first):
  /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:2007 (add_lit_target)
  /data/myproject/lib/llvm-project/openmp/cmake/OpenMPTesting.cmake:217 (add_lit_testsuite)
  /data/myproject/lib/llvm-project/openmp/runtime/test/CMakeLists.txt:46 (add_openmp_testsuite)


CMake Error at /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:1940 (add_dependencies):
  The dependency target "clang" of target
  "check-libomptarget-x86_64-pc-linux-gnu" does not exist.
Call Stack (most recent call first):
  /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:2007 (add_lit_target)
  /data/myproject/lib/llvm-project/openmp/cmake/OpenMPTesting.cmake:225 (add_lit_testsuite)
  /data/myproject/lib/llvm-project/openmp/libomptarget/test/CMakeLists.txt:23 (add_openmp_testsuite)


CMake Error at /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:1940 (add_dependencies):
  The dependency target "clang" of target
  "check-libomptarget-x86_64-pc-linux-gnu-LTO" does not exist.
Call Stack (most recent call first):
  /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:2007 (add_lit_target)
  /data/myproject/lib/llvm-project/openmp/cmake/OpenMPTesting.cmake:225 (add_lit_testsuite)
  /data/myproject/lib/llvm-project/openmp/libomptarget/test/CMakeLists.txt:23 (add_openmp_testsuite)


CMake Error at /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:1940 (add_dependencies):
  The dependency target "clang" of target "check-libomptarget" does not
  exist.
Call Stack (most recent call first):
  /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:2007 (add_lit_target)
  /data/myproject/lib/llvm-project/openmp/cmake/OpenMPTesting.cmake:217 (add_lit_testsuite)
  /data/myproject/lib/llvm-project/openmp/libomptarget/test/CMakeLists.txt:40 (add_openmp_testsuite)


CMake Error at /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:1940 (add_dependencies):
  The dependency target "clang" of target "check-libarcher" does not exist.
Call Stack (most recent call first):
  /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:2007 (add_lit_target)
  /data/myproject/lib/llvm-project/openmp/cmake/OpenMPTesting.cmake:225 (add_lit_testsuite)
  /data/myproject/lib/llvm-project/openmp/tools/archer/tests/CMakeLists.txt:38 (add_openmp_testsuite)


CMake Error at /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:1940 (add_dependencies):
  The dependency target "clang" of target "check-ompt-multiplex" does not
  exist.
Call Stack (most recent call first):
  /data/myproject/lib/llvm-project/llvm/cmake/modules/AddLLVM.cmake:2007 (add_lit_target)
  /data/myproject/lib/llvm-project/openmp/cmake/OpenMPTesting.cmake:225 (add_lit_testsuite)
  /data/myproject/lib/llvm-project/openmp/tools/multiplex/tests/CMakeLists.txt:17 (add_openmp_testsuite)


-- Generating done
CMake Generate step failed.  Build files cannot be regenerated correctly.

I checked and confirmed that e.g. add_lit_target requests clang as a dependency, yet am not sure why it can’t satisfy that dependency. In theory, the two ways of building should likely not produce different results.

The recommended way of embedding is the opposite of your approach.

This isn’t what I’m trying to do, though, and for that I do believe I am following an approach I found recommended: [llvm-dev] Embedding llvm as a git submodule in Project

Would a fork of the project + your code work for you?

I’m not sure I understand your question, please could you clarify?

Instead of using LLVM as a git submodule, you could fork the LLVM monorepo and add your own code.