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    llvm_add_library(${name} ${libkind} ${srcs} LINK_LIBS
48                                ${PARAM_LINK_LIBS}
49                                DEPENDS ${PARAM_DEPENDS})
50
51    if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "liblldb")
52      if (PARAM_SHARED)
53        set(out_dir lib${LLVM_LIBDIR_SUFFIX})
54        if(${name} STREQUAL "liblldb" AND LLDB_BUILD_FRAMEWORK)
55          set(out_dir ${LLDB_FRAMEWORK_INSTALL_DIR})
56        endif()
57        install(TARGETS ${name}
58          COMPONENT ${name}
59          RUNTIME DESTINATION bin
60          LIBRARY DESTINATION ${out_dir}
61          ARCHIVE DESTINATION ${out_dir})
62      else()
63        install(TARGETS ${name}
64          COMPONENT ${name}
65          LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
66          ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
67      endif()
68      if (NOT CMAKE_CONFIGURATION_TYPES)
69        add_llvm_install_targets(install-${name}
70                                 DEPENDS ${name}
71                                 COMPONENT ${name})
72      endif()
73    endif()
74  endif()
75
76  # Hack: only some LLDB libraries depend on the clang autogenerated headers,
77  # but it is simple enough to make all of LLDB depend on some of those
78  # headers without negatively impacting much of anything.
79  add_dependencies(${name} clang-tablegen-targets)
80
81  # Add in any extra C++ compilation flags for this library.
82  target_compile_options(${name} PRIVATE ${PARAM_EXTRA_CXXFLAGS})
83
84  set_target_properties(${name} PROPERTIES FOLDER "lldb libraries")
85endfunction(add_lldb_library)
86
87function(add_lldb_executable name)
88  cmake_parse_arguments(ARG
89    "INCLUDE_IN_FRAMEWORK;GENERATE_INSTALL"
90    ""
91    "LINK_LIBS;LINK_COMPONENTS"
92    ${ARGN}
93    )
94
95  list(APPEND LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
96  add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
97
98  target_link_libraries(${name} PRIVATE ${ARG_LINK_LIBS})
99  set_target_properties(${name} PROPERTIES
100    FOLDER "lldb executables")
101
102  if(LLDB_BUILD_FRAMEWORK)
103    if(ARG_INCLUDE_IN_FRAMEWORK)
104      if(NOT IOS)
105        set(resource_dir "/Resources")
106        set(resource_dots "../")
107      endif()
108      string(REGEX REPLACE "[^/]+" ".." _dots ${LLDB_FRAMEWORK_INSTALL_DIR})
109      set_target_properties(${name} PROPERTIES
110            RUNTIME_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:liblldb>${resource_dir}
111            BUILD_WITH_INSTALL_RPATH On
112            INSTALL_RPATH "@loader_path/../../../${resource_dots}${_dots}/${LLDB_FRAMEWORK_INSTALL_DIR}")
113      # For things inside the framework we don't need functional install targets
114      # because CMake copies the resources and headers from the build directory.
115      # But we still need this target to exist in order to use the
116      # LLVM_DISTRIBUTION_COMPONENTS build option. We also need the
117      # install-liblldb target to depend on this tool, so that it gets put into
118      # the Resources directory before the framework is installed.
119      if(ARG_GENERATE_INSTALL)
120        add_custom_target(install-${name} DEPENDS ${name})
121        add_dependencies(install-liblldb ${name})
122        add_custom_target(install-${name}-stripped DEPENDS ${name})
123        add_dependencies(install-liblldb-stripped ${name})
124      endif()
125    else()
126      set_target_properties(${name} PROPERTIES
127            BUILD_WITH_INSTALL_RPATH On
128            INSTALL_RPATH "@loader_path/../${LLDB_FRAMEWORK_INSTALL_DIR}")
129    endif()
130  endif()
131
132  if(ARG_GENERATE_INSTALL AND NOT (ARG_INCLUDE_IN_FRAMEWORK AND LLDB_BUILD_FRAMEWORK ))
133    install(TARGETS ${name}
134          COMPONENT ${name}
135          RUNTIME DESTINATION bin)
136    if (NOT CMAKE_CONFIGURATION_TYPES)
137      add_llvm_install_targets(install-${name}
138                               DEPENDS ${name}
139                               COMPONENT ${name})
140    endif()
141  endif()
142
143  if(ARG_INCLUDE_IN_FRAMEWORK AND LLDB_BUILD_FRAMEWORK)
144    add_llvm_tool_symlink(${name} ${name} ALWAYS_GENERATE SKIP_INSTALL
145                            OUTPUT_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
146  endif()
147endfunction(add_lldb_executable)
148
149function(add_lldb_tool name)
150  add_lldb_executable(${name} GENERATE_INSTALL ${ARGN})
151endfunction()
152
153# Support appending linker flags to an existing target.
154# This will preserve the existing linker flags on the
155# target, if there are any.
156function(lldb_append_link_flags target_name new_link_flags)
157  # Retrieve existing linker flags.
158  get_target_property(current_link_flags ${target_name} LINK_FLAGS)
159
160  # If we had any linker flags, include them first in the new linker flags.
161  if(current_link_flags)
162    set(new_link_flags "${current_link_flags} ${new_link_flags}")
163  endif()
164
165  # Now set them onto the target.
166  set_target_properties(${target_name} PROPERTIES LINK_FLAGS ${new_link_flags})
167endfunction()
168