1function(lldb_link_common_libs name targetkind)
2  if (NOT LLDB_USED_LIBS)
3    return()
4  endif()
5
6  if(${targetkind} MATCHES "SHARED")
7    set(LINK_KEYWORD PRIVATE)
8  endif()
9
10  if(${targetkind} MATCHES "SHARED" OR ${targetkind} MATCHES "EXE")
11    if (LLDB_LINKER_SUPPORTS_GROUPS)
12      target_link_libraries(${name} ${LINK_KEYWORD}
13                            -Wl,--start-group ${LLDB_USED_LIBS} -Wl,--end-group)
14    else()
15      target_link_libraries(${name} ${LINK_KEYWORD} ${LLDB_USED_LIBS})
16    endif()
17  endif()
18endfunction(lldb_link_common_libs)
19
20function(add_lldb_library name)
21  # only supported parameters to this macro are the optional
22  # MODULE;SHARED;STATIC library type and source files
23  cmake_parse_arguments(PARAM
24    "MODULE;SHARED;STATIC;OBJECT"
25    ""
26    "DEPENDS"
27    ${ARGN})
28  llvm_process_sources(srcs ${PARAM_UNPARSED_ARGUMENTS})
29
30  if (MSVC_IDE OR XCODE)
31    string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
32    list(GET split_path -1 dir)
33    file(GLOB_RECURSE headers
34      ../../include/lldb${dir}/*.h)
35    set(srcs ${srcs} ${headers})
36  endif()
37  if (PARAM_MODULE)
38    set(libkind MODULE)
39  elseif (PARAM_SHARED)
40    set(libkind SHARED)
41  elseif (PARAM_OBJECT)
42    set(libkind OBJECT)
43  else ()
44    # PARAM_STATIC or library type unspecified. BUILD_SHARED_LIBS
45    # does not control the kind of libraries created for LLDB,
46    # only whether or not they link to shared/static LLVM/Clang
47    # libraries.
48    set(libkind STATIC)
49  endif()
50
51  #PIC not needed on Win
52  if (NOT WIN32)
53    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
54  endif()
55
56  if (PARAM_OBJECT)
57    add_library(${name} ${libkind} ${srcs})
58  else()
59    if (PARAM_SHARED)
60      if (LLDB_LINKER_SUPPORTS_GROUPS)
61        llvm_add_library(${name} ${libkind} ${srcs} LINK_LIBS
62                                -Wl,--start-group ${LLDB_USED_LIBS} -Wl,--end-group
63                                -Wl,--start-group ${CLANG_USED_LIBS} -Wl,--end-group
64                                DEPENDS ${PARAM_DEPENDS}
65          )
66      else()
67        llvm_add_library(${name} ${libkind} ${srcs} LINK_LIBS
68                                ${LLDB_USED_LIBS} ${CLANG_USED_LIBS}
69                                DEPENDS ${PARAM_DEPENDS}
70          )
71      endif()
72    else()
73      llvm_add_library(${name} ${libkind} ${srcs} DEPENDS ${PARAM_DEPENDS})
74    endif()
75
76    if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "liblldb")
77      if (PARAM_SHARED)
78        set(out_dir lib${LLVM_LIBDIR_SUFFIX})
79        if(${name} STREQUAL "liblldb" AND LLDB_BUILD_FRAMEWORK)
80          set(out_dir ${LLDB_FRAMEWORK_INSTALL_DIR})
81        endif()
82        install(TARGETS ${name}
83          COMPONENT ${name}
84          RUNTIME DESTINATION bin
85          LIBRARY DESTINATION ${out_dir}
86          ARCHIVE DESTINATION ${out_dir})
87      else()
88        install(TARGETS ${name}
89          COMPONENT ${name}
90          LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
91          ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
92      endif()
93      if (NOT CMAKE_CONFIGURATION_TYPES)
94        add_custom_target(install-${name}
95                          DEPENDS ${name}
96                          COMMAND "${CMAKE_COMMAND}"
97                                  -DCMAKE_INSTALL_COMPONENT=${name}
98                                  -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
99      endif()
100    endif()
101  endif()
102
103  # Hack: only some LLDB libraries depend on the clang autogenerated headers,
104  # but it is simple enough to make all of LLDB depend on some of those
105  # headers without negatively impacting much of anything.
106  get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS)
107  if(CLANG_TABLEGEN_TARGETS)
108    add_dependencies(${name} ${CLANG_TABLEGEN_TARGETS})
109  endif()
110
111  set_target_properties(${name} PROPERTIES FOLDER "lldb libraries")
112endfunction(add_lldb_library)
113
114function(add_lldb_executable name)
115  cmake_parse_arguments(ARG "INCLUDE_IN_FRAMEWORK;GENERATE_INSTALL" "" "" ${ARGN})
116  add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
117  set_target_properties(${name} PROPERTIES
118    FOLDER "lldb executables")
119
120  if(LLDB_BUILD_FRAMEWORK)
121    if(ARG_INCLUDE_IN_FRAMEWORK)
122      string(REGEX REPLACE "[^/]+" ".." _dots ${LLDB_FRAMEWORK_INSTALL_DIR})
123      set_target_properties(${name} PROPERTIES
124            RUNTIME_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:liblldb>/Resources
125            BUILD_WITH_INSTALL_RPATH On
126            INSTALL_RPATH "@loader_path/../../../../${_dots}/${LLDB_FRAMEWORK_INSTALL_DIR}")
127      # For things inside the framework we don't need functional install targets
128      # because CMake copies the resources and headers from the build directory.
129      # But we still need this target to exist in order to use the
130      # LLVM_DISTRIBUTION_COMPONENTS build option. We also need the
131      # install-liblldb target to depend on this tool, so that it gets put into
132      # the Resources directory before the framework is installed.
133      if(ARG_GENERATE_INSTALL)
134        add_custom_target(install-${name} DEPENDS ${name})
135        add_dependencies(install-liblldb ${name})
136      endif()
137    else()
138      set_target_properties(${name} PROPERTIES
139            BUILD_WITH_INSTALL_RPATH On
140            INSTALL_RPATH "@loader_path/../${LLDB_FRAMEWORK_INSTALL_DIR}")
141    endif()
142  endif()
143
144  if(ARG_GENERATE_INSTALL AND NOT (ARG_INCLUDE_IN_FRAMEWORK AND LLDB_BUILD_FRAMEWORK ))
145    install(TARGETS ${name}
146          COMPONENT ${name}
147          RUNTIME DESTINATION bin)
148    if (NOT CMAKE_CONFIGURATION_TYPES)
149      add_custom_target(install-${name}
150                        DEPENDS ${name}
151                        COMMAND "${CMAKE_COMMAND}"
152                                -DCMAKE_INSTALL_COMPONENT=${name}
153                                -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
154    endif()
155  endif()
156
157  if(ARG_INCLUDE_IN_FRAMEWORK AND LLDB_BUILD_FRAMEWORK)
158    add_llvm_tool_symlink(${name} ${name} ALWAYS_GENERATE SKIP_INSTALL
159                            OUTPUT_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
160  endif()
161endfunction(add_lldb_executable)
162
163function(add_lldb_tool name)
164  add_lldb_executable(${name} GENERATE_INSTALL ${ARGN})
165endfunction()
166
167# Support appending linker flags to an existing target.
168# This will preserve the existing linker flags on the
169# target, if there are any.
170function(lldb_append_link_flags target_name new_link_flags)
171  # Retrieve existing linker flags.
172  get_target_property(current_link_flags ${target_name} LINK_FLAGS)
173
174  # If we had any linker flags, include them first in the new linker flags.
175  if(current_link_flags)
176    set(new_link_flags "${current_link_flags} ${new_link_flags}")
177  endif()
178
179  # Now set them onto the target.
180  set_target_properties(${target_name} PROPERTIES LINK_FLAGS ${new_link_flags})
181endfunction()
182