[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / lldb / tools / debugserver / source / CMakeLists.txt
blob86ad62de6088a94f2b5d1f1871a3ebdacd0982d4
1 include(CheckCXXCompilerFlag)
2 include(CheckLibraryExists)
3 include_directories(${CMAKE_CURRENT_BINARY_DIR}/..)
4 include_directories(${LLDB_SOURCE_DIR}/source)
6 include_directories(MacOSX)
8 function(check_certificate identity result_valid)
9   execute_process(
10     COMMAND security find-certificate -Z -p -c ${identity} /Library/Keychains/System.keychain
11     RESULT_VARIABLE exit_code OUTPUT_QUIET ERROR_QUIET)
12   if(exit_code)
13     set(${result_valid} FALSE PARENT_SCOPE)
14   else()
15     set(${result_valid} TRUE PARENT_SCOPE)
16   endif()
17 endfunction()
19 function(get_debugserver_codesign_identity result)
20   string(CONCAT not_found_help
21     "This will cause failures in the test suite."
22     "Pass '-DLLDB_USE_SYSTEM_DEBUGSERVER=ON' to use the system one instead."
23     "See 'Code Signing on macOS' in the documentation."
24   )
26   # Explicit override: warn if unavailable
27   if(LLDB_CODESIGN_IDENTITY)
28     set(${result} ${LLDB_CODESIGN_IDENTITY} PARENT_SCOPE)
29     check_certificate(${LLDB_CODESIGN_IDENTITY} available)
30     if(NOT available)
31       message(WARNING "LLDB_CODESIGN_IDENTITY not found: '${LLDB_CODESIGN_IDENTITY}' ${not_found_help}")
32     endif()
33     return()
34   endif()
36   # Development signing identity: use if available
37   check_certificate(lldb_codesign available)
38   if(available)
39     set(${result} lldb_codesign PARENT_SCOPE)
40     return()
41   endif()
43   message(WARNING "Development code sign identity not found: 'lldb_codesign' ${not_found_help}")
45   # LLVM pendant: fallback if available
46   if(LLVM_CODESIGNING_IDENTITY)
47     check_certificate(${LLVM_CODESIGNING_IDENTITY} available)
48     if(available)
49       set(${result} ${LLVM_CODESIGNING_IDENTITY} PARENT_SCOPE)
50       return()
51     endif()
52   endif()
54   # Ad-hoc signing: last resort
55   set(${result} "-" PARENT_SCOPE)
56 endfunction()
58 # debugserver does not depend on intrinsics_gen, or on llvm. Set the common
59 # llvm dependencies in the current scope to the empty set.
60 set(LLVM_COMMON_DEPENDS)
62 set(DEBUGSERVER_RESOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../resources")
63 set(DEBUGSERVER_INFO_PLIST "${DEBUGSERVER_RESOURCE_DIR}/lldb-debugserver-Info.plist")
65 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -Wl,-sectcreate,__TEXT,__info_plist,${DEBUGSERVER_INFO_PLIST}")
67 check_cxx_compiler_flag("-Wno-gnu-zero-variadic-macro-arguments"
68                         CXX_SUPPORTS_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS)
69 if (CXX_SUPPORTS_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS)
70   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-gnu-zero-variadic-macro-arguments")
71 endif ()
73 check_cxx_compiler_flag("-Wno-zero-length-array"
74                         CXX_SUPPORTS_NO_ZERO_LENGTH_ARRAY)
75 if (CXX_SUPPORTS_NO_ZERO_LENGTH_ARRAY)
76   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-zero-length-array")
77 endif ()
79 check_cxx_compiler_flag("-Wno-extended-offsetof"
80                         CXX_SUPPORTS_NO_EXTENDED_OFFSETOF)
81 if (CXX_SUPPORTS_NO_EXTENDED_OFFSETOF)
82   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-extended-offsetof")
83 endif ()
85 include(CheckCSourceCompiles)
86 check_c_source_compiles(
87     "
88     #include <TargetConditionals.h>
89     #if TARGET_CPU_ARM64
90     #if TARGET_OS_OSX
91     #warning Building for macOS
92     #else
93     #error Not building for macOS
94     #endif
95     #else
96     #error Not building for ARM64
97     #endif
98     int main() { return 0; }
99     "
100     BUILDING_FOR_ARM64_OSX
103 # You can only debug arm64e processes using an arm64e debugserver.
104 option(LLDB_ENABLE_ARM64E_DEBUGSERVER "Build debugserver for arm64 and arm64e" OFF)
105 if (BUILDING_FOR_ARM64_OSX AND LLDB_ENABLE_ARM64E_DEBUGSERVER)
106   set(CMAKE_OSX_ARCHITECTURES "arm64;arm64e")
107 endif ()
109 check_library_exists(compression compression_encode_buffer "" HAVE_LIBCOMPRESSION)
111 find_library(SECURITY_LIBRARY Security)
113 add_subdirectory(MacOSX)
115 set(LLDB_CODESIGN_IDENTITY "" CACHE STRING
116     "Identity override for debugserver; see 'Code Signing on macOS' in the documentation (Darwin only)")
118 get_debugserver_codesign_identity(debugserver_codesign_identity)
120 # Override locally, so the identity is used for targets created in this scope.
121 set(LLVM_CODESIGNING_IDENTITY ${debugserver_codesign_identity})
123 # Use the same identity later in the test suite.
124 set_property(GLOBAL PROPERTY
125   LLDB_DEBUGSERVER_CODESIGN_IDENTITY ${debugserver_codesign_identity})
127 if(APPLE)
128   if(APPLE_EMBEDDED)
129     find_library(BACKBOARD_LIBRARY BackBoardServices
130       PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)
131     find_library(FRONTBOARD_LIBRARY FrontBoardServices
132       PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)
133     find_library(SPRINGBOARD_LIBRARY SpringBoardServices
134       PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)
135     find_library(MOBILESERVICES_LIBRARY MobileCoreServices
136       PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)
137     find_library(LOCKDOWN_LIBRARY lockdown)
138     if (APPLE_EMBEDDED STREQUAL "watchos")
139       find_library(CAROUSELSERVICES_LIBRARY CarouselServices
140         PATHS ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks)
141     endif()
143     if(NOT BACKBOARD_LIBRARY)
144       set(SKIP_TEST_DEBUGSERVER ON CACHE BOOL "" FORCE)
145     endif()
146   endif()
147 endif()
149 if(HAVE_LIBCOMPRESSION)
150   set(LIBCOMPRESSION compression)
151 endif()
153 if(LLDB_USE_ENTITLEMENTS)
154   if(APPLE_EMBEDDED)
155     set(entitlements ${DEBUGSERVER_RESOURCE_DIR}/debugserver-entitlements.plist)
156   else()
157     if (LLDB_USE_PRIVATE_ENTITLEMENTS)
158       set(entitlements ${DEBUGSERVER_RESOURCE_DIR}/debugserver-macosx-private-entitlements.plist)
159     else()
160       set(entitlements ${DEBUGSERVER_RESOURCE_DIR}/debugserver-macosx-entitlements.plist)
161     endif()
162   endif()
163 endif()
165 add_definitions(-DLLDB_USE_OS_LOG)
167 if(${CMAKE_OSX_SYSROOT} MATCHES ".Internal.sdk$")
168   message(STATUS "LLDB debugserver energy support is enabled")
169   add_definitions(-DLLDB_ENERGY)
170   set(ENERGY_LIBRARY -lpmenergy -lpmsample)
171 else()
172   message(STATUS "LLDB debugserver energy support is disabled")
173 endif()
175 set(generated_mach_interfaces
176   ${CMAKE_CURRENT_BINARY_DIR}/mach_exc.h
177   ${CMAKE_CURRENT_BINARY_DIR}/mach_excServer.c
178   ${CMAKE_CURRENT_BINARY_DIR}/mach_excUser.c
179   )
181 set(MIG_ARCH_FLAGS "")
182 if (DEFINED MIG_ARCHS)
183   foreach(ARCH ${MIG_ARCHS})
184     set(MIG_ARCH_FLAGS "${MIG_ARCH_FLAGS} -arch ${ARCH}")
185   endforeach()
186 endif()
187 separate_arguments(MIG_ARCH_FLAGS_SEPARTED NATIVE_COMMAND "${MIG_ARCH_FLAGS}")
189 add_custom_command(OUTPUT ${generated_mach_interfaces}
190   VERBATIM COMMAND mig ${MIG_ARCH_FLAGS_SEPARTED} -isysroot ${CMAKE_OSX_SYSROOT} ${CMAKE_CURRENT_SOURCE_DIR}/MacOSX/dbgnub-mig.defs
191   DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/MacOSX/dbgnub-mig.defs
192   )
194 set(DEBUGSERVER_VERS_GENERATED_FILE ${CMAKE_CURRENT_BINARY_DIR}/debugserver_vers.c)
195 configure_file(debugserver_vers.c.in
196                ${DEBUGSERVER_VERS_GENERATED_FILE} @ONLY)
198 set(lldbDebugserverCommonSources
199   DNBArch.cpp
200   DNBBreakpoint.cpp
201   DNB.cpp
202   DNBDataRef.cpp
203   DNBError.cpp
204   DNBLog.cpp
205   DNBRegisterInfo.cpp
206   DNBThreadResumeActions.cpp
207   JSON.cpp
208   StdStringExtractor.cpp
209   StringConvert.cpp
210   # JSON reader depends on the following LLDB-common files
211   ${LLDB_SOURCE_DIR}/source/Host/common/SocketAddress.cpp
212   # end JSON reader dependencies
213   libdebugserver.cpp
214   PseudoTerminal.cpp
215   PThreadEvent.cpp
216   PThreadMutex.cpp
217   RNBContext.cpp
218   RNBRemote.cpp
219   RNBServices.cpp
220   RNBSocket.cpp
221   SysSignal.cpp
222   TTYState.cpp
224   MacOSX/CFBundle.cpp
225   MacOSX/CFString.cpp
226   MacOSX/Genealogy.cpp
227   MacOSX/MachException.cpp
228   MacOSX/MachProcess.mm
229   MacOSX/MachTask.mm
230   MacOSX/MachThread.cpp
231   MacOSX/MachThreadList.cpp
232   MacOSX/MachVMMemory.cpp
233   MacOSX/MachVMRegion.cpp
234   MacOSX/OsLogger.cpp
235   ${generated_mach_interfaces}
236   ${DEBUGSERVER_VERS_GENERATED_FILE})
238 # Tell LLVM not to complain about these source files.
239 set(LLVM_OPTIONAL_SOURCES
240   ${lldbDebugserverCommonSources}
241   debugserver.cpp)
243 add_lldb_library(lldbDebugserverCommon ${lldbDebugserverCommonSources})
244 set_target_properties(lldbDebugserverCommon PROPERTIES FOLDER "lldb libraries/debugserver")
246 target_link_libraries(lldbDebugserverCommon
247                       INTERFACE ${COCOA_LIBRARY}
248                       ${CORE_FOUNDATION_LIBRARY}
249                       ${FOUNDATION_LIBRARY}
250                       ${BACKBOARD_LIBRARY}
251                       ${FRONTBOARD_LIBRARY}
252                       ${SPRINGBOARD_LIBRARY}
253                       ${MOBILESERVICES_LIBRARY}
254                       ${LOCKDOWN_LIBRARY}
255                       ${CAROUSELSERVICES_LIBRARY}
256                       lldbDebugserverArchSupport
257                       ${FOUNDATION_LIBRARY}
258                       ${SECURITY_LIBRARY}
259                       ${LIBCOMPRESSION}
260                       ${ENERGY_LIBRARY})
261 if(HAVE_LIBCOMPRESSION)
262   set_property(TARGET lldbDebugserverCommon APPEND PROPERTY
263                 COMPILE_DEFINITIONS HAVE_LIBCOMPRESSION)
264 endif()
265 add_lldb_tool(debugserver ADD_TO_FRAMEWORK
266   debugserver.cpp
267   LINK_LIBS lldbDebugserverCommon
268   ENTITLEMENTS ${entitlements}
271 # Workaround for Xcode-specific code-signing behavior:
272 # The XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY option causes debugserver to be copied
273 # into the framework first and code-signed afterwards. Sign the copy manually.
274 if (debugserver_codesign_identity AND LLDB_BUILD_FRAMEWORK AND
275     CMAKE_GENERATOR STREQUAL "Xcode")
276   if(NOT CMAKE_CODESIGN_ALLOCATE)
277     execute_process(
278       COMMAND xcrun -f codesign_allocate
279       OUTPUT_STRIP_TRAILING_WHITESPACE
280       OUTPUT_VARIABLE CMAKE_CODESIGN_ALLOCATE
281     )
282   endif()
283   if(entitlements)
284     set(pass_entitlements --entitlements ${entitlements})
285   endif()
287   set(copy_location ${LLDB_FRAMEWORK_ABSOLUTE_BUILD_DIR}/LLDB.framework/Versions/${LLDB_FRAMEWORK_VERSION}/Resources/debugserver)
289   add_custom_command(TARGET debugserver POST_BUILD
290     COMMAND ${CMAKE_COMMAND} -E
291             env CODESIGN_ALLOCATE=${CMAKE_CODESIGN_ALLOCATE}
292             xcrun codesign -f -s ${debugserver_codesign_identity}
293             ${pass_entitlements} ${copy_location}
294     COMMENT "Code-sign debugserver copy in the build-tree framework: ${copy_location}"
295   )
296 endif()
298 set_target_properties(debugserver PROPERTIES FOLDER "lldb libraries/debugserver")
300 if(APPLE_EMBEDDED)
301   set_property(TARGET lldbDebugserverCommon APPEND PROPERTY COMPILE_DEFINITIONS
302     WITH_LOCKDOWN
303     WITH_FBS
304     WITH_BKS
305     )
306   if(CAROUSELSERVICES_LIBRARY)
307     set_property(TARGET lldbDebugserverCommon APPEND PROPERTY COMPILE_DEFINITIONS
308       WITH_CAROUSEL
309       )
310   endif()
311   set_property(TARGET debugserver APPEND PROPERTY COMPILE_DEFINITIONS
312     WITH_LOCKDOWN
313     WITH_FBS
314     WITH_BKS
315     )
316   set_property(TARGET lldbDebugserverCommon APPEND PROPERTY COMPILE_FLAGS
317     -F${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks
318     )
320   add_lldb_library(lldbDebugserverCommon_NonUI ${lldbDebugserverCommonSources})
321   target_link_libraries(lldbDebugserverCommon_NonUI
322                       INTERFACE ${COCOA_LIBRARY}
323                       ${CORE_FOUNDATION_LIBRARY}
324                       ${FOUNDATION_LIBRARY}
325                       lldbDebugserverArchSupport
326                       ${SECURITY_LIBRARY}
327                       ${LIBCOMPRESSION})
328   if(HAVE_LIBCOMPRESSION)
329     set_property(TARGET lldbDebugserverCommon_NonUI APPEND PROPERTY
330                   COMPILE_DEFINITIONS HAVE_LIBCOMPRESSION)
331   endif()
333   add_lldb_tool(debugserver-nonui
334     debugserver.cpp
336     LINK_LIBS
337       lldbDebugserverCommon_NonUI
339     ENTITLEMENTS
340       ${entitlements}
341     )
342 endif()