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