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