LLVM系列第一章:编译LLVM源码

本文详细记录了在macOS上从源码编译到安装LLVM的步骤,包括获取源码、配置编译选项、使用CMake和Ninja编译工具、测试LLVM及安装过程。尽管编译过程中出现了一些测试错误,但不影响LLVM的正常使用。
摘要由CSDN通过智能技术生成

系列文章目录

LLVM系列第一章:编译LLVM源码
LLVM系列第二章:模块Module
LLVM系列第三章:函数Function
LLVM系列第四章:逻辑代码块Block
LLVM系列第五章:全局变量Global Variable
LLVM系列第六章:函数返回值Return
LLVM系列第七章:函数参数Function Arguments
LLVM系列第八章:算术运算语句Arithmetic Statement
LLVM系列第九章:控制流语句if-else
LLVM系列第十章:控制流语句if-else-phi
LLVM系列第十一章:写一个Hello World
LLVM系列第十二章:写一个简单的词法分析器Lexer
LLVM系列第十三章:写一个简单的语法分析器Parser
LLVM系列第十四章:写一个简单的语义分析器Semantic Analyzer
LLVM系列第十五章:写一个简单的中间代码生成器IR Generator
LLVM系列第十六章:写一个简单的编译器
LLVM系列第十七章:for循环
LLVM系列第十八章:写一个简单的IR处理流程Pass
LLVM系列第十九章:写一个简单的Module Pass
LLVM系列第二十章:写一个简单的Function Pass
LLVM系列第二十一章:写一个简单的Loop Pass
LLVM系列第二十二章:写一个简单的编译时函数调用统计器(Pass)
LLVM系列第二十三章:写一个简单的运行时函数调用统计器(Pass)
LLVM系列第二十四章:用Xcode编译调试LLVM源码
LLVM系列第二十五章:简单统计一下LLVM源码行数
LLVM系列第二十六章:理解LLVMContext
LLVM系列第二十七章:理解IRBuilder
LLVM系列第二十八章:写一个JIT Hello World
LLVM系列第二十九章:写一个简单的常量加法“消除”工具(Pass)

flex&bison系列



前言

LLVM是一个著名的开源编译器项目、库、工具,在此记录下编译LLVM源码的过程,以备查阅。

一、操作系统

我用的操作系统是macOS 11.6.4:

System Version:        macOS 11.6.4 (20G417)
Kernel Version:        Darwin 20.6.0

Model Name:            MacBook Pro
Model Identifier:      MacBookPro15,1
Processor Name:        8-Core Intel Core i9
Processor Speed:       2.3 GHz
Number of Processors:  1
Total Number of Cores: 8
Memory:                16 GB

二、开发环境

用到的工具有Clang (Xcode)、Git、CMake以及Ninja。

关于安装Xcode, 可参考这个简易教程:

How to download Xcode DMG or XIP file? - Stack Overflow

安装后确认信息如下(示例):

% clang -v
                                                        
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: x86_64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

