1function(add_lldb_library name)
2  # only supported parameters to this macro are the optional
3  # MODULE;SHARED;STATIC library type and source files
4  cmake_parse_arguments(PARAM
5    "MODULE;SHARED;STATIC;OBJECT;PLUGIN"
6    ""
7    "EXTRA_CXXFLAGS;DEPENDS;LINK_LIBS;LINK_COMPONENTS"
8    ${ARGN})
9  llvm_process_sources(srcs ${PARAM_UNPARSED_ARGUMENTS})
10  list(APPEND LLVM_LINK_COMPONENTS ${PARAM_LINK_COMPONENTS})
11
12  if(PARAM_PLUGIN)
13    set_property(GLOBAL APPEND PROPERTY LLDB_PLUGINS ${name})
14  endif()
15
16  if (MSVC_IDE OR XCODE)
17    string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
18    list(GET split_path -1 dir)
19    file(GLOB_RECURSE headers
20      ../../include/lldb${dir}/*.h)
21    set(srcs ${srcs} ${headers})
22  endif()
23  if (PARAM_MODULE)
24    set(libkind MODULE)
25  elseif (PARAM_SHARED)
26    set(libkind SHARED)
27  elseif (PARAM_OBJECT)
28    set(libkind OBJECT)
29  else ()
30    # PARAM_STATIC or library type unspecified. BUILD_SHARED_LIBS
31    # does not control the kind of libraries created for LLDB,
32    # only whether or not they link to shared/static LLVM/Clang
33    # libraries.
34    set(libkind STATIC)
35  endif()
36
37  #PIC not needed on Win
38  # FIXME: Setting CMAKE_CXX_FLAGS here is a no-op, use target_compile_options
39  # or omit this logic instead.
40  if (NOT WIN32)
41    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
42  endif()
43
44  if (PARAM_OBJECT)
45    add_library(${name} ${libkind} ${srcs})
46  else()
47    if(LLDB_NO_INSTALL_DEFAULT_RPATH)
48      set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)
49    endif()
50
51    llvm_add_library(${name} ${libkind} ${srcs}
52      LINK_LIBS ${PARAM_LINK_LIBS}
53      DEPENDS ${PARAM_DEPENDS}
54      ${pass_NO_INSTALL_RPATH}
55    )
56
57    if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "liblldb")
58      if (PARAM_SHARED)
59        if(${name} STREQUAL "liblldb" AND LLDB_BUILD_FRAMEWORK)
60          if(LLDB_FRAMEWORK_INSTALL_DIR)
61            set(install_dir ${LLDB_FRAMEWORK_INSTALL_DIR})
62          else()
63            set(install_dir ".")
64          endif()
65        else()
66          set(install_dir lib${LLVM_LIBDIR_SUFFIX})
67        endif()
68        install(TARGETS ${name}
69          COMPONENT ${name}
70          RUNTIME DESTINATION bin
71          LIBRARY DESTINATION ${install_dir}
72          ARCHIVE DESTINATION ${install_dir})
73      else()
74        install(TARGETS ${name}
75          COMPONENT ${name}
76          LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
77          ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
78      endif()
79      if (NOT CMAKE_CONFIGURATION_TYPES)
80        add_llvm_install_targets(install-${name}
81                                 DEPENDS $<TARGET_FILE:${name}>
82                                 COMPONENT ${name})
83      endif()
84    endif()
85  endif()
86
87
88  # Hack: only some LLDB libraries depend on the clang autogenerated headers,
89  # but it is simple enough to make all of LLDB depend on some of those
90  # headers without negatively impacting much of anything.
91  add_dependencies(${name} clang-tablegen-targets)
92
93  # Add in any extra C++ compilation flags for this library.
94  target_compile_options(${name} PRIVATE ${PARAM_EXTRA_CXXFLAGS})
95
96  set_target_properties(${name} PROPERTIES FOLDER "lldb libraries")
97endfunction(add_lldb_library)
98
99function(add_lldb_executable name)
100  cmake_parse_arguments(ARG
101    "GENERATE_INSTALL"
102    "ENTITLEMENTS"
103    "LINK_LIBS;LINK_COMPONENTS"
104    ${ARGN}
105    )
106
107  if(LLDB_NO_INSTALL_DEFAULT_RPATH)
108    set(pass_NO_INSTALL_RPATH NO_INSTALL_RPATH)
109  endif()
110
111  list(APPEND LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
112  add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS}
113    ENTITLEMENTS ${ARG_ENTITLEMENTS}
114    ${pass_NO_INSTALL_RPATH}
115  )
116
117  target_link_libraries(${name} PRIVATE ${ARG_LINK_LIBS})
118  set_target_properties(${name} PROPERTIES FOLDER "lldb executables")
119
120  if(ARG_GENERATE_INSTALL)
121    install(TARGETS ${name}
122            COMPONENT ${name}
123            RUNTIME DESTINATION bin)
124    if (NOT CMAKE_CONFIGURATION_TYPES)
125      add_llvm_install_targets(install-${name}
126                               DEPENDS ${name}
127                               COMPONENT ${name})
128    endif()
129  endif()
130endfunction(add_lldb_executable)
131
132function(add_lldb_tool name)
133  add_lldb_executable(${name} GENERATE_INSTALL ${ARGN})
134endfunction()
135
136# Support appending linker flags to an existing target.
137# This will preserve the existing linker flags on the
138# target, if there are any.
139function(lldb_append_link_flags target_name new_link_flags)
140  # Retrieve existing linker flags.
141  get_target_property(current_link_flags ${target_name} LINK_FLAGS)
142
143  # If we had any linker flags, include them first in the new linker flags.
144  if(current_link_flags)
145    set(new_link_flags "${current_link_flags} ${new_link_flags}")
146  endif()
147
148  # Now set them onto the target.
149  set_target_properties(${target_name} PROPERTIES LINK_FLAGS ${new_link_flags})
150endfunction()
151
152# For tools that depend on liblldb, account for varying directory structures in
153# which LLDB.framework can be used and distributed: In the build-tree we find it
154# by its absolute target path. This is only relevant for running the test suite.
155# In the install step CMake will remove this entry and insert the final RPATHs.
156# These are relative to the file path from where the tool will be loaded on the
157# enduser system.
158#
159# Note that the LLVM install-tree doesn't match the enduser system structure
160# for LLDB.framework, so by default dependent tools will not be functional in
161# their install location. The LLDB_FRAMEWORK_INSTALL_DIR variable allows to fix
162# this. If specified, it causes the install-tree location of the framework to be
163# added as an extra RPATH below.
164#
165function(lldb_setup_framework_rpaths_in_tool name)
166  # In the build-tree, we know the exact path to the binary in the framework.
167  set(rpath_build_tree "$<TARGET_FILE:liblldb>")
168
169  # The installed framework is relocatable and can be in different locations.
170  set(rpaths_install_tree "@loader_path/../../../SharedFrameworks")
171  list(APPEND rpaths_install_tree "@loader_path/../../System/Library/PrivateFrameworks")
172  list(APPEND rpaths_install_tree "@loader_path/../../Library/PrivateFrameworks")
173
174  if(LLDB_FRAMEWORK_INSTALL_DIR)
175    set(rpaths_install_tree "@loader_path/../${LLDB_FRAMEWORK_INSTALL_DIR}")
176  endif()
177
178  # If LLDB_NO_INSTALL_DEFAULT_RPATH was NOT enabled (default), this overwrites
179  # the default settings from llvm_setup_rpath().
180  set_target_properties(${name} PROPERTIES
181    BUILD_WITH_INSTALL_RPATH OFF
182    BUILD_RPATH "${rpath_build_tree}"
183    INSTALL_RPATH "${rpaths_install_tree}"
184  )
185
186  add_dependencies(${name} lldb-framework)
187endfunction()
188