1 cmake_minimum_required(VERSION 3.13.4)
3 # If we are not building as a part of LLVM, build LLD as an
4 # standalone project, using LLVM as an external library:
5 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
7 set(LLD_BUILT_STANDALONE TRUE)
10 # Must go below project(..)
11 include(GNUInstallDirs)
13 if(LLD_BUILT_STANDALONE)
14 set(CMAKE_INCLUDE_CURRENT_DIR ON)
16 # Rely on llvm-config.
17 set(LLVM_CONFIG_OUTPUT)
19 set (LLVM_CONFIG_FOUND 1)
20 message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
21 message(DEPRECATION "Using llvm-config to detect the LLVM installation is \
22 deprecated. The installed cmake files should be used \
23 instead. CMake should be able to detect your LLVM install \
24 automatically, but you can also use LLVM_DIR to specify \
25 the path containing LLVMConfig.cmake.")
26 set(CONFIG_COMMAND ${LLVM_CONFIG}
33 COMMAND ${CONFIG_COMMAND}
34 RESULT_VARIABLE HAD_ERROR
35 OUTPUT_VARIABLE LLVM_CONFIG_OUTPUT
39 "[ \t]*[\r\n]+[ \t]*" ";"
40 LLVM_CONFIG_OUTPUT ${LLVM_CONFIG_OUTPUT})
42 string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
43 message(STATUS "${CONFIG_COMMAND_STR}")
44 message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
47 list(GET LLVM_CONFIG_OUTPUT 0 MAIN_INCLUDE_DIR)
48 list(GET LLVM_CONFIG_OUTPUT 1 LLVM_OBJ_ROOT)
49 list(GET LLVM_CONFIG_OUTPUT 2 MAIN_SRC_DIR)
50 list(GET LLVM_CONFIG_OUTPUT 3 LLVM_CONFIG_CMAKE_DIR)
52 # Normalize LLVM_CMAKE_DIR. --cmakedir might contain backslashes.
53 # CMake assumes slashes as PATH.
54 file(TO_CMAKE_PATH ${LLVM_CONFIG_CMAKE_DIR} LLVM_CMAKE_DIR)
57 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
58 list(APPEND CMAKE_MODULE_PATH ${LLVM_DIR})
60 # We can't check LLVM_CONFIG here, because find_package(LLVM ...) also sets
62 if (NOT LLVM_CONFIG_FOUND)
63 # Pull values from LLVMConfig.cmake. We can drop this once the llvm-config
65 set(MAIN_INCLUDE_DIR ${LLVM_INCLUDE_DIR})
66 set(LLVM_OBJ_DIR ${LLVM_BINARY_DIR})
69 set(LLVM_MAIN_INCLUDE_DIR ${MAIN_INCLUDE_DIR} CACHE PATH "Path to llvm/include")
70 set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
71 set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
73 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
76 # They are used as destination of target generators.
77 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
78 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
82 include(HandleLLVMOptions)
83 include(GetErrcMessages)
86 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
88 include_directories("${LLVM_BINARY_DIR}/include" ${LLVM_INCLUDE_DIRS})
89 link_directories(${LLVM_LIBRARY_DIRS})
91 if(LLVM_INCLUDE_TESTS)
92 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
93 COMPONENTS Interpreter)
95 # Check prebuilt llvm/utils.
96 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
97 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
98 set(LLVM_UTILS_PROVIDED ON)
101 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
102 # Note: path not really used, except for checking if lit was found
103 set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
104 if(NOT LLVM_UTILS_PROVIDED)
105 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
106 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
107 set(LLVM_UTILS_PROVIDED ON)
108 set(LLD_TEST_DEPS FileCheck not)
110 set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
111 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
112 AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
113 AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
114 add_subdirectory(${UNITTEST_DIR} utils/unittest)
117 # Seek installed Lit.
118 find_program(LLVM_LIT
119 NAMES llvm-lit lit.py lit
120 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
121 DOC "Path to lit.py")
125 # Define the default arguments to use with 'lit', and an option for the user
127 set(LIT_ARGS_DEFAULT "-sv")
129 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
131 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
133 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
135 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
136 if(WIN32 AND NOT CYGWIN)
137 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
140 set(LLVM_INCLUDE_TESTS OFF)
149 set(LLD_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
150 set(LLD_INCLUDE_DIR ${LLD_SOURCE_DIR}/include )
151 set(LLD_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
153 set(LLD_VENDOR ${PACKAGE_VENDOR} CACHE STRING
154 "Vendor-specific text for showing with version information.")
157 add_definitions(-DLLD_VENDOR="${LLD_VENDOR}")
160 # Compute the LLD version from the LLVM version.
161 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" LLD_VERSION
163 message(STATUS "LLD version: ${LLD_VERSION}")
165 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" LLD_VERSION_MAJOR
167 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" LLD_VERSION_MINOR
170 # Configure the Version.inc file.
172 ${CMAKE_CURRENT_SOURCE_DIR}/include/lld/Common/Version.inc.in
173 ${CMAKE_CURRENT_BINARY_DIR}/include/lld/Common/Version.inc)
176 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
177 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
178 "the makefiles distributed with LLVM. Please create a directory and run cmake "
179 "from there, passing the path to this source directory as the last argument. "
180 "This process created the file `CMakeCache.txt' and the directory "
181 "`CMakeFiles'. Please delete them.")
184 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
185 set(LLVM_COMMON_CMAKE_UTILS ${LLD_SOURCE_DIR}/../cmake)
188 # Add path for custom modules.
189 list(INSERT CMAKE_MODULE_PATH 0
190 "${LLD_SOURCE_DIR}/cmake/modules"
191 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
197 "Enable VTune user task tracking."
202 include_directories(${VTune_INCLUDE_DIRS})
203 list(APPEND LLVM_COMMON_LIBS ${VTune_LIBRARIES})
204 add_definitions(-DLLD_HAS_VTUNE)
208 option(LLD_BUILD_TOOLS
209 "Build the lld tools. If OFF, just generate build targets." ON)
211 option(LLD_DEFAULT_LD_LLD_IS_MINGW
212 "Use MinGW as the default backend for ld.lld. If OFF, ELF will be used." OFF)
213 if (LLD_DEFAULT_LD_LLD_IS_MINGW)
214 add_definitions("-DLLD_DEFAULT_LD_LLD_IS_MINGW=1")
218 add_definitions(-wd4530) # Suppress 'warning C4530: C++ exception handler used, but unwind semantics are not enabled.'
219 add_definitions(-wd4062) # Suppress 'warning C4062: enumerator X in switch of enum Y is not handled' from system header.
222 include_directories(BEFORE
223 ${CMAKE_CURRENT_BINARY_DIR}/include
224 ${CMAKE_CURRENT_SOURCE_DIR}/include
227 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
228 install(DIRECTORY include/
229 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
235 add_subdirectory(Common)
236 add_subdirectory(tools/lld)
238 if (LLVM_INCLUDE_TESTS)
239 add_subdirectory(test)
242 add_subdirectory(docs)
243 add_subdirectory(COFF)
244 add_subdirectory(ELF)
245 add_subdirectory(MachO)
246 add_subdirectory(MinGW)
247 add_subdirectory(wasm)
249 add_subdirectory(cmake/modules)