Unable to build LLVM release/16.x on Ubuntu inside VirtualBox

I am trying to build release/16.x of llvm-project; but hitting a snafu:

First things first, the system I am on is as follows

Inside my Oracle VirtualBox 7.0, I have
RAM: 5 GiB
CPU: 3 x Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
OS: Ubuntu 22.04.2 LTS
Clang: Ubuntu clang version 14.0.0-1ubuntu1
CMake: cmake version 3.22.1
Ninja: 1.10.1
Gold: GNU gold (GNU Binutils for Ubuntu 2.38) 1.16
Python: Python 3.10

After cloning the release/16.x branch, I am using the following configuration:

mkdir -p build; mkdir -p installed; cd build; \
cmake -G "Ninja" \
-DCMAKE_C_COMPILER=$(which clang) \
-DCMAKE_CXX_COMPILER=$(which clang++) \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
-DLLVM_TARGETS_TO_BUILD="X86;RISCV" \
-DCMAKE_BUILD_TYPE="Debug" \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_PARALLEL_COMPILE_JOBS=2 \
-DCMAKE_INSTALL_PREFIX=$(pwd)/../installed \
-DLLVM_PARALLEL_LINK_JOBS=2 \
-DLLVM_BUILD_LLVM_DYLIB=ON \
-DLLVM_LINK_LLVM_DYLIB=ON \
-DBUILD_SHARED_LIBS=OFF \
-DLLVM_USE_SPLIT_DWARF=ON \
-DLLVM_USE_LINKER=gold \
-DLLVM_OPTIMIZED_TABLEGEN=ON \
-DCMAKE_EXE_LINKER_FLAGS='-Wl,-no-keep-memory,-fuse-ld=gold' \
../llvm

The cmake configuration completes successfully, and build files are written:

...
...
-- Configuring done
-- Generating done
-- Build files have been written to: /media/sf_shared_folder/myProject/llvm-project/build

Subsequently, when I am trying to build the project using ninja, I am facing the following problem:

ninja -j2 all
[243/3771] Building native llvm-tblgen...
[236/236] Linking CXX executable bin/llvm-tblgen
[326/3771] Building Attributes.inc...
FAILED: include/llvm/IR/Attributes.inc /media/sf_shared_folder/myProject/llvm-project/build/include/llvm/IR/Attributes.inc 
cd /media/sf_shared_folder/myProject/llvm-project/build && /media/sf_shared_folder/myProject/llvm-project/build/NATIVE/bin/llvm-tblgen -gen-attrs -I /media/sf_shared_folder/myProject/llvm-project/llvm/include/llvm/IR -I/media/sf_shared_folder/myProject/llvm-project/build/include -I/media/sf_shared_folder/myProject/llvm-project/llvm/include /media/sf_shared_folder/myProject/llvm-project/llvm/include/llvm/IR/Attributes.td --write-if-changed -o include/llvm/IR/Attributes.inc -d include/llvm/IR/Attributes.inc.d
/bin/sh: 1: /media/sf_shared_folder/myProject/llvm-project/build/NATIVE/bin/llvm-tblgen: Exec format error
[327/3771] Building CXX object tools/clang/utils/TableGen/CMakeFiles/clang-tblgen.dir/ClangAttrEmitter.cpp.o
ninja: build stopped: subcommand failed.

Now, what is this llvm-tblgen: Exec format error and how do I fix this for a successful build?

I have also raised the issue over at the github repo for llvm-project, issue 62429

This is likely the first time the build system has tried to run a binary it’s just built.

Is the which clang definitely configured to produce binaries for the host system? What does file report on the llvm-tblgen it’s complaining about?

So the fix was not even related to LLVM. The real problem was that I had cloned the LLVM project to a shared_folder (a directory shared between guest OS and host OS, under VirtualBox). Now, with Windows 10 host, the symbolic link creation is messed up for the guest OS.

Since LLVM was not throwing an error specific to this, I was unable to detect this. So I tried to use the following command:

cmake -G "Ninja" \
-DCMAKE_C_COMPILER=$(which clang) \
-DCMAKE_CXX_COMPILER=$(which clang++) \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
-DLLVM_TARGETS_TO_BUILD="X86;RISCV" \
-DCMAKE_BUILD_TYPE="Debug" \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_PARALLEL_COMPILE_JOBS=2 \
-DCMAKE_INSTALL_PREFIX=$(pwd)/../installed \
-DLLVM_PARALLEL_LINK_JOBS=2 \
-DLLVM_BUILD_LLVM_DYLIB=OFF \
-DLLVM_LINK_LLVM_DYLIB=OFF \
-DBUILD_SHARED_LIBS=ON \
-DLLVM_USE_SPLIT_DWARF=ON \
-DLLVM_USE_LINKER=gold \
-DLLVM_OPTIMIZED_TABLEGEN=ON \
-DCMAKE_EXE_LINKER_FLAGS='-Wl,-no-keep-memory,-fuse-ld=gold' \
../llvm

So, with BUILD_SHARED_LIBS=ON during ninja -j2 the build system immediately complained that it is not able to create symbolic links. Armed with this information, I cloned the project on a path internal to the guest OS and ran the original command (in question). This resulted in a successful build.

Also, the cmake configuration in the question results in smallest build time (which can be further reduced with appropriate parallel job numbers)

Depending on your use case:
-DCMAKE_BUILD_TYPE=“Release”
-DBUILD_SHARED_LIBS=OFF

might help to reduce the build time.

Yeah, I meant smallest build time when Debug options are needed.
:slightly_smiling_face:

A complete cmake tutorial is available at: Building LLVM with CMake — LLVM 17.0.0git documentation