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})
23 tablegen(LLDB ${LTG_UNPARSED_ARGUMENTS})
26 add_public_tablegen_target(${LTG_TARGET})
27 set_target_properties( ${LTG_TARGET} PROPERTIES FOLDER "LLDB tablegenning")
28 set_property(GLOBAL APPEND PROPERTY LLDB_TABLEGEN_TARGETS ${LTG_TARGET})
30 endfunction(lldb_tablegen)
32 function(add_lldb_library name)
33 include_directories(BEFORE
34 ${CMAKE_CURRENT_BINARY_DIR}
37 # only supported parameters to this macro are the optional
38 # MODULE;SHARED;STATIC library type and source files
39 cmake_parse_arguments(PARAM
40 "MODULE;SHARED;STATIC;OBJECT;PLUGIN;FRAMEWORK;NO_INTERNAL_DEPENDENCIES;NO_PLUGIN_DEPENDENCIES"
41 "INSTALL_PREFIX;ENTITLEMENTS"
42 "EXTRA_CXXFLAGS;DEPENDS;LINK_LIBS;LINK_COMPONENTS;CLANG_LIBS"
44 llvm_process_sources(srcs ${PARAM_UNPARSED_ARGUMENTS})
45 list(APPEND LLVM_LINK_COMPONENTS ${PARAM_LINK_COMPONENTS})
47 if(PARAM_NO_INTERNAL_DEPENDENCIES)
48 foreach(link_lib ${PARAM_LINK_LIBS})
49 if (link_lib MATCHES "^lldb")
51 "Library ${name} cannot depend on any other lldb libs "
52 "(Found ${link_lib} in LINK_LIBS)")
57 if(PARAM_NO_PLUGIN_DEPENDENCIES)
58 foreach(link_lib ${PARAM_LINK_LIBS})
59 if (link_lib MATCHES "^lldbPlugin")
61 "Library ${name} cannot depend on a plugin (Found ${link_lib} in "
68 set_property(GLOBAL APPEND PROPERTY LLDB_PLUGINS ${name})
71 if (MSVC_IDE OR XCODE)
72 string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
73 list(GET split_path -1 dir)
74 file(GLOB_RECURSE headers
75 ../../include/lldb${dir}/*.h)
76 set(srcs ${srcs} ${headers})
85 # PARAM_STATIC or library type unspecified. BUILD_SHARED_LIBS
86 # does not control the kind of libraries created for LLDB,
87 # only whether or not they link to shared/static LLVM/Clang
92 #PIC not needed on Win
93 # FIXME: Setting CMAKE_CXX_FLAGS here is a no-op, use target_compile_options
94 # or omit this logic instead.
96 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
100 add_library(${name} ${libkind} ${srcs})
102 if(PARAM_ENTITLEMENTS)
103 set(pass_ENTITLEMENTS ENTITLEMENTS ${PARAM_ENTITLEMENTS})
106 if(LLDB_NO_INSTALL_DEFAULT_RPATH)
107 set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)
110 llvm_add_library(${name} ${libkind} ${srcs}
111 LINK_LIBS ${PARAM_LINK_LIBS}
112 DEPENDS ${PARAM_DEPENDS}
114 ${pass_NO_INSTALL_RPATH}
117 if(CLANG_LINK_CLANG_DYLIB)
118 target_link_libraries(${name} PRIVATE clang-cpp)
120 target_link_libraries(${name} PRIVATE ${PARAM_CLANG_LIBS})
124 # A target cannot be changed to a FRAMEWORK after calling install() because
125 # this may result in the wrong install DESTINATION. The FRAMEWORK property
126 # must be set earlier.
128 set_target_properties(${name} PROPERTIES FRAMEWORK ON)
132 set(install_dest lib${LLVM_LIBDIR_SUFFIX})
133 if(PARAM_INSTALL_PREFIX)
134 set(install_dest ${PARAM_INSTALL_PREFIX})
136 # RUNTIME is relevant for DLL platforms, FRAMEWORK for macOS
137 install(TARGETS ${name} COMPONENT ${name}
138 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
139 LIBRARY DESTINATION ${install_dest}
140 ARCHIVE DESTINATION ${install_dest}
141 FRAMEWORK DESTINATION ${install_dest})
142 if (NOT CMAKE_CONFIGURATION_TYPES)
143 add_llvm_install_targets(install-${name}
149 # Hack: only some LLDB libraries depend on the clang autogenerated headers,
150 # but it is simple enough to make all of LLDB depend on some of those
151 # headers without negatively impacting much of anything.
152 if(NOT LLDB_BUILT_STANDALONE)
153 add_dependencies(${name} clang-tablegen-targets)
156 # Add in any extra C++ compilation flags for this library.
157 target_compile_options(${name} PRIVATE ${PARAM_EXTRA_CXXFLAGS})
160 get_property(parent_dir DIRECTORY PROPERTY PARENT_DIRECTORY)
161 if(EXISTS ${parent_dir})
162 get_filename_component(category ${parent_dir} NAME)
163 set_target_properties(${name} PROPERTIES FOLDER "lldb plugins/${category}")
166 set_target_properties(${name} PROPERTIES FOLDER "lldb libraries")
169 # If we want to export all lldb symbols (i.e LLDB_EXPORT_ALL_SYMBOLS=ON), we
170 # need to use default visibility for all LLDB libraries even if a global
171 # `CMAKE_CXX_VISIBILITY_PRESET=hidden`is present.
172 if (LLDB_EXPORT_ALL_SYMBOLS)
173 set_target_properties(${name} PROPERTIES CXX_VISIBILITY_PRESET default)
175 endfunction(add_lldb_library)
177 function(add_lldb_executable name)
178 cmake_parse_arguments(ARG
180 "INSTALL_PREFIX;ENTITLEMENTS"
181 "LINK_LIBS;CLANG_LIBS;LINK_COMPONENTS;BUILD_RPATH;INSTALL_RPATH"
186 set(pass_ENTITLEMENTS ENTITLEMENTS ${ARG_ENTITLEMENTS})
189 if(LLDB_NO_INSTALL_DEFAULT_RPATH)
190 set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)
193 list(APPEND LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
194 add_llvm_executable(${name}
196 ${pass_NO_INSTALL_RPATH}
197 ${ARG_UNPARSED_ARGUMENTS}
200 target_link_libraries(${name} PRIVATE ${ARG_LINK_LIBS})
201 if(CLANG_LINK_CLANG_DYLIB)
202 target_link_libraries(${name} PRIVATE clang-cpp)
204 target_link_libraries(${name} PRIVATE ${ARG_CLANG_LIBS})
206 set_target_properties(${name} PROPERTIES FOLDER "lldb executables")
209 set_target_properties(${name} PROPERTIES BUILD_RPATH "${ARG_BUILD_RPATH}")
212 if (ARG_INSTALL_RPATH)
213 set_target_properties(${name} PROPERTIES
214 BUILD_WITH_INSTALL_RPATH OFF
215 INSTALL_RPATH "${ARG_INSTALL_RPATH}")
218 if(ARG_GENERATE_INSTALL)
219 set(install_dest bin)
220 if(ARG_INSTALL_PREFIX)
221 set(install_dest ${ARG_INSTALL_PREFIX})
223 install(TARGETS ${name} COMPONENT ${name}
224 RUNTIME DESTINATION ${install_dest}
225 LIBRARY DESTINATION ${install_dest}
226 BUNDLE DESTINATION ${install_dest}
227 FRAMEWORK DESTINATION ${install_dest})
228 if (NOT CMAKE_CONFIGURATION_TYPES)
229 add_llvm_install_targets(install-${name}
233 if(APPLE AND ARG_INSTALL_PREFIX)
234 lldb_add_post_install_steps_darwin(${name} ${ARG_INSTALL_PREFIX})
240 macro(add_lldb_tool_subdirectory name)
241 add_llvm_subdirectory(LLDB TOOL ${name})
244 function(add_lldb_tool name)
245 cmake_parse_arguments(ARG "ADD_TO_FRAMEWORK" "" "" ${ARGN})
246 if(LLDB_BUILD_FRAMEWORK AND ARG_ADD_TO_FRAMEWORK)
247 set(subdir LLDB.framework/Versions/${LLDB_FRAMEWORK_VERSION}/Resources)
248 add_lldb_executable(${name}
250 INSTALL_PREFIX ${LLDB_FRAMEWORK_INSTALL_DIR}/${subdir}
251 ${ARG_UNPARSED_ARGUMENTS}
253 lldb_add_to_buildtree_lldb_framework(${name} ${subdir})
257 add_lldb_executable(${name} GENERATE_INSTALL ${ARG_UNPARSED_ARGUMENTS})
260 # The test suite relies on finding LLDB.framework binary resources in the
261 # build-tree. Remove them before installing to avoid collisions with their
262 # own install targets.
263 function(lldb_add_to_buildtree_lldb_framework name subdir)
264 # Destination for the copy in the build-tree. While the framework target may
265 # not exist yet, it will exist when the generator expression gets expanded.
266 set(copy_dest "${LLDB_FRAMEWORK_ABSOLUTE_BUILD_DIR}/${subdir}/$<TARGET_FILE_NAME:${name}>")
268 # Copy into the given subdirectory for testing.
269 add_custom_command(TARGET ${name} POST_BUILD
270 COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${name}> ${copy_dest}
271 COMMENT "Copy ${name} to ${copy_dest}"
274 # Create a custom target to remove the copy again from LLDB.framework in the
276 add_custom_target(${name}-cleanup
277 COMMAND ${CMAKE_COMMAND} -E remove ${copy_dest}
278 COMMENT "Removing ${name} from LLDB.framework")
279 add_dependencies(lldb-framework-cleanup
283 # Add extra install steps for dSYM creation and stripping for the given target.
284 function(lldb_add_post_install_steps_darwin name install_prefix)
286 message(WARNING "Darwin-specific functionality; not currently available on non-Apple platforms.")
290 get_target_property(output_name ${name} OUTPUT_NAME)
292 set(output_name ${name})
295 get_target_property(is_framework ${name} FRAMEWORK)
297 get_target_property(buildtree_dir ${name} LIBRARY_OUTPUT_DIRECTORY)
299 set(bundle_subdir ${output_name}.framework/Versions/${LLDB_FRAMEWORK_VERSION}/)
301 message(SEND_ERROR "Framework target ${name} missing property for output directory. Cannot generate post-install steps.")
305 get_target_property(target_type ${name} TYPE)
306 if(target_type STREQUAL "EXECUTABLE")
307 set(buildtree_dir ${LLVM_RUNTIME_OUTPUT_INTDIR})
309 # Only ever install shared libraries.
310 set(output_name "lib${output_name}.dylib")
311 set(buildtree_dir ${LLVM_LIBRARY_OUTPUT_INTDIR})
316 if(NOT LLDB_SKIP_DSYM)
317 set(dsym_name ${output_name}.dSYM)
319 set(dsym_name ${output_name}.framework.dSYM)
321 if(LLDB_DEBUGINFO_INSTALL_PREFIX)
322 # This makes the path absolute, so we must respect DESTDIR.
323 set(dsym_name "\$ENV\{DESTDIR\}${LLDB_DEBUGINFO_INSTALL_PREFIX}/${dsym_name}")
326 set(buildtree_name ${buildtree_dir}/${bundle_subdir}${output_name})
327 install(CODE "message(STATUS \"Externalize debuginfo: ${dsym_name}\")" COMPONENT ${name})
328 install(CODE "execute_process(COMMAND xcrun dsymutil -o=${dsym_name} ${buildtree_name})"
332 if(NOT LLDB_SKIP_STRIP)
333 # Strip distribution binary with -ST (removing debug symbol table entries and
334 # Swift symbols). Avoid CMAKE_INSTALL_DO_STRIP and llvm_externalize_debuginfo()
335 # as they can't be configured sufficiently.
336 set(installtree_name "\$ENV\{DESTDIR\}${install_prefix}/${bundle_subdir}${output_name}")
337 install(CODE "message(STATUS \"Stripping: ${installtree_name}\")" COMPONENT ${name})
338 install(CODE "execute_process(COMMAND xcrun strip -ST ${installtree_name})"
343 # CMake's set_target_properties() doesn't allow to pass lists for RPATH
344 # properties directly (error: "called with incorrect number of arguments").
345 # Instead of defining two list variables each time, use this helper function.
346 function(lldb_setup_rpaths name)
347 cmake_parse_arguments(LIST "" "" "BUILD_RPATH;INSTALL_RPATH" ${ARGN})
348 set_target_properties(${name} PROPERTIES
349 BUILD_WITH_INSTALL_RPATH OFF
350 BUILD_RPATH "${LIST_BUILD_RPATH}"
351 INSTALL_RPATH "${LIST_INSTALL_RPATH}"
355 function(lldb_find_system_debugserver path)
356 execute_process(COMMAND xcode-select -p
357 RESULT_VARIABLE exit_code
358 OUTPUT_VARIABLE xcode_dev_dir
359 ERROR_VARIABLE error_msg
360 OUTPUT_STRIP_TRAILING_WHITESPACE)
362 message(WARNING "`xcode-select -p` failed:\n${error_msg}")
364 set(subpath "LLDB.framework/Resources/debugserver")
365 set(path_shared "${xcode_dev_dir}/../SharedFrameworks/${subpath}")
366 set(path_private "${xcode_dev_dir}/Library/PrivateFrameworks/${subpath}")
368 if(EXISTS ${path_shared})
369 set(${path} ${path_shared} PARENT_SCOPE)
370 elseif(EXISTS ${path_private})
371 set(${path} ${path_private} PARENT_SCOPE)
373 message(WARNING "System debugserver requested, but not found. "
374 "Candidates don't exist: ${path_shared}\n${path_private}")
379 function(lldb_find_python_module module)
380 set(MODULE_FOUND PY_${module}_FOUND)
381 if (DEFINED ${MODULE_FOUND})
385 execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import ${module}"
386 RESULT_VARIABLE status
390 set(${MODULE_FOUND} OFF CACHE BOOL "Failed to find python module '${module}'")
391 message(STATUS "Could NOT find Python module '${module}'")
393 set(${MODULE_FOUND} ON CACHE BOOL "Found python module '${module}'")
394 message(STATUS "Found Python module '${module}'")
398 # Removes all module flags from the current CMAKE_CXX_FLAGS. Used for
399 # the Objective-C++ code in lldb which we don't want to build with modules.
400 # Reasons for this are that modules with Objective-C++ would require that
401 # all LLVM/Clang modules are Objective-C++ compatible (which they are likely
402 # not) and we would have rebuild a second set of modules just for the few
403 # Objective-C++ files in lldb (which slows down the build process).
404 macro(remove_module_flags)
405 string(REGEX REPLACE "-fmodules-cache-path=[^ ]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
406 string(REGEX REPLACE "-fmodules-local-submodule-visibility" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
407 string(REGEX REPLACE "-fmodules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
408 string(REGEX REPLACE "-gmodules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
409 string(REGEX REPLACE "-fcxx-modules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")