399 lines · plain
1include(GNUInstallDirs)2 3function(lldb_tablegen)4 # Syntax:5 # lldb_tablegen output-file [tablegen-arg ...] SOURCE source-file6 # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]7 #8 # Generates a custom command for invoking tblgen as9 #10 # tblgen source-file -o=output-file tablegen-arg ...11 #12 # and, if cmake-target-name is provided, creates a custom target for13 # executing the custom command depending on output-file. It is14 # possible to list more files to depend after DEPENDS.15 16 cmake_parse_arguments(LTG "" "SOURCE;TARGET" "" ${ARGN})17 18 if(NOT LTG_SOURCE)19 message(FATAL_ERROR "SOURCE source-file required by lldb_tablegen")20 endif()21 22 set(LLVM_TARGET_DEFINITIONS ${LTG_SOURCE})23 24 if (LLVM_USE_SANITIZER MATCHES ".*Address.*")25 list(APPEND LTG_UNPARSED_ARGUMENTS -DLLDB_SANITIZED)26 endif()27 28 tablegen(LLDB ${LTG_UNPARSED_ARGUMENTS})29 30 if(LTG_TARGET)31 add_public_tablegen_target(${LTG_TARGET})32 set_property(GLOBAL APPEND PROPERTY LLDB_TABLEGEN_TARGETS ${LTG_TARGET})33 endif()34endfunction(lldb_tablegen)35 36function(add_lldb_library name)37 include_directories(BEFORE38 ${CMAKE_CURRENT_BINARY_DIR}39)40 41 cmake_parse_arguments(PARAM42 "MODULE;SHARED;STATIC;OBJECT;PLUGIN;FRAMEWORK;NO_INTERNAL_DEPENDENCIES;NO_PLUGIN_DEPENDENCIES"43 "INSTALL_PREFIX"44 "LINK_LIBS;CLANG_LIBS"45 ${ARGN})46 47 if(PARAM_NO_INTERNAL_DEPENDENCIES)48 foreach(link_lib ${PARAM_LINK_LIBS})49 if (link_lib MATCHES "^lldb")50 message(FATAL_ERROR51 "Library ${name} cannot depend on any other lldb libs "52 "(Found ${link_lib} in LINK_LIBS)")53 endif()54 endforeach()55 endif()56 57 if(PARAM_NO_PLUGIN_DEPENDENCIES)58 foreach(link_lib ${PARAM_LINK_LIBS})59 if (link_lib MATCHES "^lldbPlugin")60 message(FATAL_ERROR61 "Library ${name} cannot depend on a plugin (Found ${link_lib} in "62 "LINK_LIBS)")63 endif()64 endforeach()65 endif()66 67 if(PARAM_PLUGIN)68 set_property(GLOBAL APPEND PROPERTY LLDB_PLUGINS ${name})69 endif()70 71 if (PARAM_MODULE)72 set(libkind MODULE)73 elseif (PARAM_SHARED)74 set(libkind SHARED)75 elseif (PARAM_OBJECT)76 set(libkind OBJECT)77 else ()78 # PARAM_STATIC or library type unspecified. BUILD_SHARED_LIBS79 # does not control the kind of libraries created for LLDB,80 # only whether or not they link to shared/static LLVM/Clang81 # libraries.82 set(libkind STATIC)83 endif()84 85 if(LLDB_NO_INSTALL_DEFAULT_RPATH)86 set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)87 endif()88 89 llvm_add_library(${name} ${libkind}90 ${PARAM_UNPARSED_ARGUMENTS}91 LINK_LIBS ${PARAM_LINK_LIBS}92 ${pass_NO_INSTALL_RPATH}93 )94 95 if(CLANG_LINK_CLANG_DYLIB)96 target_link_libraries(${name} PRIVATE clang-cpp)97 else()98 target_link_libraries(${name} PRIVATE ${PARAM_CLANG_LIBS})99 endif()100 101 # A target cannot be changed to a FRAMEWORK after calling install() because102 # this may result in the wrong install DESTINATION. The FRAMEWORK property103 # must be set earlier.104 if(PARAM_FRAMEWORK)105 set_target_properties(${name} PROPERTIES FRAMEWORK ON)106 endif()107 108 if(PARAM_SHARED)109 set(install_dest lib${LLVM_LIBDIR_SUFFIX})110 if(PARAM_INSTALL_PREFIX)111 set(install_dest ${PARAM_INSTALL_PREFIX})112 endif()113 # RUNTIME is relevant for DLL platforms, FRAMEWORK for macOS114 install(TARGETS ${name} COMPONENT ${name}115 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"116 LIBRARY DESTINATION ${install_dest}117 ARCHIVE DESTINATION ${install_dest}118 FRAMEWORK DESTINATION ${install_dest})119 if (NOT CMAKE_CONFIGURATION_TYPES)120 add_llvm_install_targets(install-${name}121 DEPENDS ${name}122 COMPONENT ${name})123 endif()124 endif()125 126 # Hack: only some LLDB libraries depend on the clang autogenerated headers,127 # but it is simple enough to make all of LLDB depend on some of those128 # headers without negatively impacting much of anything.129 if(NOT LLDB_BUILT_STANDALONE)130 add_dependencies(${name} clang-tablegen-targets)131 endif()132 133 if(PARAM_PLUGIN)134 get_property(parent_dir DIRECTORY PROPERTY PARENT_DIRECTORY)135 if(EXISTS ${parent_dir})136 get_filename_component(category ${parent_dir} NAME)137 set_target_properties(${name} PROPERTIES FOLDER "LLDB/Plugins/${category}")138 endif()139 else()140 set_target_properties(${name} PROPERTIES FOLDER "LLDB/Libraries")141 endif()142 143 # If we want to export all lldb symbols (i.e LLDB_EXPORT_ALL_SYMBOLS=ON), we144 # need to use default visibility for all LLDB libraries even if a global145 # `CMAKE_CXX_VISIBILITY_PRESET=hidden`is present.146 if (LLDB_EXPORT_ALL_SYMBOLS)147 set_target_properties(${name} PROPERTIES CXX_VISIBILITY_PRESET default)148 endif()149endfunction(add_lldb_library)150 151function(add_lldb_executable name)152 cmake_parse_arguments(ARG153 "GENERATE_INSTALL"154 "INSTALL_PREFIX"155 "LINK_LIBS;CLANG_LIBS;LINK_COMPONENTS;BUILD_RPATH;INSTALL_RPATH"156 ${ARGN}157 )158 159 if(LLDB_NO_INSTALL_DEFAULT_RPATH)160 set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)161 endif()162 163 list(APPEND LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})164 add_llvm_executable(${name}165 ${pass_NO_INSTALL_RPATH}166 ${ARG_UNPARSED_ARGUMENTS}167 )168 169 target_link_libraries(${name} PRIVATE ${ARG_LINK_LIBS})170 if(WIN32)171 list(FIND ARG_LINK_LIBS liblldb LIBLLDB_INDEX)172 if(NOT LIBLLDB_INDEX EQUAL -1)173 if (MSVC)174 target_link_options(${name} PRIVATE "/DELAYLOAD:$<TARGET_FILE_NAME:liblldb>")175 target_link_libraries(${name} PRIVATE delayimp)176 elseif (MINGW AND LINKER_IS_LLD)177 # LLD can delay load just by passing a --delayload flag, as long as the import178 # library is a short type import library (which LLD and MS link.exe produce).179 # With ld.bfd, with long import libraries (as produced by GNU binutils), one180 # has to generate a separate delayload import library with dlltool.181 target_link_options(${name} PRIVATE "-Wl,--delayload=$<TARGET_FILE_NAME:liblldb>")182 elseif (DEFINED LLDB_PYTHON_DLL_RELATIVE_PATH)183 # If liblldb can't be delayloaded, then LLDB_PYTHON_DLL_RELATIVE_PATH will not184 # have any effect.185 message(WARNING "liblldb is not delay loaded, LLDB_PYTHON_DLL_RELATIVE_PATH has no effect")186 endif()187 endif()188 endif()189 if(CLANG_LINK_CLANG_DYLIB)190 target_link_libraries(${name} PRIVATE clang-cpp)191 else()192 target_link_libraries(${name} PRIVATE ${ARG_CLANG_LIBS})193 endif()194 195 if (ARG_BUILD_RPATH)196 set_target_properties(${name} PROPERTIES BUILD_RPATH "${ARG_BUILD_RPATH}")197 endif()198 199 if (ARG_INSTALL_RPATH)200 set_target_properties(${name} PROPERTIES201 BUILD_WITH_INSTALL_RPATH OFF202 INSTALL_RPATH "${ARG_INSTALL_RPATH}")203 endif()204 205 if(ARG_GENERATE_INSTALL)206 set(install_dest bin)207 if(ARG_INSTALL_PREFIX)208 set(install_dest ${ARG_INSTALL_PREFIX})209 endif()210 install(TARGETS ${name} COMPONENT ${name}211 RUNTIME DESTINATION ${install_dest}212 LIBRARY DESTINATION ${install_dest}213 BUNDLE DESTINATION ${install_dest}214 FRAMEWORK DESTINATION ${install_dest})215 if (NOT CMAKE_CONFIGURATION_TYPES)216 add_llvm_install_targets(install-${name}217 DEPENDS ${name}218 COMPONENT ${name})219 endif()220 if(APPLE AND ARG_INSTALL_PREFIX)221 lldb_add_post_install_steps_darwin(${name} ${ARG_INSTALL_PREFIX})222 endif()223 endif()224endfunction()225 226 227macro(add_lldb_tool_subdirectory name)228 add_llvm_subdirectory(LLDB TOOL ${name})229endmacro()230 231function(add_lldb_tool name)232 cmake_parse_arguments(ARG "ADD_TO_FRAMEWORK" "" "" ${ARGN})233 if(LLDB_BUILD_FRAMEWORK AND ARG_ADD_TO_FRAMEWORK)234 set(subdir LLDB.framework/Versions/${LLDB_FRAMEWORK_VERSION}/Resources)235 add_lldb_executable(${name}236 GENERATE_INSTALL237 INSTALL_PREFIX ${LLDB_FRAMEWORK_INSTALL_DIR}/${subdir}238 ${ARG_UNPARSED_ARGUMENTS}239 )240 lldb_add_to_buildtree_lldb_framework(${name} ${subdir})241 return()242 endif()243 244 add_lldb_executable(${name} GENERATE_INSTALL ${ARG_UNPARSED_ARGUMENTS})245 set_target_properties(${name} PROPERTIES XCODE_GENERATE_SCHEME ON)246endfunction()247 248# The test suite relies on finding LLDB.framework binary resources in the249# build-tree. Remove them before installing to avoid collisions with their250# own install targets.251function(lldb_add_to_buildtree_lldb_framework name subdir)252 # Destination for the copy in the build-tree. While the framework target may253 # not exist yet, it will exist when the generator expression gets expanded.254 set(copy_dest "${LLDB_FRAMEWORK_ABSOLUTE_BUILD_DIR}/${subdir}/$<TARGET_FILE_NAME:${name}>")255 256 # Copy into the given subdirectory for testing.257 add_custom_command(TARGET ${name} POST_BUILD258 COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${name}> ${copy_dest}259 COMMENT "Copy ${name} to ${copy_dest}"260 )261 262 # Create a custom target to remove the copy again from LLDB.framework in the263 # build tree.264 add_custom_target(${name}-cleanup265 COMMAND ${CMAKE_COMMAND} -E remove ${copy_dest}266 COMMENT "Removing ${name} from LLDB.framework")267 add_dependencies(lldb-framework-cleanup268 ${name}-cleanup)269endfunction()270 271# Add extra install steps for dSYM creation and stripping for the given target.272function(lldb_add_post_install_steps_darwin name install_prefix)273 if(NOT APPLE)274 message(WARNING "Darwin-specific functionality; not currently available on non-Apple platforms.")275 return()276 endif()277 278 get_target_property(output_name ${name} OUTPUT_NAME)279 if(NOT output_name)280 set(output_name ${name})281 endif()282 283 get_target_property(is_framework ${name} FRAMEWORK)284 if(is_framework)285 get_target_property(buildtree_dir ${name} LIBRARY_OUTPUT_DIRECTORY)286 if(buildtree_dir)287 set(bundle_subdir ${output_name}.framework/Versions/${LLDB_FRAMEWORK_VERSION}/)288 else()289 message(SEND_ERROR "Framework target ${name} missing property for output directory. Cannot generate post-install steps.")290 return()291 endif()292 else()293 get_target_property(target_type ${name} TYPE)294 if(target_type STREQUAL "EXECUTABLE")295 set(buildtree_dir ${LLVM_RUNTIME_OUTPUT_INTDIR})296 else()297 # Only ever install shared libraries.298 set(output_name "lib${output_name}.dylib")299 set(buildtree_dir ${LLVM_LIBRARY_OUTPUT_INTDIR})300 endif()301 endif()302 303 # Generate dSYM304 if(NOT LLDB_SKIP_DSYM)305 set(dsym_name ${output_name}.dSYM)306 if(is_framework)307 set(dsym_name ${output_name}.framework.dSYM)308 endif()309 if(LLDB_DEBUGINFO_INSTALL_PREFIX)310 # This makes the path absolute, so we must respect DESTDIR.311 set(dsym_name "\$ENV\{DESTDIR\}${LLDB_DEBUGINFO_INSTALL_PREFIX}/${dsym_name}")312 endif()313 314 set(buildtree_name ${buildtree_dir}/${bundle_subdir}${output_name})315 install(CODE "message(STATUS \"Externalize debuginfo: ${dsym_name}\")" COMPONENT ${name})316 install(CODE "execute_process(COMMAND xcrun dsymutil -o=${dsym_name} ${buildtree_name})"317 COMPONENT ${name})318 endif()319 320 if(NOT LLDB_SKIP_STRIP)321 # Strip distribution binary with -ST (removing debug symbol table entries and322 # Swift symbols). Avoid CMAKE_INSTALL_DO_STRIP and llvm_externalize_debuginfo()323 # as they can't be configured sufficiently.324 set(installtree_name "\$ENV\{DESTDIR\}${install_prefix}/${bundle_subdir}${output_name}")325 install(CODE "message(STATUS \"Stripping: ${installtree_name}\")" COMPONENT ${name})326 install(CODE "execute_process(COMMAND xcrun strip -ST ${installtree_name})"327 COMPONENT ${name})328 endif()329endfunction()330 331# CMake's set_target_properties() doesn't allow to pass lists for RPATH332# properties directly (error: "called with incorrect number of arguments").333# Instead of defining two list variables each time, use this helper function.334function(lldb_setup_rpaths name)335 cmake_parse_arguments(LIST "" "" "BUILD_RPATH;INSTALL_RPATH" ${ARGN})336 set_target_properties(${name} PROPERTIES337 BUILD_WITH_INSTALL_RPATH OFF338 BUILD_RPATH "${LIST_BUILD_RPATH}"339 INSTALL_RPATH "${LIST_INSTALL_RPATH}"340 )341endfunction()342 343function(lldb_find_system_debugserver path)344 execute_process(COMMAND xcode-select -p345 RESULT_VARIABLE exit_code346 OUTPUT_VARIABLE xcode_dev_dir347 ERROR_VARIABLE error_msg348 OUTPUT_STRIP_TRAILING_WHITESPACE)349 if(exit_code)350 message(WARNING "`xcode-select -p` failed:\n${error_msg}")351 else()352 set(subpath "LLDB.framework/Resources/debugserver")353 set(path_shared "${xcode_dev_dir}/../SharedFrameworks/${subpath}")354 set(path_private "${xcode_dev_dir}/Library/PrivateFrameworks/${subpath}")355 356 if(EXISTS ${path_shared})357 set(${path} ${path_shared} PARENT_SCOPE)358 elseif(EXISTS ${path_private})359 set(${path} ${path_private} PARENT_SCOPE)360 else()361 message(WARNING "System debugserver requested, but not found. "362 "Candidates don't exist: ${path_shared}\n${path_private}")363 endif()364 endif()365endfunction()366 367function(lldb_find_python_module module)368 set(MODULE_FOUND PY_${module}_FOUND)369 if (${MODULE_FOUND})370 return()371 endif()372 373 execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import ${module}"374 RESULT_VARIABLE status375 ERROR_QUIET)376 377 if (status)378 set(${MODULE_FOUND} OFF PARENT_SCOPE)379 message(STATUS "Could NOT find Python module '${module}'")380 else()381 set(${MODULE_FOUND} ON PARENT_SCOPE)382 message(STATUS "Found Python module '${module}'")383 endif()384endfunction()385 386# Removes all module flags from the current CMAKE_CXX_FLAGS. Used for387# the Objective-C++ code in lldb which we don't want to build with modules.388# Reasons for this are that modules with Objective-C++ would require that389# all LLVM/Clang modules are Objective-C++ compatible (which they are likely390# not) and we would have rebuild a second set of modules just for the few391# Objective-C++ files in lldb (which slows down the build process).392macro(remove_module_flags)393 string(REGEX REPLACE "-fmodules-cache-path=[^ ]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")394 string(REGEX REPLACE "-fmodules-local-submodule-visibility" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")395 string(REGEX REPLACE "-fmodules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")396 string(REGEX REPLACE "-gmodules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")397 string(REGEX REPLACE "-fcxx-modules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")398endmacro()399