AddLLDB.cmake
11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
function(lldb_tablegen)
# Syntax:
# lldb_tablegen output-file [tablegen-arg ...] SOURCE source-file
# [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
#
# Generates a custom command for invoking tblgen as
#
# tblgen source-file -o=output-file tablegen-arg ...
#
# and, if cmake-target-name is provided, creates a custom target for
# executing the custom command depending on output-file. It is
# possible to list more files to depend after DEPENDS.
cmake_parse_arguments(LTG "" "SOURCE;TARGET" "" ${ARGN})
if(NOT LTG_SOURCE)
message(FATAL_ERROR "SOURCE source-file required by lldb_tablegen")
endif()
set(LLVM_TARGET_DEFINITIONS ${LTG_SOURCE})
tablegen(LLDB ${LTG_UNPARSED_ARGUMENTS})
if(LTG_TARGET)
add_public_tablegen_target(${LTG_TARGET})
set_target_properties( ${LTG_TARGET} PROPERTIES FOLDER "LLDB tablegenning")
set_property(GLOBAL APPEND PROPERTY LLDB_TABLEGEN_TARGETS ${LTG_TARGET})
endif()
endfunction(lldb_tablegen)
function(add_lldb_library name)
include_directories(BEFORE
${CMAKE_CURRENT_BINARY_DIR}
)
# only supported parameters to this macro are the optional
# MODULE;SHARED;STATIC library type and source files
cmake_parse_arguments(PARAM
"MODULE;SHARED;STATIC;OBJECT;PLUGIN;FRAMEWORK"
"INSTALL_PREFIX;ENTITLEMENTS"
"EXTRA_CXXFLAGS;DEPENDS;LINK_LIBS;LINK_COMPONENTS;CLANG_LIBS"
${ARGN})
llvm_process_sources(srcs ${PARAM_UNPARSED_ARGUMENTS})
list(APPEND LLVM_LINK_COMPONENTS ${PARAM_LINK_COMPONENTS})
if(PARAM_PLUGIN)
set_property(GLOBAL APPEND PROPERTY LLDB_PLUGINS ${name})
endif()
if (MSVC_IDE OR XCODE)
string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
list(GET split_path -1 dir)
file(GLOB_RECURSE headers
../../include/lldb${dir}/*.h)
set(srcs ${srcs} ${headers})
endif()
if (PARAM_MODULE)
set(libkind MODULE)
elseif (PARAM_SHARED)
set(libkind SHARED)
elseif (PARAM_OBJECT)
set(libkind OBJECT)
else ()
# PARAM_STATIC or library type unspecified. BUILD_SHARED_LIBS
# does not control the kind of libraries created for LLDB,
# only whether or not they link to shared/static LLVM/Clang
# libraries.
set(libkind STATIC)
endif()
#PIC not needed on Win
# FIXME: Setting CMAKE_CXX_FLAGS here is a no-op, use target_compile_options
# or omit this logic instead.
if (NOT WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
endif()
if (PARAM_OBJECT)
add_library(${name} ${libkind} ${srcs})
else()
if(PARAM_ENTITLEMENTS)
set(pass_ENTITLEMENTS ENTITLEMENTS ${PARAM_ENTITLEMENTS})
endif()
if(LLDB_NO_INSTALL_DEFAULT_RPATH)
set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)
endif()
llvm_add_library(${name} ${libkind} ${srcs}
LINK_LIBS ${PARAM_LINK_LIBS}
DEPENDS ${PARAM_DEPENDS}
${pass_ENTITLEMENTS}
${pass_NO_INSTALL_RPATH}
)
if(CLANG_LINK_CLANG_DYLIB)
target_link_libraries(${name} PRIVATE clang-cpp)
else()
target_link_libraries(${name} PRIVATE ${PARAM_CLANG_LIBS})
endif()
endif()
# A target cannot be changed to a FRAMEWORK after calling install() because
# this may result in the wrong install DESTINATION. The FRAMEWORK property
# must be set earlier.
if(PARAM_FRAMEWORK)
set_target_properties(liblldb PROPERTIES FRAMEWORK ON)
endif()
if(PARAM_SHARED)
set(install_dest lib${LLVM_LIBDIR_SUFFIX})
if(PARAM_INSTALL_PREFIX)
set(install_dest ${PARAM_INSTALL_PREFIX})
endif()
# RUNTIME is relevant for DLL platforms, FRAMEWORK for macOS
install(TARGETS ${name} COMPONENT ${name}
RUNTIME DESTINATION bin
LIBRARY DESTINATION ${install_dest}
ARCHIVE DESTINATION ${install_dest}
FRAMEWORK DESTINATION ${install_dest})
if (NOT CMAKE_CONFIGURATION_TYPES)
add_llvm_install_targets(install-${name}
DEPENDS ${name}
COMPONENT ${name})
endif()
endif()
# Hack: only some LLDB libraries depend on the clang autogenerated headers,
# but it is simple enough to make all of LLDB depend on some of those
# headers without negatively impacting much of anything.
if(NOT LLDB_BUILT_STANDALONE)
add_dependencies(${name} clang-tablegen-targets)
endif()
# Add in any extra C++ compilation flags for this library.
target_compile_options(${name} PRIVATE ${PARAM_EXTRA_CXXFLAGS})
if(PARAM_PLUGIN)
get_property(parent_dir DIRECTORY PROPERTY PARENT_DIRECTORY)
if(EXISTS ${parent_dir})
get_filename_component(category ${parent_dir} NAME)
set_target_properties(${name} PROPERTIES FOLDER "lldb plugins/${category}")
endif()
else()
set_target_properties(${name} PROPERTIES FOLDER "lldb libraries")
endif()
endfunction(add_lldb_library)
function(add_lldb_executable name)
cmake_parse_arguments(ARG
"GENERATE_INSTALL"
"INSTALL_PREFIX;ENTITLEMENTS"
"LINK_LIBS;CLANG_LIBS;LINK_COMPONENTS;BUILD_RPATH;INSTALL_RPATH"
${ARGN}
)
if(ARG_ENTITLEMENTS)
set(pass_ENTITLEMENTS ENTITLEMENTS ${ARG_ENTITLEMENTS})
endif()
if(LLDB_NO_INSTALL_DEFAULT_RPATH)
set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)
endif()
list(APPEND LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
add_llvm_executable(${name}
${pass_ENTITLEMENTS}
${pass_NO_INSTALL_RPATH}
${ARG_UNPARSED_ARGUMENTS}
)
target_link_libraries(${name} PRIVATE ${ARG_LINK_LIBS})
if(CLANG_LINK_CLANG_DYLIB)
target_link_libraries(${name} PRIVATE clang-cpp)
else()
target_link_libraries(${name} PRIVATE ${ARG_CLANG_LIBS})
endif()
set_target_properties(${name} PROPERTIES FOLDER "lldb executables")
if (ARG_BUILD_RPATH)
set_target_properties(${name} PROPERTIES BUILD_RPATH "${ARG_BUILD_RPATH}")
endif()
if (ARG_INSTALL_RPATH)
set_target_properties(${name} PROPERTIES
BUILD_WITH_INSTALL_RPATH OFF
INSTALL_RPATH "${ARG_INSTALL_RPATH}")
endif()
if(ARG_GENERATE_INSTALL)
set(install_dest bin)
if(ARG_INSTALL_PREFIX)
set(install_dest ${ARG_INSTALL_PREFIX})
endif()
install(TARGETS ${name} COMPONENT ${name}
RUNTIME DESTINATION ${install_dest}
LIBRARY DESTINATION ${install_dest}
BUNDLE DESTINATION ${install_dest}
FRAMEWORK DESTINATION ${install_dest})
if (NOT CMAKE_CONFIGURATION_TYPES)
add_llvm_install_targets(install-${name}
DEPENDS ${name}
COMPONENT ${name})
endif()
if(APPLE AND ARG_INSTALL_PREFIX)
lldb_add_post_install_steps_darwin(${name} ${ARG_INSTALL_PREFIX})
endif()
endif()
endfunction()
macro(add_lldb_tool_subdirectory name)
add_llvm_subdirectory(LLDB TOOL ${name})
endmacro()
function(add_lldb_tool name)
cmake_parse_arguments(ARG "ADD_TO_FRAMEWORK" "" "" ${ARGN})
if(LLDB_BUILD_FRAMEWORK AND ARG_ADD_TO_FRAMEWORK)
set(subdir LLDB.framework/Versions/${LLDB_FRAMEWORK_VERSION}/Resources)
add_lldb_executable(${name}
GENERATE_INSTALL
INSTALL_PREFIX ${LLDB_FRAMEWORK_INSTALL_DIR}/${subdir}
${ARG_UNPARSED_ARGUMENTS}
)
lldb_add_to_buildtree_lldb_framework(${name} ${subdir})
return()
endif()
add_lldb_executable(${name} GENERATE_INSTALL ${ARG_UNPARSED_ARGUMENTS})
endfunction()
# The test suite relies on finding LLDB.framework binary resources in the
# build-tree. Remove them before installing to avoid collisions with their
# own install targets.
function(lldb_add_to_buildtree_lldb_framework name subdir)
# Destination for the copy in the build-tree. While the framework target may
# not exist yet, it will exist when the generator expression gets expanded.
set(copy_dest "${LLDB_FRAMEWORK_ABSOLUTE_BUILD_DIR}/${subdir}/$<TARGET_FILE_NAME:${name}>")
# Copy into the given subdirectory for testing.
add_custom_command(TARGET ${name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${name}> ${copy_dest}
COMMENT "Copy ${name} to ${copy_dest}"
)
endfunction()
# Add extra install steps for dSYM creation and stripping for the given target.
function(lldb_add_post_install_steps_darwin name install_prefix)
if(NOT APPLE)
message(WARNING "Darwin-specific functionality; not currently available on non-Apple platforms.")
return()
endif()
get_target_property(output_name ${name} OUTPUT_NAME)
if(NOT output_name)
set(output_name ${name})
endif()
get_target_property(is_framework ${name} FRAMEWORK)
if(is_framework)
get_target_property(buildtree_dir ${name} LIBRARY_OUTPUT_DIRECTORY)
if(buildtree_dir)
set(bundle_subdir ${output_name}.framework/Versions/${LLDB_FRAMEWORK_VERSION}/)
else()
message(SEND_ERROR "Framework target ${name} missing property for output directory. Cannot generate post-install steps.")
return()
endif()
else()
get_target_property(target_type ${name} TYPE)
if(target_type STREQUAL "EXECUTABLE")
set(buildtree_dir ${LLVM_RUNTIME_OUTPUT_INTDIR})
else()
# Only ever install shared libraries.
set(output_name "lib${output_name}.dylib")
set(buildtree_dir ${LLVM_LIBRARY_OUTPUT_INTDIR})
endif()
endif()
# Generate dSYM
set(dsym_name ${output_name}.dSYM)
if(is_framework)
set(dsym_name ${output_name}.framework.dSYM)
endif()
if(LLDB_DEBUGINFO_INSTALL_PREFIX)
# This makes the path absolute, so we must respect DESTDIR.
set(dsym_name "\$ENV\{DESTDIR\}${LLDB_DEBUGINFO_INSTALL_PREFIX}/${dsym_name}")
endif()
set(buildtree_name ${buildtree_dir}/${bundle_subdir}${output_name})
install(CODE "message(STATUS \"Externalize debuginfo: ${dsym_name}\")" COMPONENT ${name})
install(CODE "execute_process(COMMAND xcrun dsymutil -o=${dsym_name} ${buildtree_name})"
COMPONENT ${name})
if(NOT LLDB_SKIP_STRIP)
# Strip distribution binary with -ST (removing debug symbol table entries and
# Swift symbols). Avoid CMAKE_INSTALL_DO_STRIP and llvm_externalize_debuginfo()
# as they can't be configured sufficiently.
set(installtree_name "\$ENV\{DESTDIR\}${install_prefix}/${bundle_subdir}${output_name}")
install(CODE "message(STATUS \"Stripping: ${installtree_name}\")" COMPONENT ${name})
install(CODE "execute_process(COMMAND xcrun strip -ST ${installtree_name})"
COMPONENT ${name})
endif()
endfunction()
# CMake's set_target_properties() doesn't allow to pass lists for RPATH
# properties directly (error: "called with incorrect number of arguments").
# Instead of defining two list variables each time, use this helper function.
function(lldb_setup_rpaths name)
cmake_parse_arguments(LIST "" "" "BUILD_RPATH;INSTALL_RPATH" ${ARGN})
set_target_properties(${name} PROPERTIES
BUILD_WITH_INSTALL_RPATH OFF
BUILD_RPATH "${LIST_BUILD_RPATH}"
INSTALL_RPATH "${LIST_INSTALL_RPATH}"
)
endfunction()
function(lldb_find_system_debugserver path)
execute_process(COMMAND xcode-select -p
RESULT_VARIABLE exit_code
OUTPUT_VARIABLE xcode_dev_dir
ERROR_VARIABLE error_msg
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(exit_code)
message(WARNING "`xcode-select -p` failed:\n${error_msg}")
else()
set(subpath "LLDB.framework/Resources/debugserver")
set(path_shared "${xcode_dev_dir}/../SharedFrameworks/${subpath}")
set(path_private "${xcode_dev_dir}/Library/PrivateFrameworks/${subpath}")
if(EXISTS ${path_shared})
set(${path} ${path_shared} PARENT_SCOPE)
elseif(EXISTS ${path_private})
set(${path} ${path_private} PARENT_SCOPE)
else()
message(WARNING "System debugserver requested, but not found. "
"Candidates don't exist: ${path_shared}\n${path_private}")
endif()
endif()
endfunction()