1include(CheckCXXSymbolExists)
2include(CheckTypeSize)
3
4set(LLDB_PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
5set(LLDB_SOURCE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/source")
6set(LLDB_INCLUDE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/include")
7
8set(LLDB_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
9set(LLDB_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
10
11if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
12  message(FATAL_ERROR
13    "In-source builds are not allowed. CMake would overwrite the makefiles "
14    "distributed with LLDB. Please create a directory and run cmake from "
15    "there, passing the path to this source directory as the last argument. "
16    "This process created the file `CMakeCache.txt' and the directory "
17    "`CMakeFiles'. Please delete them.")
18endif()
19
20set(LLDB_LINKER_SUPPORTS_GROUPS OFF)
21if (LLVM_COMPILER_IS_GCC_COMPATIBLE AND NOT "${CMAKE_SYSTEM_NAME}" MATCHES "Darwin")
22  # The Darwin linker doesn't understand --start-group/--end-group.
23  set(LLDB_LINKER_SUPPORTS_GROUPS ON)
24endif()
25
26macro(add_optional_dependency variable description package found)
27  cmake_parse_arguments(ARG
28    "QUIET"
29    "VERSION"
30    ""
31    ${ARGN})
32
33  set(${variable} "Auto" CACHE STRING "${description} On, Off or Auto (default)")
34  string(TOUPPER "${${variable}}" ${variable})
35
36  if("${${variable}}" STREQUAL "AUTO")
37    set(find_package TRUE)
38    set(maybe_required)
39  elseif(${${variable}})
40    set(find_package TRUE)
41    set(maybe_required REQUIRED)
42  else()
43    set(find_package FALSE)
44    set(${variable} FALSE)
45  endif()
46
47  if(${find_package})
48    set(maybe_quiet)
49    if(ARG_QUIET)
50      set(maybe_quiet QUIET)
51    endif()
52    find_package(${package} ${ARG_VERSION} ${maybe_required} ${maybe_quiet})
53    set(${variable} "${${found}}")
54  endif()
55
56  message(STATUS "${description}: ${${variable}}")
57endmacro()
58
59add_optional_dependency(LLDB_ENABLE_SWIG "Enable SWIG to generate LLDB bindings" SWIG SWIG_FOUND VERSION 3)
60add_optional_dependency(LLDB_ENABLE_LIBEDIT "Enable editline support in LLDB" LibEdit LibEdit_FOUND)
61add_optional_dependency(LLDB_ENABLE_CURSES "Enable curses support in LLDB" CursesAndPanel CURSESANDPANEL_FOUND)
62add_optional_dependency(LLDB_ENABLE_LZMA "Enable LZMA compression support in LLDB" LibLZMA LIBLZMA_FOUND)
63add_optional_dependency(LLDB_ENABLE_LUA "Enable Lua scripting support in LLDB" LuaAndSwig LUAANDSWIG_FOUND)
64add_optional_dependency(LLDB_ENABLE_PYTHON "Enable Python scripting support in LLDB" PythonAndSwig PYTHONANDSWIG_FOUND)
65add_optional_dependency(LLDB_ENABLE_LIBXML2 "Enable Libxml 2 support in LLDB" LibXml2 LIBXML2_FOUND VERSION 2.8)
66add_optional_dependency(LLDB_ENABLE_FBSDVMCORE "Enable libfbsdvmcore support in LLDB" FBSDVMCore FBSDVMCore_FOUND QUIET)
67
68option(LLDB_USE_ENTITLEMENTS "When codesigning, use entitlements if available" ON)
69option(LLDB_BUILD_FRAMEWORK "Build LLDB.framework (Darwin only)" OFF)
70option(LLDB_NO_INSTALL_DEFAULT_RPATH "Disable default RPATH settings in binaries" OFF)
71option(LLDB_USE_SYSTEM_DEBUGSERVER "Use the system's debugserver for testing (Darwin only)." OFF)
72option(LLDB_SKIP_STRIP "Whether to skip stripping of binaries when installing lldb." OFF)
73option(LLDB_SKIP_DSYM "Whether to skip generating a dSYM when installing lldb." OFF)
74
75set(LLDB_GLOBAL_INIT_DIRECTORY "" CACHE STRING
76  "Path to the global lldbinit directory. Relative paths are resolved relative to the
77  directory containing the LLDB library.")
78
79if (LLDB_USE_SYSTEM_DEBUGSERVER)
80  # The custom target for the system debugserver has no install target, so we
81  # need to remove it from the LLVM_DISTRIBUTION_COMPONENTS list.
82  if (LLVM_DISTRIBUTION_COMPONENTS)
83    list(REMOVE_ITEM LLVM_DISTRIBUTION_COMPONENTS debugserver)
84    set(LLVM_DISTRIBUTION_COMPONENTS ${LLVM_DISTRIBUTION_COMPONENTS} CACHE STRING "" FORCE)
85  endif()
86endif()
87
88if(LLDB_BUILD_FRAMEWORK)
89  if(NOT APPLE)
90    message(FATAL_ERROR "LLDB.framework can only be generated when targeting Apple platforms")
91  endif()
92
93  set(LLDB_FRAMEWORK_VERSION A CACHE STRING "LLDB.framework version (default is A)")
94  set(LLDB_FRAMEWORK_BUILD_DIR bin CACHE STRING "Output directory for LLDB.framework")
95  set(LLDB_FRAMEWORK_INSTALL_DIR Library/Frameworks CACHE STRING "Install directory for LLDB.framework")
96
97  get_filename_component(LLDB_FRAMEWORK_ABSOLUTE_BUILD_DIR ${LLDB_FRAMEWORK_BUILD_DIR} ABSOLUTE
98    BASE_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR})
99
100  # Essentially, emit the framework's dSYM outside of the framework directory.
101  set(LLDB_DEBUGINFO_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin CACHE STRING
102      "Directory to emit dSYM files stripped from executables and libraries (Darwin Only)")
103
104  # Custom target to remove the targets (binaries, directories) that were
105  # copied into LLDB.framework in the build tree.
106  #
107  # These targets need to be removed before the install phase because otherwise
108  # because otherwise they may overwrite already installed binaries with the
109  # wrong RPATH (i.e. build RPATH instead of install RPATH).
110  #
111  # This target needs to be created here (rather than in API/CMakeLists.txt)
112  # because add_lldb_tool creates the custom rules to copy the binaries before
113  # the framework target exists and that's the only place where this is
114  # tracked.
115  add_custom_target(lldb-framework-cleanup
116    COMMENT "Cleaning up build-tree frameworks in preparation for install")
117endif()
118
119if(APPLE AND CMAKE_GENERATOR STREQUAL Xcode)
120  if(NOT LLDB_EXPLICIT_XCODE_CACHE_USED)
121    message(WARNING
122      "When building with Xcode, we recommend using the corresponding cache script. "
123      "If this was a mistake, clean your build directory and re-run CMake with:\n"
124      "  -C ${CMAKE_SOURCE_DIR}/cmake/caches/Apple-lldb-Xcode.cmake\n"
125      "See: https://lldb.llvm.org/resources/build.html#cmakegeneratedxcodeproject\n")
126  endif()
127endif()
128
129if (NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
130  set(LLDB_EXPORT_ALL_SYMBOLS 0 CACHE BOOL
131    "Causes lldb to export all symbols when building liblldb.")
132else()
133  # Windows doesn't support toggling this, so don't bother making it a
134  # cache variable.
135  set(LLDB_EXPORT_ALL_SYMBOLS 0)
136endif()
137
138if ((NOT MSVC) OR MSVC12)
139  add_definitions( -DHAVE_ROUND )
140endif()
141
142# Check if we libedit capable of handling wide characters (built with
143# '--enable-widec').
144if (LLDB_ENABLE_LIBEDIT)
145  set(CMAKE_REQUIRED_LIBRARIES ${LibEdit_LIBRARIES})
146  set(CMAKE_REQUIRED_INCLUDES ${LibEdit_INCLUDE_DIRS})
147  check_symbol_exists(el_winsertstr histedit.h LLDB_EDITLINE_USE_WCHAR)
148  set(CMAKE_EXTRA_INCLUDE_FILES histedit.h)
149  check_type_size(el_rfunc_t LLDB_EL_RFUNC_T_SIZE)
150  if (LLDB_EL_RFUNC_T_SIZE STREQUAL "")
151    set(LLDB_HAVE_EL_RFUNC_T 0)
152  else()
153    set(LLDB_HAVE_EL_RFUNC_T 1)
154  endif()
155  set(CMAKE_REQUIRED_LIBRARIES)
156  set(CMAKE_REQUIRED_INCLUDES)
157  set(CMAKE_EXTRA_INCLUDE_FILES)
158endif()
159
160if (LLDB_ENABLE_PYTHON)
161  if(CMAKE_SYSTEM_NAME MATCHES "Windows")
162    set(default_embed_python_home ON)
163  else()
164    set(default_embed_python_home OFF)
165  endif()
166  option(LLDB_EMBED_PYTHON_HOME
167    "Embed PYTHONHOME in the binary. If set to OFF, PYTHONHOME environment variable will be used to to locate Python."
168    ${default_embed_python_home})
169
170  include_directories(${Python3_INCLUDE_DIRS})
171  if (LLDB_EMBED_PYTHON_HOME)
172    get_filename_component(PYTHON_HOME "${Python3_EXECUTABLE}" DIRECTORY)
173    set(LLDB_PYTHON_HOME "${PYTHON_HOME}" CACHE STRING
174      "Path to use as PYTHONHOME in lldb. If a relative path is specified, it will be resolved at runtime relative to liblldb directory.")
175  endif()
176endif()
177
178if (LLVM_EXTERNAL_CLANG_SOURCE_DIR)
179  include_directories(${LLVM_EXTERNAL_CLANG_SOURCE_DIR}/include)
180else ()
181  include_directories(${CMAKE_SOURCE_DIR}/tools/clang/include)
182endif ()
183include_directories("${CMAKE_CURRENT_BINARY_DIR}/../clang/include")
184
185# GCC silently accepts any -Wno-<foo> option, but warns about those options
186# being unrecognized only if the compilation triggers other warnings to be
187# printed. Therefore, check for whether the compiler supports options in the
188# form -W<foo>, and if supported, add the corresponding -Wno-<foo> option.
189
190# Disable GCC warnings
191check_cxx_compiler_flag("-Wdeprecated-declarations" CXX_SUPPORTS_DEPRECATED_DECLARATIONS)
192append_if(CXX_SUPPORTS_DEPRECATED_DECLARATIONS "-Wno-deprecated-declarations" CMAKE_CXX_FLAGS)
193
194check_cxx_compiler_flag("-Wunknown-pragmas" CXX_SUPPORTS_UNKNOWN_PRAGMAS)
195append_if(CXX_SUPPORTS_UNKNOWN_PRAGMAS "-Wno-unknown-pragmas" CMAKE_CXX_FLAGS)
196
197check_cxx_compiler_flag("-Wstrict-aliasing" CXX_SUPPORTS_STRICT_ALIASING)
198append_if(CXX_SUPPORTS_STRICT_ALIASING "-Wno-strict-aliasing" CMAKE_CXX_FLAGS)
199
200check_cxx_compiler_flag("-Wstringop-truncation" CXX_SUPPORTS_STRINGOP_TRUNCATION)
201append_if(CXX_SUPPORTS_STRINGOP_TRUNCATION "-Wno-stringop-truncation" CMAKE_CXX_FLAGS)
202
203# Disable Clang warnings
204check_cxx_compiler_flag("-Wdeprecated-register" CXX_SUPPORTS_DEPRECATED_REGISTER)
205append_if(CXX_SUPPORTS_DEPRECATED_REGISTER "-Wno-deprecated-register" CMAKE_CXX_FLAGS)
206
207check_cxx_compiler_flag("-Wvla-extension" CXX_SUPPORTS_VLA_EXTENSION)
208append_if(CXX_SUPPORTS_VLA_EXTENSION "-Wno-vla-extension" CMAKE_CXX_FLAGS)
209
210# Disable MSVC warnings
211if( MSVC )
212  add_definitions(
213    -wd4018 # Suppress 'warning C4018: '>=' : signed/unsigned mismatch'
214    -wd4068 # Suppress 'warning C4068: unknown pragma'
215    -wd4150 # Suppress 'warning C4150: deletion of pointer to incomplete type'
216    -wd4201 # Suppress 'warning C4201: nonstandard extension used: nameless struct/union'
217    -wd4251 # Suppress 'warning C4251: T must have dll-interface to be used by clients of class U.'
218    -wd4521 # Suppress 'warning C4521: 'type' : multiple copy constructors specified'
219    -wd4530 # Suppress 'warning C4530: C++ exception handler used, but unwind semantics are not enabled.'
220  )
221endif()
222
223# Use the Unicode (UTF-16) APIs by default on Win32
224if (CMAKE_SYSTEM_NAME MATCHES "Windows")
225    add_definitions( -D_UNICODE -DUNICODE )
226endif()
227
228# If LLDB_VERSION_* is specified, use it, if not use LLVM_VERSION_*.
229if(NOT DEFINED LLDB_VERSION_MAJOR)
230  set(LLDB_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
231endif()
232if(NOT DEFINED LLDB_VERSION_MINOR)
233  set(LLDB_VERSION_MINOR ${LLVM_VERSION_MINOR})
234endif()
235if(NOT DEFINED LLDB_VERSION_PATCH)
236  set(LLDB_VERSION_PATCH ${LLVM_VERSION_PATCH})
237endif()
238if(NOT DEFINED LLDB_VERSION_SUFFIX)
239  set(LLDB_VERSION_SUFFIX ${LLVM_VERSION_SUFFIX})
240endif()
241set(LLDB_VERSION "${LLDB_VERSION_MAJOR}.${LLDB_VERSION_MINOR}.${LLDB_VERSION_PATCH}${LLDB_VERSION_SUFFIX}")
242message(STATUS "LLDB version: ${LLDB_VERSION}")
243
244if (LLDB_ENABLE_LZMA)
245  include_directories(${LIBLZMA_INCLUDE_DIRS})
246endif()
247
248if (LLDB_ENABLE_LIBXML2)
249  include_directories(${LIBXML2_INCLUDE_DIR})
250endif()
251
252include_directories(BEFORE
253  ${CMAKE_CURRENT_BINARY_DIR}/include
254  ${CMAKE_CURRENT_SOURCE_DIR}/include
255  )
256
257if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
258  install(DIRECTORY include/
259    COMPONENT lldb-headers
260    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
261    FILES_MATCHING
262    PATTERN "*.h"
263    PATTERN ".cmake" EXCLUDE
264    )
265
266  install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
267    COMPONENT lldb-headers
268    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
269    FILES_MATCHING
270    PATTERN "*.h"
271    PATTERN ".cmake" EXCLUDE
272    )
273
274  add_custom_target(lldb-headers)
275  set_target_properties(lldb-headers PROPERTIES FOLDER "lldb misc")
276
277  if (NOT CMAKE_CONFIGURATION_TYPES)
278    add_llvm_install_targets(install-lldb-headers
279                             COMPONENT lldb-headers)
280  endif()
281endif()
282
283
284# If LLDB is building against a prebuilt Clang, then the Clang resource
285# directory that LLDB is using for its embedded Clang instance needs to point
286# to the resource directory of the used Clang installation.
287if (NOT TARGET clang-resource-headers)
288  set(LLDB_CLANG_RESOURCE_DIR_NAME "${LLVM_VERSION_MAJOR}")
289  # Iterate over the possible places where the external resource directory
290  # could be and pick the first that exists.
291  foreach(CANDIDATE "${Clang_DIR}/../.." "${LLVM_DIR}" "${LLVM_LIBRARY_DIRS}"
292                    "${LLVM_BUILD_LIBRARY_DIR}"
293                    "${LLVM_LIBRARY_DIR}")
294    # Build the resource directory path by appending 'clang/<version number>'.
295    set(CANDIDATE_RESOURCE_DIR "${CANDIDATE}/clang/${LLDB_CLANG_RESOURCE_DIR_NAME}")
296    if (IS_DIRECTORY "${CANDIDATE_RESOURCE_DIR}")
297      set(LLDB_EXTERNAL_CLANG_RESOURCE_DIR "${CANDIDATE_RESOURCE_DIR}")
298      break()
299    endif()
300  endforeach()
301
302  if (NOT LLDB_EXTERNAL_CLANG_RESOURCE_DIR)
303    message(FATAL_ERROR "Expected directory for clang-resource headers not found: ${LLDB_EXTERNAL_CLANG_RESOURCE_DIR}")
304  endif()
305endif()
306
307# Find Apple-specific libraries or frameworks that may be needed.
308if (APPLE)
309  if(NOT APPLE_EMBEDDED)
310    find_library(CARBON_LIBRARY Carbon)
311    find_library(CORE_SERVICES_LIBRARY CoreServices)
312  endif()
313  find_library(FOUNDATION_LIBRARY Foundation)
314  find_library(CORE_FOUNDATION_LIBRARY CoreFoundation)
315  find_library(SECURITY_LIBRARY Security)
316  include_directories(${LIBXML2_INCLUDE_DIR})
317endif()
318
319if( WIN32 AND NOT CYGWIN )
320  set(PURE_WINDOWS 1)
321endif()
322
323if(NOT PURE_WINDOWS)
324  set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
325  find_package(Threads REQUIRED)
326endif()
327
328# Figure out if lldb could use lldb-server.  If so, then we'll
329# ensure we build lldb-server when an lldb target is being built.
330if (CMAKE_SYSTEM_NAME MATCHES "Android|Darwin|FreeBSD|Linux|NetBSD|Windows")
331  set(LLDB_CAN_USE_LLDB_SERVER ON)
332else()
333  set(LLDB_CAN_USE_LLDB_SERVER OFF)
334endif()
335
336# Figure out if lldb could use debugserver.  If so, then we'll
337# ensure we build debugserver when we build lldb.
338if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
339    set(LLDB_CAN_USE_DEBUGSERVER ON)
340else()
341    set(LLDB_CAN_USE_DEBUGSERVER OFF)
342endif()
343
344if ((CMAKE_SYSTEM_NAME MATCHES "Android") AND LLVM_BUILD_STATIC AND
345    ((ANDROID_ABI MATCHES "armeabi") OR (ANDROID_ABI MATCHES "mips")))
346  add_definitions(-DANDROID_USE_ACCEPT_WORKAROUND)
347endif()
348
349include(LLDBGenerateConfig)
350