How to build aarch64 clangd on x86_64 host machine?

Hi all,

I would like to know if my compilation method is correct. What is the official method for compiling the aarch64 version in LLVM?

I want to run clangd, clang-format, clang-tidy on an aarch64 architecture board, and I wanto compile the ‘aarch64 clang tools’ using a computer with x86_64 architecture. Just to confirm again, the architecture of the compiling machine is x86_64(host), and the target platform is aarch64(target).
I attempted the task with the following command:

git clone --depth 1 --branch llvmorg-16.0.6 https://github.com/llvm/llvm-project.git
mkdir -p llvm-project/tmp_build && cd llvm-project/tmp_build

cmake -G "Unix Makefiles" \
	-D CMAKE_C_COMPILER=/usr/bin/aarch64-linux-gnu-gcc-8 \
	-D CMAKE_CXX_COMPILER=/usr/bin/aarch64-linux-gnu-g++-8 \
	-D CMAKE_LINKER=/usr/bin/aarch64-linux-gnu-ld \
	-D CMAKE_ASM_COMPILER=/usr/bin/aarch64-linux-gnu-as \
	-D CMAKE_AR=/usr/bin/aarch64-linux-gnu-ar \
	-D CMAKE_NM=/usr/bin/aarch64-linux-gnu-nm \
	-D CMAKE_RANLIB=/usr/bin/aarch64-linux-gnu-ranlib \
	-D CMAKE_BUILD_TYPE=Release \
	-D CMAKE_VERBOSE_MAKEFILE=ON \
	-D LLVM_ENABLE_PROJECTS="clang;clang-tools-extra" \
	../llvm
	
make -j7

Here is the error log:

cd /home/user/llvm-project/tmp_build/tools/clang/tools/extra/clang-tidy/misc && ../../../../../../bin/clang-tidy-confusable-chars-gen /home/user/llvm-project/clang-tools-extra/clang-tidy/misc/ConfusableTable/confusables.txt /home/user/llvm-project/tmp_build/tools/clang/tools/extra/clang-tidy/misc/Confusables.inc
/lib/ld-linux-aarch64.so.1: No such file or directory

It builds the ‘clang-tidy-confusable-chars-gen’ tools using an ARM64 compiler, but it will be used on a host x86_64 machine. I’m searching for the compile log.

/usr/bin/aarch64-linux-gnu-g++-8   -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wno-comment -Wno-misleading-indentation -ffunction-sections -fdata-sections -fno-common -Woverloaded-virtual -fno-strict-aliasing -O3 -DNDEBUG   -Wl,-rpath-link,/home/user/llvm-project/tmp_build/./lib  -Wl,--gc-sections CMakeFiles/clang-tidy-confusable-chars-gen.dir/BuildConfusableTable.cpp.o  -o ../../../../../../../bin/clang-tidy-confusable-chars-gen  -Wl,-rpath,"\$ORIGIN/../lib" ../../../../../../../lib/libLLVMSupport.a -lpthread -lrt -ldl -lpthread -lm ../../../../../../../lib/libLLVMDemangle.a

With questions like this, one can never confirm too many times. Thank you for being so clear :slight_smile:

Correct, ld-linux-aarch64.so.1 would be the dynamic linker if you were on ARM64.

Here is a command I use for cross compiling lldb in the same way:

LLVM_HOST_BUILD_DIR=/work/open_source/lldb-cross-compile/build-host/

cmake -G Ninja \
  -DCMAKE_SYSTEM_NAME=Linux \
  -DCMAKE_SYSTEM_PROCESSOR=AArch64 \
  -DCMAKE_C_COMPILER=aarch64-unknown-linux-gnu-gcc \
  -DCMAKE_CXX_COMPILER=aarch64-unknown-linux-gnu-g++ \
  -DLLVM_NATIVE_TOOL_DIR=$LLVM_HOST_BUILD_DIR/bin \
  -DLLVM_HOST_TRIPLE=aarch64-unknown-linux-gnu \
  -DLLVM_ENABLE_PROJECTS="clang;lldb" \
  -DCMAKE_LIBRARY_ARCHITECTURE=aarch64-unknown-linux-gnu \
  -DCMAKE_BUILD_TYPE=Release \
  ../llvm-project/llvm

The key settings are:

  • CMAKE_SYSTEM_NAME / CMAKE_SYSTEM_PROCESSOR - when you set these you’re telling CMake that you’re cross compiling. Might not be required but it’ll avoid potential confusion.
  • LLVM_NATIVE_TOOL_DIR - This points to an existing x86 build where you already have tools such as the one your build was trying to run. llvm-tblgen is another common example here.

The others may not be needed, it has been a while since I put that command together.

Once you’ve told CMake you’re cross compiling, and where the native tools are, llvm will decide as appropriate whether to use the native tool instead or just skip it entirely.

If you still have the same error, it would be worth looking at the CMake files that choose to invoke that program and seeing if they check for either of those variables.

1 Like

A quick look shows that the CMake config checks for a variable LLVM_USE_HOST_TOOLS which is set when CMAKE_CROSSCOMPILING is set, and that should be set once you’ve set CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR.

(starting from llvm-project/clang-tools-extra/clang-tidy/misc/CMakeLists.txt at dacbf4a7094a72e7651737ac1f137b03b5d5b2d8 · llvm/llvm-project · GitHub)

So you should be good to go.

Thank you once again for your invaluable support, I have successfully compiled the arm64 version of clangd.

1 Like