Skip to content

Commit 0c49484

Browse files
[CMake] Always build debugserver on Darwin and allow tests to use the system's one
Summary: We can always build debugserver, but we can't always sign it to be useable for testing. `LLDB_USE_SYSTEM_DEBUGSERVER` should only tell whether or not the system debugserver should be used for testing. The old behavior complicated the logic around debugserver a lot. The new logic sorts out most of it. Please note that this patch is in early stage and needs some more testing. It should not affect platfroms other than Darwin. It builds on Davide's approach to validate the code-signing identity at configuration time. What do you think? Reviewers: xiaobai, JDevlieghere, davide, compnerd, friss, labath, mgorny, jasonmolenda Reviewed By: JDevlieghere Subscribers: lldb-commits, #lldb Tags: #lldb Differential Revision: https://reviews.llvm.org/D64806 llvm-svn: 366433
1 parent 6a61bea commit 0c49484

File tree

7 files changed

+115
-145
lines changed

7 files changed

+115
-145
lines changed

lldb/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ if(LLDB_INCLUDE_TESTS)
107107
list(APPEND LLDB_TEST_DEPS lldb-server)
108108
endif()
109109

110-
if(TARGET debugserver)
111-
list(APPEND LLDB_TEST_DEPS debugserver)
112-
endif()
113-
114110
if(TARGET lldb-mi)
115111
list(APPEND LLDB_TEST_DEPS lldb-mi)
116112
endif()

lldb/cmake/modules/AddLLDB.cmake

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,27 @@ function(lldb_setup_rpaths name)
276276
INSTALL_RPATH "${LIST_INSTALL_RPATH}"
277277
)
278278
endfunction()
279+
280+
function(lldb_find_system_debugserver path)
281+
execute_process(COMMAND xcode-select -p
282+
RESULT_VARIABLE exit_code
283+
OUTPUT_VARIABLE xcode_dev_dir
284+
ERROR_VARIABLE error_msg
285+
OUTPUT_STRIP_TRAILING_WHITESPACE)
286+
if(exit_code)
287+
message(WARNING "`xcode-select -p` failed:\n${error_msg}")
288+
else()
289+
set(subpath "LLDB.framework/Resources/debugserver")
290+
set(path_shared "${xcode_dev_dir}/../SharedFrameworks/${subpath}")
291+
set(path_private "${xcode_dev_dir}/Library/PrivateFrameworks/${subpath}")
292+
293+
if(EXISTS ${path_shared})
294+
set(${path} ${path_shared} PARENT_SCOPE)
295+
elseif(EXISTS ${path_private})
296+
set(${path} ${path_private} PARENT_SCOPE)
297+
else()
298+
message(WARNING "System debugserver requested, but not found. "
299+
"Candidates don't exist: ${path_shared}\n${path_private}")
300+
endif()
301+
endif()
302+
endfunction()

