如何为 Android 编译 LLVM/Clang?

How to compile LLVM/Clang for Android?

我正在尝试在 mac.

上使用 Cmake 为 Android 编译 LLVM/Clang 8.0.1(因为不再支持我之前使用的 autotools)

我正在使用 https://github.com/llvm/llvm-project.git。 在构建目录中,我使用以下配置脚本(遵循 manuals):

cmake ../llvm \
-DCMAKE_INSTALL_PREFIX=/Users/asmirnov/Library/Android/llvm_android_arm \
-DLLVM_ENABLE_PROJECTS=clang \
-DLLVM_ENABLE_TERMINFO=OFF \
-DLLVM_ENABLE_THREADS=OFF \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_TARGETS_TO_BUILD="ARM;X86" \
-DLIBCLANG_BUILD_STATIC=ON \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_ENABLE_ZLIB=OFF \
\
-DCMAKE_CROSSCOMPILING=True \
-DLLVM_TABLEGEN=/Users/asmirnov/Library/Android/llvm/bin/llvm-tblgen \
-DCLANG_TABLEGEN=/Users/asmirnov/Documents/dev/src/llvm-project/build/bin/clang-tblgen \
-DLLVM_DEFAULT_TARGET_TRIPLE=arm-linux-gnueabihf \
-DLLVM_TARGET_ARCH=ARM \
-DBUILD_SHARED_LIBS=ON \
-DLLVM_ENABLE_PIC=False \
-DCMAKE_TOOLCHAIN_FILE=/Users/asmirnov/Library/Android/ndk/android-ndk-r20/build/cmake/android.toolchain.cmake

如您所见,我正在使用来自 Android NDK (20) 的 CMake 工具链文件。 我也在本地编译它并传递 LLVM_TABLEGENCLANG_TABLEGEN。 配置成功,但在构建过程中出现以下错误:

/Users/asmirnov/Documents/dev/src/llvm-project/llvm/lib/Transforms/Hello/Hello.cpp:27: error: undefined reference to 'llvm::Pass::~Pass()'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/lib/Transforms/Hello/Hello.cpp:33: error: undefined reference to 'llvm::errs()'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/lib/Transforms/Hello/Hello.cpp:34: error: undefined reference to 'llvm::errs()'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/lib/Transforms/Hello/Hello.cpp:34: error: undefined reference to 'llvm::Value::getName() const'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/lib/Transforms/Hello/Hello.cpp:34: error: undefined reference to 'llvm::raw_ostream::write_escaped(llvm::StringRef, bool)'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/include/llvm/Support/raw_ostream.h:149: error: undefined reference to 'llvm::raw_ostream::write(unsigned char)'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/include/llvm/Support/raw_ostream.h:174: error: undefined reference to 'llvm::raw_ostream::write(char const*, unsigned int)'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/lib/Transforms/Hello/Hello.cpp:45: error: undefined reference to 'llvm::Pass::~Pass()'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/lib/Transforms/Hello/Hello.cpp:51: error: undefined reference to 'llvm::errs()'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/lib/Transforms/Hello/Hello.cpp:52: error: undefined reference to 'llvm::errs()'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/lib/Transforms/Hello/Hello.cpp:52: error: undefined reference to 'llvm::Value::getName() const'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/lib/Transforms/Hello/Hello.cpp:52: error: undefined reference to 'llvm::raw_ostream::write_escaped(llvm::StringRef, bool)'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/include/llvm/PassSupport.h:98: error: undefined reference to 'llvm::PassRegistry::getPassRegistry()'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/include/llvm/PassSupport.h:98: error: undefined reference to 'llvm::PassRegistry::registerPass(llvm::PassInfo const&, bool)'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/include/llvm/PassSupport.h:98: error: undefined reference to 'llvm::PassRegistry::getPassRegistry()'
/Users/asmirnov/Documents/dev/src/llvm-project/llvm/include/llvm/PassSupport.h:98: error: undefined reference to 'llvm::PassRegistry::registerPass(llvm::PassInfo const&, bool)'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::Pass::~Pass()'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::Pass::getPassName() const'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::Pass::print(llvm::raw_ostream&, llvm::Module const*) const'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::FunctionPass::createPrinterPass(llvm::raw_ostream&, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&) const'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::FunctionPass::assignPassManager(llvm::PMStack&, llvm::PassManagerType)'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::Pass::preparePassManager(llvm::PMStack&)'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::FunctionPass::getPotentialPassManagerType() const'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::Pass::getAnalysisUsage(llvm::AnalysisUsage&) const'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::Pass::releaseMemory()'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::Pass::getAdjustedAnalysisPointer(void const*)'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::Pass::getAsImmutablePass()'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::Pass::getAsPMDataManager()'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::Pass::verifyAnalysis() const'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello: error: undefined reference to 'llvm::Pass::dumpPassStructure(unsigned int)'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::Pass::~Pass()'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::Pass::getPassName() const'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::Pass::print(llvm::raw_ostream&, llvm::Module const*) const'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::FunctionPass::createPrinterPass(llvm::raw_ostream&, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&) const'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::FunctionPass::assignPassManager(llvm::PMStack&, llvm::PassManagerType)'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::Pass::preparePassManager(llvm::PMStack&)'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::FunctionPass::getPotentialPassManagerType() const'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::Pass::releaseMemory()'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::Pass::getAdjustedAnalysisPointer(void const*)'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::Pass::getAsImmutablePass()'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::Pass::getAsPMDataManager()'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::Pass::verifyAnalysis() const'
CMakeFiles/LLVMHello.dir/Hello.cpp.o:Hello.cpp:vtable for (anonymous namespace)::Hello2: error: undefined reference to 'llvm::Pass::dumpPassStructure(unsigned int)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [lib/LLVMHello.so] Error 1
make[1]: *** [lib/Transforms/Hello/CMakeFiles/LLVMHello.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

我做错了什么吗?是 LLVM/Clang 错误吗?任何解决方法?

我不知道这是不是你唯一的问题,但这绝对是错误的,可能会导致 Clang 找不到正确的库:

-DLLVM_DEFAULT_TARGET_TRIPLE=arm-linux-gnueabihf

Android 是 arm-linux-androideabi$API_LEVELarm-linux-gnueabihf 是一个 GNU/Linux 目标。

是的,这需要一些类似于 this 的解决方法。也使用 -DLLVM_DEFAULT_TARGET_TRIPLE="armv7a-linux-androideabi"