1 include(GNUInstallDirs)
3 function(lldb_tablegen)
5 # lldb_tablegen output-file [tablegen-arg ...] SOURCE source-file
6 # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
8 # Generates a custom command for invoking tblgen as
10 # tblgen source-file -o=output-file tablegen-arg ...
12 # and, if cmake-target-name is provided, creates a custom target for
13 # executing the custom command depending on output-file. It is
14 # possible to list more files to depend after DEPENDS.
16 cmake_parse_arguments(LTG "" "SOURCE;TARGET" "" ${ARGN})
19 message(FATAL_ERROR "SOURCE source-file required by lldb_tablegen")
22 set(LLVM_TARGET_DEFINITIONS ${LTG_SOURCE})
24 if (LLVM_USE_SANITIZER MATCHES ".*Address.*")
25 list(APPEND LTG_UNPARSED_ARGUMENTS -DLLDB_SANITIZED)
28 tablegen(LLDB ${LTG_UNPARSED_ARGUMENTS})
31 add_public_tablegen_target(${LTG_TARGET})
32 set_property(GLOBAL APPEND PROPERTY LLDB_TABLEGEN_TARGETS ${LTG_TARGET})
34 endfunction(lldb_tablegen)
36 function(add_lldb_library name)
37 include_directories(BEFORE
38 ${CMAKE_CURRENT_BINARY_DIR}
41 # only supported parameters to this macro are the optional
42 # MODULE;SHARED;STATIC library type and source files
43 cmake_parse_arguments(PARAM
44 "MODULE;SHARED;STATIC;OBJECT;PLUGIN;FRAMEWORK;NO_INTERNAL_DEPENDENCIES;NO_PLUGIN_DEPENDENCIES"
45 "INSTALL_PREFIX;ENTITLEMENTS"
46 "EXTRA_CXXFLAGS;DEPENDS;LINK_LIBS;LINK_COMPONENTS;CLANG_LIBS"
48 llvm_process_sources(srcs ${PARAM_UNPARSED_ARGUMENTS})
49 list(APPEND LLVM_LINK_COMPONENTS ${PARAM_LINK_COMPONENTS})
51 if(PARAM_NO_INTERNAL_DEPENDENCIES)
52 foreach(link_lib ${PARAM_LINK_LIBS})
53 if (link_lib MATCHES "^lldb")
55 "Library ${name} cannot depend on any other lldb libs "
56 "(Found ${link_lib} in LINK_LIBS)")
61 if(PARAM_NO_PLUGIN_DEPENDENCIES)
62 foreach(link_lib ${PARAM_LINK_LIBS})
63 if (link_lib MATCHES "^lldbPlugin")
65 "Library ${name} cannot depend on a plugin (Found ${link_lib} in "
72 set_property(GLOBAL APPEND PROPERTY LLDB_PLUGINS ${name})
75 if (MSVC_IDE OR XCODE)
76 string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
77 list(GET split_path -1 dir)
78 file(GLOB_RECURSE headers
79 ../../include/lldb${dir}/*.h)
80 set(srcs ${srcs} ${headers})
89 # PARAM_STATIC or library type unspecified. BUILD_SHARED_LIBS
90 # does not control the kind of libraries created for LLDB,
91 # only whether or not they link to shared/static LLVM/Clang
96 #PIC not needed on Win
97 # FIXME: Setting CMAKE_CXX_FLAGS here is a no-op, use target_compile_options
98 # or omit this logic instead.
100 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
104 add_library(${name} ${libkind} ${srcs})
106 if(PARAM_ENTITLEMENTS)
107 set(pass_ENTITLEMENTS ENTITLEMENTS ${PARAM_ENTITLEMENTS})
110 if(LLDB_NO_INSTALL_DEFAULT_RPATH)
111 set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)
114 llvm_add_library(${name} ${libkind} ${srcs}
115 LINK_LIBS ${PARAM_LINK_LIBS}
116 DEPENDS ${PARAM_DEPENDS}
118 ${pass_NO_INSTALL_RPATH}
121 if(CLANG_LINK_CLANG_DYLIB)
122 target_link_libraries(${name} PRIVATE clang-cpp)
124 target_link_libraries(${name} PRIVATE ${PARAM_CLANG_LIBS})
128 # A target cannot be changed to a FRAMEWORK after calling install() because
129 # this may result in the wrong install DESTINATION. The FRAMEWORK property
130 # must be set earlier.
132 set_target_properties(${name} PROPERTIES FRAMEWORK ON)
136 set(install_dest lib${LLVM_LIBDIR_SUFFIX})
137 if(PARAM_INSTALL_PREFIX)
138 set(install_dest ${PARAM_INSTALL_PREFIX})
140 # RUNTIME is relevant for DLL platforms, FRAMEWORK for macOS
141 install(TARGETS ${name} COMPONENT ${name}
142 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
143 LIBRARY DESTINATION ${install_dest}
144 ARCHIVE DESTINATION ${install_dest}
145 FRAMEWORK DESTINATION ${install_dest})
146 if (NOT CMAKE_CONFIGURATION_TYPES)
147 add_llvm_install_targets(install-${name}
153 # Hack: only some LLDB libraries depend on the clang autogenerated headers,
154 # but it is simple enough to make all of LLDB depend on some of those
155 # headers without negatively impacting much of anything.
156 if(NOT LLDB_BUILT_STANDALONE)
157 add_dependencies(${name} clang-tablegen-targets)
160 # Add in any extra C++ compilation flags for this library.
161 target_compile_options(${name} PRIVATE ${PARAM_EXTRA_CXXFLAGS})
164 get_property(parent_dir DIRECTORY PROPERTY PARENT_DIRECTORY)
165 if(EXISTS ${parent_dir})
166 get_filename_component(category ${parent_dir} NAME)
167 set_target_properties(${name} PROPERTIES FOLDER "LLDB/Plugins/${category}")
170 set_target_properties(${name} PROPERTIES FOLDER "LLDB/Libraries")
173 # If we want to export all lldb symbols (i.e LLDB_EXPORT_ALL_SYMBOLS=ON), we
174 # need to use default visibility for all LLDB libraries even if a global
175 # `CMAKE_CXX_VISIBILITY_PRESET=hidden`is present.
176 if (LLDB_EXPORT_ALL_SYMBOLS)
177 set_target_properties(${name} PROPERTIES CXX_VISIBILITY_PRESET default)
179 endfunction(add_lldb_library)
181 function(add_lldb_executable name)
182 cmake_parse_arguments(ARG
184 "INSTALL_PREFIX;ENTITLEMENTS"
185 "LINK_LIBS;CLANG_LIBS;LINK_COMPONENTS;BUILD_RPATH;INSTALL_RPATH"
190 set(pass_ENTITLEMENTS ENTITLEMENTS ${ARG_ENTITLEMENTS})
193 if(LLDB_NO_INSTALL_DEFAULT_RPATH)
194 set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)
197 list(APPEND LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
198 add_llvm_executable(${name}
200 ${pass_NO_INSTALL_RPATH}
201 ${ARG_UNPARSED_ARGUMENTS}
204 target_link_libraries(${name} PRIVATE ${ARG_LINK_LIBS})
205 if(CLANG_LINK_CLANG_DYLIB)
206 target_link_libraries(${name} PRIVATE clang-cpp)
208 target_link_libraries(${name} PRIVATE ${ARG_CLANG_LIBS})
212 set_target_properties(${name} PROPERTIES BUILD_RPATH "${ARG_BUILD_RPATH}")
215 if (ARG_INSTALL_RPATH)
216 set_target_properties(${name} PROPERTIES
217 BUILD_WITH_INSTALL_RPATH OFF
218 INSTALL_RPATH "${ARG_INSTALL_RPATH}")
221 if(ARG_GENERATE_INSTALL)
222 set(install_dest bin)
223 if(ARG_INSTALL_PREFIX)
224 set(install_dest ${ARG_INSTALL_PREFIX})
226 install(TARGETS ${name} COMPONENT ${name}
227 RUNTIME DESTINATION ${install_dest}
228 LIBRARY DESTINATION ${install_dest}
229 BUNDLE DESTINATION ${install_dest}
230 FRAMEWORK DESTINATION ${install_dest})
231 if (NOT CMAKE_CONFIGURATION_TYPES)
232 add_llvm_install_targets(install-${name}
236 if(APPLE AND ARG_INSTALL_PREFIX)
237 lldb_add_post_install_steps_darwin(${name} ${ARG_INSTALL_PREFIX})
243 macro(add_lldb_tool_subdirectory name)
244 add_llvm_subdirectory(LLDB TOOL ${name})
247 function(add_lldb_tool name)
248 cmake_parse_arguments(ARG "ADD_TO_FRAMEWORK" "" "" ${ARGN})
249 if(LLDB_BUILD_FRAMEWORK AND ARG_ADD_TO_FRAMEWORK)
250 set(subdir LLDB.framework/Versions/${LLDB_FRAMEWORK_VERSION}/Resources)
251 add_lldb_executable(${name}
253 INSTALL_PREFIX ${LLDB_FRAMEWORK_INSTALL_DIR}/${subdir}
254 ${ARG_UNPARSED_ARGUMENTS}
256 lldb_add_to_buildtree_lldb_framework(${name} ${subdir})
260 add_lldb_executable(${name} GENERATE_INSTALL ${ARG_UNPARSED_ARGUMENTS})
261 set_target_properties(${name} PROPERTIES XCODE_GENERATE_SCHEME ON)
264 # The test suite relies on finding LLDB.framework binary resources in the
265 # build-tree. Remove them before installing to avoid collisions with their
266 # own install targets.
267 function(lldb_add_to_buildtree_lldb_framework name subdir)
268 # Destination for the copy in the build-tree. While the framework target may
269 # not exist yet, it will exist when the generator expression gets expanded.
270 set(copy_dest "${LLDB_FRAMEWORK_ABSOLUTE_BUILD_DIR}/${subdir}/$<TARGET_FILE_NAME:${name}>")
272 # Copy into the given subdirectory for testing.
273 add_custom_command(TARGET ${name} POST_BUILD
274 COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${name}> ${copy_dest}
275 COMMENT "Copy ${name} to ${copy_dest}"
278 # Create a custom target to remove the copy again from LLDB.framework in the
280 add_custom_target(${name}-cleanup
281 COMMAND ${CMAKE_COMMAND} -E remove ${copy_dest}
282 COMMENT "Removing ${name} from LLDB.framework")
283 add_dependencies(lldb-framework-cleanup
287 # Add extra install steps for dSYM creation and stripping for the given target.
288 function(lldb_add_post_install_steps_darwin name install_prefix)
290 message(WARNING "Darwin-specific functionality; not currently available on non-Apple platforms.")
294 get_target_property(output_name ${name} OUTPUT_NAME)
296 set(output_name ${name})
299 get_target_property(is_framework ${name} FRAMEWORK)
301 get_target_property(buildtree_dir ${name} LIBRARY_OUTPUT_DIRECTORY)
303 set(bundle_subdir ${output_name}.framework/Versions/${LLDB_FRAMEWORK_VERSION}/)
305 message(SEND_ERROR "Framework target ${name} missing property for output directory. Cannot generate post-install steps.")
309 get_target_property(target_type ${name} TYPE)
310 if(target_type STREQUAL "EXECUTABLE")
311 set(buildtree_dir ${LLVM_RUNTIME_OUTPUT_INTDIR})
313 # Only ever install shared libraries.
314 set(output_name "lib${output_name}.dylib")
315 set(buildtree_dir ${LLVM_LIBRARY_OUTPUT_INTDIR})
320 if(NOT LLDB_SKIP_DSYM)
321 set(dsym_name ${output_name}.dSYM)
323 set(dsym_name ${output_name}.framework.dSYM)
325 if(LLDB_DEBUGINFO_INSTALL_PREFIX)
326 # This makes the path absolute, so we must respect DESTDIR.
327 set(dsym_name "\$ENV\{DESTDIR\}${LLDB_DEBUGINFO_INSTALL_PREFIX}/${dsym_name}")
330 set(buildtree_name ${buildtree_dir}/${bundle_subdir}${output_name})
331 install(CODE "message(STATUS \"Externalize debuginfo: ${dsym_name}\")" COMPONENT ${name})
332 install(CODE "execute_process(COMMAND xcrun dsymutil -o=${dsym_name} ${buildtree_name})"
336 if(NOT LLDB_SKIP_STRIP)
337 # Strip distribution binary with -ST (removing debug symbol table entries and
338 # Swift symbols). Avoid CMAKE_INSTALL_DO_STRIP and llvm_externalize_debuginfo()
339 # as they can't be configured sufficiently.
340 set(installtree_name "\$ENV\{DESTDIR\}${install_prefix}/${bundle_subdir}${output_name}")
341 install(CODE "message(STATUS \"Stripping: ${installtree_name}\")" COMPONENT ${name})
342 install(CODE "execute_process(COMMAND xcrun strip -ST ${installtree_name})"
347 # CMake's set_target_properties() doesn't allow to pass lists for RPATH
348 # properties directly (error: "called with incorrect number of arguments").
349 # Instead of defining two list variables each time, use this helper function.
350 function(lldb_setup_rpaths name)
351 cmake_parse_arguments(LIST "" "" "BUILD_RPATH;INSTALL_RPATH" ${ARGN})
352 set_target_properties(${name} PROPERTIES
353 BUILD_WITH_INSTALL_RPATH OFF
354 BUILD_RPATH "${LIST_BUILD_RPATH}"
355 INSTALL_RPATH "${LIST_INSTALL_RPATH}"
359 function(lldb_find_system_debugserver path)
360 execute_process(COMMAND xcode-select -p
361 RESULT_VARIABLE exit_code
362 OUTPUT_VARIABLE xcode_dev_dir
363 ERROR_VARIABLE error_msg
364 OUTPUT_STRIP_TRAILING_WHITESPACE)
366 message(WARNING "`xcode-select -p` failed:\n${error_msg}")
368 set(subpath "LLDB.framework/Resources/debugserver")
369 set(path_shared "${xcode_dev_dir}/../SharedFrameworks/${subpath}")
370 set(path_private "${xcode_dev_dir}/Library/PrivateFrameworks/${subpath}")
372 if(EXISTS ${path_shared})
373 set(${path} ${path_shared} PARENT_SCOPE)
374 elseif(EXISTS ${path_private})
375 set(${path} ${path_private} PARENT_SCOPE)
377 message(WARNING "System debugserver requested, but not found. "
378 "Candidates don't exist: ${path_shared}\n${path_private}")
383 function(lldb_find_python_module module)
384 set(MODULE_FOUND PY_${module}_FOUND)
389 execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import ${module}"
390 RESULT_VARIABLE status
394 set(${MODULE_FOUND} OFF PARENT_SCOPE)
395 message(STATUS "Could NOT find Python module '${module}'")
397 set(${MODULE_FOUND} ON PARENT_SCOPE)
398 message(STATUS "Found Python module '${module}'")
402 # Removes all module flags from the current CMAKE_CXX_FLAGS. Used for
403 # the Objective-C++ code in lldb which we don't want to build with modules.
404 # Reasons for this are that modules with Objective-C++ would require that
405 # all LLVM/Clang modules are Objective-C++ compatible (which they are likely
406 # not) and we would have rebuild a second set of modules just for the few
407 # Objective-C++ files in lldb (which slows down the build process).
408 macro(remove_module_flags)
409 string(REGEX REPLACE "-fmodules-cache-path=[^ ]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
410 string(REGEX REPLACE "-fmodules-local-submodule-visibility" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
411 string(REGEX REPLACE "-fmodules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
412 string(REGEX REPLACE "-gmodules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
413 string(REGEX REPLACE "-fcxx-modules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")