Fix modules build after PassManagerImpl.h addition
[lldb.git] / lld / CMakeLists.txt
1 # Check if lld is built as a standalone project.
2 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
3   project(lld)
4   cmake_minimum_required(VERSION 3.4.3)
5
6   set(CMAKE_INCLUDE_CURRENT_DIR ON)
7   set(LLD_BUILT_STANDALONE TRUE)
8
9   find_program(LLVM_CONFIG_PATH "llvm-config" DOC "Path to llvm-config binary")
10   if(NOT LLVM_CONFIG_PATH)
11     message(FATAL_ERROR "llvm-config not found: specify LLVM_CONFIG_PATH")
12   endif()
13
14   execute_process(COMMAND "${LLVM_CONFIG_PATH}"
15                           "--obj-root"
16                           "--includedir"
17                           "--cmakedir"
18                           "--src-root"
19                   RESULT_VARIABLE HAD_ERROR
20                   OUTPUT_VARIABLE LLVM_CONFIG_OUTPUT
21                   OUTPUT_STRIP_TRAILING_WHITESPACE)
22   if(HAD_ERROR)
23     message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
24   endif()
25
26   string(REGEX REPLACE "[ \t]*[\r\n]+[ \t]*" ";" LLVM_CONFIG_OUTPUT "${LLVM_CONFIG_OUTPUT}")
27
28   list(GET LLVM_CONFIG_OUTPUT 0 OBJ_ROOT)
29   list(GET LLVM_CONFIG_OUTPUT 1 MAIN_INCLUDE_DIR)
30   list(GET LLVM_CONFIG_OUTPUT 2 LLVM_CMAKE_PATH)
31   list(GET LLVM_CONFIG_OUTPUT 3 MAIN_SRC_DIR)
32
33   set(LLVM_OBJ_ROOT ${OBJ_ROOT} CACHE PATH "path to LLVM build tree")
34   set(LLVM_MAIN_INCLUDE_DIR ${MAIN_INCLUDE_DIR} CACHE PATH "path to llvm/include")
35   set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
36
37   file(TO_CMAKE_PATH ${LLVM_OBJ_ROOT} LLVM_BINARY_DIR)
38
39   if(NOT EXISTS "${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
40     message(FATAL_ERROR "LLVMConfig.cmake not found")
41   endif()
42   include("${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
43
44   list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
45
46   set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
47   include_directories("${LLVM_BINARY_DIR}/include" ${LLVM_INCLUDE_DIRS})
48   link_directories(${LLVM_LIBRARY_DIRS})
49
50   set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
51   set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
52   find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
53
54   include(AddLLVM)
55   include(TableGen)
56   include(HandleLLVMOptions)
57
58   if(LLVM_INCLUDE_TESTS)
59     include(FindPythonInterp)
60     if(NOT PYTHONINTERP_FOUND)
61       message(FATAL_ERROR
62 "Unable to find Python interpreter, required for testing.
63
64 Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
65     endif()
66
67     if(${PYTHON_VERSION_STRING} VERSION_LESS 2.7)
68       message(FATAL_ERROR "Python 2.7 or newer is required")
69     endif()
70
71     # Check prebuilt llvm/utils.
72     if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
73         AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
74       set(LLVM_UTILS_PROVIDED ON)
75     endif()
76
77     if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
78       # Note: path not really used, except for checking if lit was found
79       set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
80       if(NOT LLVM_UTILS_PROVIDED)
81         add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
82         add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
83         set(LLVM_UTILS_PROVIDED ON)
84         set(LLD_TEST_DEPS FileCheck not)
85       endif()
86       set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
87       if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
88           AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
89           AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
90         add_subdirectory(${UNITTEST_DIR} utils/unittest)
91       endif()
92     else()
93       # Seek installed Lit.
94       find_program(LLVM_LIT
95                    NAMES llvm-lit lit.py lit
96                    PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
97                    DOC "Path to lit.py")
98     endif()
99
100     if(LLVM_LIT)
101       # Define the default arguments to use with 'lit', and an option for the user
102       # to override.
103       set(LIT_ARGS_DEFAULT "-sv")
104       if (MSVC OR XCODE)
105         set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
106       endif()
107       set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
108
109       # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
110       if(WIN32 AND NOT CYGWIN)
111         set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
112       endif()
113     else()
114       set(LLVM_INCLUDE_TESTS OFF)
115     endif()
116   endif()
117 endif()
118
119 set(LLD_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
120 set(LLD_INCLUDE_DIR ${LLD_SOURCE_DIR}/include )
121 set(LLD_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
122
123 # Compute the LLD version from the LLVM version.
124 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" LLD_VERSION
125   ${PACKAGE_VERSION})
126 message(STATUS "LLD version: ${LLD_VERSION}")
127
128 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" LLD_VERSION_MAJOR
129   ${LLD_VERSION})
130 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" LLD_VERSION_MINOR
131   ${LLD_VERSION})
132
133 # Configure the Version.inc file.
134 configure_file(
135   ${CMAKE_CURRENT_SOURCE_DIR}/include/lld/Common/Version.inc.in
136   ${CMAKE_CURRENT_BINARY_DIR}/include/lld/Common/Version.inc)
137
138
139 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
140   message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
141 "the makefiles distributed with LLVM. Please create a directory and run cmake "
142 "from there, passing the path to this source directory as the last argument. "
143 "This process created the file `CMakeCache.txt' and the directory "
144 "`CMakeFiles'. Please delete them.")
145 endif()
146
147 list (APPEND CMAKE_MODULE_PATH "${LLD_SOURCE_DIR}/cmake/modules")
148
149 include(AddLLD)
150
151 option(LLD_USE_VTUNE
152        "Enable VTune user task tracking."
153        OFF)
154 if (LLD_USE_VTUNE)
155   find_package(VTune)
156   if (VTUNE_FOUND)
157     include_directories(${VTune_INCLUDE_DIRS})
158     list(APPEND LLVM_COMMON_LIBS ${VTune_LIBRARIES})
159     add_definitions(-DLLD_HAS_VTUNE)
160   endif()
161 endif()
162
163 option(LLD_BUILD_TOOLS
164   "Build the lld tools. If OFF, just generate build targets." ON)
165
166 if (MSVC)
167   add_definitions(-wd4530) # Suppress 'warning C4530: C++ exception handler used, but unwind semantics are not enabled.'
168   add_definitions(-wd4062) # Suppress 'warning C4062: enumerator X in switch of enum Y is not handled' from system header.
169 endif()
170
171 include_directories(BEFORE
172   ${CMAKE_CURRENT_BINARY_DIR}/include
173   ${CMAKE_CURRENT_SOURCE_DIR}/include
174   )
175
176 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
177   install(DIRECTORY include/
178     DESTINATION include
179     FILES_MATCHING
180     PATTERN "*.h"
181     PATTERN ".svn" EXCLUDE
182     )
183 endif()
184
185 add_subdirectory(Common)
186 add_subdirectory(lib)
187 add_subdirectory(tools/lld)
188
189 if (LLVM_INCLUDE_TESTS)
190   add_subdirectory(test)
191   add_subdirectory(unittests)
192 endif()
193
194 add_subdirectory(docs)
195 add_subdirectory(COFF)
196 add_subdirectory(ELF)
197 add_subdirectory(MinGW)
198 add_subdirectory(wasm)