在Win10上编译LLVM
代码仓库:https://github.com/llvm/llvm-project
LLVM_TARGETS_TO_BUILD
-DLLVM_TARGETS_TO_BUILD="X86;PowerPC". The full list, as of March 2023, is: AArch64;AMDGPU;ARM;AVR;BPF;Hexagon;Lanai;LoongArch;Mips;MSP430;NVPTX;PowerPC;RISCV;Sparc;SystemZ;VE;WebAssembly;X86;XCore
-DLLVM_TARGETS_TO_BUILD=host // 将编译当前机器的架构的二进制 on an x86_64 machine will include the X86 target.
To enable all of them, use: -DLLVM_TARGETS_TO_BUILD=all
LLVM_ENABLE_PROJECTS
full list:bolt;clang;clang-tools-extra;compiler-rt;cross-project-tests;libc;libclc;lld;lldb;mlir;openmp;polly;pstl
To enable all of them, use: LLVM_ENABLE_RUNTIMES=all
LLVM_ENABLE_RUNTIMES
full list:libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp;llvm-libgcc;offload
To enable all of them, use: LLVM_ENABLE_RUNTIMES=all
CMAKE_BUILD_TYPE
This configures the optimization level for make or ninja builds.
Possible values:
|
Build Type |
Optimizations |
Debug Info |
Assertions |
Best suited for |
|---|---|---|---|---|
|
Release |
For Speed |
No |
No |
Users of LLVM and Clang |
|
Debug |
None |
Yes |
Yes |
Developers of LLVM |
|
RelWithDebInfo |
For Speed |
Yes |
No |
Users that also need Debug |
|
MinSizeRel |
For Size |
No |
No |
When disk space matters |
更多详见:https://llvm.org/docs/CMake.html
在windows上编译llvm