对于其它工具,可借助brew (https://brew.sh) 进行安装(示例):

brew install git cmake ninja

安装后确认信息如下(示例):

% brew list --version git cmake ninja

cmake 3.22.1
git 2.32.0
ninja 1.10.2_1

三、下载源码

使用命令如下(示例):

git clone https://github.com/llvm/llvm-project.git
cd llvm-project
git checkout -b llvmorg-12.0.0

确认branch的信息如下(示例):

% git branch
                   
* llvmorg-12.0.0
  main

四、编译并安装LLVM

我们可使用工具CMake和Ninja来编译LLVM源代码。

1. 生成项目配置

使用命令如下(示例):

cd llvm-project
mkdir build
cd build

# Compile clang as well
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DLLVM_ENABLE_PROJECTS=clang ../llvm

# Or, don't compile clang
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug ../llvm

输出log如下(示例):

-- The C compiler identification is AppleClang 13.0.0.13000029
-- The CXX compiler identification is AppleClang 13.0.0.13000029
-- The ASM compiler identification is Clang with GNU-like command-line
-- Found assembler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features - done
-- No build type selected, default to Debug
-- clang project is enabled
-- clang-tools-extra project is disabled
-- compiler-rt project is disabled
-- debuginfo-tests project is disabled
-- libc project is disabled
-- libclc project is disabled
-- libcxx project is disabled
-- libcxxabi project is disabled
-- libunwind project is disabled
-- lld project is disabled
-- lldb project is disabled
-- mlir project is disabled
-- openmp project is disabled
-- parallel-libs project is disabled
-- polly project is disabled
-- pstl project is disabled
-- flang project is disabled
-- Found libtool - /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool
-- Looking for dlfcn.h - found
-- Looking for errno.h - found
-- Looking for fcntl.h - found
-- Looking for link.h - not found
-- Looking for malloc/malloc.h - found
-- Looking for pthread.h - found
-- Looking for signal.h - found
-- Looking for sys/ioctl.h - found
-- Looking for sys/mman.h - found
-- Looking for sys/param.h - found
-- Looking for sys/resource.h - found
-- Looking for sys/stat.h - found
-- Looking for sys/time.h - found
-- Looking for sys/types.h - found
-- Looking for sysexits.h - found
-- Looking for termios.h - found
-- Looking for unistd.h - found
-- Looking for valgrind/valgrind.h - not found
-- Looking for fenv.h - found
-- Looking for FE_ALL_EXCEPT - found
-- Looking for FE_INEXACT - found
-- Looking for mach/mach.h - found
-- Looking for histedit.h - found
-- Looking for CrashReporterClient.h - not found
-- Performing Test HAVE_CRASHREPORTER_INFO - Success
-- Looking for pthread_create in pthread - found
-- Looking for pthread_getspecific in pthread - found
-- Looking for pthread_rwlock_init in pthread - found
-- Looking for pthread_mutex_lock in pthread - found
-- Looking for dlopen in dl - found
-- Looking for clock_gettime in rt - not found
-- Looking for pfm_initialize in pfm - not found
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Found ZLIB: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/lib/libz.tbd (found version "1.2.11") 
-- Looking for compress2 - found
-- Found LibXml2: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/lib/libxml2.tbd (found version "2.9.4") 
-- Looking for xmlReadMemory - found
-- Looking for el_init in edit - found
-- Looking for xar_open in xar - found
-- Looking for arc4random - found
-- Looking for backtrace - found
-- backtrace facility detected in default set of libraries
-- Found Backtrace: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include  
-- Performing Test C_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW - Success
-- Looking for __register_frame - found
-- Looking for __deregister_frame - found
-- Looking for _Unwind_Backtrace - found
-- Looking for getpagesize - found
-- Looking for sysconf - found
-- Looking for getrusage - found
-- Looking for setrlimit - found
-- Looking for isatty - found
-- Looking for futimens - found
-- Looking for futimes - found
-- Looking for posix_fallocate - not found
-- Looking for lseek64 - not found
-- Looking for mallctl - not found
-- Looking for mallinfo - not found
-- Looking for malloc_zone_statistics - found
-- Looking for getrlimit - found
-- Looking for posix_spawn - found
-- Looking for pread - found
-- Looking for sbrk - found
-- Looking for strerror - found
-- Looking for strerror_r - found
-- Looking for strerror_s - not found
-- Looking for setenv - found
-- Looking for dlopen - found
-- Looking for dladdr - found
-- Performing Test HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC - Success
-- Performing Test HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC - Failed
-- Looking for __GLIBC__ - not found
-- Looking for pthread_getname_np - found
-- Looking for pthread_setname_np - found
-- Performing Test HAVE_STD_IS_TRIVIALLY_COPYABLE - Success
-- Performing Test HAVE_CXX_ATOMICS_WITHOUT_LIB - Success
-- Performing Test HAVE_CXX_ATOMICS64_WITHOUT_LIB - Success
-- Performing Test LLVM_HAS_ATOMICS - Success
-- Performing Test SUPPORTS_VARIADIC_MACROS_FLAG - Success
-- Performing Test SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG - Success
-- Native target architecture is X86
-- Threads enabled.
-- Doxygen disabled.
-- Go bindings enabled.
-- Ninja version: 1.10.2
-- Found ld64 - /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
-- Could NOT find OCaml (missing: OCAMLFIND OCAML_VERSION OCAML_STDLIB_PATH) 
-- Could NOT find OCaml (missing: OCAMLFIND OCAML_VERSION OCAML_STDLIB_PATH) 
-- OCaml bindings disabled.
-- Could NOT find Python module pygments
-- Could NOT find Python module pygments.lexers.c_cpp
-- Could NOT find Python module yaml
-- LLVM host triple: x86_64-apple-darwin20.6.0
-- LLVM default target triple: x86_64-apple-darwin20.6.0
-- Performing Test C_SUPPORTS_FPIC - Success
-- Performing Test CXX_SUPPORTS_FPIC - Success
-- Building with -fPIC
-- Performing Test SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG - Success
-- Performing Test C_SUPPORTS_WERROR_DATE_TIME - Success
-- Performing Test CXX_SUPPORTS_WERROR_DATE_TIME - Success
-- Performing Test CXX_SUPPORTS_WERROR_UNGUARDED_AVAILABILITY_NEW - Success
-- Performing Test CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG - Success
-- Performing Test C_SUPPORTS_IMPLICIT_FALLTHROUGH_FLAG - Success
-- Performing Test CXX_SUPPORTS_IMPLICIT_FALLTHROUGH_FLAG - Success
-- Performing Test C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG - Success
-- Performing Test CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG - Success
-- Performing Test CXX_SUPPORTS_CLASS_MEMACCESS_FLAG - Failed
-- Performing Test CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG - Success
-- Performing Test CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR - Success
-- Performing Test C_SUPPORTS_DELETE_NON_VIRTUAL_DTOR_FLAG - Success
-- Performing Test CXX_SUPPORTS_DELETE_NON_VIRTUAL_DTOR_FLAG - Success
-- Performing Test CXX_SUPPORTS_SUGGEST_OVERRIDE_FLAG - Success
-- Performing Test CXX_WSUGGEST_OVERRIDE_ALLOWS_ONLY_FINAL - Success
-- Performing Test C_WCOMMENT_ALLOWS_LINE_WRAP - Success
-- Performing Test C_SUPPORTS_STRING_CONVERSION_FLAG - Success
-- Performing Test CXX_SUPPORTS_STRING_CONVERSION_FLAG - Success
-- Performing Test LINKER_SUPPORTS_COLOR_DIAGNOSTICS - Failed
-- Looking for os_signpost_interval_begin - found
-- Performing Test macos_signposts_usable - Success
-- Found Python3: /usr/local/Frameworks/Python.framework/Versions/3.9/bin/python3.9 (found version "3.9.6") found components: Interpreter 
-- Linker detection: ld64
-- Found Git: /usr/local/bin/git (found version "2.32.0") 
-- Targeting AArch64
-- Targeting AMDGPU
-- Targeting ARM
-- Targeting AVR
-- Targeting BPF
-- Targeting Hexagon
-- Targeting Lanai
-- Targeting Mips
-- Targeting MSP430
-- Targeting NVPTX
-- Targeting PowerPC
-- Targeting RISCV
-- Targeting Sparc
-- Targeting SystemZ
-- Targeting WebAssembly
-- Targeting X86
-- Targeting XCore
-- Looking for sys/resource.h - found
-- Clang version: 12.0.1
-- Performing Test CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG - Success
-- Host linker version: 711
-- Looking for include file CoreServices/CoreServices.h - found
-- Registering Bye as a pass plugin (static build: OFF)
-- Failed to find LLVM FileCheck
-- Version: 0.0.0
-- Performing Test HAVE_CXX_FLAG_STD_CXX11 - Success
-- Performing Test HAVE_CXX_FLAG_WALL - Success
-- Performing Test HAVE_CXX_FLAG_WEXTRA - Success
-- Performing Test HAVE_CXX_FLAG_WSHADOW - Success
-- Performing Test HAVE_CXX_FLAG_PEDANTIC - Success
-- Performing Test HAVE_CXX_FLAG_PEDANTIC_ERRORS - Success
-- Performing Test HAVE_CXX_FLAG_WSHORTEN_64_TO_32 - Success
-- Performing Test HAVE_CXX_FLAG_WFLOAT_EQUAL - Success
-- Performing Test HAVE_CXX_FLAG_FSTRICT_ALIASING - Success
-- Performing Test HAVE_CXX_FLAG_FNO_EXCEPTIONS - Success
-- Performing Test HAVE_CXX_FLAG_WNO_SUGGEST_OVERRIDE - Success
-- Performing Test HAVE_CXX_FLAG_WSTRICT_ALIASING - Success
-- Performing Test HAVE_CXX_FLAG_WD654 - Failed
-- Performing Test HAVE_CXX_FLAG_WTHREAD_SAFETY - Success
-- Performing Test HAVE_THREAD_SAFETY_ATTRIBUTES -- failed to compile
-- Performing Test HAVE_CXX_FLAG_COVERAGE - Success
-- Performing Test HAVE_GNU_POSIX_REGEX -- failed to compile
-- Performing Test HAVE_POSIX_REGEX -- success
-- Performing Test HAVE_STEADY_CLOCK -- success
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/wuzhanglin/Sources/OpenSource/compiler/llvm-project/build

2. 编译LLVM

使用命令如下(示例):

ninja

整个编译过程大约需要30至60分钟。输出log如下(示例):

...
[164/3972] Building CXX object lib/CodeGen/CMakeFiles/LLVMCodeGen.dir/BranchFolding.cpp.o
...
[3808/3808] Linking CXX executable bin/llvm-lto2

编译完成后,我们可以测试一下,确保一切正常(示例):

ninja check-all

整个测试过程大约需要一个小时。输出log如下(示例):

[390/704] cd .../llvm-project/clang/...al/Frameworks/Python.framework/Versions/3.9/bin/python3.9 -m unittest discover
..............................................................................................................................
----------------------------------------------------------------------
Ran 126 tests in 21.427s

OK
[703/704] Running all regression tests
llvm-lit: .../llvm-project/llvm/utils/lit/lit/llvm/config.py:385: note: using clang: .../llvm-project/build/bin/clang
llvm-lit: .../llvm-project/llvm/utils/lit/lit/util.py:399: note: using SDKROOT: '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk'
llvm-lit: .../llvm-project/build/utils/lit/tests/lit.cfg:79: warning: Setting a timeout per test not supported. Requires the Python psutil module but it could not be found. Try installing it via pip or via your operating system's package manager. Some tests will be skipped and the --timeout command line argument will not work.
FAIL: LLVM :: Bindings/Go/go.test (34351 of 69399)
******************** TEST 'LLVM :: Bindings/Go/go.test' FAILED ********************
Script:
--
: 'RUN: at line 1';   .../llvm-project/build/bin/llvm-go go=/usr/local/go/bin/go test llvm.org/llvm/bindings/go/llvm
--
Exit Code: 1

Command Output (stdout):
--
FAIL	llvm.org/llvm/bindings/go/llvm [build failed]
FAIL
--
Command Output (stderr):
--
# runtime/cgo
_cgo_export.c:3:10: fatal error: 'stdlib.h' file not found
--
********************
FAIL: LLVM :: tools/lto/no-bitcode.s (69143 of 69399)
******************** TEST 'LLVM :: tools/lto/no-bitcode.s' FAILED ********************
Script:
--
: 'RUN: at line 3';   .../llvm-project/build/bin/llvm-mc -triple=arm64-apple-ios7.0.0 -filetype=obj -o .../llvm-project/build/test/tools/lto/Output/no-bitcode.s.tmp.o
: 'RUN: at line 4';   .../llvm-project/build/bin/llvm-ar r .../llvm-project/build/test/tools/lto/Output/no-bitcode.s.tmp.a .../llvm-project/build/test/tools/lto/Output/no-bitcode.s.tmp.o
: 'RUN: at line 5';   /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld -lto_library .../llvm-project/build/test/../lib/libLTO.dylib -arch x86_64 -dylib -mllvm -O0 -o .../llvm-project/build/test/tools/lto/Output/no-bitcode.s.tmp.dylib .../llvm-project/build/test/tools/lto/Output/no-bitcode.s.tmp.a
--
Exit Code: 1
Command Output (stderr):
--
.../llvm-project/build/bin/llvm-ar: warning: creating .../llvm-project/build/test/tools/lto/Output/no-bitcode.s.tmp.a
ld: warning: platform not specified
ld: warning: No platform min-version specified on command line
ld: warning: ignoring file .../llvm-project/build/test/tools/lto/Output/no-bitcode.s.tmp.a, building for -x86_64 but attempting to link with file built for iOS-arm64
ld: dynamic main executables must link with libSystem.dylib for architecture x86_64
--
********************
FAIL: LLVM :: tools/lto/opt-level.ll (69226 of 69399)
******************** TEST 'LLVM :: tools/lto/opt-level.ll' FAILED ********************
Script:
--
: 'RUN: at line 1';   .../llvm-project/build/bin/llvm-as .../llvm-project/llvm/test/tools/lto/opt-level.ll -o .../llvm-project/build/test/tools/lto/Output/opt-level.ll.tmp.o
: 'RUN: at line 2';   /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld -lto_library .../llvm-project/build/test/../lib/libLTO.dylib -arch x86_64 -dylib -mllvm -O0 -o .../llvm-project/build/test/tools/lto/Output/opt-level.ll.tmp.dylib .../llvm-project/build/test/tools/lto/Output/opt-level.ll.tmp.o
: 'RUN: at line 3';   .../llvm-project/build/bin/llvm-nm --no-llvm-bc .../llvm-project/build/test/tools/lto/Output/opt-level.ll.tmp.dylib | .../llvm-project/build/bin/FileCheck --allow-unused-prefixes=false --check-prefix=CHECK-O0 .../llvm-project/llvm/test/tools/lto/opt-level.ll
: 'RUN: at line 4';   /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld -lto_library .../llvm-project/build/test/../lib/libLTO.dylib -arch x86_64 -dylib -mllvm -O2 -o .../llvm-project/build/test/tools/lto/Output/opt-level.ll.tmp.dylib .../llvm-project/build/test/tools/lto/Output/opt-level.ll.tmp.o
: 'RUN: at line 5';   .../llvm-project/build/bin/llvm-nm --no-llvm-bc .../llvm-project/build/test/tools/lto/Output/opt-level.ll.tmp.dylib | .../llvm-project/build/bin/FileCheck --allow-unused-prefixes=false --check-prefix=CHECK-O2 .../llvm-project/llvm/test/tools/lto/opt-level.ll
--
Exit Code: 1
Command Output (stderr):
--
ld: warning: platform not specified
ld: warning: No platform min-version specified on command line
ld: dynamic main executables must link with libSystem.dylib for architecture x86_64
--
********************
FAIL: LLVM :: tools/lto/print-stats.ll (69227 of 69399)
******************** TEST 'LLVM :: tools/lto/print-stats.ll' FAILED ********************
Script:
--
: 'RUN: at line 1';   .../llvm-project/build/bin/llvm-as .../llvm-project/llvm/test/tools/lto/print-stats.ll -o .../llvm-project/build/test/tools/lto/Output/print-stats.ll.tmp.o
: 'RUN: at line 2';   /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld -lto_library .../llvm-project/build/test/../lib/libLTO.dylib -arch x86_64 -dylib -mllvm -stats -o .../llvm-project/build/test/tools/lto/Output/print-stats.ll.tmp.dylib .../llvm-project/build/test/tools/lto/Output/print-stats.ll.tmp.o 2>&1 | .../llvm-project/build/bin/FileCheck --allow-unused-prefixes=false --check-prefix=STATS .../llvm-project/llvm/test/tools/lto/print-stats.ll
: 'RUN: at line 3';   /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld -lto_library .../llvm-project/build/test/../lib/libLTO.dylib -arch x86_64 -dylib -o .../llvm-project/build/test/tools/lto/Output/print-stats.ll.tmp.dylib .../llvm-project/build/test/tools/lto/Output/print-stats.ll.tmp.o 2>&1 | .../llvm-project/build/bin/FileCheck --allow-unused-prefixes=false --check-prefix=NO_STATS .../llvm-project/llvm/test/tools/lto/print-stats.ll
--
Exit Code: 1
********************
FAIL: LLVM :: tools/lto/hide-linkonce-odr.ll (69229 of 69399)
******************** TEST 'LLVM :: tools/lto/hide-linkonce-odr.ll' FAILED ********************
Script:
--
: 'RUN: at line 1';   .../llvm-project/build/bin/llvm-as .../llvm-project/llvm/test/tools/lto/hide-linkonce-odr.ll -o .../llvm-project/build/test/tools/lto/Output/hide-linkonce-odr.ll.tmp.o
: 'RUN: at line 2';   /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld -lto_library .../llvm-project/build/test/../lib/libLTO.dylib -dylib -arch x86_64 -macosx_version_min 10.10.0 -o .../llvm-project/build/test/tools/lto/Output/hide-linkonce-odr.ll.tmp.dylib .../llvm-project/build/test/tools/lto/Output/hide-linkonce-odr.ll.tmp.o -save-temps  -undefined dynamic_lookup -exported_symbol _c -exported_symbol _b  -exported_symbol _GlobLinkonce
: 'RUN: at line 4';   .../llvm-project/build/bin/llvm-dis .../llvm-project/build/test/tools/lto/Output/hide-linkonce-odr.ll.tmp.dylib.lto.opt.bc -o - | .../llvm-project/build/bin/FileCheck --allow-unused-prefixes=false --check-prefix=IR .../llvm-project/llvm/test/tools/lto/hide-linkonce-odr.ll
: 'RUN: at line 10';   .../llvm-project/build/bin/llvm-nm .../llvm-project/build/test/tools/lto/Output/hide-linkonce-odr.ll.tmp.dylib | .../llvm-project/build/bin/FileCheck --allow-unused-prefixes=false --check-prefix=NM .../llvm-project/llvm/test/tools/lto/hide-linkonce-odr.ll
--
Exit Code: 1
Command Output (stderr):
--
ld: dynamic main executables must link with libSystem.dylib for architecture x86_64
--
********************
********************
Failed Tests (5):
  LLVM :: Bindings/Go/go.test
  LLVM :: tools/lto/hide-linkonce-odr.ll
  LLVM :: tools/lto/no-bitcode.s
  LLVM :: tools/lto/opt-level.ll
  LLVM :: tools/lto/print-stats.ll

Testing Time: 4120.19s
  Unsupported      :   718
  Passed           : 68490
  Expectedly Failed:   186
  Failed           :     5

1 warning(s) in tests
FAILED: CMakeFiles/check-all .../llvm-project/build/CMakeFiles/check-all 
cd .../llvm-project/build && /usr/local/Frameworks/Python.framework/Versions/3.9/bin/python3.9 .../llvm-project/build/./bin/llvm-lit -sv --param USE_Z3_SOLVER=0 .../llvm-project/build/tools/clang/test .../llvm-project/build/utils/lit .../llvm-project/build/test
ninja: build stopped: subcommand failed.

有少量测试错误,但目前对我们使用LLVM没有影响。

3. 快速试用llc工具

我们可以简单试用一下LLVM的llc工具,进一步确保一切正常。使用命令如下(示例):

./bin/llc --version

输出log如下(示例):

LLVM (http://llvm.org/):
  LLVM version 12.0.1
  DEBUG build with assertions.
  Default target: x86_64-apple-darwin20.6.0
  Host CPU: skylake

  Registered Targets:
    aarch64    - AArch64 (little endian)
    aarch64_32 - AArch64 (little endian ILP32)
    aarch64_be - AArch64 (big endian)
    amdgcn     - AMD GCN GPUs
    arm        - ARM
    arm64      - ARM64 (little endian)
    arm64_32   - ARM64 (little endian ILP32)
    armeb      - ARM (big endian)
    avr        - Atmel AVR Microcontroller
    bpf        - BPF (host endian)
    bpfeb      - BPF (big endian)
    bpfel      - BPF (little endian)
    hexagon    - Hexagon
    lanai      - Lanai
    mips       - MIPS (32-bit big endian)
    mips64     - MIPS (64-bit big endian)
    mips64el   - MIPS (64-bit little endian)
    mipsel     - MIPS (32-bit little endian)
    msp430     - MSP430 [experimental]
    nvptx      - NVIDIA PTX 32-bit
    nvptx64    - NVIDIA PTX 64-bit
    ppc32      - PowerPC 32
    ppc32le    - PowerPC 32 LE
    ppc64      - PowerPC 64
    ppc64le    - PowerPC 64 LE
    r600       - AMD GPUs HD2XXX-HD6XXX
    riscv32    - 32-bit RISC-V
    riscv64    - 64-bit RISC-V
    sparc      - Sparc
    sparcel    - Sparc LE
    sparcv9    - Sparc V9
    systemz    - SystemZ
    thumb      - Thumb
    thumbeb    - Thumb (big endian)
    wasm32     - WebAssembly 32-bit
    wasm64     - WebAssembly 64-bit
    x86        - 32-bit X86: Pentium-Pro and above
    x86-64     - 64-bit X86: EM64T and AMD64
    xcore      - XCore

4. 安装LLVM

使用命令如下(示例):

sudo ninja install

(注意如果不用sudo的话,可能会因为文件访问权限问题,而导致安装失败。)

这样就把LLVM安装到了/usr/local目录下,输出log如下(示例):

[0/1] Install the project...
-- Install configuration: "Debug"
-- Installing: /usr/local/include/llvm
...
...
...
-- Installing: /usr/local/lib/cmake/llvm/./CheckLinkerFlag.cmake

我们快速地测试一下,确保安装成功。使用命令如下(示例):

which llc; llc --version

输出log如下(示例):

/usr/local/bin/llc
LLVM (http://llvm.org/):
  LLVM version 12.0.1
  DEBUG build with assertions.
  Default target: x86_64-apple-darwin20.6.0
  Host CPU: skylake

  Registered Targets:
    aarch64    - AArch64 (little endian)
    ...
    ...
    x86-64     - 64-bit X86: EM64T and AMD64
    xcore      - XCore

跟上一步的log是一样的。

总结

我们利用Git下载了LLVM的源代码,用CMake和Ninja工具编译并安装了LLVM,最后还简单地试用了一下LLVM工具库中的llc工具。

好的,我可以为您提供一些关于在Linux上编译LLVM源代码的基本步骤。请注意,由于不同的Linux发行版可能有不同的安装要求,所以下面的步骤可能需要根据您的特定情况进行调整。 1. 首先,您需要安装一些必要的依赖项,例如CMake、GCC、Python等。您可以使用以下命令在Ubuntu上安装这些依赖项: ```sh sudo apt-get install cmake build-essential python ``` 2. 下载最新版本的LLVM源代码。您可以在LLVM官方网站(https://llvm.org/)上找到源代码的下载链接。例如,您可以使用以下命令从LLVM官方网站上下载最新版本的源代码: ```sh wget https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.1/llvm-project-12.0.1.tar.xz ``` 3. 解压缩下载的源代码包并进入解压后的目录: ```sh tar -xf llvm-project-12.0.1.tar.xz cd llvm-project-12.0.1 ``` 4. 创建一个构建目录并进入该目录: ```sh mkdir build cd build ``` 5. 运行CMake,指定LLVM源代码的路径和其他配置选项。例如,您可以使用以下命令启动CMake: ```sh cmake -G "Unix Makefiles" -DLLVM_ENABLE_PROJECTS="clang;lld" -DCMAKE_BUILD_TYPE=Release ../llvm ``` 6. 运行make命令来编译LLVM和其他组件: ```sh make -j8 ``` 在这个命令中,“-j8”选项表示使用8个并行处理器编译代码。您可以根据您的机器配置进行调整。 7. 安装编译后的LLVM和其他组件: ```sh sudo make install ``` 完成上述步骤后,您应该能够在Linux上成功编译LLVM源代码。请注意,这只是一个基本的指南,您可能需要根据您的特定情况进行其他调整或安装其他依赖项。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值