LLVM build always fails if examples/docs directories are missing

I’ve noticed some strange behavior when building LLVM/MLIR (from the llvmorg-19.1.4 tag) that I’m not sure is a bug (but it certainly seems so).

I am running CMake as follows (in a ubuntu:latest Docker container):

    cmake -S llvm \
          -B build-debug \
          -G Ninja \
          -DCMAKE_BUILD_TYPE=Debug \
          -DCMAKE_C_COMPILER=clang \
          -DCMAKE_CXX_COMPILER=clang++ \
          -DLLVM_ENABLE_PROJECTS="mlir" \
          -DLLVM_TARGETS_TO_BUILD="X86;AArch64;RISCV" \
          -DLLVM_INCLUDE_TESTS=OFF \
          -DLLVM_BUILD_TESTS=OFF \
          -DLLVM_INCLUDE_BENCHMARKS=OFF \
          -DLLVM_BUILD_BENCHMARKS=OFF \
          -DLLVM_INCLUDE_EXAMPLES=OFF \
          -DLLVM_BUILD_EXAMPLES=OFF \
          -DLLVM_INCLUDE_DOCS=OFF \
          -DLLVM_BUILD_DOCS=OFF \
          -DLLVM_INSTALL_UTILS=ON \
          -DLLVM_OPTIMIZED_TABLEGEN=ON \
          -DLLVM_PARALLEL_TABLEGEN_JOBS=$(( $(nproc) / 2 )) \
          -DLLVM_PARALLEL_COMPILE_JOBS=$(( $(nproc) / 2 )) \
          -DLLVM_CCACHE_BUILD=ON \
          -DLLVM_USE_LINKER=lld \
          -DLLVM_RAM_PER_LINK_JOB=10000 \
          -DLLVM_USE_SPLIT_DWARF=ON \
          -DLLVM_APPEND_VC_REV=OFF \
          -DMLIR_ENABLE_BINDINGS_PYTHON=ON \
          -DPython3_EXECUTABLE=$(which python3)

Building after that works if I have the whole source tree checked out but as an experiment I’ve tried to do a sparse checkout, not including stuff that I don’t intend to build anyways, namely: llvm/test, llvm/unittests, llvm/examples , llvm/docs and the same directories under mlir/. This does NOT work, because CMake tries to include some of these directories anyways (but not all of them! only examples and docs as far as I can tell). The problem seems to be that another cmake is spawned under build-debug/NATIVE that resets LLVM_INCLUDE_EXAMPLES and LLVM_INCLUDE_DOCS to their default values. Is this intentional behavior?

Seems like a bad interaction with the -DLLVM_OPTIMIZED_TABLEGEN=ON option, this is what creates the NATIVE sub-build. It should not reset these options of course, patch welcome on this :slight_smile:

That makes sense, I’ll try my best to figure this out although I have a feeling this is non-trivial.

llvm-project/llvm/cmake/modules/CrossCompile.cmake at 0ffdaf445e72a152cc0707c9885046e55127af31 · llvm/llvm-project · GitHub should be the relevant CMake code; in theory you should just have to add the additional variables that need to be propagated there.