build.windows.clang.bat内容如下:
setlocal enableextensions set "ROOT_DIR=%~dp0" set "PROJECT_DIR=%ROOT_DIR%\llvm" set "path=%~dp0tools\bin;C:\Python36-32;%path%" set "CLANG_BUILD_DIR=%ROOT_DIR%\build\windows" set "CLANG_INSTALL_DIR=%ROOT_DIR%\install\windows" mkdir %CLANG_BUILD_DIR% set "LLVM_ENABLE_PROJECTS=clang;lld" set "LLVM_TARGETS_TO_BUILD=X86;WebAssembly" set "CMAKE_BUILD_TYPE=RelWithDebInfo" set "LIBFFI_DIR=%ROOT_DIR%\..\cppvm\source\cppvm-libffi\install\windows\x86_64" set "CMAKE_PARAM=-DLLVM_ENABLE_PROJECTS="%LLVM_ENABLE_PROJECTS%" -DLLVM_INCLUDE_EXAMPLES=OFF -DLLVM_INCLUDE_TESTS=OFF -DLLVM_INCLUDE_DOCS=OFF -DLLVM_ENABLE_LLD=On -DLLVM_EXPORT_SYMBOLS_FOR_PLUGINS=On -DLLVM_TARGETS_TO_BUILD="%LLVM_TARGETS_TO_BUILD%" -DLLVM_ENABLE_FFI=ON -DFFI_INCLUDE_DIR=%LIBFFI_DIR%\include -DLLVM_ENABLE_DUMP=On -DFFI_LIBRARY_DIR=%LIBFFI_DIR%\lib -DLLVM_OPTIMIZED_TABLEGEN=ON -DCMAKE_BUILD_TYPE=%CMAKE_BUILD_TYPE% -DCMAKE_INSTALL_PREFIX=%CLANG_INSTALL_DIR% %PROJECT_DIR%" cmake.exe -S %PROJECT_DIR% -B %CLANG_BUILD_DIR% -G "Visual Studio 16 2019" -A x64 -Thost=x64 -DLLVM_COMPILER_JOBS=14 -DCMAKE_C_FLAGS="/bigobj" -DCMAKE_CXX_FLAGS="/bigobj" %CMAKE_PARAM% cmake --build %CLANG_BUILD_DIR% --config "%CMAKE_BUILD_TYPE%" --target INSTALL -- /m pause
注:已安装Visual Studio 2019 Community版本
报错:
LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_prep_cif referenced in function "bool __cdecl ffiInvoke(void (__cdecl*)(void),class llvm::Function *,class llvm::ArrayRef <struct llvm::GenericValue>,class llvm::DataLayout const &,struct llvm::GenericValue &)" (?ffiInvoke@@YA_NP6AXXZPEAVFunction@llvm@@V?$ArrayRef@UGenericValue@llvm@@@2@AEBVDataLayout@2@AEAUGenericValue@2@@Z) [G:\svn\llvm -project\build\windows\tools\lli\lli.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_call referenced in function "bool __cdecl ffiInvoke(void (__cdecl*)(void),class llvm::Function *,class llvm::ArrayRef<str uct llvm::GenericValue>,class llvm::DataLayout const &,struct llvm::GenericValue &)" (?ffiInvoke@@YA_NP6AXXZPEAVFunction@llvm@@V?$ArrayRef@UGenericValue@llvm@@@2@AEBVDataLayout@2@AEAUGenericValue@2@@Z) [G:\svn\llvm-pro ject\build\windows\tools\lli\lli.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_void referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_typ e@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\lli\lli.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_sint8 referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_ty pe@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\lli\lli.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_sint16 referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_t ype@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\lli\lli.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_sint32 referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_t ype@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\lli\lli.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_sint64 referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_t ype@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\lli\lli.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_float referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_ty pe@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\lli\lli.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_double referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_t ype@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\lli\lli.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_pointer referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_ type@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\lli\lli.vcxproj] G:\svn\llvm-project\build\windows\RelWithDebInfo\bin\lli.exe : fatal error LNK1120: 10 unresolved externals [G:\svn\llvm-project\build\windows\tools\lli\lli.vcxproj]
LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_prep_cif referenced in function "bool __cdecl ffiInvoke(void (__cdecl*)(void),class llvm::Function *,class llvm::ArrayRef <struct llvm::GenericValue>,class llvm::DataLayout const &,struct llvm::GenericValue &)" (?ffiInvoke@@YA_NP6AXXZPEAVFunction@llvm@@V?$ArrayRef@UGenericValue@llvm@@@2@AEBVDataLayout@2@AEAUGenericValue@2@@Z) [G:\svn\llvm -project\build\windows\tools\llvm-shlib\LLVM-C.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_call referenced in function "bool __cdecl ffiInvoke(void (__cdecl*)(void),class llvm::Function *,class llvm::ArrayRef<str uct llvm::GenericValue>,class llvm::DataLayout const &,struct llvm::GenericValue &)" (?ffiInvoke@@YA_NP6AXXZPEAVFunction@llvm@@V?$ArrayRef@UGenericValue@llvm@@@2@AEBVDataLayout@2@AEAUGenericValue@2@@Z) [G:\svn\llvm-pro ject\build\windows\tools\llvm-shlib\LLVM-C.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_void referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_typ e@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\llvm-shlib\LLVM-C.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_sint8 referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_ty pe@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\llvm-shlib\LLVM-C.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_sint16 referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_t ype@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\llvm-shlib\LLVM-C.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_sint32 referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_t ype@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\llvm-shlib\LLVM-C.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_sint64 referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_t ype@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\llvm-shlib\LLVM-C.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_float referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_ty pe@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\llvm-shlib\LLVM-C.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_double referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_t ype@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\llvm-shlib\LLVM-C.vcxproj] LLVMInterpreter.lib(ExternalFunctions.obj) : error LNK2019: unresolved external symbol __imp_ffi_type_pointer referenced in function "struct _ffi_type * __cdecl ffiTypeFor(class llvm::Type *)" (?ffiTypeFor@@YAPEAU_ffi_ type@@PEAVType@llvm@@@Z) [G:\svn\llvm-project\build\windows\tools\llvm-shlib\LLVM-C.vcxproj] G:\svn\llvm-project\build\windows\RelWithDebInfo\bin\LLVM-C.dll : fatal error LNK1120: 10 unresolved externals [G:\svn\llvm-project\build\windows\tools\llvm-shlib\LLVM-C.vcxproj]
原因是:使用了静态库版的ffi.lib,而在G:\svn\llvm-project\llvm\lib\ExecutionEngine\Interpreter\ExternalFunctions.cpp中引用ffi.h,会将FFI_API定义为__declspec(dllimport),导致名字修饰都会带上__imp_前缀,从而找不到这些符号
/* Need minimal decorations for DLLs to work on Windows. GCC has autoimport and autoexport. Always mark externally visible symbols as dllimport for MSVC clients, even if it means an extra indirection when using the static version of the library. Besides, as a workaround, they can define FFI_BUILDING if they *know* they are going to link with the static library. */ #if defined _MSC_VER # if defined FFI_BUILDING_DLL /* Building libffi.DLL with msvcc.sh */ # define FFI_API __declspec(dllexport) # elif !defined FFI_BUILDING /* Importing libffi.DLL */ # define FFI_API __declspec(dllimport) # else /* Building/linking static library */ # define FFI_API # endif #else # define FFI_API #endif
解决办法:预定义FFI_BUILDING宏
add_definitions("-DFFI_BUILDING")

tag为llvmorg-16.0.6代码编译报错:
G:\svn\llvm-project\clang\lib\Lex\UnicodeCharSets.h(1,1): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss [G:\svn\llvm-project\build\windows\tools\clang\lib\Lex\obj.clangLex.vcxproj] G:\svn\llvm-project\clang\lib\Lex\UnicodeCharSets.h(394,1): error C2226: syntax error: unexpected type 'llvm::sys::UnicodeCharRange' [G:\svn\llvm-project\build\windows\tools\clang\lib\Lex\obj.clan gLex.vcxproj] G:\svn\llvm-project\clang\lib\Lex\UnicodeCharSets.h(394,69): error C2143: syntax error: missing ';' before '{' [G:\svn\llvm-project\build\windows\tools\clang\lib\Lex\obj.clangLex.vcxproj] G:\svn\llvm-project\clang\lib\Lex\UnicodeCharSets.h(394,69): error C2447: '{': missing function header (old-style formal list?) [G:\svn\llvm-project\build\windows\tools\clang\lib\Lex\obj.clangLex. vcxproj] clang-offload-packager.vcxproj -> G:\svn\llvm-project\build\windows\RelWithDebInfo\bin\clang-offload-packager.exe clang-offload-bundler.vcxproj -> G:\svn\llvm-project\build\windows\RelWithDebInfo\bin\clang-offload-bundler.exe G:\svn\llvm-project\clang\lib\Lex\Lexer.cpp(1479,59): error C2664: 'llvm::sys::UnicodeCharSet::UnicodeCharSet(llvm::sys::UnicodeCharSet::CharRanges)': cannot convert argument 1 from 'const llvm::s ys::UnicodeCharRange []' to 'llvm::sys::UnicodeCharSet::CharRanges' [G:\svn\llvm-project\build\windows\tools\clang\lib\Lex\obj.clangLex.vcxproj] G:\svn\llvm-project\clang\lib\Lex\Lexer.cpp(1480,50): message : No constructor could take the source type, or constructor overload resolution was ambiguous [G:\svn\llvm-project\build\windows\tools \clang\lib\Lex\obj.clangLex.vcxproj] G:\svn\llvm-project\llvm\include\llvm/Support/UnicodeCharRanges.h(55,3): message : see declaration of 'llvm::sys::UnicodeCharSet::UnicodeCharSet' [G:\svn\llvm-project\build\windows\tools\clang\lib \Lex\obj.clangLex.vcxproj] G:\svn\llvm-project\clang\lib\Lex\Lexer.cpp(1508,9): error C2065: 'C11AllowedIDCharRanges': undeclared identifier [G:\svn\llvm-project\build\windows\tools\clang\lib\Lex\obj.clangLex.vcxproj] G:\svn\llvm-project\clang\lib\Lex\Lexer.cpp(1548,59): error C2664: 'llvm::sys::UnicodeCharSet::UnicodeCharSet(llvm::sys::UnicodeCharSet::CharRanges)': cannot convert argument 1 from 'const llvm::s ys::UnicodeCharRange []' to 'llvm::sys::UnicodeCharSet::CharRanges' [G:\svn\llvm-project\build\windows\tools\clang\lib\Lex\obj.clangLex.vcxproj]
导致原因是:llvm-project\clang\lib\Lex\UnicodeCharSets.h文件在utf8编码下,有注释乱码

解决方法:将乱码的注释删除掉
浙公网安备 33010602011771号