338 lines · plain
1include(CheckCXXCompilerFlag)2include(CheckLibraryExists)3include_directories(${CMAKE_CURRENT_BINARY_DIR}/..)4include_directories(${LLDB_SOURCE_DIR}/source)5include_directories(MacOSX)6include_directories(MacOSX/x86_64)7include_directories(MacOSX/arm64)8 9function(check_certificate identity result_valid)10 execute_process(11 COMMAND security find-certificate -Z -p -c ${identity} /Library/Keychains/System.keychain12 RESULT_VARIABLE exit_code OUTPUT_QUIET ERROR_QUIET)13 if(exit_code)14 set(${result_valid} FALSE PARENT_SCOPE)15 else()16 set(${result_valid} TRUE PARENT_SCOPE)17 endif()18endfunction()19 20function(get_debugserver_codesign_identity result)21 string(CONCAT not_found_help22 "This will cause failures in the test suite. "23 "Pass '-DLLDB_USE_SYSTEM_DEBUGSERVER=ON' to use the system one instead. "24 "See 'Code Signing on macOS' in the documentation."25 )26 27 # Explicit override: warn if unavailable28 if(LLDB_CODESIGN_IDENTITY)29 set(${result} ${LLDB_CODESIGN_IDENTITY} PARENT_SCOPE)30 check_certificate(${LLDB_CODESIGN_IDENTITY} available)31 if(NOT available)32 message(WARNING "LLDB_CODESIGN_IDENTITY not found: '${LLDB_CODESIGN_IDENTITY}' ${not_found_help}")33 endif()34 return()35 endif()36 37 # Development signing identity: use if available38 check_certificate(lldb_codesign available)39 if(available)40 set(${result} lldb_codesign PARENT_SCOPE)41 return()42 endif()43 44 message(WARNING "Development code sign identity not found: 'lldb_codesign' ${not_found_help}")45 46 # LLVM pendant: fallback if available47 if(LLVM_CODESIGNING_IDENTITY)48 check_certificate(${LLVM_CODESIGNING_IDENTITY} available)49 if(available)50 set(${result} ${LLVM_CODESIGNING_IDENTITY} PARENT_SCOPE)51 return()52 endif()53 endif()54 55 # Ad-hoc signing: last resort56 set(${result} "-" PARENT_SCOPE)57endfunction()58 59# debugserver does not depend on intrinsics_gen, or on llvm. Set the common60# llvm dependencies in the current scope to the empty set.61set(LLVM_COMMON_DEPENDS)62 63set(DEBUGSERVER_RESOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../resources")64set(DEBUGSERVER_INFO_PLIST "${DEBUGSERVER_RESOURCE_DIR}/lldb-debugserver-Info.plist")65 66set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -Wl,-sectcreate,__TEXT,__info_plist,${DEBUGSERVER_INFO_PLIST}")67 68check_cxx_compiler_flag("-Wno-gnu-zero-variadic-macro-arguments"69 CXX_SUPPORTS_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS)70if (CXX_SUPPORTS_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS)71 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-gnu-zero-variadic-macro-arguments")72endif ()73 74check_cxx_compiler_flag("-Wno-zero-length-array"75 CXX_SUPPORTS_NO_ZERO_LENGTH_ARRAY)76if (CXX_SUPPORTS_NO_ZERO_LENGTH_ARRAY)77 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-zero-length-array")78endif ()79 80check_cxx_compiler_flag("-Wno-extended-offsetof"81 CXX_SUPPORTS_NO_EXTENDED_OFFSETOF)82if (CXX_SUPPORTS_NO_EXTENDED_OFFSETOF)83 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-extended-offsetof")84endif ()85 86include(CheckCSourceCompiles)87check_c_source_compiles(88 "89 #include <TargetConditionals.h>90 #if TARGET_CPU_ARM6491 #if TARGET_OS_OSX92 #warning Building for macOS93 #else94 #error Not building for macOS95 #endif96 #else97 #error Not building for ARM6498 #endif99 int main(void) { return 0; }100 "101 BUILDING_FOR_ARM64_OSX102)103 104# You can only debug arm64e processes using an arm64e debugserver.105option(LLDB_ENABLE_ARM64E_DEBUGSERVER "Build debugserver for arm64 and arm64e" OFF)106if (BUILDING_FOR_ARM64_OSX AND LLDB_ENABLE_ARM64E_DEBUGSERVER)107 set(CMAKE_OSX_ARCHITECTURES "arm64;arm64e")108endif ()109 110find_library(SECURITY_LIBRARY Security)111 112set(LLDB_CODESIGN_IDENTITY "" CACHE STRING113 "Identity override for debugserver; see 'Code Signing on macOS' in the documentation (Darwin only)")114 115get_debugserver_codesign_identity(debugserver_codesign_identity)116 117# Override locally, so the identity is used for targets created in this scope.118set(LLVM_CODESIGNING_IDENTITY ${debugserver_codesign_identity})119 120if(APPLE)121 set(LIBCOMPRESSION compression)122 if(APPLE_EMBEDDED)123 find_library(BACKBOARD_LIBRARY BackBoardServices124 PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)125 find_library(FRONTBOARD_LIBRARY FrontBoardServices126 PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)127 find_library(SPRINGBOARD_LIBRARY SpringBoardServices128 PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)129 find_library(MOBILESERVICES_LIBRARY MobileCoreServices130 PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)131 find_library(LOCKDOWN_LIBRARY lockdown)132 if (APPLE_EMBEDDED STREQUAL "watchos")133 find_library(CAROUSELSERVICES_LIBRARY CarouselServices134 PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)135 endif()136 137 if(NOT BACKBOARD_LIBRARY)138 set(SKIP_TEST_DEBUGSERVER ON CACHE BOOL "" FORCE)139 endif()140 endif()141endif()142 143if(LLDB_USE_ENTITLEMENTS)144 if(APPLE_EMBEDDED)145 set(entitlements ${DEBUGSERVER_RESOURCE_DIR}/debugserver-entitlements.plist)146 else()147 if (LLDB_USE_PRIVATE_ENTITLEMENTS)148 set(entitlements ${DEBUGSERVER_RESOURCE_DIR}/debugserver-macosx-private-entitlements.plist)149 else()150 set(entitlements ${DEBUGSERVER_RESOURCE_DIR}/debugserver-macosx-entitlements.plist)151 endif()152 endif()153endif()154 155add_definitions(-DLLDB_USE_OS_LOG)156 157# Make sure we have the macOS SDK root as mig needs it and will silently158# fail to generate its output files without it.159if(CMAKE_OSX_SYSROOT)160 set(MIG_SYSROOT ${CMAKE_OSX_SYSROOT})161else()162 execute_process(COMMAND xcrun --show-sdk-path163 OUTPUT_VARIABLE MIG_SYSROOT164 ERROR_QUIET165 OUTPUT_STRIP_TRAILING_WHITESPACE)166endif()167 168if(NOT MIG_SYSROOT)169 message(FATAL_ERROR "Unable to obtain sysroot required by mig (Mach Interface Generator). Set CMAKE_OSX_SYSROOT to explicitly specify a sysroot.")170endif()171 172if(${CMAKE_OSX_SYSROOT} MATCHES ".Internal.sdk$")173 message(STATUS "LLDB debugserver energy support is enabled")174 add_definitions(-DLLDB_ENERGY)175 set(ENERGY_LIBRARY -lpmenergy -lpmsample)176else()177 message(STATUS "LLDB debugserver energy support is disabled")178endif()179 180set(generated_mach_interfaces181 ${CMAKE_CURRENT_BINARY_DIR}/mach_exc.h182 ${CMAKE_CURRENT_BINARY_DIR}/mach_excServer.c183 ${CMAKE_CURRENT_BINARY_DIR}/mach_excUser.c184 )185 186set(MIG_ARCH_FLAGS "")187if (DEFINED MIG_ARCHS)188 foreach(ARCH ${MIG_ARCHS})189 set(MIG_ARCH_FLAGS "${MIG_ARCH_FLAGS} -arch ${ARCH}")190 endforeach()191endif()192separate_arguments(MIG_ARCH_FLAGS_SEPARTED NATIVE_COMMAND "${MIG_ARCH_FLAGS}")193 194add_custom_command(OUTPUT ${generated_mach_interfaces}195 VERBATIM COMMAND mig ${MIG_ARCH_FLAGS_SEPARTED} -isysroot ${MIG_SYSROOT} ${CMAKE_CURRENT_SOURCE_DIR}/MacOSX/dbgnub-mig.defs196 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/MacOSX/dbgnub-mig.defs197 )198 199set(DEBUGSERVER_VERS_GENERATED_FILE ${CMAKE_CURRENT_BINARY_DIR}/debugserver_vers.c)200configure_file(debugserver_vers.c.in201 ${DEBUGSERVER_VERS_GENERATED_FILE} @ONLY)202 203set(lldbDebugserverCommonSources204 DNBArch.cpp205 DNBBreakpoint.cpp206 DNB.cpp207 DNBDataRef.cpp208 DNBError.cpp209 DNBLog.cpp210 DNBRegisterInfo.cpp211 DNBThreadResumeActions.cpp212 JSON.cpp213 StdStringExtractor.cpp214 StringConvert.cpp215 # JSON reader depends on the following LLDB-common files216 ${LLDB_SOURCE_DIR}/source/Host/common/SocketAddress.cpp217 # end JSON reader dependencies218 libdebugserver.cpp219 PseudoTerminal.cpp220 PThreadEvent.cpp221 RNBContext.cpp222 RNBRemote.cpp223 RNBServices.cpp224 RNBSocket.cpp225 SysSignal.cpp226 TTYState.cpp227 228 MacOSX/CFBundle.cpp229 MacOSX/CFString.cpp230 MacOSX/Genealogy.cpp231 MacOSX/MachException.cpp232 MacOSX/MachProcess.mm233 MacOSX/MachTask.mm234 MacOSX/MachThread.cpp235 MacOSX/MachThreadList.cpp236 MacOSX/MachVMMemory.cpp237 MacOSX/MachVMRegion.cpp238 MacOSX/OsLogger.cpp239 MacOSX/arm64/DNBArchImplARM64.cpp240 MacOSX/x86_64/DNBArchImplX86_64.cpp241 ${generated_mach_interfaces}242 ${DEBUGSERVER_VERS_GENERATED_FILE})243 244# Tell LLVM not to complain about these source files.245set(LLVM_OPTIONAL_SOURCES246 ${lldbDebugserverCommonSources}247 debugserver.cpp)248 249add_lldb_library(lldbDebugserverCommon ${lldbDebugserverCommonSources})250set_target_properties(lldbDebugserverCommon PROPERTIES FOLDER "lldb libraries/debugserver")251 252target_link_libraries(lldbDebugserverCommon253 INTERFACE ${COCOA_LIBRARY}254 ${CORE_FOUNDATION_LIBRARY}255 ${FOUNDATION_LIBRARY}256 ${BACKBOARD_LIBRARY}257 ${FRONTBOARD_LIBRARY}258 ${SPRINGBOARD_LIBRARY}259 ${MOBILESERVICES_LIBRARY}260 ${LOCKDOWN_LIBRARY}261 ${CAROUSELSERVICES_LIBRARY}262 ${FOUNDATION_LIBRARY}263 ${SECURITY_LIBRARY}264 ${LIBCOMPRESSION}265 ${ENERGY_LIBRARY})266add_lldb_tool(debugserver ADD_TO_FRAMEWORK267 debugserver.cpp268 ENTITLEMENTS ${entitlements}269 LINK_LIBS lldbDebugserverCommon270)271 272# Workaround for Xcode-specific code-signing behavior:273# The XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY option causes debugserver to be copied274# into the framework first and code-signed afterwards. Sign the copy manually.275if (debugserver_codesign_identity AND LLDB_BUILD_FRAMEWORK AND276 CMAKE_GENERATOR STREQUAL "Xcode")277 if(NOT CMAKE_CODESIGN_ALLOCATE)278 execute_process(279 COMMAND xcrun -f codesign_allocate280 OUTPUT_STRIP_TRAILING_WHITESPACE281 OUTPUT_VARIABLE CMAKE_CODESIGN_ALLOCATE282 )283 endif()284 if(entitlements)285 set(pass_entitlements --entitlements ${entitlements})286 endif()287 288 set(copy_location ${LLDB_FRAMEWORK_ABSOLUTE_BUILD_DIR}/LLDB.framework/Versions/${LLDB_FRAMEWORK_VERSION}/Resources/debugserver)289 290 add_custom_command(TARGET debugserver POST_BUILD291 COMMAND ${CMAKE_COMMAND} -E292 env CODESIGN_ALLOCATE=${CMAKE_CODESIGN_ALLOCATE}293 xcrun codesign -f -s ${debugserver_codesign_identity}294 ${pass_entitlements} ${copy_location}295 COMMENT "Code-sign debugserver copy in the build-tree framework: ${copy_location}"296 )297endif()298 299set_target_properties(debugserver PROPERTIES FOLDER "lldb libraries/debugserver")300 301if(APPLE_EMBEDDED)302 set_property(TARGET lldbDebugserverCommon APPEND PROPERTY COMPILE_DEFINITIONS303 WITH_LOCKDOWN304 WITH_FBS305 WITH_BKS306 )307 if(CAROUSELSERVICES_LIBRARY)308 set_property(TARGET lldbDebugserverCommon APPEND PROPERTY COMPILE_DEFINITIONS309 WITH_CAROUSEL310 )311 endif()312 set_property(TARGET debugserver APPEND PROPERTY COMPILE_DEFINITIONS313 WITH_LOCKDOWN314 WITH_FBS315 WITH_BKS316 )317 set_property(TARGET lldbDebugserverCommon APPEND PROPERTY COMPILE_FLAGS318 -F${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks319 )320 321 add_lldb_library(lldbDebugserverCommon_NonUI ${lldbDebugserverCommonSources})322 target_link_libraries(lldbDebugserverCommon_NonUI323 INTERFACE ${COCOA_LIBRARY}324 ${CORE_FOUNDATION_LIBRARY}325 ${FOUNDATION_LIBRARY}326 ${SECURITY_LIBRARY}327 ${LIBCOMPRESSION})328 329 add_lldb_tool(debugserver-nonui330 debugserver.cpp331 332 ENTITLEMENTS333 ${entitlements}334 LINK_LIBS335 lldbDebugserverCommon_NonUI336 )337endif()338