Skip to content

Commit b07c654

Browse files
committed
[BOLT] Enable standalone build
1 parent d12e45a commit b07c654

File tree

6 files changed

+268
-6
lines changed

6 files changed

+268
-6
lines changed

bolt/CMakeLists.txt

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
include(ExternalProject)
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
4+
set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
5+
endif()
6+
include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
7+
NO_POLICY_SCOPE)
8+
9+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
10+
project(bolt)
11+
set(BOLT_BUILT_STANDALONE TRUE)
12+
endif()
213

314
set(BOLT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
415
set(BOLT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
@@ -7,6 +18,137 @@ set(CMAKE_CXX_STANDARD 17)
718
# Add path for custom modules.
819
list(INSERT CMAKE_MODULE_PATH 0 "${BOLT_SOURCE_DIR}/cmake/modules")
920

21+
# standalone build, copied from clang
22+
if(BOLT_BUILT_STANDALONE)
23+
set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
24+
set(CMAKE_CXX_STANDARD_REQUIRED YES)
25+
set(CMAKE_CXX_EXTENSIONS NO)
26+
27+
if(NOT MSVC_IDE)
28+
set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
29+
CACHE BOOL "Enable assertions")
30+
# Assertions should follow llvm-config's.
31+
mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
32+
endif()
33+
34+
find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
35+
list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
36+
37+
# Turn into CACHE PATHs for overwritting
38+
39+
# We can't check LLVM_CONFIG here, because find_package(LLVM ...) also sets
40+
# LLVM_CONFIG.
41+
if (NOT LLVM_CONFIG_FOUND)
42+
# Pull values from LLVMConfig.cmake. We can drop this once the llvm-config
43+
# path is removed.
44+
set(INCLUDE_DIRS ${LLVM_INCLUDE_DIRS})
45+
set(LLVM_OBJ_DIR "${LLVM_BINARY_DIR}")
46+
# N.B. this is just a default value, the CACHE PATHs below can be overridden.
47+
set(MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm")
48+
else()
49+
set(INCLUDE_DIRS "${LLVM_BINARY_DIR}/include" "${MAIN_INCLUDE_DIR}")
50+
endif()
51+
52+
set(LLVM_INCLUDE_DIRS ${INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed")
53+
set(LLVM_BINARY_DIR "${LLVM_OBJ_ROOT}" CACHE PATH "Path to LLVM build tree")
54+
set(LLVM_MAIN_SRC_DIR "${MAIN_SRC_DIR}" CACHE PATH "Path to LLVM source tree")
55+
56+
set(LLVM_TOOLS_BINARY_DIR "${LLVM_TOOLS_BINARY_DIR}" CACHE PATH "Path to llvm/bin")
57+
set(LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE PATH "Path to llvm/lib")
58+
59+
find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
60+
NO_DEFAULT_PATH)
61+
62+
# They are used as destination of target generators.
63+
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
64+
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
65+
if(WIN32 OR CYGWIN)
66+
# DLL platform -- put DLLs into bin.
67+
set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
68+
else()
69+
set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
70+
endif()
71+
72+
option(LLVM_INSTALL_TOOLCHAIN_ONLY
73+
"Only include toolchain files in the 'install' target." OFF)
74+
75+
option(LLVM_FORCE_USE_OLD_TOOLCHAIN
76+
"Set to ON to force using an old, unsupported host toolchain." OFF)
77+
option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
78+
option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)
79+
80+
include(AddLLVM)
81+
include(TableGen)
82+
include(HandleLLVMOptions)
83+
include(CheckAtomic)
84+
include(GetErrcMessages)
85+
include(LLVMDistributionSupport)
86+
87+
if (NOT DEFINED LLVM_INCLUDE_TESTS)
88+
set(LLVM_INCLUDE_TESTS ON)
89+
endif()
90+
91+
include_directories(${LLVM_INCLUDE_DIRS})
92+
link_directories("${LLVM_LIBRARY_DIR}")
93+
94+
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
95+
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
96+
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
97+
98+
find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
99+
COMPONENTS Interpreter)
100+
101+
if(LLVM_INCLUDE_TESTS)
102+
# Check prebuilt llvm/utils.
103+
if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
104+
AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
105+
AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
106+
set(LLVM_UTILS_PROVIDED ON)
107+
endif()
108+
109+
# Seek installed Lit.
110+
find_program(LLVM_LIT
111+
NAMES llvm-lit lit.py lit
112+
PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
113+
DOC "Path to lit.py")
114+
115+
if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
116+
# Note: path not really used, except for checking if lit was found
117+
if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
118+
add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit utils/llvm-lit)
119+
endif()
120+
if(NOT LLVM_UTILS_PROVIDED)
121+
add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
122+
add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count)
123+
add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
124+
set(LLVM_UTILS_PROVIDED ON)
125+
set(CLANG_TEST_DEPS FileCheck count not)
126+
endif()
127+
endif()
128+
129+
if(LLVM_LIT)
130+
# Define the default arguments to use with 'lit', and an option for the user
131+
# to override.
132+
set(LIT_ARGS_DEFAULT "-sv")
133+
if (MSVC OR XCODE)
134+
set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
135+
endif()
136+
set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
137+
138+
get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
139+
140+
# On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
141+
if( WIN32 AND NOT CYGWIN )
142+
set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
143+
endif()
144+
else()
145+
set(LLVM_INCLUDE_TESTS OFF)
146+
endif()
147+
148+
umbrella_lit_testsuite_begin(check-all)
149+
endif() # LLVM_INCLUDE_TESTS
150+
endif() # standalone
151+
10152
# Determine default set of targets to build -- the intersection of
11153
# those BOLT supports and those LLVM is targeting.
12154
set(BOLT_TARGETS_TO_BUILD_all "AArch64;X86;RISCV")
@@ -92,6 +234,8 @@ if (BOLT_ENABLE_RUNTIME)
92234
if(CMAKE_SYSROOT)
93235
list(APPEND extra_args -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
94236
endif()
237+
238+
include(ExternalProject)
95239
ExternalProject_Add(bolt_rt
96240
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/runtime"
97241
STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/bolt_rt-stamps

bolt/lib/Target/AArch64/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,39 @@ set(LLVM_LINK_COMPONENTS
44
AArch64Desc
55
)
66

7+
if(BOLT_BUILT_STANDALONE)
8+
# tablegen, copied from llvm/lib/Target/AAarch64/CMakeLists.txt
9+
set(LLVM_TARGET_DEFINITIONS ${LLVM_MAIN_SRC_DIR}/lib/Target/AArch64/AArch64.td)
10+
list(APPEND LLVM_TABLEGEN_FLAGS -I ${LLVM_MAIN_SRC_DIR}/lib/Target/AArch64)
11+
tablegen(LLVM AArch64GenAsmMatcher.inc -gen-asm-matcher)
12+
tablegen(LLVM AArch64GenAsmWriter.inc -gen-asm-writer)
13+
tablegen(LLVM AArch64GenAsmWriter1.inc -gen-asm-writer -asmwriternum=1)
14+
tablegen(LLVM AArch64GenCallingConv.inc -gen-callingconv)
15+
tablegen(LLVM AArch64GenDAGISel.inc -gen-dag-isel)
16+
tablegen(LLVM AArch64GenDisassemblerTables.inc -gen-disassembler)
17+
tablegen(LLVM AArch64GenFastISel.inc -gen-fast-isel)
18+
tablegen(LLVM AArch64GenGlobalISel.inc -gen-global-isel)
19+
tablegen(LLVM AArch64GenO0PreLegalizeGICombiner.inc -gen-global-isel-combiner
20+
-combiners="AArch64O0PreLegalizerCombiner")
21+
tablegen(LLVM AArch64GenPreLegalizeGICombiner.inc -gen-global-isel-combiner
22+
-combiners="AArch64PreLegalizerCombiner")
23+
tablegen(LLVM AArch64GenPostLegalizeGICombiner.inc -gen-global-isel-combiner
24+
-combiners="AArch64PostLegalizerCombiner")
25+
tablegen(LLVM AArch64GenPostLegalizeGILowering.inc -gen-global-isel-combiner
26+
-combiners="AArch64PostLegalizerLowering")
27+
tablegen(LLVM AArch64GenInstrInfo.inc -gen-instr-info)
28+
tablegen(LLVM AArch64GenMCCodeEmitter.inc -gen-emitter)
29+
tablegen(LLVM AArch64GenMCPseudoLowering.inc -gen-pseudo-lowering)
30+
tablegen(LLVM AArch64GenRegisterBank.inc -gen-register-bank)
31+
tablegen(LLVM AArch64GenRegisterInfo.inc -gen-register-info)
32+
tablegen(LLVM AArch64GenSubtargetInfo.inc -gen-subtarget)
33+
tablegen(LLVM AArch64GenSystemOperands.inc -gen-searchable-tables)
34+
tablegen(LLVM AArch64GenExegesis.inc -gen-exegesis)
35+
36+
add_public_tablegen_target(AArch64CommonTableGen)
37+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
38+
endif()
39+
740
add_llvm_library(LLVMBOLTTargetAArch64
841
AArch64MCPlusBuilder.cpp
942

bolt/lib/Target/RISCV/CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,37 @@ set(LLVM_LINK_COMPONENTS
44
RISCVDesc
55
)
66

7+
if(BOLT_BUILT_STANDALONE)
8+
# tablegen, copied from llvm/lib/Target/RISCV/CMakeLists.txt
9+
set(LLVM_TARGET_DEFINITIONS ${LLVM_MAIN_SRC_DIR}/lib/Target/RISCV/RISCV.td)
10+
list(APPEND LLVM_TABLEGEN_FLAGS -I ${LLVM_MAIN_SRC_DIR}/lib/Target/RISCV)
11+
tablegen(LLVM RISCVGenAsmMatcher.inc -gen-asm-matcher)
12+
tablegen(LLVM RISCVGenAsmWriter.inc -gen-asm-writer)
13+
tablegen(LLVM RISCVGenCompressInstEmitter.inc -gen-compress-inst-emitter)
14+
tablegen(LLVM RISCVGenMacroFusion.inc -gen-macro-fusion-pred)
15+
tablegen(LLVM RISCVGenDAGISel.inc -gen-dag-isel)
16+
tablegen(LLVM RISCVGenDisassemblerTables.inc -gen-disassembler)
17+
tablegen(LLVM RISCVGenInstrInfo.inc -gen-instr-info)
18+
tablegen(LLVM RISCVGenMCCodeEmitter.inc -gen-emitter)
19+
tablegen(LLVM RISCVGenMCPseudoLowering.inc -gen-pseudo-lowering)
20+
tablegen(LLVM RISCVGenRegisterBank.inc -gen-register-bank)
21+
tablegen(LLVM RISCVGenRegisterInfo.inc -gen-register-info)
22+
tablegen(LLVM RISCVGenSearchableTables.inc -gen-searchable-tables)
23+
tablegen(LLVM RISCVGenSubtargetInfo.inc -gen-subtarget)
24+
25+
set(LLVM_TARGET_DEFINITIONS ${LLVM_MAIN_SRC_DIR}/lib/Target/RISCV/RISCVGISel.td)
26+
tablegen(LLVM RISCVGenGlobalISel.inc -gen-global-isel)
27+
tablegen(LLVM RISCVGenO0PreLegalizeGICombiner.inc -gen-global-isel-combiner
28+
-combiners="RISCVO0PreLegalizerCombiner")
29+
tablegen(LLVM RISCVGenPreLegalizeGICombiner.inc -gen-global-isel-combiner
30+
-combiners="RISCVPreLegalizerCombiner")
31+
tablegen(LLVM RISCVGenPostLegalizeGICombiner.inc -gen-global-isel-combiner
32+
-combiners="RISCVPostLegalizerCombiner")
33+
34+
add_public_tablegen_target(RISCVCommonTableGen)
35+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
36+
endif()
37+
738
add_llvm_library(LLVMBOLTTargetRISCV
839
RISCVMCPlusBuilder.cpp
940

bolt/lib/Target/X86/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ set(LLVM_LINK_COMPONENTS
55
X86Desc
66
)
77

8+
if(BOLT_BUILT_STANDALONE)
9+
# tablegen, copied from llvm/lib/Target/X86/CMakeLists.txt
10+
set(LLVM_TARGET_DEFINITIONS ${LLVM_MAIN_SRC_DIR}/lib/Target/X86/X86.td)
11+
list(APPEND LLVM_TABLEGEN_FLAGS -I ${LLVM_MAIN_SRC_DIR}/lib/Target/X86)
12+
tablegen(LLVM X86GenAsmMatcher.inc -gen-asm-matcher)
13+
tablegen(LLVM X86GenAsmWriter.inc -gen-asm-writer)
14+
tablegen(LLVM X86GenAsmWriter1.inc -gen-asm-writer -asmwriternum=1)
15+
tablegen(LLVM X86GenCallingConv.inc -gen-callingconv)
16+
tablegen(LLVM X86GenDAGISel.inc -gen-dag-isel)
17+
tablegen(LLVM X86GenDisassemblerTables.inc -gen-disassembler)
18+
tablegen(LLVM X86GenCompressEVEXTables.inc -gen-x86-compress-evex-tables)
19+
tablegen(LLVM X86GenExegesis.inc -gen-exegesis)
20+
tablegen(LLVM X86GenFastISel.inc -gen-fast-isel)
21+
tablegen(LLVM X86GenGlobalISel.inc -gen-global-isel)
22+
tablegen(LLVM X86GenInstrInfo.inc -gen-instr-info
23+
-instr-info-expand-mi-operand-info=0)
24+
tablegen(LLVM X86GenMnemonicTables.inc -gen-x86-mnemonic-tables -asmwriternum=1)
25+
tablegen(LLVM X86GenRegisterBank.inc -gen-register-bank)
26+
tablegen(LLVM X86GenRegisterInfo.inc -gen-register-info)
27+
tablegen(LLVM X86GenSubtargetInfo.inc -gen-subtarget)
28+
tablegen(LLVM X86GenFoldTables.inc -gen-x86-fold-tables -asmwriternum=1)
29+
30+
add_public_tablegen_target(X86CommonTableGen)
31+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
32+
endif()
33+
834
add_llvm_library(LLVMBOLTTargetX86
935
X86MCPlusBuilder.cpp
1036
X86MCSymbolizer.cpp

bolt/lib/Utils/CMakeLists.txt

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1+
find_first_existing_vc_file("${LLVM_MAIN_SRC_DIR}" llvm_vc)
2+
find_first_existing_vc_file("${BOLT_SOURCE_DIR}" bolt_vc)
3+
4+
# The VC revision include that we want to generate.
5+
set(version_inc "${CMAKE_CURRENT_BINARY_DIR}/VCSVersion.inc")
6+
7+
set(generate_vcs_version_script "${LLVM_CMAKE_DIR}/GenerateVersionFromVCS.cmake")
8+
9+
if(llvm_vc AND LLVM_APPEND_VC_REV)
10+
set(llvm_source_dir ${LLVM_MAIN_SRC_DIR})
11+
endif()
12+
13+
# Create custom target to generate the VC revision include.
14+
add_custom_command(OUTPUT "${version_inc}"
15+
DEPENDS "${llvm_vc}" "${bolt_vc}" "${generate_vcs_version_script}"
16+
COMMAND ${CMAKE_COMMAND} "-DNAMES=BOLT"
17+
"-DHEADER_FILE=${version_inc}"
18+
"-DBOLT_SOURCE_DIR=${BOLT_SOURCE_DIR}"
19+
"-DLLVM_VC_REPOSITORY=${llvm_vc_repository}"
20+
"-DLLVM_VC_REVISION=${llvm_vc_revision}"
21+
"-DLLVM_FORCE_VC_REVISION=${LLVM_FORCE_VC_REVISION}"
22+
"-DLLVM_FORCE_VC_REPOSITORY=${LLVM_FORCE_VC_REPOSITORY}"
23+
-P "${generate_vcs_version_script}")
24+
25+
# Mark the generated header as being generated.
26+
set_source_files_properties("${version_inc}"
27+
PROPERTIES GENERATED TRUE
28+
HEADER_FILE_ONLY TRUE)
29+
30+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
31+
132
add_llvm_library(LLVMBOLTUtils
233
CommandLineOpts.cpp
334
Utils.cpp
4-
35+
${version_inc}
536
DISABLE_LLVM_LINK_LLVM_DYLIB
637

738
LINK_LIBS
839
${LLVM_PTHREAD_LIB}
940

10-
DEPENDS
11-
llvm_vcsrevision_h
12-
1341
LINK_COMPONENTS
1442
Support
1543
)

bolt/lib/Utils/CommandLineOpts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "bolt/Utils/CommandLineOpts.h"
14-
#include "llvm/Support/VCSRevision.h"
14+
#include "VCSVersion.inc"
1515

1616
using namespace llvm;
1717

0 commit comments

Comments
 (0)