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_target_properties( ${LTG_TARGET} PROPERTIES FOLDER "LLDB tablegenning")
33 set_property(GLOBAL APPEND PROPERTY LLDB_TABLEGEN_TARGETS ${LTG_TARGET})
35 endfunction(lldb_tablegen)
37 function(add_lldb_library name)
38 include_directories(BEFORE
39 ${CMAKE_CURRENT_BINARY_DIR}
42 # only supported parameters to this macro are the optional
43 # MODULE;SHARED;STATIC library type and source files
44 cmake_parse_arguments(PARAM
45 "MODULE;SHARED;STATIC;OBJECT;PLUGIN;FRAMEWORK;NO_INTERNAL_DEPENDENCIES;NO_PLUGIN_DEPENDENCIES"
46 "INSTALL_PREFIX;ENTITLEMENTS"
47 "EXTRA_CXXFLAGS;DEPENDS;LINK_LIBS;LINK_COMPONENTS;CLANG_LIBS"
49 llvm_process_sources(srcs ${PARAM_UNPARSED_ARGUMENTS})
50 list(APPEND LLVM_LINK_COMPONENTS ${PARAM_LINK_COMPONENTS})
52 if(PARAM_NO_INTERNAL_DEPENDENCIES)
53 foreach(link_lib ${PARAM_LINK_LIBS})
54 if (link_lib MATCHES "^lldb")
56 "Library ${name} cannot depend on any other lldb libs "
57 "(Found ${link_lib} in LINK_LIBS)")
62 if(PARAM_NO_PLUGIN_DEPENDENCIES)
63 foreach(link_lib ${PARAM_LINK_LIBS})
64 if (link_lib MATCHES "^lldbPlugin")
66 "Library ${name} cannot depend on a plugin (Found ${link_lib} in "
73 set_property(GLOBAL APPEND PROPERTY LLDB_PLUGINS ${name})
76 if (MSVC_IDE OR XCODE)
77 string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
78 list(GET split_path -1 dir)
79 file(GLOB_RECURSE headers
80 ../../include/lldb${dir}/*.h)
81 set(srcs ${srcs} ${headers})
90 # PARAM_STATIC or library type unspecified. BUILD_SHARED_LIBS
91 # does not control the kind of libraries created for LLDB,
92 # only whether or not they link to shared/static LLVM/Clang
97 #PIC not needed on Win
98 # FIXME: Setting CMAKE_CXX_FLAGS here is a no-op, use target_compile_options
99 # or omit this logic instead.
101 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
105 add_library(${name} ${libkind} ${srcs})
107 if(PARAM_ENTITLEMENTS)
108 set(pass_ENTITLEMENTS ENTITLEMENTS ${PARAM_ENTITLEMENTS})
111 if(LLDB_NO_INSTALL_DEFAULT_RPATH)
112 set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)
115 llvm_add_library(${name} ${libkind} ${srcs}
116 LINK_LIBS ${PARAM_LINK_LIBS}
117 DEPENDS ${PARAM_DEPENDS}
119 ${pass_NO_INSTALL_RPATH}
122 if(CLANG_LINK_CLANG_DYLIB)
123 target_link_libraries(${name} PRIVATE clang-cpp)
125 target_link_libraries(${name} PRIVATE ${PARAM_CLANG_LIBS})
129 # A target cannot be changed to a FRAMEWORK after calling install() because
130 # this may result in the wrong install DESTINATION. The FRAMEWORK property
131 # must be set earlier.
133 set_target_properties(${name} PROPERTIES FRAMEWORK ON)
137 set(install_dest lib${LLVM_LIBDIR_SUFFIX})
138 if(PARAM_INSTALL_PREFIX)
139 set(install_dest ${PARAM_INSTALL_PREFIX})
141 # RUNTIME is relevant for DLL platforms, FRAMEWORK for macOS
142 install(TARGETS ${name} COMPONENT ${name}
143 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
144 LIBRARY DESTINATION ${install_dest}
145 ARCHIVE DESTINATION ${install_dest}
146 FRAMEWORK DESTINATION ${install_dest})
147 if (NOT CMAKE_CONFIGURATION_TYPES)
148 add_llvm_install_targets(install-${name}
154 # Hack: only some LLDB libraries depend on the clang autogenerated headers,
155 # but it is simple enough to make all of LLDB depend on some of those
156 # headers without negatively impacting much of anything.
157 if(NOT LLDB_BUILT_STANDALONE)
158 add_dependencies(${name} clang-tablegen-targets)
161 # Add in any extra C++ compilation flags for this library.
162 target_compile_options(${name} PRIVATE ${PARAM_EXTRA_CXXFLAGS})
165 get_property(parent_dir DIRECTORY PROPERTY PARENT_DIRECTORY)
166 if(EXISTS ${parent_dir})
167 get_filename_component(category ${parent_dir} NAME)
168 set_target_properties(${name} PROPERTIES FOLDER "lldb plugins/${category}")
171 set_target_properties(${name} PROPERTIES FOLDER "lldb libraries")
174 # If we want to export all lldb symbols (i.e LLDB_EXPORT_ALL_SYMBOLS=ON), we
175 # need to use default visibility for all LLDB libraries even if a global
176 # `CMAKE_CXX_VISIBILITY_PRESET=hidden`is present.
177 if (LLDB_EXPORT_ALL_SYMBOLS)
178 set_target_properties(${name} PROPERTIES CXX_VISIBILITY_PRESET default)
180 endfunction(add_lldb_library)
182 function(add_lldb_executable name)
183 cmake_parse_arguments(ARG
185 "INSTALL_PREFIX;ENTITLEMENTS"
186 "LINK_LIBS;CLANG_LIBS;LINK_COMPONENTS;BUILD_RPATH;INSTALL_RPATH"
191 set(pass_ENTITLEMENTS ENTITLEMENTS ${ARG_ENTITLEMENTS})
194 if(LLDB_NO_INSTALL_DEFAULT_RPATH)
195 set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)
198 list(APPEND LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
199 add_llvm_executable(${name}
201 ${pass_NO_INSTALL_RPATH}
202 ${ARG_UNPARSED_ARGUMENTS}
205 target_link_libraries(${name} PRIVATE ${ARG_LINK_LIBS})
206 if(CLANG_LINK_CLANG_DYLIB)
207 target_link_libraries(${name} PRIVATE clang-cpp)
209 target_link_libraries(${name} PRIVATE ${ARG_CLANG_LIBS})
211 set_target_properties(${name} PROPERTIES FOLDER "lldb executables")
214 set_target_properties(${name} PROPERTIES BUILD_RPATH "${ARG_BUILD_RPATH}")
217 if (ARG_INSTALL_RPATH)
218 set_target_properties(${name} PROPERTIES
219 BUILD_WITH_INSTALL_RPATH OFF
220 INSTALL_RPATH "${ARG_INSTALL_RPATH}")
223 if(ARG_GENERATE_INSTALL)
224 set(install_dest bin)
225 if(ARG_INSTALL_PREFIX)
226 set(install_dest ${ARG_INSTALL_PREFIX})
228 install(TARGETS ${name} COMPONENT ${name}
229 RUNTIME DESTINATION ${install_dest}
230 LIBRARY DESTINATION ${install_dest}
231 BUNDLE DESTINATION ${install_dest}
232 FRAMEWORK DESTINATION ${install_dest})
233 if (NOT CMAKE_CONFIGURATION_TYPES)
234 add_llvm_install_targets(install-${name}
238 if(APPLE AND ARG_INSTALL_PREFIX)
239 lldb_add_post_install_steps_darwin(${name} ${ARG_INSTALL_PREFIX})
245 macro(add_lldb_tool_subdirectory name)
246 add_llvm_subdirectory(LLDB TOOL ${name})
249 function(add_lldb_tool name)
250 cmake_parse_arguments(ARG "ADD_TO_FRAMEWORK" "" "" ${ARGN})
251 if(LLDB_BUILD_FRAMEWORK AND ARG_ADD_TO_FRAMEWORK)
252 set(subdir LLDB.framework/Versions/${LLDB_FRAMEWORK_VERSION}/Resources)
253 add_lldb_executable(${name}
255 INSTALL_PREFIX ${LLDB_FRAMEWORK_INSTALL_DIR}/${subdir}
256 ${ARG_UNPARSED_ARGUMENTS}
258 lldb_add_to_buildtree_lldb_framework(${name} ${subdir})
262 add_lldb_executable(${name} GENERATE_INSTALL ${ARG_UNPARSED_ARGUMENTS})
265 # The test suite relies on finding LLDB.framework binary resources in the
266 # build-tree. Remove them before installing to avoid collisions with their
267 # own install targets.
268 function(lldb_add_to_buildtree_lldb_framework name subdir)
269 # Destination for the copy in the build-tree. While the framework target may
270 # not exist yet, it will exist when the generator expression gets expanded.
271 set(copy_dest "${LLDB_FRAMEWORK_ABSOLUTE_BUILD_DIR}/${subdir}/$<TARGET_FILE_NAME:${name}>")
273 # Copy into the given subdirectory for testing.
274 add_custom_command(TARGET ${name} POST_BUILD
275 COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${name}> ${copy_dest}
276 COMMENT "Copy ${name} to ${copy_dest}"
279 # Create a custom target to remove the copy again from LLDB.framework in the
281 add_custom_target(${name}-cleanup
282 COMMAND ${CMAKE_COMMAND} -E remove ${copy_dest}
283 COMMENT "Removing ${name} from LLDB.framework")
284 add_dependencies(lldb-framework-cleanup
288 # Add extra install steps for dSYM creation and stripping for the given target.
289 function(lldb_add_post_install_steps_darwin name install_prefix)
291 message(WARNING "Darwin-specific functionality; not currently available on non-Apple platforms.")
295 get_target_property(output_name ${name} OUTPUT_NAME)
297 set(output_name ${name})
300 get_target_property(is_framework ${name} FRAMEWORK)
302 get_target_property(buildtree_dir ${name} LIBRARY_OUTPUT_DIRECTORY)
304 set(bundle_subdir ${output_name}.framework/Versions/${LLDB_FRAMEWORK_VERSION}/)
306 message(SEND_ERROR "Framework target ${name} missing property for output directory. Cannot generate post-install steps.")
310 get_target_property(target_type ${name} TYPE)
311 if(target_type STREQUAL "EXECUTABLE")
312 set(buildtree_dir ${LLVM_RUNTIME_OUTPUT_INTDIR})
314 # Only ever install shared libraries.
315 set(output_name "lib${output_name}.dylib")
316 set(buildtree_dir ${LLVM_LIBRARY_OUTPUT_INTDIR})
321 if(NOT LLDB_SKIP_DSYM)
322 set(dsym_name ${output_name}.dSYM)
324 set(dsym_name ${output_name}.framework.dSYM)
326 if(LLDB_DEBUGINFO_INSTALL_PREFIX)
327 # This makes the path absolute, so we must respect DESTDIR.
328 set(dsym_name "\$ENV\{DESTDIR\}${LLDB_DEBUGINFO_INSTALL_PREFIX}/${dsym_name}")
331 set(buildtree_name ${buildtree_dir}/${bundle_subdir}${output_name})
332 install(CODE "message(STATUS \"Externalize debuginfo: ${dsym_name}\")" COMPONENT ${name})
333 install(CODE "execute_process(COMMAND xcrun dsymutil -o=${dsym_name} ${buildtree_name})"
337 if(NOT LLDB_SKIP_STRIP)
338 # Strip distribution binary with -ST (removing debug symbol table entries and
339 # Swift symbols). Avoid CMAKE_INSTALL_DO_STRIP and llvm_externalize_debuginfo()
340 # as they can't be configured sufficiently.
341 set(installtree_name "\$ENV\{DESTDIR\}${install_prefix}/${bundle_subdir}${output_name}")
342 install(CODE "message(STATUS \"Stripping: ${installtree_name}\")" COMPONENT ${name})
343 install(CODE "execute_process(COMMAND xcrun strip -ST ${installtree_name})"
348 # CMake's set_target_properties() doesn't allow to pass lists for RPATH
349 # properties directly (error: "called with incorrect number of arguments").
350 # Instead of defining two list variables each time, use this helper function.
351 function(lldb_setup_rpaths name)
352 cmake_parse_arguments(LIST "" "" "BUILD_RPATH;INSTALL_RPATH" ${ARGN})
353 set_target_properties(${name} PROPERTIES
354 BUILD_WITH_INSTALL_RPATH OFF
355 BUILD_RPATH "${LIST_BUILD_RPATH}"
356 INSTALL_RPATH "${LIST_INSTALL_RPATH}"
360 function(lldb_find_system_debugserver path)
361 execute_process(COMMAND xcode-select -p
362 RESULT_VARIABLE exit_code
363 OUTPUT_VARIABLE xcode_dev_dir
364 ERROR_VARIABLE error_msg
365 OUTPUT_STRIP_TRAILING_WHITESPACE)
367 message(WARNING "`xcode-select -p` failed:\n${error_msg}")
369 set(subpath "LLDB.framework/Resources/debugserver")
370 set(path_shared "${xcode_dev_dir}/../SharedFrameworks/${subpath}")
371 set(path_private "${xcode_dev_dir}/Library/PrivateFrameworks/${subpath}")
373 if(EXISTS ${path_shared})
374 set(${path} ${path_shared} PARENT_SCOPE)
375 elseif(EXISTS ${path_private})
376 set(${path} ${path_private} PARENT_SCOPE)
378 message(WARNING "System debugserver requested, but not found. "
379 "Candidates don't exist: ${path_shared}\n${path_private}")
384 function(lldb_find_python_module module)
385 set(MODULE_FOUND PY_${module}_FOUND)
386 if (DEFINED ${MODULE_FOUND})
390 execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import ${module}"
391 RESULT_VARIABLE status
395 set(${MODULE_FOUND} OFF CACHE BOOL "Failed to find python module '${module}'")
396 message(STATUS "Could NOT find Python module '${module}'")
398 set(${MODULE_FOUND} ON CACHE BOOL "Found python module '${module}'")
399 message(STATUS "Found Python module '${module}'")
403 # Removes all module flags from the current CMAKE_CXX_FLAGS. Used for
404 # the Objective-C++ code in lldb which we don't want to build with modules.
405 # Reasons for this are that modules with Objective-C++ would require that
406 # all LLVM/Clang modules are Objective-C++ compatible (which they are likely
407 # not) and we would have rebuild a second set of modules just for the few
408 # Objective-C++ files in lldb (which slows down the build process).
409 macro(remove_module_flags)
410 string(REGEX REPLACE "-fmodules-cache-path=[^ ]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
411 string(REGEX REPLACE "-fmodules-local-submodule-visibility" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
412 string(REGEX REPLACE "-fmodules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
413 string(REGEX REPLACE "-gmodules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
414 string(REGEX REPLACE "-fcxx-modules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")