[libunwind] r278067 - Allow building both shared and static library
Petr Hosek via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 8 15:55:48 PDT 2016
Author: phosek
Date: Mon Aug 8 17:55:48 2016
New Revision: 278067
URL: http://llvm.org/viewvc/llvm-project?rev=278067&view=rev
Log:
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
Modified:
libunwind/trunk/CMakeLists.txt
libunwind/trunk/src/CMakeLists.txt
Modified: libunwind/trunk/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/libunwind/trunk/CMakeLists.txt?rev=278067&r1=278066&r2=278067&view=diff
==============================================================================
--- libunwind/trunk/CMakeLists.txt (original)
+++ libunwind/trunk/CMakeLists.txt Mon Aug 8 17:55:48 2016
@@ -104,6 +104,7 @@ option(LIBUNWIND_ENABLE_ASSERTIONS "Enab
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 STR
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
Modified: libunwind/trunk/src/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/libunwind/trunk/src/CMakeLists.txt?rev=278067&r1=278066&r2=278067&view=diff
==============================================================================
--- libunwind/trunk/src/CMakeLists.txt (original)
+++ libunwind/trunk/src/CMakeLists.txt Mon Aug 8 17:55:48 2016
@@ -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_FLA
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})
More information about the cfe-commits
mailing list