llvm/tools/llvm-shlib/CMakeLists.txt
Andrew Wilkins f5148ebe0a [cmake] rework LLVM_LINK_LLVM_DYLIB option handling
Summary:
This diff attempts to address the concerns raised in
http://reviews.llvm.org/D12488.

We introduce a new USE_SHARED option to llvm_config,
which, if set, causes the target to be linked against
libLLVM.

add_llvm_utility now uniformly disables linking against
libLLVM. These utilities are not intended for distribution,
and this keeps the option handling more centralised.

llvm-shlib is now processes before any other "tools"
subdirectories, ensuring the libLLVM target is defined
before its dependents.

One main difference from what was requested: llvm_config
does not prune LLVM_DYLIB_COMPONENTS from the components
passed into explicit_llvm_config. This is because the "all"
component does something special, adding additional
libraries (namely libLTO). Adding the component libraries
after libLLVM should not be a problem, as symbols will be
resolved in libLLVM first.

Finally, I'm not really happy with the
DISABLE_LLVM_LINK_LLVM option, but I'm not sure of a
better way to get the following:
 - link all tools and shared libraries to libLLVM if
   LLVM_LINK_LLVM_DYLIB is set
 - some way of explicitly *not* doing so for utilities
   and libLLVM itself
Suggestions for improvement here are particularly welcome.

Reviewers: beanz

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D12590

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@246918 91177308-0d34-0410-b5e6-96231b3b80d8
2015-09-05 08:27:33 +00:00

105 lines
3.9 KiB
CMake

# This tool creates a shared library from the LLVM libraries. Generating this
# library is enabled by setting LLVM_BUILD_LLVM_DYLIB=yes on the CMake
# commandline. By default the shared library only exports the LLVM C API.
add_definitions( -DLLVM_VERSION_INFO=\"${PACKAGE_VERSION}\" )
set(SOURCES
libllvm.cpp
)
llvm_map_components_to_libnames(LIB_NAMES ${LLVM_DYLIB_COMPONENTS})
if(LLVM_LINK_LLVM_DYLIB)
if(NOT LLVM_DYLIB_EXPORT_ALL)
message(FATAL_ERROR "LLVM_DYLIB_EXPORT_ALL must be ON when LLVM_LINK_LLVM_DYLIB is ON")
endif()
# libLLVM.so should not have any dependencies on any other LLVM
# shared libraries. When using the "all" pseudo-component,
# LLVM_AVAILABLE_LIBS is added to the dependencies, which may
# contain shared libraries (e.g. libLTO).
#
# Also exclude libLLVMTableGen for the following reasons:
# - it is only used by internal *-tblgen utilities;
# - it pollutes the global options space.
foreach(lib ${LIB_NAMES})
get_target_property(t ${lib} TYPE)
if("${lib}" STREQUAL "LLVMTableGen")
elseif("x${t}" STREQUAL "xSTATIC_LIBRARY")
list(APPEND FILTERED_LIB_NAMES ${lib})
endif()
endforeach()
set(LIB_NAMES ${FILTERED_LIB_NAMES})
endif()
if(NOT DEFINED LLVM_DYLIB_EXPORTED_SYMBOL_FILE)
if( WIN32 AND NOT CYGWIN )
message(FATAL_ERROR "Auto-generation not implemented for Win32 without GNU utils. Please specify LLVM_EXPORTED_SYMBOL_FILE.")
endif()
# To get the export list for a single llvm library:
# nm ${LIB_PATH} | awk "/T _LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_PATH}.exports
set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_BINARY_DIR}/libllvm.exports)
if (NOT LLVM_DYLIB_EXPORT_ALL)
foreach (lib ${LIB_NAMES})
set(LIB_DIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
set(LIB_NAME ${LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${lib})
set(LIB_PATH ${LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
set(LIB_EXPORTS_PATH ${LIB_NAME}.exports)
list(APPEND LLVM_DYLIB_REQUIRED_EXPORTS ${LIB_EXPORTS_PATH})
add_custom_command(OUTPUT ${LIB_EXPORTS_PATH}
COMMAND nm ${LIB_PATH} | awk "/T _LLVM/ || /T LLVM/ { print $3 }" | sort -u | sed -e "s/^_//g" > ${LIB_EXPORTS_PATH}
WORKING_DIRECTORY ${LIB_DIR}
DEPENDS ${lib}
COMMENT "Generating Export list for ${lib}..."
VERBATIM )
endforeach ()
endif()
if (LLVM_DYLIB_EXPORT_ALL)
add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
COMMAND echo \"LLVM*\" > ${LLVM_EXPORTED_SYMBOL_FILE} && echo \"_Z*llvm*\" >> ${LLVM_EXPORTED_SYMBOL_FILE}
WORKING_DIRECTORY ${LIB_DIR}
DEPENDS ${LLVM_DYLIB_REQUIRED_EXPORTS}
COMMENT "Generating combined export list...")
else()
add_custom_command(OUTPUT ${LLVM_EXPORTED_SYMBOL_FILE}
COMMAND cat ${LLVM_DYLIB_REQUIRED_EXPORTS} > ${LLVM_EXPORTED_SYMBOL_FILE}
WORKING_DIRECTORY ${LIB_DIR}
DEPENDS ${LLVM_DYLIB_REQUIRED_EXPORTS}
COMMENT "Generating combined export list...")
endif()
add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
else()
set(LLVM_EXPORTED_SYMBOL_FILE ${LLVM_DYLIB_EXPORTED_SYMBOL_FILE})
add_custom_target(libLLVMExports DEPENDS ${LLVM_EXPORTED_SYMBOL_FILE})
endif()
add_llvm_library(LLVM SHARED DISABLE_LLVM_LINK_LLVM_DYLIB ${SOURCES})
list(REMOVE_DUPLICATES LIB_NAMES)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") # FIXME: It should be "GNU ld for elf"
# GNU ld doesn't resolve symbols in the version script.
set(LIB_NAMES -Wl,--whole-archive ${LIB_NAMES} -Wl,--no-whole-archive)
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
endif()
target_link_libraries(LLVM PRIVATE ${LIB_NAMES})
add_dependencies(LLVM libLLVMExports)
if (APPLE)
set_property(TARGET LLVM APPEND_STRING PROPERTY
LINK_FLAGS
" -compatibility_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR} -current_version ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")
endif()