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"
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  endif()
95
96  if(PARAM_SHARED)
97    set(install_dest lib${LLVM_LIBDIR_SUFFIX})
98    if(PARAM_INSTALL_PREFIX)
99      set(install_dest ${PARAM_INSTALL_PREFIX})
100    endif()
101    # RUNTIME is relevant for DLL platforms, FRAMEWORK for macOS
102    install(TARGETS ${name} COMPONENT ${name}
103      RUNTIME DESTINATION bin
104      LIBRARY DESTINATION ${install_dest}
105      ARCHIVE DESTINATION ${install_dest}
106      FRAMEWORK DESTINATION ${install_dest})
107    if (NOT CMAKE_CONFIGURATION_TYPES)
108      add_llvm_install_targets(install-${name}
109                              DEPENDS ${name}
110                              COMPONENT ${name})
111    endif()
112  endif()
113
114  # Hack: only some LLDB libraries depend on the clang autogenerated headers,
115  # but it is simple enough to make all of LLDB depend on some of those
116  # headers without negatively impacting much of anything.
117  if(NOT LLDB_BUILT_STANDALONE)
118    add_dependencies(${name} clang-tablegen-targets)
119  endif()
120
121  # Add in any extra C++ compilation flags for this library.
122  target_compile_options(${name} PRIVATE ${PARAM_EXTRA_CXXFLAGS})
123
124  if(PARAM_PLUGIN)
125    get_property(parent_dir DIRECTORY PROPERTY PARENT_DIRECTORY)
126    if(EXISTS ${parent_dir})
127      get_filename_component(category ${parent_dir} NAME)
128      set_target_properties(${name} PROPERTIES FOLDER "lldb plugins/${category}")
129    endif()
130  else()
131    set_target_properties(${name} PROPERTIES FOLDER "lldb libraries")
132  endif()
133endfunction(add_lldb_library)
134
135function(add_lldb_executable name)
136  cmake_parse_arguments(ARG
137    "GENERATE_INSTALL"
138    "INSTALL_PREFIX;ENTITLEMENTS"
139    "LINK_LIBS;LINK_COMPONENTS"
140    ${ARGN}
141    )
142
143  if(ARG_ENTITLEMENTS)
144    set(pass_ENTITLEMENTS ENTITLEMENTS ${ARG_ENTITLEMENTS})
145  endif()
146
147  if(LLDB_NO_INSTALL_DEFAULT_RPATH)
148    set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)
149  endif()
150
151  list(APPEND LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
152  add_llvm_executable(${name}
153    ${pass_ENTITLEMENTS}
154    ${pass_NO_INSTALL_RPATH}
155    ${ARG_UNPARSED_ARGUMENTS}
156  )
157
158  target_link_libraries(${name} PRIVATE ${ARG_LINK_LIBS})
159  set_target_properties(${name} PROPERTIES FOLDER "lldb executables")
160
161  if(ARG_GENERATE_INSTALL)
162    set(install_dest bin)
163    if(ARG_INSTALL_PREFIX)
164      set(install_dest ${ARG_INSTALL_PREFIX})
165    endif()
166    install(TARGETS ${name} COMPONENT ${name}
167            RUNTIME DESTINATION ${install_dest})
168    if (NOT CMAKE_CONFIGURATION_TYPES)
169      add_llvm_install_targets(install-${name}
170                               DEPENDS ${name}
171                               COMPONENT ${name})
172    endif()
173    if(APPLE AND ARG_INSTALL_PREFIX)
174      lldb_add_post_install_steps_darwin(${name} ${ARG_INSTALL_PREFIX})
175    endif()
176  endif()
177endfunction()
178
179
180macro(add_lldb_tool_subdirectory name)
181  add_llvm_subdirectory(LLDB TOOL ${name})
182endmacro()
183
184function(add_lldb_tool name)
185  cmake_parse_arguments(ARG "ADD_TO_FRAMEWORK" "" "" ${ARGN})
186  if(LLDB_BUILD_FRAMEWORK AND ARG_ADD_TO_FRAMEWORK)
187    set(subdir LLDB.framework/Versions/${LLDB_FRAMEWORK_VERSION}/Resources)
188    add_lldb_executable(${name}
189      GENERATE_INSTALL
190      INSTALL_PREFIX ${LLDB_FRAMEWORK_INSTALL_DIR}/${subdir}
191      ${ARG_UNPARSED_ARGUMENTS}
192    )
193    lldb_add_to_buildtree_lldb_framework(${name} ${subdir})
194    return()
195  endif()
196
197  add_lldb_executable(${name} GENERATE_INSTALL ${ARG_UNPARSED_ARGUMENTS})
198endfunction()
199
200# The test suite relies on finding LLDB.framework binary resources in the
201# build-tree. Remove them before installing to avoid collisions with their
202# own install targets.
203function(lldb_add_to_buildtree_lldb_framework name subdir)
204  # Destination for the copy in the build-tree. While the framework target may
205  # not exist yet, it will exist when the generator expression gets expanded.
206  get_target_property(framework_build_dir liblldb LIBRARY_OUTPUT_DIRECTORY)
207  set(copy_dest "${framework_build_dir}/${subdir}/$<TARGET_FILE_NAME:${name}>")
208
209  # Copy into the given subdirectory for testing.
210  add_custom_command(TARGET ${name} POST_BUILD
211    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${name}> ${copy_dest}
212    COMMENT "Copy ${name} to ${copy_dest}"
213  )
214endfunction()
215
216# Add extra install steps for dSYM creation and stripping for the given target.
217function(lldb_add_post_install_steps_darwin name install_prefix)
218  if(NOT APPLE)
219    message(WARNING "Darwin-specific functionality; not currently available on non-Apple platforms.")
220    return()
221  endif()
222
223  get_target_property(output_name ${name} OUTPUT_NAME)
224  if(NOT output_name)
225    set(output_name ${name})
226  endif()
227
228  get_target_property(is_framework ${name} FRAMEWORK)
229  if(is_framework)
230    get_target_property(buildtree_dir ${name} LIBRARY_OUTPUT_DIRECTORY)
231    if(buildtree_dir)
232      set(bundle_subdir ${output_name}.framework/Versions/${LLDB_FRAMEWORK_VERSION}/)
233    else()
234      message(SEND_ERROR "Framework target ${name} missing property for output directory. Cannot generate post-install steps.")
235      return()
236    endif()
237  else()
238    get_target_property(target_type ${name} TYPE)
239    if(target_type STREQUAL "EXECUTABLE")
240      set(buildtree_dir ${LLVM_RUNTIME_OUTPUT_INTDIR})
241    else()
242      # Only ever install shared libraries.
243      set(output_name "lib${output_name}.dylib")
244      set(buildtree_dir ${LLVM_LIBRARY_OUTPUT_INTDIR})
245    endif()
246  endif()
247
248  # Generate dSYM
249  set(dsym_name ${output_name}.dSYM)
250  if(is_framework)
251    set(dsym_name ${output_name}.framework.dSYM)
252  endif()
253  if(LLDB_DEBUGINFO_INSTALL_PREFIX)
254    # This makes the path absolute, so we must respect DESTDIR.
255    set(dsym_name "\$ENV\{DESTDIR\}${LLDB_DEBUGINFO_INSTALL_PREFIX}/${dsym_name}")
256  endif()
257
258  set(buildtree_name ${buildtree_dir}/${bundle_subdir}${output_name})
259  install(CODE "message(STATUS \"Externalize debuginfo: ${dsym_name}\")" COMPONENT ${name})
260  install(CODE "execute_process(COMMAND xcrun dsymutil -o=${dsym_name} ${buildtree_name})"
261          COMPONENT ${name})
262
263  # Strip distribution binary with -ST (removing debug symbol table entries and
264  # Swift symbols). Avoid CMAKE_INSTALL_DO_STRIP and llvm_externalize_debuginfo()
265  # as they can't be configured sufficiently.
266  set(installtree_name "\$ENV\{DESTDIR\}${install_prefix}/${bundle_subdir}${output_name}")
267  install(CODE "message(STATUS \"Stripping: ${installtree_name}\")" COMPONENT ${name})
268  install(CODE "execute_process(COMMAND xcrun strip -ST ${installtree_name})"
269          COMPONENT ${name})
270endfunction()
271
272# CMake's set_target_properties() doesn't allow to pass lists for RPATH
273# properties directly (error: "called with incorrect number of arguments").
274# Instead of defining two list variables each time, use this helper function.
275function(lldb_setup_rpaths name)
276  cmake_parse_arguments(LIST "" "" "BUILD_RPATH;INSTALL_RPATH" ${ARGN})
277  set_target_properties(${name} PROPERTIES
278    BUILD_WITH_INSTALL_RPATH OFF
279    BUILD_RPATH "${LIST_BUILD_RPATH}"
280    INSTALL_RPATH "${LIST_INSTALL_RPATH}"
281  )
282endfunction()
283
284function(lldb_find_system_debugserver path)
285  execute_process(COMMAND xcode-select -p
286                  RESULT_VARIABLE exit_code
287                  OUTPUT_VARIABLE xcode_dev_dir
288                  ERROR_VARIABLE error_msg
289                  OUTPUT_STRIP_TRAILING_WHITESPACE)
290  if(exit_code)
291    message(WARNING "`xcode-select -p` failed:\n${error_msg}")
292  else()
293    set(subpath "LLDB.framework/Resources/debugserver")
294    set(path_shared "${xcode_dev_dir}/../SharedFrameworks/${subpath}")
295    set(path_private "${xcode_dev_dir}/Library/PrivateFrameworks/${subpath}")
296
297    if(EXISTS ${path_shared})
298      set(${path} ${path_shared} PARENT_SCOPE)
299    elseif(EXISTS ${path_private})
300      set(${path} ${path_private} PARENT_SCOPE)
301    else()
302      message(WARNING "System debugserver requested, but not found. "
303                      "Candidates don't exist: ${path_shared}\n${path_private}")
304    endif()
305  endif()
306endfunction()
307