1 cmake_minimum_required(VERSION 3.20.0)
3 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
4 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
6 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
9 # If we are not building as a part of LLVM, build LLD as an
10 # standalone project, using LLVM as an external library:
11 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
13 set(LLD_BUILT_STANDALONE TRUE)
16 # Must go below project(..)
17 include(GNUInstallDirs)
19 if(LLD_BUILT_STANDALONE)
20 set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
21 set(CMAKE_CXX_STANDARD_REQUIRED YES)
22 set(CMAKE_CXX_EXTENSIONS NO)
24 set(CMAKE_INCLUDE_CURRENT_DIR ON)
26 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
27 list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
29 # Turn into CACHE PATHs for overwriting
30 set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed")
31 set(LLVM_BINARY_DIR "${LLVM_BINARY_DIR}" CACHE PATH "Path to LLVM build tree")
32 set(LLVM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" CACHE PATH "Path to LLVM source tree")
34 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
37 # They are used as destination of target generators.
38 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
39 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
43 include(HandleLLVMOptions)
44 include(GetErrcMessages)
47 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
49 include_directories(${LLVM_INCLUDE_DIRS})
50 link_directories(${LLVM_LIBRARY_DIRS})
52 if(LLVM_INCLUDE_TESTS)
53 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
54 COMPONENTS Interpreter)
56 # Check prebuilt llvm/utils.
57 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
58 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
59 set(LLVM_UTILS_PROVIDED ON)
62 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
63 # Note: path not really used, except for checking if lit was found
64 set(LLVM_EXTERNAL_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
65 if(NOT LLVM_UTILS_PROVIDED)
66 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
67 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
68 set(LLVM_UTILS_PROVIDED ON)
69 set(LLD_TEST_DEPS FileCheck not)
72 if (NOT TARGET llvm_gtest)
73 message(FATAL_ERROR "llvm-gtest not found. Please install llvm-gtest or disable tests with -DLLVM_INCLUDE_TESTS=OFF")
77 find_program(LLVM_EXTERNAL_LIT
78 NAMES llvm-lit lit.py lit
79 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
84 # Define the default arguments to use with 'lit', and an option for the user
86 set(LIT_ARGS_DEFAULT "-sv")
88 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
90 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
92 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
94 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
95 if(WIN32 AND NOT CYGWIN)
96 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
99 set(LLVM_INCLUDE_TESTS OFF)
104 set(LLD_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
105 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
106 mark_as_advanced(LLD_TOOLS_INSTALL_DIR)
108 set(LLD_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
109 set(LLD_INCLUDE_DIR ${LLD_SOURCE_DIR}/include )
110 set(LLD_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
112 set(LLD_VENDOR ${PACKAGE_VENDOR} CACHE STRING
113 "Vendor-specific text for showing with version information.")
116 add_definitions(-DLLD_VENDOR="${LLD_VENDOR}")
119 # Compute the LLD version from the LLVM version.
120 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" LLD_VERSION
122 message(STATUS "LLD version: ${LLD_VERSION}")
124 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" LLD_VERSION_MAJOR
126 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" LLD_VERSION_MINOR
129 # Configure the Version.inc file.
131 ${CMAKE_CURRENT_SOURCE_DIR}/include/lld/Common/Version.inc.in
132 ${CMAKE_CURRENT_BINARY_DIR}/include/lld/Common/Version.inc)
135 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
136 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
137 "the makefiles distributed with LLVM. Please create a directory and run cmake "
138 "from there, passing the path to this source directory as the last argument. "
139 "This process created the file `CMakeCache.txt' and the directory "
140 "`CMakeFiles'. Please delete them.")
143 # Add path for custom modules.
144 list(INSERT CMAKE_MODULE_PATH 0
145 "${LLD_SOURCE_DIR}/cmake/modules"
146 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
152 "Enable VTune user task tracking."
157 include_directories(${VTune_INCLUDE_DIRS})
158 list(APPEND LLVM_COMMON_LIBS ${VTune_LIBRARIES})
159 add_definitions(-DLLD_HAS_VTUNE)
163 option(LLD_BUILD_TOOLS
164 "Build the lld tools. If OFF, just generate build targets." ON)
166 option(LLD_DEFAULT_LD_LLD_IS_MINGW
167 "Use MinGW as the default backend for ld.lld. If OFF, ELF will be used." OFF)
168 if (LLD_DEFAULT_LD_LLD_IS_MINGW)
169 add_definitions("-DLLD_DEFAULT_LD_LLD_IS_MINGW=1")
173 add_definitions(-wd4530) # Suppress 'warning C4530: C++ exception handler used, but unwind semantics are not enabled.'
174 add_definitions(-wd4062) # Suppress 'warning C4062: enumerator X in switch of enum Y is not handled' from system header.
177 include_directories(BEFORE
178 ${CMAKE_CURRENT_BINARY_DIR}/include
179 ${CMAKE_CURRENT_SOURCE_DIR}/include
182 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
183 install(DIRECTORY include/
184 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
190 add_subdirectory(Common)
191 add_subdirectory(tools/lld)
193 if (LLVM_INCLUDE_TESTS)
194 add_custom_target(LLDUnitTests)
195 llvm_add_unittests(LLD_UNITTESTS_ADDED)
196 add_subdirectory(test)
199 add_subdirectory(docs)
200 add_subdirectory(COFF)
201 add_subdirectory(ELF)
202 add_subdirectory(MachO)
203 add_subdirectory(MinGW)
204 add_subdirectory(wasm)
206 add_subdirectory(cmake/modules)