1include(CheckCXXCompilerFlag)
2include(CheckLibraryExists)
3include_directories(${CMAKE_CURRENT_BINARY_DIR}/..)
4include_directories(${LLDB_SOURCE_DIR}/source)
5include_directories(MacOSX/DarwinLog)
6
7include_directories(MacOSX)
8#include_directories(${CMAKE_CURRENT_BINARY_DIR}/MacOSX)
9
10set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -Wl,-sectcreate,__TEXT,__info_plist,${CMAKE_CURRENT_SOURCE_DIR}/../resources/lldb-debugserver-Info.plist")
11
12check_cxx_compiler_flag("-Wno-gnu-zero-variadic-macro-arguments"
13                        CXX_SUPPORTS_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS)
14if (CXX_SUPPORTS_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS)
15  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-gnu-zero-variadic-macro-arguments")
16endif ()
17
18check_cxx_compiler_flag("-Wno-zero-length-array"
19                        CXX_SUPPORTS_NO_ZERO_LENGTH_ARRAY)
20if (CXX_SUPPORTS_NO_ZERO_LENGTH_ARRAY)
21  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-zero-length-array")
22endif ()
23
24check_cxx_compiler_flag("-Wno-extended-offsetof"
25                        CXX_SUPPORTS_NO_EXTENDED_OFFSETOF)
26if (CXX_SUPPORTS_NO_EXTENDED_OFFSETOF)
27  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-extended-offsetof")
28endif ()
29
30check_library_exists(compression compression_encode_buffer "" HAVE_LIBCOMPRESSION)
31
32add_subdirectory(MacOSX)
33
34set(generated_mach_interfaces
35  ${CMAKE_CURRENT_BINARY_DIR}/mach_exc.h
36  ${CMAKE_CURRENT_BINARY_DIR}/mach_excServer.c
37  ${CMAKE_CURRENT_BINARY_DIR}/mach_excUser.c
38  )
39add_custom_command(OUTPUT ${generated_mach_interfaces}
40  COMMAND mig ${CMAKE_CURRENT_SOURCE_DIR}/MacOSX/dbgnub-mig.defs
41  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/MacOSX/dbgnub-mig.defs
42  )
43
44set(DEBUGSERVER_VERS_GENERATED_FILE ${CMAKE_CURRENT_BINARY_DIR}/debugserver_vers.c)
45set_source_files_properties(${DEBUGSERVER_VERS_GENERATED_FILE} PROPERTIES GENERATED 1)
46
47add_custom_command(OUTPUT ${DEBUGSERVER_VERS_GENERATED_FILE}
48  COMMAND ${LLDB_SOURCE_DIR}/scripts/generate-vers.pl
49          ${LLDB_SOURCE_DIR}/lldb.xcodeproj/project.pbxproj debugserver
50          > ${DEBUGSERVER_VERS_GENERATED_FILE}
51  DEPENDS ${LLDB_SOURCE_DIR}/scripts/generate-vers.pl
52          ${LLDB_SOURCE_DIR}/lldb.xcodeproj/project.pbxproj
53  )
54
55set(lldbDebugserverCommonSources
56  DNBArch.cpp
57  DNBBreakpoint.cpp
58  DNB.cpp
59  DNBDataRef.cpp
60  DNBError.cpp
61  DNBLog.cpp
62  DNBRegisterInfo.cpp
63  DNBThreadResumeActions.cpp
64  JSON.cpp
65  StdStringExtractor.cpp
66  # JSON reader depends on the following LLDB-common files
67  ${LLDB_SOURCE_DIR}/source/Host/common/StringConvert.cpp
68  ${LLDB_SOURCE_DIR}/source/Host/common/SocketAddress.cpp
69  # end JSON reader dependencies
70  libdebugserver.cpp
71  PseudoTerminal.cpp
72  PThreadEvent.cpp
73  PThreadMutex.cpp
74  RNBContext.cpp
75  RNBRemote.cpp
76  RNBServices.cpp
77  RNBSocket.cpp
78  SysSignal.cpp
79  TTYState.cpp
80
81  MacOSX/CFBundle.cpp
82  MacOSX/CFString.cpp
83  MacOSX/Genealogy.cpp
84  MacOSX/MachException.cpp
85  MacOSX/MachProcess.mm
86  MacOSX/MachTask.mm
87  MacOSX/MachThread.cpp
88  MacOSX/MachThreadList.cpp
89  MacOSX/MachVMMemory.cpp
90  MacOSX/MachVMRegion.cpp
91  MacOSX/OsLogger.cpp
92  ${generated_mach_interfaces}
93  ${DEBUGSERVER_VERS_GENERATED_FILE})
94
95add_library(lldbDebugserverCommon ${lldbDebugserverCommonSources})
96
97# LLDB-specific identity, currently used for code signing debugserver.
98set(LLDB_CODESIGN_IDENTITY "" CACHE STRING
99    "Override code sign identity for debugserver and for use in tests; falls back to LLVM_CODESIGNING_IDENTITY if set or lldb_codesign otherwise (Darwin only)")
100
101# Determine which identity to use and store it in the separate cache entry.
102# We will query it later for LLDB_TEST_COMMON_ARGS.
103if(LLDB_CODESIGN_IDENTITY)
104  set(LLDB_CODESIGN_IDENTITY_USED ${LLDB_CODESIGN_IDENTITY} CACHE INTERNAL "" FORCE)
105elseif(LLVM_CODESIGNING_IDENTITY)
106  set(LLDB_CODESIGN_IDENTITY_USED ${LLVM_CODESIGNING_IDENTITY} CACHE INTERNAL "" FORCE)
107else()
108  set(LLDB_CODESIGN_IDENTITY_USED lldb_codesign CACHE INTERNAL "" FORCE)
109endif()
110
111# Override locally, so the identity is used for targets created in this scope.
112set(LLVM_CODESIGNING_IDENTITY ${LLDB_CODESIGN_IDENTITY_USED})
113
114option(LLDB_NO_DEBUGSERVER "Disable the debugserver target" OFF)
115option(LLDB_USE_SYSTEM_DEBUGSERVER "Use the system's debugserver instead of building it from source (Darwin only)." OFF)
116
117# Incompatible options
118if(LLDB_NO_DEBUGSERVER AND LLDB_USE_SYSTEM_DEBUGSERVER)
119  message(FATAL_ERROR "Inconsistent options: LLDB_NO_DEBUGSERVER and LLDB_USE_SYSTEM_DEBUGSERVER")
120endif()
121
122# Try to locate the system debugserver.
123# Subsequent feasibility checks depend on it.
124if(APPLE AND CMAKE_HOST_APPLE)
125  execute_process(
126    COMMAND xcode-select -p
127    OUTPUT_VARIABLE xcode_dev_dir)
128  string(STRIP ${xcode_dev_dir} xcode_dev_dir)
129
130  set(debugserver_rel_path "LLDB.framework/Resources/debugserver")
131  set(debugserver_shared "${xcode_dev_dir}/../SharedFrameworks/${debugserver_rel_path}")
132  set(debugserver_private "${xcode_dev_dir}/Library/PrivateFrameworks/${debugserver_rel_path}")
133
134  if(EXISTS ${debugserver_shared})
135    set(system_debugserver ${debugserver_shared})
136  elseif(EXISTS ${debugserver_private})
137    set(system_debugserver ${debugserver_private})
138  endif()
139endif()
140
141# Handle unavailability
142if(LLDB_USE_SYSTEM_DEBUGSERVER)
143  if(system_debugserver)
144    set(use_system_debugserver ON)
145  elseif(APPLE AND CMAKE_HOST_APPLE)
146    # Binary not found on system. Keep cached variable, to try again on reconfigure.
147    message(SEND_ERROR
148      "LLDB_USE_SYSTEM_DEBUGSERVER option set, but no debugserver found in:\
149        ${debugserver_shared}\
150        ${debugserver_private}")
151  else()
152    # Non-Apple target platform or non-Darwin host. Reset invalid cached variable.
153    message(WARNING "Reverting invalid option LLDB_USE_SYSTEM_DEBUGSERVER (Darwin only)")
154    set(LLDB_USE_SYSTEM_DEBUGSERVER OFF CACHE BOOL "" FORCE)
155  endif()
156elseif(NOT LLDB_NO_DEBUGSERVER)
157  # Default case: on Darwin we need the right code signing ID.
158  # See lldb/docs/code-signing.txt for details.
159  if(CMAKE_HOST_APPLE AND NOT LLVM_CODESIGNING_IDENTITY STREQUAL "lldb_codesign")
160    set(problem "Cannot code sign debugserver with LLVM_CODESIGNING_IDENTITY '${LLVM_CODESIGNING_IDENTITY}'.")
161    set(advice "Pass -DLLDB_CODESIGN_IDENTITY=lldb_codesign to override the LLVM value for debugserver.")
162    if(system_debugserver)
163      set(effect "Will fall back to system's debugserver.")
164      set(use_system_debugserver ON)
165    else()
166      set(effect "debugserver will not be available.")
167    endif()
168    message(WARNING "${problem} ${effect} ${advice}")
169  else()
170    set(build_and_sign_debugserver ON)
171  endif()
172endif()
173
174# TODO: We don't use the $<TARGET_FILE:debugserver> generator expression here,
175# because the value of DEBUGSERVER_PATH is used to build LLDB_DOTEST_ARGS,
176# which is used for configuring lldb-dotest.in, which does not have a generator
177# step at the moment.
178set(default_debugserver_path "${LLVM_RUNTIME_OUTPUT_INTDIR}/debugserver${CMAKE_EXECUTABLE_SUFFIX}")
179
180# Remember where debugserver binary goes and whether or not we have to test it.
181set(DEBUGSERVER_PATH "" CACHE FILEPATH "Path to debugserver")
182set(SKIP_TEST_DEBUGSERVER OFF CACHE BOOL "Building the in-tree debugserver was skipped")
183
184# Reset values in all cases in order to correctly support reconfigurations.
185if(use_system_debugserver)
186  add_custom_target(debugserver
187    COMMAND ${CMAKE_COMMAND} -E copy_if_different
188            ${system_debugserver} ${LLVM_RUNTIME_OUTPUT_INTDIR}
189    COMMENT "Copying the system debugserver to LLDB's binaries directory.")
190
191  # Don't test debugserver itself.
192  # Tests that require debugserver will use the copy.
193  set(DEBUGSERVER_PATH ${default_debugserver_path} CACHE FILEPATH "" FORCE)
194  set(SKIP_TEST_DEBUGSERVER ON CACHE BOOL "" FORCE)
195
196  message(STATUS "Copy system debugserver from: ${system_debugserver}")
197elseif(build_and_sign_debugserver)
198  # Build, sign and test debugserver (below)
199  set(DEBUGSERVER_PATH ${default_debugserver_path} CACHE FILEPATH "" FORCE)
200  set(SKIP_TEST_DEBUGSERVER OFF CACHE BOOL "" FORCE)
201
202  message(STATUS "lldb debugserver: ${DEBUGSERVER_PATH}")
203else()
204  # No tests for debugserver, no tests that require it.
205  set(DEBUGSERVER_PATH "" CACHE FILEPATH "" FORCE)
206  set(SKIP_TEST_DEBUGSERVER ON CACHE BOOL "" FORCE)
207
208  message(STATUS "lldb debugserver will not be available.")
209endif()
210
211if(APPLE)
212  if(IOS)
213    find_library(BACKBOARD_LIBRARY BackBoardServices
214      PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)
215    find_library(FRONTBOARD_LIBRARY FrontBoardServices
216      PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)
217    find_library(SPRINGBOARD_LIBRARY SpringBoardServices
218      PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)
219    find_library(MOBILESERVICES_LIBRARY MobileCoreServices
220      PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)
221    find_library(LOCKDOWN_LIBRARY lockdown)
222
223    if(NOT BACKBOARD_LIBRARY)
224      set(SKIP_TEST_DEBUGSERVER ON CACHE BOOL "" FORCE)
225    endif()
226  else()
227    find_library(COCOA_LIBRARY Cocoa)
228  endif()
229endif()
230
231if(HAVE_LIBCOMPRESSION)
232  set(LIBCOMPRESSION compression)
233endif()
234
235if(LLDB_USE_ENTITLEMENTS)
236  if(IOS)
237    set(entitlements ${CMAKE_CURRENT_SOURCE_DIR}/debugserver-entitlements.plist)
238  else()
239    # Same entitlements file as used for lldb-server
240    set(entitlements ${LLDB_SOURCE_DIR}/resources/debugserver-macosx-entitlements.plist)
241  endif()
242endif()
243
244if(build_and_sign_debugserver)
245  target_link_libraries(lldbDebugserverCommon
246                        INTERFACE ${COCOA_LIBRARY}
247                        ${CORE_FOUNDATION_LIBRARY}
248                        ${FOUNDATION_LIBRARY}
249                        ${BACKBOARD_LIBRARY}
250                        ${FRONTBOARD_LIBRARY}
251                        ${SPRINGBOARD_LIBRARY}
252                        ${MOBILESERVICES_LIBRARY}
253                        ${LOCKDOWN_LIBRARY}
254                        lldbDebugserverArchSupport
255                        lldbDebugserverDarwin_DarwinLog
256                        ${LIBCOMPRESSION})
257  if(HAVE_LIBCOMPRESSION)
258    set_property(TARGET lldbDebugserverCommon APPEND PROPERTY
259                 COMPILE_DEFINITIONS HAVE_LIBCOMPRESSION)
260  endif()
261  set(LLVM_OPTIONAL_SOURCES ${lldbDebugserverCommonSources})
262  add_lldb_tool(debugserver
263    debugserver.cpp
264
265    LINK_LIBS
266      lldbDebugserverCommon
267
268    ENTITLEMENTS
269      ${entitlements}
270    )
271  if(IOS)
272    set_property(TARGET lldbDebugserverCommon APPEND PROPERTY COMPILE_DEFINITIONS
273      WITH_LOCKDOWN
274      WITH_FBS
275      WITH_BKS
276      )
277    set_property(TARGET debugserver APPEND PROPERTY COMPILE_DEFINITIONS
278      WITH_LOCKDOWN
279      WITH_FBS
280      WITH_BKS
281      )
282    set_property(TARGET lldbDebugserverCommon APPEND PROPERTY COMPILE_FLAGS
283      -F${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks
284      )
285  endif()
286endif()
287
288if(IOS)
289  add_library(lldbDebugserverCommon_NonUI ${lldbDebugserverCommonSources})
290  target_link_libraries(lldbDebugserverCommon_NonUI
291                      INTERFACE ${COCOA_LIBRARY}
292                      ${CORE_FOUNDATION_LIBRARY}
293                      ${FOUNDATION_LIBRARY}
294                      lldbDebugserverArchSupport
295                      lldbDebugserverDarwin_DarwinLog
296                      ${LIBCOMPRESSION})
297  if(HAVE_LIBCOMPRESSION)
298    set_property(TARGET lldbDebugserverCommon_NonUI APPEND PROPERTY
299                 COMPILE_DEFINITIONS HAVE_LIBCOMPRESSION)
300  endif()
301
302  add_lldb_tool(debugserver-nonui
303    debugserver.cpp
304
305    LINK_LIBS
306      lldbDebugserverCommon_NonUI
307
308    ENTITLEMENTS
309      ${entitlements}
310    )
311endif()
312