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_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
15 set(CMAKE_CXX_STANDARD_REQUIRED YES)
16 set(CMAKE_CXX_EXTENSIONS NO)
18 set(CMAKE_INCLUDE_CURRENT_DIR ON)
20 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
21 list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
23 # Turn into CACHE PATHs for overwritting
24 set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed")
25 set(LLVM_BINARY_DIR "${LLVM_BINARY_DIR}" CACHE PATH "Path to LLVM build tree")
26 set(LLVM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" CACHE PATH "Path to LLVM source tree")
28 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR}
31 # They are used as destination of target generators.
32 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
33 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
37 include(HandleLLVMOptions)
38 include(GetErrcMessages)
41 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
43 include_directories(${LLVM_INCLUDE_DIRS})
44 link_directories(${LLVM_LIBRARY_DIRS})
46 if(LLVM_INCLUDE_TESTS)
47 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
48 COMPONENTS Interpreter)
50 # Check prebuilt llvm/utils.
51 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
52 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
53 set(LLVM_UTILS_PROVIDED ON)
56 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
57 # Note: path not really used, except for checking if lit was found
58 set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
59 if(NOT LLVM_UTILS_PROVIDED)
60 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
61 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
62 set(LLVM_UTILS_PROVIDED ON)
63 set(LLD_TEST_DEPS FileCheck not)
65 set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
66 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
67 AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
68 AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
69 add_subdirectory(${UNITTEST_DIR} utils/unittest)
74 NAMES llvm-lit lit.py lit
75 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
80 # Define the default arguments to use with 'lit', and an option for the user
82 set(LIT_ARGS_DEFAULT "-sv")
84 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
86 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
88 get_errc_messages(LLVM_LIT_ERRC_MESSAGES)
90 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
91 if(WIN32 AND NOT CYGWIN)
92 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
95 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 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
144 set(LLVM_COMMON_CMAKE_UTILS ${LLD_SOURCE_DIR}/../cmake)
147 # Add path for custom modules.
148 list(INSERT CMAKE_MODULE_PATH 0
149 "${LLD_SOURCE_DIR}/cmake/modules"
150 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
156 "Enable VTune user task tracking."
161 include_directories(${VTune_INCLUDE_DIRS})
162 list(APPEND LLVM_COMMON_LIBS ${VTune_LIBRARIES})
163 add_definitions(-DLLD_HAS_VTUNE)
167 option(LLD_BUILD_TOOLS
168 "Build the lld tools. If OFF, just generate build targets." ON)
170 option(LLD_DEFAULT_LD_LLD_IS_MINGW
171 "Use MinGW as the default backend for ld.lld. If OFF, ELF will be used." OFF)
172 if (LLD_DEFAULT_LD_LLD_IS_MINGW)
173 add_definitions("-DLLD_DEFAULT_LD_LLD_IS_MINGW=1")
177 add_definitions(-wd4530) # Suppress 'warning C4530: C++ exception handler used, but unwind semantics are not enabled.'
178 add_definitions(-wd4062) # Suppress 'warning C4062: enumerator X in switch of enum Y is not handled' from system header.
181 include_directories(BEFORE
182 ${CMAKE_CURRENT_BINARY_DIR}/include
183 ${CMAKE_CURRENT_SOURCE_DIR}/include
186 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
187 install(DIRECTORY include/
188 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
194 add_subdirectory(Common)
195 add_subdirectory(tools/lld)
197 if (LLVM_INCLUDE_TESTS)
198 add_subdirectory(test)
201 add_subdirectory(docs)
202 add_subdirectory(COFF)
203 add_subdirectory(ELF)
204 add_subdirectory(MachO)
205 add_subdirectory(MinGW)
206 add_subdirectory(wasm)
208 add_subdirectory(cmake/modules)