1cmake_minimum_required(VERSION 3.13.4) 2 3if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS) 4 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake) 5endif() 6include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake 7 NO_POLICY_SCOPE) 8 9# If we are not building as a part of LLVM, build LLD as an 10# standalone project, using LLVM as an external library: 11if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) 12 project(lld) 13 set(LLD_BUILT_STANDALONE TRUE) 14 if ("${CMAKE_VERSION}" VERSION_LESS "3.20.0") 15 message(WARNING 16 "Your CMake version is ${CMAKE_VERSION}. Starting with LLVM 17.0.0, the " 17 "minimum version of CMake required to build LLVM will become 3.20.0, and " 18 "using an older CMake will become an error. Please upgrade your CMake to " 19 "at least 3.20.0 now to avoid issues in the future!") 20 endif() 21endif() 22 23# Must go below project(..) 24include(GNUInstallDirs) 25 26if(LLD_BUILT_STANDALONE) 27 set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to") 28 set(CMAKE_CXX_STANDARD_REQUIRED YES) 29 set(CMAKE_CXX_EXTENSIONS NO) 30 31 set(CMAKE_INCLUDE_CURRENT_DIR ON) 32 33 find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}") 34 list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}") 35 36 # Turn into CACHE PATHs for overwriting 37 set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed") 38 set(LLVM_BINARY_DIR "${LLVM_BINARY_DIR}" CACHE PATH "Path to LLVM build tree") 39 set(LLVM_MAIN_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm" CACHE PATH "Path to LLVM source tree") 40 41 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR} 42 NO_DEFAULT_PATH) 43 44 # They are used as destination of target generators. 45 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin) 46 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX}) 47 48 include(AddLLVM) 49 include(TableGen) 50 include(HandleLLVMOptions) 51 include(GetErrcMessages) 52 include(CheckAtomic) 53 54 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}") 55 56 include_directories(${LLVM_INCLUDE_DIRS}) 57 link_directories(${LLVM_LIBRARY_DIRS}) 58 59 if(LLVM_INCLUDE_TESTS) 60 find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED 61 COMPONENTS Interpreter) 62 63 # Check prebuilt llvm/utils. 64 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX} 65 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX}) 66 set(LLVM_UTILS_PROVIDED ON) 67 endif() 68 69 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py) 70 # Note: path not really used, except for checking if lit was found 71 set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py) 72 if(NOT LLVM_UTILS_PROVIDED) 73 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck) 74 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not) 75 set(LLVM_UTILS_PROVIDED ON) 76 set(LLD_TEST_DEPS FileCheck not) 77 endif() 78 set(UNITTEST_DIR ${LLVM_THIRD_PARTY_DIR}/unittest) 79 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h 80 AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX} 81 AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt) 82 add_subdirectory(${UNITTEST_DIR} third-party/unittest) 83 endif() 84 else() 85 # Seek installed Lit. 86 find_program(LLVM_LIT 87 NAMES llvm-lit lit.py lit 88 PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit" 89 DOC "Path to lit.py") 90 endif() 91 92 if(LLVM_LIT) 93 # Define the default arguments to use with 'lit', and an option for the user 94 # to override. 95 set(LIT_ARGS_DEFAULT "-sv") 96 if (MSVC OR XCODE) 97 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar") 98 endif() 99 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit") 100 101 get_errc_messages(LLVM_LIT_ERRC_MESSAGES) 102 103 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools. 104 if(WIN32 AND NOT CYGWIN) 105 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools") 106 endif() 107 else() 108 set(LLVM_INCLUDE_TESTS OFF) 109 endif() 110 endif() 111 112 if(LLVM_HAVE_LIBXAR) 113 set(XAR_LIB xar) 114 endif() 115endif() # standalone 116 117set(LLD_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH 118 "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')") 119mark_as_advanced(LLD_TOOLS_INSTALL_DIR) 120 121set(LLD_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 122set(LLD_INCLUDE_DIR ${LLD_SOURCE_DIR}/include ) 123set(LLD_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) 124 125set(LLD_VENDOR ${PACKAGE_VENDOR} CACHE STRING 126 "Vendor-specific text for showing with version information.") 127 128if(LLD_VENDOR) 129 add_definitions(-DLLD_VENDOR="${LLD_VENDOR}") 130endif() 131 132# Compute the LLD version from the LLVM version. 133string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" LLD_VERSION 134 ${PACKAGE_VERSION}) 135message(STATUS "LLD version: ${LLD_VERSION}") 136 137string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" LLD_VERSION_MAJOR 138 ${LLD_VERSION}) 139string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" LLD_VERSION_MINOR 140 ${LLD_VERSION}) 141 142# Configure the Version.inc file. 143configure_file( 144 ${CMAKE_CURRENT_SOURCE_DIR}/include/lld/Common/Version.inc.in 145 ${CMAKE_CURRENT_BINARY_DIR}/include/lld/Common/Version.inc) 146 147 148if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) 149 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite " 150"the makefiles distributed with LLVM. Please create a directory and run cmake " 151"from there, passing the path to this source directory as the last argument. " 152"This process created the file `CMakeCache.txt' and the directory " 153"`CMakeFiles'. Please delete them.") 154endif() 155 156# Add path for custom modules. 157list(INSERT CMAKE_MODULE_PATH 0 158 "${LLD_SOURCE_DIR}/cmake/modules" 159 "${LLVM_COMMON_CMAKE_UTILS}/Modules" 160 ) 161 162include(AddLLD) 163 164option(LLD_ENABLE_WASM 165 "Enable support for WASM." 166 OFF) 167if (LLD_ENABLE_WASM) 168 add_definitions("-DLLD_ENABLE_WASM=1") 169endif() 170 171option(LLD_ENABLE_MACHO 172 "Enable support for MachO." 173 OFF) 174if (LLD_ENABLE_MACHO) 175 add_definitions("-DLLD_ENABLE_MACHO=1") 176endif() 177 178option(LLD_ENABLE_MINGW 179 "Enable support for MINGW." 180 OFF) 181if (LLD_ENABLE_MINGW) 182 add_definitions("-DLLD_ENABLE_MINGW=1") 183endif() 184 185option(LLD_ENABLE_COFF 186 "Enable support for COFF." 187 OFF) 188if (LLD_ENABLE_COFF) 189 add_definitions("-DLLD_ENABLE_COFF=1") 190endif() 191 192option(LLD_USE_VTUNE 193 "Enable VTune user task tracking." 194 OFF) 195if (LLD_USE_VTUNE) 196 find_package(VTune) 197 if (VTUNE_FOUND) 198 include_directories(${VTune_INCLUDE_DIRS}) 199 list(APPEND LLVM_COMMON_LIBS ${VTune_LIBRARIES}) 200 add_definitions(-DLLD_HAS_VTUNE) 201 endif() 202endif() 203 204option(LLD_BUILD_TOOLS 205 "Build the lld tools. If OFF, just generate build targets." ON) 206 207option(LLD_DEFAULT_LD_LLD_IS_MINGW 208 "Use MinGW as the default backend for ld.lld. If OFF, ELF will be used." OFF) 209if (LLD_DEFAULT_LD_LLD_IS_MINGW) 210 add_definitions("-DLLD_DEFAULT_LD_LLD_IS_MINGW=1") 211endif() 212 213if (MSVC) 214 add_definitions(-wd4530) # Suppress 'warning C4530: C++ exception handler used, but unwind semantics are not enabled.' 215 add_definitions(-wd4062) # Suppress 'warning C4062: enumerator X in switch of enum Y is not handled' from system header. 216endif() 217 218include_directories(BEFORE 219 ${CMAKE_CURRENT_BINARY_DIR}/include 220 ${CMAKE_CURRENT_SOURCE_DIR}/include 221 ) 222 223if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) 224 install(DIRECTORY include/ 225 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" 226 FILES_MATCHING 227 PATTERN "*.h" 228 ) 229endif() 230 231add_subdirectory(Common) 232add_subdirectory(tools/lld) 233 234if (LLVM_INCLUDE_TESTS) 235 add_subdirectory(test) 236endif() 237 238add_subdirectory(docs) 239add_subdirectory(COFF) 240add_subdirectory(ELF) 241if (LLD_ENABLE_MACHO) 242 add_subdirectory(MachO) 243endif() 244add_subdirectory(MinGW) 245add_subdirectory(wasm) 246 247add_subdirectory(cmake/modules) 248