llvm-project/lldb/cmake/modules/AddLLDB.cmake

166 lines
5.9 KiB
CMake
Raw Normal View History

function(add_lldb_library name)
# only supported parameters to this macro are the optional
# MODULE;SHARED;STATIC library type and source files
cmake_parse_arguments(PARAM
"MODULE;SHARED;STATIC;OBJECT;PLUGIN"
""
"EXTRA_CXXFLAGS;DEPENDS;LINK_LIBS;LINK_COMPONENTS"
${ARGN})
llvm_process_sources(srcs ${PARAM_UNPARSED_ARGUMENTS})
list(APPEND LLVM_LINK_COMPONENTS ${PARAM_LINK_COMPONENTS})
if(PARAM_PLUGIN)
set_property(GLOBAL APPEND PROPERTY LLDB_PLUGINS ${name})
endif()
if (MSVC_IDE OR XCODE)
string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
list(GET split_path -1 dir)
file(GLOB_RECURSE headers
../../include/lldb${dir}/*.h)
set(srcs ${srcs} ${headers})
endif()
if (PARAM_MODULE)
set(libkind MODULE)
elseif (PARAM_SHARED)
set(libkind SHARED)
elseif (PARAM_OBJECT)
set(libkind OBJECT)
else ()
# PARAM_STATIC or library type unspecified. BUILD_SHARED_LIBS
# does not control the kind of libraries created for LLDB,
# only whether or not they link to shared/static LLVM/Clang
# libraries.
set(libkind STATIC)
endif()
#PIC not needed on Win
# FIXME: Setting CMAKE_CXX_FLAGS here is a no-op, use target_compile_options
# or omit this logic instead.
if (NOT WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
endif()
if (PARAM_OBJECT)
add_library(${name} ${libkind} ${srcs})
else()
llvm_add_library(${name} ${libkind} ${srcs} LINK_LIBS
${PARAM_LINK_LIBS}
DEPENDS ${PARAM_DEPENDS})
if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "liblldb")
if (PARAM_SHARED)
set(out_dir lib${LLVM_LIBDIR_SUFFIX})
if(${name} STREQUAL "liblldb" AND LLDB_BUILD_FRAMEWORK)
set(out_dir ${LLDB_FRAMEWORK_INSTALL_DIR})
endif()
install(TARGETS ${name}
COMPONENT ${name}
RUNTIME DESTINATION bin
LIBRARY DESTINATION ${out_dir}
ARCHIVE DESTINATION ${out_dir})
else()
install(TARGETS ${name}
COMPONENT ${name}
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
endif()
if (NOT CMAKE_CONFIGURATION_TYPES)
add_llvm_install_targets(install-${name}
DEPENDS ${name})
endif()
endif()
endif()
# Hack: only some LLDB libraries depend on the clang autogenerated headers,
# but it is simple enough to make all of LLDB depend on some of those
# headers without negatively impacting much of anything.
add_dependencies(${name} clang-tablegen-targets)
# Add in any extra C++ compilation flags for this library.
target_compile_options(${name} PRIVATE ${PARAM_EXTRA_CXXFLAGS})
set_target_properties(${name} PROPERTIES FOLDER "lldb libraries")
endfunction(add_lldb_library)
function(add_lldb_executable name)
cmake_parse_arguments(ARG
"INCLUDE_IN_FRAMEWORK;GENERATE_INSTALL"
""
"LINK_LIBS;LINK_COMPONENTS"
${ARGN}
)
list(APPEND LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS})
[CMake] Use PRIVATE in target_link_libraries for executables We currently use target_link_libraries without an explicit scope specifier (INTERFACE, PRIVATE or PUBLIC) when linking executables. Dependencies added in this way apply to both the target and its dependencies, i.e. they become part of the executable's link interface and are transitive. Transitive dependencies generally don't make sense for executables, since you wouldn't normally be linking against an executable. This also causes issues for generating install export files when using LLVM_DISTRIBUTION_COMPONENTS. For example, clang has a lot of LLVM library dependencies, which are currently added as interface dependencies. If clang is in the distribution components but the LLVM libraries it depends on aren't (which is a perfectly legitimate use case if the LLVM libraries are being built static and there are therefore no run-time dependencies on them), CMake will complain about the LLVM libraries not being in export set when attempting to generate the install export file for clang. This is reasonable behavior on CMake's part, and the right thing is for LLVM's build system to explicitly use PRIVATE dependencies for executables. Unfortunately, CMake doesn't allow you to mix and match the keyword and non-keyword target_link_libraries signatures for a single target; i.e., if a single call to target_link_libraries for a particular target uses one of the INTERFACE, PRIVATE, or PUBLIC keywords, all other calls must also be updated to use those keywords. This means we must do this change in a single shot. I also fully expect to have missed some instances; I tested by enabling all the projects in the monorepo (except dragonegg), and configuring both with and without shared libraries, on both Darwin and Linux, but I'm planning to rely on the buildbots for other configurations (since it should be pretty easy to fix those). Even after this change, we still have a lot of target_link_libraries calls that don't specify a scope keyword, mostly for shared libraries. I'm thinking about addressing those in a follow-up, but that's a separate change IMO. Differential Revision: https://reviews.llvm.org/D40823 llvm-svn: 319840
2017-12-06 05:49:56 +08:00
target_link_libraries(${name} PRIVATE ${ARG_LINK_LIBS})
set_target_properties(${name} PROPERTIES
FOLDER "lldb executables")
if(LLDB_BUILD_FRAMEWORK)
if(ARG_INCLUDE_IN_FRAMEWORK)
if(NOT IOS)
set(resource_dir "/Resources")
set(resource_dots "../")
endif()
string(REGEX REPLACE "[^/]+" ".." _dots ${LLDB_FRAMEWORK_INSTALL_DIR})
set_target_properties(${name} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:liblldb>${resource_dir}
BUILD_WITH_INSTALL_RPATH On
INSTALL_RPATH "@loader_path/../../../${resource_dots}${_dots}/${LLDB_FRAMEWORK_INSTALL_DIR}")
# For things inside the framework we don't need functional install targets
# because CMake copies the resources and headers from the build directory.
# But we still need this target to exist in order to use the
# LLVM_DISTRIBUTION_COMPONENTS build option. We also need the
# install-liblldb target to depend on this tool, so that it gets put into
# the Resources directory before the framework is installed.
if(ARG_GENERATE_INSTALL)
add_custom_target(install-${name} DEPENDS ${name})
add_dependencies(install-liblldb ${name})
add_custom_target(install-${name}-stripped DEPENDS ${name})
add_dependencies(install-liblldb-stripped ${name})
endif()
else()
set_target_properties(${name} PROPERTIES
BUILD_WITH_INSTALL_RPATH On
INSTALL_RPATH "@loader_path/../${LLDB_FRAMEWORK_INSTALL_DIR}")
endif()
endif()
if(ARG_GENERATE_INSTALL AND NOT (ARG_INCLUDE_IN_FRAMEWORK AND LLDB_BUILD_FRAMEWORK ))
install(TARGETS ${name}
COMPONENT ${name}
RUNTIME DESTINATION bin)
if (NOT CMAKE_CONFIGURATION_TYPES)
add_llvm_install_targets(install-${name}
DEPENDS ${name})
endif()
endif()
if(ARG_INCLUDE_IN_FRAMEWORK AND LLDB_BUILD_FRAMEWORK)
add_llvm_tool_symlink(${name} ${name} ALWAYS_GENERATE SKIP_INSTALL
OUTPUT_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
endif()
endfunction(add_lldb_executable)
function(add_lldb_tool name)
add_lldb_executable(${name} GENERATE_INSTALL ${ARGN})
endfunction()
# Support appending linker flags to an existing target.
# This will preserve the existing linker flags on the
# target, if there are any.
function(lldb_append_link_flags target_name new_link_flags)
# Retrieve existing linker flags.
get_target_property(current_link_flags ${target_name} LINK_FLAGS)
# If we had any linker flags, include them first in the new linker flags.
if(current_link_flags)
set(new_link_flags "${current_link_flags} ${new_link_flags}")
endif()
# Now set them onto the target.
set_target_properties(${target_name} PROPERTIES LINK_FLAGS ${new_link_flags})
endfunction()