1 cmake_minimum_required(VERSION 3.20.0)
2 set(LLVM_SUBPROJECT_TITLE "LLD")
4 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
5 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
7 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
10 # If we are not building as a part of LLVM, build LLD as an
11 # standalone project, using LLVM as an external library:
12 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
14 set(LLD_BUILT_STANDALONE TRUE)
17 # Must go below project(..)
18 include(GNUInstallDirs)
20 if(LLD_BUILT_STANDALONE)
21 set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
22 set(CMAKE_CXX_STANDARD_REQUIRED YES)
23 set(CMAKE_CXX_EXTENSIONS NO)
25 set(CMAKE_INCLUDE_CURRENT_DIR ON)
27 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
28 list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
30 # Turn into CACHE PATHs for overwriting
31 set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed")
32 set(LLVM_BINARY_DIR "${LLVM_BINARY_DIR}" CACHE PATH "Path to LLVM build tree")
33 set(LLVM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" CACHE PATH "Path to LLVM source tree")
35 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
38 # They are used as destination of target generators.
39 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
40 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
44 include(HandleLLVMOptions)
45 include(GetErrcMessages)
48 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
50 include_directories(${LLVM_INCLUDE_DIRS})
51 link_directories(${LLVM_LIBRARY_DIRS})
53 if(LLVM_INCLUDE_TESTS)
54 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
55 COMPONENTS Interpreter)
57 # Check prebuilt llvm/utils.
58 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
59 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
60 set(LLVM_UTILS_PROVIDED ON)
63 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
64 # Note: path not really used, except for checking if lit was found
65 set(LLVM_EXTERNAL_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
66 if(NOT LLVM_UTILS_PROVIDED)
67 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
68 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
69 set(LLVM_UTILS_PROVIDED ON)
70 set(LLD_TEST_DEPS FileCheck not)
73 if (NOT TARGET llvm_gtest)
74 message(FATAL_ERROR "llvm-gtest not found. Please install llvm-gtest or disable tests with -DLLVM_INCLUDE_TESTS=OFF")
78 find_program(LLVM_EXTERNAL_LIT
79 NAMES llvm-lit lit.py lit
80 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
85 # Define the default arguments to use with 'lit', and an option for the user
87 set(LIT_ARGS_DEFAULT "-sv")
89 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
91 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
93 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
95 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
96 if(WIN32 AND NOT CYGWIN)
97 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
100 set(LLVM_INCLUDE_TESTS OFF)
105 set(LLD_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
106 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
107 mark_as_advanced(LLD_TOOLS_INSTALL_DIR)
109 set(LLD_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
110 set(LLD_INCLUDE_DIR ${LLD_SOURCE_DIR}/include )
111 set(LLD_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
113 set(LLD_VENDOR ${PACKAGE_VENDOR} CACHE STRING
114 "Vendor-specific text for showing with version information.")
117 add_definitions(-DLLD_VENDOR="${LLD_VENDOR}")
120 # Compute the LLD version from the LLVM version.
121 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" LLD_VERSION
123 message(STATUS "LLD version: ${LLD_VERSION}")
125 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" LLD_VERSION_MAJOR
127 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" LLD_VERSION_MINOR
130 # Configure the Version.inc file.
132 ${CMAKE_CURRENT_SOURCE_DIR}/include/lld/Common/Version.inc.in
133 ${CMAKE_CURRENT_BINARY_DIR}/include/lld/Common/Version.inc)
136 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
137 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
138 "the makefiles distributed with LLVM. Please create a directory and run cmake "
139 "from there, passing the path to this source directory as the last argument. "
140 "This process created the file `CMakeCache.txt' and the directory "
141 "`CMakeFiles'. Please delete them.")
144 # Add path for custom modules.
145 list(INSERT CMAKE_MODULE_PATH 0
146 "${LLD_SOURCE_DIR}/cmake/modules"
147 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
153 "Enable VTune user task tracking."
158 include_directories(${VTune_INCLUDE_DIRS})
159 list(APPEND LLVM_COMMON_LIBS ${VTune_LIBRARIES})
160 add_definitions(-DLLD_HAS_VTUNE)
164 option(LLD_BUILD_TOOLS
165 "Build the lld tools. If OFF, just generate build targets." ON)
167 option(LLD_DEFAULT_LD_LLD_IS_MINGW
168 "Use MinGW as the default backend for ld.lld. If OFF, ELF will be used." OFF)
169 if (LLD_DEFAULT_LD_LLD_IS_MINGW)
170 add_definitions("-DLLD_DEFAULT_LD_LLD_IS_MINGW=1")
174 add_definitions(-wd4530) # Suppress 'warning C4530: C++ exception handler used, but unwind semantics are not enabled.'
175 add_definitions(-wd4062) # Suppress 'warning C4062: enumerator X in switch of enum Y is not handled' from system header.
178 include_directories(BEFORE
179 ${CMAKE_CURRENT_BINARY_DIR}/include
180 ${CMAKE_CURRENT_SOURCE_DIR}/include
183 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
184 install(DIRECTORY include/
185 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
191 add_subdirectory(Common)
192 add_subdirectory(tools/lld)
194 if (LLVM_INCLUDE_TESTS)
195 add_custom_target(LLDUnitTests)
196 set_target_properties(LLDUnitTests PROPERTIES FOLDER "LLD/Tests")
197 if (TARGET llvm_gtest)
198 add_subdirectory(unittests)
200 add_subdirectory(test)
203 add_subdirectory(docs)
204 add_subdirectory(COFF)
205 add_subdirectory(ELF)
206 add_subdirectory(MachO)
207 add_subdirectory(MinGW)
208 add_subdirectory(wasm)
210 add_subdirectory(cmake/modules)