lldb/cmake/modules/LLDBConfig.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ option(LLDB_USE_SYSTEM_SIX "Use six.py shipped with system and do not install a
5050
option(LLDB_USE_ENTITLEMENTS "When codesigning, use entitlements if available" ON)
5151
option(LLDB_BUILD_FRAMEWORK "Build LLDB.framework (Darwin only)" OFF)
5252
option(LLDB_NO_INSTALL_DEFAULT_RPATH "Disable default RPATH settings in binaries" OFF)
53+
option(LLDB_USE_SYSTEM_DEBUGSERVER "Use the system's debugserver for testing (Darwin only)." OFF)
5354

5455
if(LLDB_BUILD_FRAMEWORK)
5556
if(NOT APPLE)

lldb/test/CMakeLists.txt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,6 @@ if ( CMAKE_SYSTEM_NAME MATCHES "Windows" )
7474
endif()
7575
endif()
7676

77-
if(LLDB_CODESIGN_IDENTITY_USED)
78-
list(APPEND LLDB_TEST_COMMON_ARGS --codesign-identity "${LLDB_CODESIGN_IDENTITY_USED}")
79-
endif()
80-
81-
if(LLDB_BUILD_FRAMEWORK)
82-
get_target_property(framework_target_dir liblldb LIBRARY_OUTPUT_DIRECTORY)
83-
list(APPEND LLDB_TEST_COMMON_ARGS --framework ${framework_target_dir}/LLDB.framework)
84-
endif()
85-
8677
if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows|Darwin")
8778
list(APPEND LLDB_TEST_COMMON_ARGS
8879
--env ARCHIVER=${CMAKE_AR} --env OBJCOPY=${CMAKE_OBJCOPY})
@@ -94,12 +85,28 @@ if (NOT "${LLDB_LIT_TOOLS_DIR}" STREQUAL "")
9485
endif()
9586
endif()
9687

97-
if(CMAKE_HOST_APPLE AND DEBUGSERVER_PATH)
98-
list(APPEND LLDB_TEST_COMMON_ARGS --server ${DEBUGSERVER_PATH})
99-
endif()
88+
if(CMAKE_HOST_APPLE)
89+
if(LLDB_BUILD_FRAMEWORK)
90+
get_target_property(framework_build_dir liblldb LIBRARY_OUTPUT_DIRECTORY)
91+
list(APPEND LLDB_TEST_COMMON_ARGS --framework ${framework_build_dir}/LLDB.framework)
92+
endif()
93+
94+
# Use the same identity for testing
95+
get_property(code_sign_identity_used GLOBAL PROPERTY LLDB_DEBUGSERVER_CODESIGN_IDENTITY)
96+
if(code_sign_identity_used)
97+
list(APPEND LLDB_TEST_COMMON_ARGS --codesign-identity "${code_sign_identity_used}")
98+
endif()
10099

101-
if(SKIP_TEST_DEBUGSERVER)
102-
list(APPEND LLDB_TEST_COMMON_ARGS --out-of-tree-debugserver)
100+
if(LLDB_USE_SYSTEM_DEBUGSERVER)
101+
lldb_find_system_debugserver(system_debugserver_path)
102+
message(STATUS "LLDB tests use out-of-tree debugserver: ${system_debugserver_path}")
103+
list(APPEND LLDB_TEST_COMMON_ARGS --out-of-tree-debugserver)
104+
else()
105+
set(debugserver_path ${LLVM_RUNTIME_OUTPUT_INTDIR}/debugserver)
106+
message(STATUS "LLDB Tests use just-built debugserver: ${debugserver_path}")
107+
list(APPEND LLDB_TEST_COMMON_ARGS --server ${debugserver_path})
108+
add_dependencies(lldb-test-deps debugserver)
109+
endif()
103110
endif()
104111

105112
set(LLDB_DOTEST_ARGS ${LLDB_TEST_COMMON_ARGS};${LLDB_TEST_USER_ARGS})

lldb/tools/debugserver/source/CMakeLists.txt

Lines changed: 61 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,58 @@ include_directories(MacOSX/DarwinLog)
66

77
include_directories(MacOSX)
88

9+
function(check_certificate identity result_valid)
10+
execute_process(
11+
COMMAND security find-certificate -Z -p -c ${identity} /Library/Keychains/System.keychain
12+
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()
18+
endfunction()
19+
20+
function(get_debugserver_codesign_identity result)
21+
string(CONCAT not_found_help
22+
"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 unavailable
28+
if(LLDB_CODESIGN_IDENTITY)
29+
set(${result} ${LLDB_CODESIGN_IDENTITY} PARENT_SCOPE)
30+
check_certificate(${LLDB_CODESIGN_IDENTITY} available)
31+
if(NOT available AND NOT LLDB_USE_SYSTEM_DEBUGSERVER)
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 available
38+
check_certificate(lldb_codesign available)
39+
if(available)
40+
set(${result} lldb_codesign PARENT_SCOPE)
41+
return()
42+
endif()
43+
44+
if(NOT LLDB_USE_SYSTEM_DEBUGSERVER)
45+
message(WARNING "Development code sign identiy not found: 'lldb_codesign' ${not_found_help}")
46+
endif()
47+
48+
# LLVM pendant: fallback if available
49+
if(LLVM_CODESIGNING_IDENTITY)
50+
check_certificate(${LLVM_CODESIGNING_IDENTITY} available)
51+
if(available)
52+
set(${result} ${LLVM_CODESIGNING_IDENTITY} PARENT_SCOPE)
53+
return()
54+
endif()
55+
endif()
56+
57+
# Ad-hoc signing: last resort
58+
set(${result} "-" PARENT_SCOPE)
59+
endfunction()
60+
961
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -Wl,-sectcreate,__TEXT,__info_plist,${CMAKE_CURRENT_SOURCE_DIR}/../resources/lldb-debugserver-Info.plist")
1062

1163
check_cxx_compiler_flag("-Wno-gnu-zero-variadic-macro-arguments"
@@ -30,132 +82,17 @@ check_library_exists(compression compression_encode_buffer "" HAVE_LIBCOMPRESSIO
3082

3183
add_subdirectory(MacOSX)
3284

33-
# LLDB-specific identity, currently used for code signing debugserver.
3485
set(LLDB_CODESIGN_IDENTITY "" CACHE STRING
35-
"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)")
36-
37-
# Determine which identity to use and store it in the separate cache entry.
38-
# We will query it later for LLDB_TEST_COMMON_ARGS.
39-
if(LLDB_CODESIGN_IDENTITY)
40-
set(LLDB_CODESIGN_IDENTITY_USED ${LLDB_CODESIGN_IDENTITY} CACHE INTERNAL "" FORCE)
41-
elseif(LLVM_CODESIGNING_IDENTITY)
42-
set(LLDB_CODESIGN_IDENTITY_USED ${LLVM_CODESIGNING_IDENTITY} CACHE INTERNAL "" FORCE)
43-
else()
44-
set(LLDB_CODESIGN_IDENTITY_USED lldb_codesign CACHE INTERNAL "" FORCE)
45-
endif()
86+
"Identity override for debugserver; see 'Code Signing on macOS' in the documentation (Darwin only)")
4687

47-
# Override locally, so the identity is used for targets created in this scope.
48-
set(LLVM_CODESIGNING_IDENTITY ${LLDB_CODESIGN_IDENTITY_USED})
49-
50-
option(LLDB_NO_DEBUGSERVER "Disable the debugserver target" OFF)
51-
option(LLDB_USE_SYSTEM_DEBUGSERVER "Use the system's debugserver instead of building it from source (Darwin only)." OFF)
88+
get_debugserver_codesign_identity(debugserver_codesign_identity)
5289

53-
# Incompatible options
54-
if(LLDB_NO_DEBUGSERVER AND LLDB_USE_SYSTEM_DEBUGSERVER)
55-
message(FATAL_ERROR "Inconsistent options: LLDB_NO_DEBUGSERVER and LLDB_USE_SYSTEM_DEBUGSERVER")
56-
endif()
57-
58-
# Try to locate the system debugserver.
59-
# Subsequent feasibility checks depend on it.
60-
if(APPLE AND CMAKE_HOST_APPLE)
61-
execute_process(
62-
COMMAND xcode-select -p
63-
OUTPUT_VARIABLE xcode_dev_dir)
64-
string(STRIP ${xcode_dev_dir} xcode_dev_dir)
65-
66-
set(debugserver_rel_path "LLDB.framework/Resources/debugserver")
67-
set(debugserver_shared "${xcode_dev_dir}/../SharedFrameworks/${debugserver_rel_path}")
68-
set(debugserver_private "${xcode_dev_dir}/Library/PrivateFrameworks/${debugserver_rel_path}")
69-
70-
if(EXISTS ${debugserver_shared})
71-
set(system_debugserver ${debugserver_shared})
72-
elseif(EXISTS ${debugserver_private})
73-
set(system_debugserver ${debugserver_private})
74-
endif()
75-
endif()
76-
77-
# Handle unavailability
78-
if(LLDB_USE_SYSTEM_DEBUGSERVER)
79-
if(system_debugserver)
80-
set(use_system_debugserver ON)
81-
elseif(APPLE AND CMAKE_HOST_APPLE)
82-
# Binary not found on system. Keep cached variable, to try again on reconfigure.
83-
message(SEND_ERROR
84-
"LLDB_USE_SYSTEM_DEBUGSERVER option set, but no debugserver found in:\
85-
${debugserver_shared}\
86-
${debugserver_private}")
87-
else()
88-
# Non-Apple target platform or non-Darwin host. Reset invalid cached variable.
89-
message(WARNING "Reverting invalid option LLDB_USE_SYSTEM_DEBUGSERVER (Darwin only)")
90-
set(LLDB_USE_SYSTEM_DEBUGSERVER OFF CACHE BOOL "" FORCE)
91-
endif()
92-
elseif(NOT LLDB_NO_DEBUGSERVER)
93-
# Default case: on Darwin we need the right code signing ID.
94-
# See lldb/docs/code-signing.txt for details.
95-
if(CMAKE_HOST_APPLE AND NOT LLVM_CODESIGNING_IDENTITY STREQUAL "lldb_codesign")
96-
message(WARNING "Codesigning debugserver with identity ${LLVM_CODESIGNING_IDENTITY}. "
97-
"The usual setup uses the \"lldb_codesign\" identity created with "
98-
"scripts/macos-setup-codesign.sh. As a result your debugserver might "
99-
"not be able to attach to processes.\n"
100-
"Pass -DLLDB_CODESIGN_IDENTITY=lldb_codesign to use the development "
101-
"signing identity.")
102-
endif()
103-
set(build_and_sign_debugserver ON)
104-
endif()
105-
106-
# TODO: We don't use the $<TARGET_FILE:debugserver> generator expression here,
107-
# because the value of DEBUGSERVER_PATH is used to build LLDB_DOTEST_ARGS,
108-
# which is used for configuring lldb-dotest.in, which does not have a generator
109-
# step at the moment.
110-
set(default_debugserver_path "${LLVM_RUNTIME_OUTPUT_INTDIR}/debugserver${CMAKE_EXECUTABLE_SUFFIX}")
111-
112-
# Remember where debugserver binary goes and whether or not we have to test it.
113-
set(DEBUGSERVER_PATH "" CACHE FILEPATH "Path to debugserver")
114-
set(SKIP_TEST_DEBUGSERVER OFF CACHE BOOL "Building the in-tree debugserver was skipped")
115-
116-
# Reset values in all cases in order to correctly support reconfigurations.
117-
if(use_system_debugserver)
118-
add_custom_target(debugserver
119-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
120-
${system_debugserver} ${LLVM_RUNTIME_OUTPUT_INTDIR}
121-
COMMENT "Copying the system debugserver to LLDB's binaries directory.")
122-
123-
set_target_properties(debugserver PROPERTIES FOLDER "lldb libraries/debugserver")
124-
125-
# Don't test debugserver itself.
126-
# Tests that require debugserver will use the copy.
127-
set(DEBUGSERVER_PATH ${default_debugserver_path} CACHE FILEPATH "" FORCE)
128-
set(SKIP_TEST_DEBUGSERVER ON CACHE BOOL "" FORCE)
129-
130-
message(STATUS "Copy system debugserver from: ${system_debugserver}")
131-
elseif(build_and_sign_debugserver)
132-
# Build, sign and test debugserver (below)
133-
set(DEBUGSERVER_PATH ${default_debugserver_path} CACHE FILEPATH "" FORCE)
134-
set(SKIP_TEST_DEBUGSERVER OFF CACHE BOOL "" FORCE)
135-
136-
message(STATUS "lldb debugserver: ${DEBUGSERVER_PATH}")
137-
else()
138-
# No tests for debugserver, no tests that require it.
139-
set(DEBUGSERVER_PATH "" CACHE FILEPATH "" FORCE)
140-
set(SKIP_TEST_DEBUGSERVER ON CACHE BOOL "" FORCE)
141-
142-
message(STATUS "lldb debugserver will not be available.")
143-
endif()
90+
# Override locally, so the identity is used for targets created in this scope.
91+
set(LLVM_CODESIGNING_IDENTITY ${debugserver_codesign_identity})
14492

145-
# On MacOS, debugserver needs to be codesigned when built. Check if we have
146-
# a certificate instead of failing in the middle of the build.
147-
if(build_and_sign_debugserver)
148-
execute_process(
149-
COMMAND security find-certificate -Z -p -c ${LLDB_CODESIGN_IDENTITY_USED} /Library/Keychains/System.keychain
150-
RESULT_VARIABLE cert_return
151-
OUTPUT_QUIET
152-
ERROR_QUIET)
153-
154-
if (cert_return)
155-
message(FATAL_ERROR "Certificate for debugserver not found. Run scripts/macos-setup-codesign.sh or "
156-
"use the system debugserver passing -DLLDB_USE_SYSTEM_DEBUGSERVER=ON to CMake")
157-
endif()
158-
endif()
93+
# Use the same identity later in the test suite.
94+
set_property(GLOBAL PROPERTY
95+
LLDB_DEBUGSERVER_CODESIGN_IDENTITY ${debugserver_codesign_identity})
15996

16097
if(APPLE)
16198
if(IOS)
@@ -190,7 +127,7 @@ if(LLDB_USE_ENTITLEMENTS)
190127
endif()
191128
endif()
192129

193-
if(build_and_sign_debugserver)
130+
#if(build_and_sign_debugserver)
194131
set(generated_mach_interfaces
195132
${CMAKE_CURRENT_BINARY_DIR}/mach_exc.h
196133
${CMAKE_CURRENT_BINARY_DIR}/mach_excServer.c
@@ -318,4 +255,4 @@ if(build_and_sign_debugserver)
318255
${entitlements}
319256
)
320257
endif()
321-
endif()
258+
#endif()

lldb/unittests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ add_subdirectory(tools)
7878
add_subdirectory(UnwindAssembly)
7979
add_subdirectory(Utility)
8080

81-
if(LLDB_CAN_USE_DEBUGSERVER AND NOT SKIP_TEST_DEBUGSERVER)
81+
if(LLDB_CAN_USE_DEBUGSERVER AND NOT LLDB_USE_SYSTEM_DEBUGSERVER)
8282
add_subdirectory(debugserver)
8383
endif()

lldb/unittests/tools/lldb-server/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ endfunction()
1313
add_lldb_test_executable(thread_inferior inferior/thread_inferior.cpp)
1414
add_lldb_test_executable(environment_check inferior/environment_check.cpp)
1515

16-
if(DEBUGSERVER_PATH)
17-
add_definitions(-DLLDB_SERVER="${DEBUGSERVER_PATH}" -DLLDB_SERVER_IS_DEBUGSERVER=1)
16+
if(LLDB_CAN_USE_DEBUGSERVER)
17+
if(LLDB_USE_SYSTEM_DEBUGSERVER)
18+
lldb_find_system_debugserver(debugserver_path)
19+
else()
20+
set(debugserver_path $<TARGET_FILE:debugserver>)
21+
endif()
22+
add_definitions(-DLLDB_SERVER="${debugserver_path}" -DLLDB_SERVER_IS_DEBUGSERVER=1)
1823
else()
1924
add_definitions(-DLLDB_SERVER="$<TARGET_FILE:lldb-server>" -DLLDB_SERVER_IS_DEBUGSERVER=0)
2025
endif()

0 commit comments

Comments
 (0)