Allow building both shared and static library

This change allows building both shared and static version of libunwind
in a single build, sharing object files between both versions.

Differential Revision: https://reviews.llvm.org/D23233

llvm-svn: 278067
This commit is contained in:
Petr Hosek 2016-08-08 22:55:48 +00:00
parent 776c6828a5
commit f3f5f1209e
2 changed files with 42 additions and 16 deletions

View File

@ -104,6 +104,7 @@ option(LIBUNWIND_ENABLE_ASSERTIONS "Enable assertions independent of build mode.
option(LIBUNWIND_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
option(LIBUNWIND_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
option(LIBUNWIND_ENABLE_SHARED "Build libunwind as a shared library." ON)
option(LIBUNWIND_ENABLE_STATIC "Build libunwind as a static library." ON)
option(LIBUNWIND_ENABLE_CROSS_UNWINDING "Enable cross-platform unwinding support." OFF)
option(LIBUNWIND_ENABLE_ARM_WMMX "Enable unwinding support for ARM WMMX registers." OFF)
@ -111,6 +112,10 @@ set(LIBUNWIND_TARGET_TRIPLE "" CACHE STRING "Target triple for cross compiling."
set(LIBUNWIND_GCC_TOOLCHAIN "" CACHE PATH "GCC toolchain for cross compiling.")
set(LIBUNWIND_SYSROOT "" CACHE PATH "Sysroot for cross compiling.")
if (NOT LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_STATIC)
message(FATAL_ERROR "libunwind must be built as either a shared or static library.")
endif()
# Check that we can build with 32 bits if requested.
if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32)
if (LIBUNWIND_BUILD_32_BITS AND NOT LLVM_BUILD_32_BITS) # Don't duplicate the output from LLVM

View File

@ -49,20 +49,12 @@ set(LIBUNWIND_SOURCES
${LIBUNWIND_C_SOURCES}
${LIBUNWIND_ASM_SOURCES})
if (LIBUNWIND_ENABLE_SHARED)
add_library(unwind SHARED ${LIBUNWIND_SOURCES} ${LIBUNWIND_HEADERS})
else()
add_library(unwind STATIC ${LIBUNWIND_SOURCES} ${LIBUNWIND_HEADERS})
endif ()
# Generate library list.
set(libraries ${LIBUNWINDCXX_ABI_LIBRARIES})
append_if(libraries LIBUNWIND_HAS_C_LIB c)
append_if(libraries LIBUNWIND_HAS_DL_LIB dl)
append_if(libraries LIBUNWIND_HAS_PTHREAD_LIB pthread)
target_link_libraries(unwind ${libraries})
# Setup flags.
append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_FPIC_FLAG -fPIC)
append_if(LIBUNWIND_CXX_FLAGS LIBUNWIND_HAS_NO_RTTI_FLAG -fno-rtti)
@ -97,19 +89,48 @@ string(REPLACE ";" " " LIBUNWIND_CXX_FLAGS "${LIBUNWIND_CXX_FLAGS}")
string(REPLACE ";" " " LIBUNWIND_C_FLAGS "${LIBUNWIND_C_FLAGS}")
string(REPLACE ";" " " LIBUNWIND_LINK_FLAGS "${LIBUNWIND_LINK_FLAGS}")
set_target_properties(unwind
PROPERTIES
COMPILE_FLAGS "${LIBUNWIND_COMPILE_FLAGS}"
LINK_FLAGS "${LIBUNWIND_LINK_FLAGS}"
OUTPUT_NAME "unwind"
VERSION "1.0"
SOVERSION "1")
set_property(SOURCE ${LIBUNWIND_CXX_SOURCES}
APPEND_STRING PROPERTY COMPILE_FLAGS " ${CMAKE_CXX_FLAGS} ${LIBUNWIND_CXX_FLAGS}")
set_property(SOURCE ${LIBUNWIND_C_SOURCES}
APPEND_STRING PROPERTY COMPILE_FLAGS " ${CMAKE_C_FLAGS} ${LIBUNWIND_C_FLAGS}")
install(TARGETS unwind
# Add a object library that contains the compiled source files.
add_library(unwind_objects OBJECT ${LIBUNWIND_SOURCES} ${LIBUNWIND_HEADERS})
set_target_properties(unwind_objects
PROPERTIES
COMPILE_FLAGS "${LIBUNWIND_COMPILE_FLAGS}")
set(LIBUNWIND_TARGETS)
# Build the shared library.
if (LIBUNWIND_ENABLE_SHARED)
add_library(unwind_shared SHARED $<TARGET_OBJECTS:unwind_objects>)
target_link_libraries(unwind_shared ${libraries})
set_target_properties(unwind_shared
PROPERTIES
LINK_FLAGS "${LIBUNWIND_LINK_FLAGS}"
OUTPUT_NAME "unwind"
VERSION "1.0"
SOVERSION "1")
list(APPEND LIBUNWIND_TARGETS "unwind_shared")
endif()
# Build the static library.
if (LIBUNWIND_ENABLE_STATIC)
add_library(unwind_static STATIC $<TARGET_OBJECTS:unwind_objects>)
target_link_libraries(unwind_static ${libraries})
set_target_properties(unwind_static
PROPERTIES
LINK_FLAGS "${LIBUNWIND_LINK_FLAGS}"
OUTPUT_NAME "unwind")
list(APPEND LIBUNWIND_TARGETS "unwind_static")
endif()
# Add a meta-target for both libraries.
add_custom_target(unwind DEPENDS ${LIBUNWIND_TARGETS})
install(TARGETS ${LIBUNWIND_TARGETS}
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})