1*f6aab3d8Srobertinclude(GNUInstallDirs) 2*f6aab3d8Srobert 3061da546Spatrickfunction(lldb_tablegen) 4061da546Spatrick # Syntax: 5061da546Spatrick # lldb_tablegen output-file [tablegen-arg ...] SOURCE source-file 6061da546Spatrick # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]] 7061da546Spatrick # 8061da546Spatrick # Generates a custom command for invoking tblgen as 9061da546Spatrick # 10061da546Spatrick # tblgen source-file -o=output-file tablegen-arg ... 11061da546Spatrick # 12061da546Spatrick # and, if cmake-target-name is provided, creates a custom target for 13061da546Spatrick # executing the custom command depending on output-file. It is 14061da546Spatrick # possible to list more files to depend after DEPENDS. 15061da546Spatrick 16061da546Spatrick cmake_parse_arguments(LTG "" "SOURCE;TARGET" "" ${ARGN}) 17061da546Spatrick 18061da546Spatrick if(NOT LTG_SOURCE) 19061da546Spatrick message(FATAL_ERROR "SOURCE source-file required by lldb_tablegen") 20061da546Spatrick endif() 21061da546Spatrick 22061da546Spatrick set(LLVM_TARGET_DEFINITIONS ${LTG_SOURCE}) 23061da546Spatrick tablegen(LLDB ${LTG_UNPARSED_ARGUMENTS}) 24061da546Spatrick 25061da546Spatrick if(LTG_TARGET) 26061da546Spatrick add_public_tablegen_target(${LTG_TARGET}) 27061da546Spatrick set_target_properties( ${LTG_TARGET} PROPERTIES FOLDER "LLDB tablegenning") 28061da546Spatrick set_property(GLOBAL APPEND PROPERTY LLDB_TABLEGEN_TARGETS ${LTG_TARGET}) 29061da546Spatrick endif() 30061da546Spatrickendfunction(lldb_tablegen) 31061da546Spatrick 32061da546Spatrickfunction(add_lldb_library name) 33061da546Spatrick include_directories(BEFORE 34061da546Spatrick ${CMAKE_CURRENT_BINARY_DIR} 35061da546Spatrick) 36061da546Spatrick 37061da546Spatrick # only supported parameters to this macro are the optional 38061da546Spatrick # MODULE;SHARED;STATIC library type and source files 39061da546Spatrick cmake_parse_arguments(PARAM 40061da546Spatrick "MODULE;SHARED;STATIC;OBJECT;PLUGIN;FRAMEWORK" 41061da546Spatrick "INSTALL_PREFIX;ENTITLEMENTS" 42061da546Spatrick "EXTRA_CXXFLAGS;DEPENDS;LINK_LIBS;LINK_COMPONENTS;CLANG_LIBS" 43061da546Spatrick ${ARGN}) 44061da546Spatrick llvm_process_sources(srcs ${PARAM_UNPARSED_ARGUMENTS}) 45061da546Spatrick list(APPEND LLVM_LINK_COMPONENTS ${PARAM_LINK_COMPONENTS}) 46061da546Spatrick 47061da546Spatrick if(PARAM_PLUGIN) 48061da546Spatrick set_property(GLOBAL APPEND PROPERTY LLDB_PLUGINS ${name}) 49061da546Spatrick endif() 50061da546Spatrick 51061da546Spatrick if (MSVC_IDE OR XCODE) 52061da546Spatrick string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR}) 53061da546Spatrick list(GET split_path -1 dir) 54061da546Spatrick file(GLOB_RECURSE headers 55061da546Spatrick ../../include/lldb${dir}/*.h) 56061da546Spatrick set(srcs ${srcs} ${headers}) 57061da546Spatrick endif() 58061da546Spatrick if (PARAM_MODULE) 59061da546Spatrick set(libkind MODULE) 60061da546Spatrick elseif (PARAM_SHARED) 61061da546Spatrick set(libkind SHARED) 62061da546Spatrick elseif (PARAM_OBJECT) 63061da546Spatrick set(libkind OBJECT) 64061da546Spatrick else () 65061da546Spatrick # PARAM_STATIC or library type unspecified. BUILD_SHARED_LIBS 66061da546Spatrick # does not control the kind of libraries created for LLDB, 67061da546Spatrick # only whether or not they link to shared/static LLVM/Clang 68061da546Spatrick # libraries. 69061da546Spatrick set(libkind STATIC) 70061da546Spatrick endif() 71061da546Spatrick 72061da546Spatrick #PIC not needed on Win 73061da546Spatrick # FIXME: Setting CMAKE_CXX_FLAGS here is a no-op, use target_compile_options 74061da546Spatrick # or omit this logic instead. 75061da546Spatrick if (NOT WIN32) 76061da546Spatrick set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") 77061da546Spatrick endif() 78061da546Spatrick 79061da546Spatrick if (PARAM_OBJECT) 80061da546Spatrick add_library(${name} ${libkind} ${srcs}) 81061da546Spatrick else() 82061da546Spatrick if(PARAM_ENTITLEMENTS) 83061da546Spatrick set(pass_ENTITLEMENTS ENTITLEMENTS ${PARAM_ENTITLEMENTS}) 84061da546Spatrick endif() 85061da546Spatrick 86061da546Spatrick if(LLDB_NO_INSTALL_DEFAULT_RPATH) 87061da546Spatrick set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH) 88061da546Spatrick endif() 89061da546Spatrick 90061da546Spatrick llvm_add_library(${name} ${libkind} ${srcs} 91061da546Spatrick LINK_LIBS ${PARAM_LINK_LIBS} 92061da546Spatrick DEPENDS ${PARAM_DEPENDS} 93061da546Spatrick ${pass_ENTITLEMENTS} 94061da546Spatrick ${pass_NO_INSTALL_RPATH} 95061da546Spatrick ) 96061da546Spatrick 97061da546Spatrick if(CLANG_LINK_CLANG_DYLIB) 98061da546Spatrick target_link_libraries(${name} PRIVATE clang-cpp) 99061da546Spatrick else() 100061da546Spatrick target_link_libraries(${name} PRIVATE ${PARAM_CLANG_LIBS}) 101061da546Spatrick endif() 102061da546Spatrick endif() 103061da546Spatrick 104061da546Spatrick # A target cannot be changed to a FRAMEWORK after calling install() because 105061da546Spatrick # this may result in the wrong install DESTINATION. The FRAMEWORK property 106061da546Spatrick # must be set earlier. 107061da546Spatrick if(PARAM_FRAMEWORK) 108*f6aab3d8Srobert set_target_properties(${name} PROPERTIES FRAMEWORK ON) 109061da546Spatrick endif() 110061da546Spatrick 111061da546Spatrick if(PARAM_SHARED) 112061da546Spatrick set(install_dest lib${LLVM_LIBDIR_SUFFIX}) 113061da546Spatrick if(PARAM_INSTALL_PREFIX) 114061da546Spatrick set(install_dest ${PARAM_INSTALL_PREFIX}) 115061da546Spatrick endif() 116061da546Spatrick # RUNTIME is relevant for DLL platforms, FRAMEWORK for macOS 117061da546Spatrick install(TARGETS ${name} COMPONENT ${name} 118*f6aab3d8Srobert RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" 119061da546Spatrick LIBRARY DESTINATION ${install_dest} 120061da546Spatrick ARCHIVE DESTINATION ${install_dest} 121061da546Spatrick FRAMEWORK DESTINATION ${install_dest}) 122061da546Spatrick if (NOT CMAKE_CONFIGURATION_TYPES) 123061da546Spatrick add_llvm_install_targets(install-${name} 124061da546Spatrick DEPENDS ${name} 125061da546Spatrick COMPONENT ${name}) 126061da546Spatrick endif() 127061da546Spatrick endif() 128061da546Spatrick 129061da546Spatrick # Hack: only some LLDB libraries depend on the clang autogenerated headers, 130061da546Spatrick # but it is simple enough to make all of LLDB depend on some of those 131061da546Spatrick # headers without negatively impacting much of anything. 132061da546Spatrick if(NOT LLDB_BUILT_STANDALONE) 133061da546Spatrick add_dependencies(${name} clang-tablegen-targets) 134061da546Spatrick endif() 135061da546Spatrick 136061da546Spatrick # Add in any extra C++ compilation flags for this library. 137061da546Spatrick target_compile_options(${name} PRIVATE ${PARAM_EXTRA_CXXFLAGS}) 138061da546Spatrick 139061da546Spatrick if(PARAM_PLUGIN) 140061da546Spatrick get_property(parent_dir DIRECTORY PROPERTY PARENT_DIRECTORY) 141061da546Spatrick if(EXISTS ${parent_dir}) 142061da546Spatrick get_filename_component(category ${parent_dir} NAME) 143061da546Spatrick set_target_properties(${name} PROPERTIES FOLDER "lldb plugins/${category}") 144061da546Spatrick endif() 145061da546Spatrick else() 146061da546Spatrick set_target_properties(${name} PROPERTIES FOLDER "lldb libraries") 147061da546Spatrick endif() 148061da546Spatrickendfunction(add_lldb_library) 149061da546Spatrick 150061da546Spatrickfunction(add_lldb_executable name) 151061da546Spatrick cmake_parse_arguments(ARG 152061da546Spatrick "GENERATE_INSTALL" 153061da546Spatrick "INSTALL_PREFIX;ENTITLEMENTS" 154061da546Spatrick "LINK_LIBS;CLANG_LIBS;LINK_COMPONENTS;BUILD_RPATH;INSTALL_RPATH" 155061da546Spatrick ${ARGN} 156061da546Spatrick ) 157061da546Spatrick 158061da546Spatrick if(ARG_ENTITLEMENTS) 159061da546Spatrick set(pass_ENTITLEMENTS ENTITLEMENTS ${ARG_ENTITLEMENTS}) 160061da546Spatrick endif() 161061da546Spatrick 162061da546Spatrick if(LLDB_NO_INSTALL_DEFAULT_RPATH) 163061da546Spatrick set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH) 164061da546Spatrick endif() 165061da546Spatrick 166061da546Spatrick list(APPEND LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS}) 167061da546Spatrick add_llvm_executable(${name} 168061da546Spatrick ${pass_ENTITLEMENTS} 169061da546Spatrick ${pass_NO_INSTALL_RPATH} 170061da546Spatrick ${ARG_UNPARSED_ARGUMENTS} 171061da546Spatrick ) 172061da546Spatrick 173061da546Spatrick target_link_libraries(${name} PRIVATE ${ARG_LINK_LIBS}) 174061da546Spatrick if(CLANG_LINK_CLANG_DYLIB) 175061da546Spatrick target_link_libraries(${name} PRIVATE clang-cpp) 176061da546Spatrick else() 177061da546Spatrick target_link_libraries(${name} PRIVATE ${ARG_CLANG_LIBS}) 178061da546Spatrick endif() 179061da546Spatrick set_target_properties(${name} PROPERTIES FOLDER "lldb executables") 180061da546Spatrick 181061da546Spatrick if (ARG_BUILD_RPATH) 182061da546Spatrick set_target_properties(${name} PROPERTIES BUILD_RPATH "${ARG_BUILD_RPATH}") 183061da546Spatrick endif() 184061da546Spatrick 185061da546Spatrick if (ARG_INSTALL_RPATH) 186061da546Spatrick set_target_properties(${name} PROPERTIES 187061da546Spatrick BUILD_WITH_INSTALL_RPATH OFF 188061da546Spatrick INSTALL_RPATH "${ARG_INSTALL_RPATH}") 189061da546Spatrick endif() 190061da546Spatrick 191061da546Spatrick if(ARG_GENERATE_INSTALL) 192061da546Spatrick set(install_dest bin) 193061da546Spatrick if(ARG_INSTALL_PREFIX) 194061da546Spatrick set(install_dest ${ARG_INSTALL_PREFIX}) 195061da546Spatrick endif() 196061da546Spatrick install(TARGETS ${name} COMPONENT ${name} 197061da546Spatrick RUNTIME DESTINATION ${install_dest} 198061da546Spatrick LIBRARY DESTINATION ${install_dest} 199061da546Spatrick BUNDLE DESTINATION ${install_dest} 200061da546Spatrick FRAMEWORK DESTINATION ${install_dest}) 201061da546Spatrick if (NOT CMAKE_CONFIGURATION_TYPES) 202061da546Spatrick add_llvm_install_targets(install-${name} 203061da546Spatrick DEPENDS ${name} 204061da546Spatrick COMPONENT ${name}) 205061da546Spatrick endif() 206061da546Spatrick if(APPLE AND ARG_INSTALL_PREFIX) 207061da546Spatrick lldb_add_post_install_steps_darwin(${name} ${ARG_INSTALL_PREFIX}) 208061da546Spatrick endif() 209061da546Spatrick endif() 210061da546Spatrickendfunction() 211061da546Spatrick 212061da546Spatrick 213061da546Spatrickmacro(add_lldb_tool_subdirectory name) 214061da546Spatrick add_llvm_subdirectory(LLDB TOOL ${name}) 215061da546Spatrickendmacro() 216061da546Spatrick 217061da546Spatrickfunction(add_lldb_tool name) 218061da546Spatrick cmake_parse_arguments(ARG "ADD_TO_FRAMEWORK" "" "" ${ARGN}) 219061da546Spatrick if(LLDB_BUILD_FRAMEWORK AND ARG_ADD_TO_FRAMEWORK) 220061da546Spatrick set(subdir LLDB.framework/Versions/${LLDB_FRAMEWORK_VERSION}/Resources) 221061da546Spatrick add_lldb_executable(${name} 222061da546Spatrick GENERATE_INSTALL 223061da546Spatrick INSTALL_PREFIX ${LLDB_FRAMEWORK_INSTALL_DIR}/${subdir} 224061da546Spatrick ${ARG_UNPARSED_ARGUMENTS} 225061da546Spatrick ) 226061da546Spatrick lldb_add_to_buildtree_lldb_framework(${name} ${subdir}) 227061da546Spatrick return() 228061da546Spatrick endif() 229061da546Spatrick 230061da546Spatrick add_lldb_executable(${name} GENERATE_INSTALL ${ARG_UNPARSED_ARGUMENTS}) 231061da546Spatrickendfunction() 232061da546Spatrick 233061da546Spatrick# The test suite relies on finding LLDB.framework binary resources in the 234061da546Spatrick# build-tree. Remove them before installing to avoid collisions with their 235061da546Spatrick# own install targets. 236061da546Spatrickfunction(lldb_add_to_buildtree_lldb_framework name subdir) 237061da546Spatrick # Destination for the copy in the build-tree. While the framework target may 238061da546Spatrick # not exist yet, it will exist when the generator expression gets expanded. 239061da546Spatrick set(copy_dest "${LLDB_FRAMEWORK_ABSOLUTE_BUILD_DIR}/${subdir}/$<TARGET_FILE_NAME:${name}>") 240061da546Spatrick 241061da546Spatrick # Copy into the given subdirectory for testing. 242061da546Spatrick add_custom_command(TARGET ${name} POST_BUILD 243061da546Spatrick COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${name}> ${copy_dest} 244061da546Spatrick COMMENT "Copy ${name} to ${copy_dest}" 245061da546Spatrick ) 246*f6aab3d8Srobert 247*f6aab3d8Srobert # Create a custom target to remove the copy again from LLDB.framework in the 248*f6aab3d8Srobert # build tree. 249*f6aab3d8Srobert # Intentionally use remove_directory because the target can be a either a 250*f6aab3d8Srobert # file or directory and using remove_directory is harmless for files. 251*f6aab3d8Srobert add_custom_target(${name}-cleanup 252*f6aab3d8Srobert COMMAND ${CMAKE_COMMAND} -E remove_directory ${copy_dest} 253*f6aab3d8Srobert COMMENT "Removing ${name} from LLDB.framework") 254*f6aab3d8Srobert add_dependencies(lldb-framework-cleanup 255*f6aab3d8Srobert ${name}-cleanup) 256061da546Spatrickendfunction() 257061da546Spatrick 258061da546Spatrick# Add extra install steps for dSYM creation and stripping for the given target. 259061da546Spatrickfunction(lldb_add_post_install_steps_darwin name install_prefix) 260061da546Spatrick if(NOT APPLE) 261061da546Spatrick message(WARNING "Darwin-specific functionality; not currently available on non-Apple platforms.") 262061da546Spatrick return() 263061da546Spatrick endif() 264061da546Spatrick 265061da546Spatrick get_target_property(output_name ${name} OUTPUT_NAME) 266061da546Spatrick if(NOT output_name) 267061da546Spatrick set(output_name ${name}) 268061da546Spatrick endif() 269061da546Spatrick 270061da546Spatrick get_target_property(is_framework ${name} FRAMEWORK) 271061da546Spatrick if(is_framework) 272061da546Spatrick get_target_property(buildtree_dir ${name} LIBRARY_OUTPUT_DIRECTORY) 273061da546Spatrick if(buildtree_dir) 274061da546Spatrick set(bundle_subdir ${output_name}.framework/Versions/${LLDB_FRAMEWORK_VERSION}/) 275061da546Spatrick else() 276061da546Spatrick message(SEND_ERROR "Framework target ${name} missing property for output directory. Cannot generate post-install steps.") 277061da546Spatrick return() 278061da546Spatrick endif() 279061da546Spatrick else() 280061da546Spatrick get_target_property(target_type ${name} TYPE) 281061da546Spatrick if(target_type STREQUAL "EXECUTABLE") 282061da546Spatrick set(buildtree_dir ${LLVM_RUNTIME_OUTPUT_INTDIR}) 283061da546Spatrick else() 284061da546Spatrick # Only ever install shared libraries. 285061da546Spatrick set(output_name "lib${output_name}.dylib") 286061da546Spatrick set(buildtree_dir ${LLVM_LIBRARY_OUTPUT_INTDIR}) 287061da546Spatrick endif() 288061da546Spatrick endif() 289061da546Spatrick 290061da546Spatrick # Generate dSYM 291be691f3bSpatrick if(NOT LLDB_SKIP_DSYM) 292061da546Spatrick set(dsym_name ${output_name}.dSYM) 293061da546Spatrick if(is_framework) 294061da546Spatrick set(dsym_name ${output_name}.framework.dSYM) 295061da546Spatrick endif() 296061da546Spatrick if(LLDB_DEBUGINFO_INSTALL_PREFIX) 297061da546Spatrick # This makes the path absolute, so we must respect DESTDIR. 298061da546Spatrick set(dsym_name "\$ENV\{DESTDIR\}${LLDB_DEBUGINFO_INSTALL_PREFIX}/${dsym_name}") 299061da546Spatrick endif() 300061da546Spatrick 301061da546Spatrick set(buildtree_name ${buildtree_dir}/${bundle_subdir}${output_name}) 302061da546Spatrick install(CODE "message(STATUS \"Externalize debuginfo: ${dsym_name}\")" COMPONENT ${name}) 303061da546Spatrick install(CODE "execute_process(COMMAND xcrun dsymutil -o=${dsym_name} ${buildtree_name})" 304061da546Spatrick COMPONENT ${name}) 305be691f3bSpatrick endif() 306061da546Spatrick 307061da546Spatrick if(NOT LLDB_SKIP_STRIP) 308061da546Spatrick # Strip distribution binary with -ST (removing debug symbol table entries and 309061da546Spatrick # Swift symbols). Avoid CMAKE_INSTALL_DO_STRIP and llvm_externalize_debuginfo() 310061da546Spatrick # as they can't be configured sufficiently. 311061da546Spatrick set(installtree_name "\$ENV\{DESTDIR\}${install_prefix}/${bundle_subdir}${output_name}") 312061da546Spatrick install(CODE "message(STATUS \"Stripping: ${installtree_name}\")" COMPONENT ${name}) 313061da546Spatrick install(CODE "execute_process(COMMAND xcrun strip -ST ${installtree_name})" 314061da546Spatrick COMPONENT ${name}) 315061da546Spatrick endif() 316061da546Spatrickendfunction() 317061da546Spatrick 318061da546Spatrick# CMake's set_target_properties() doesn't allow to pass lists for RPATH 319061da546Spatrick# properties directly (error: "called with incorrect number of arguments"). 320061da546Spatrick# Instead of defining two list variables each time, use this helper function. 321061da546Spatrickfunction(lldb_setup_rpaths name) 322061da546Spatrick cmake_parse_arguments(LIST "" "" "BUILD_RPATH;INSTALL_RPATH" ${ARGN}) 323061da546Spatrick set_target_properties(${name} PROPERTIES 324061da546Spatrick BUILD_WITH_INSTALL_RPATH OFF 325061da546Spatrick BUILD_RPATH "${LIST_BUILD_RPATH}" 326061da546Spatrick INSTALL_RPATH "${LIST_INSTALL_RPATH}" 327061da546Spatrick ) 328061da546Spatrickendfunction() 329061da546Spatrick 330061da546Spatrickfunction(lldb_find_system_debugserver path) 331061da546Spatrick execute_process(COMMAND xcode-select -p 332061da546Spatrick RESULT_VARIABLE exit_code 333061da546Spatrick OUTPUT_VARIABLE xcode_dev_dir 334061da546Spatrick ERROR_VARIABLE error_msg 335061da546Spatrick OUTPUT_STRIP_TRAILING_WHITESPACE) 336061da546Spatrick if(exit_code) 337061da546Spatrick message(WARNING "`xcode-select -p` failed:\n${error_msg}") 338061da546Spatrick else() 339061da546Spatrick set(subpath "LLDB.framework/Resources/debugserver") 340061da546Spatrick set(path_shared "${xcode_dev_dir}/../SharedFrameworks/${subpath}") 341061da546Spatrick set(path_private "${xcode_dev_dir}/Library/PrivateFrameworks/${subpath}") 342061da546Spatrick 343061da546Spatrick if(EXISTS ${path_shared}) 344061da546Spatrick set(${path} ${path_shared} PARENT_SCOPE) 345061da546Spatrick elseif(EXISTS ${path_private}) 346061da546Spatrick set(${path} ${path_private} PARENT_SCOPE) 347061da546Spatrick else() 348061da546Spatrick message(WARNING "System debugserver requested, but not found. " 349061da546Spatrick "Candidates don't exist: ${path_shared}\n${path_private}") 350061da546Spatrick endif() 351061da546Spatrick endif() 352061da546Spatrickendfunction() 353dda28197Spatrick 354dda28197Spatrick# Removes all module flags from the current CMAKE_CXX_FLAGS. Used for 355dda28197Spatrick# the Objective-C++ code in lldb which we don't want to build with modules. 356dda28197Spatrick# Reasons for this are that modules with Objective-C++ would require that 357dda28197Spatrick# all LLVM/Clang modules are Objective-C++ compatible (which they are likely 358dda28197Spatrick# not) and we would have rebuild a second set of modules just for the few 359dda28197Spatrick# Objective-C++ files in lldb (which slows down the build process). 360dda28197Spatrickmacro(remove_module_flags) 361dda28197Spatrick string(REGEX REPLACE "-fmodules-cache-path=[^ ]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 362dda28197Spatrick string(REGEX REPLACE "-fmodules-local-submodule-visibility" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 363dda28197Spatrick string(REGEX REPLACE "-fmodules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 364dda28197Spatrick string(REGEX REPLACE "-gmodules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 365dda28197Spatrick string(REGEX REPLACE "-fcxx-modules" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 366dda28197Spatrickendmacro() 367