Merge pull request #595 from dota17/static_shared

Support to build both static and shared libraries
This commit is contained in:
Eric Hawicz
2020-05-07 23:23:15 -04:00
committed by GitHub
3 changed files with 23 additions and 5 deletions

View File

@@ -69,6 +69,7 @@ include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
option(BUILD_SHARED_LIBS "Default to building shared libraries" ON)
option(BUILD_STATIC_LIBS "Default to building static libraries" ON)
# Generate a release merge and test it to verify the correctness of republishing the package.
ADD_CUSTOM_TARGET(distcheck
@@ -391,7 +392,7 @@ add_library(${PROJECT_NAME}
set_target_properties(${PROJECT_NAME} PROPERTIES
VERSION 5.0.0
SOVERSION 5)
list(APPEND CMAKE_TARGETS ${PROJECT_NAME})
# If json-c is used as subroject it set to target correct interface -I flags and allow
# to build external target without extra include_directories(...)
target_include_directories(${PROJECT_NAME}
@@ -400,6 +401,21 @@ target_include_directories(${PROJECT_NAME}
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
)
# Allow to build static and shared libraries at the same time
if (BUILD_STATIC_LIBS)
set(STATIC_LIB ${PROJECT_NAME}-static)
add_library(${STATIC_LIB} STATIC
${JSON_C_SOURCES}
${JSON_C_HEADERS}
)
# rename the static library
set_target_properties(${STATIC_LIB} PROPERTIES
OUTPUT_NAME ${PROJECT_NAME}
)
list(APPEND CMAKE_TARGETS ${STATIC_LIB})
endif ()
# Always create new install dirs with 0755 permissions, regardless of umask
set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
OWNER_READ
@@ -411,7 +427,7 @@ set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
WORLD_EXECUTE
)
install(TARGETS ${PROJECT_NAME}
install(TARGETS ${CMAKE_TARGETS}
EXPORT ${PROJECT_NAME}-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}

View File

@@ -97,7 +97,8 @@ Variable | Type | Description
---------------------|--------|--------------
CMAKE_INSTALL_PREFIX | String | The install location.
CMAKE_BUILD_TYPE | String | Defaults to "debug"
BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library instead.
BUILD_SHARED_LIBS | Bool | The default build generates a dynamic (dll/so) library. Set this to OFF to create a static library only.
BUILD_STATIC_LIBS | Bool | The default build generates a static (lib/a) library. Set this to OFF to create a shared library only.
ENABLE_RDRAND | Bool | Enable RDRAND Hardware RNG Hash Seed
ENABLE_THREADING | Bool | Enable partial threading support
DISABLE_WERROR | Bool | Disable use of -Werror
@@ -106,7 +107,8 @@ DISABLE_BSYMBOLIC | Bool | Disable use of -Bsymbolic-functions
Pass these options as `-D` on CMake's command-line.
```sh
cmake -DBUILD_SHARED_LIBS=OFF ...
# build a static library only
cmake -DBUILD_SHARED_LIBS=OFF ..
```
### Building with partial threading support

View File

@@ -65,7 +65,7 @@ while [ $# -gt 0 ] ; do
FLAGS+=(-DBUILD_SHARED_LIBS=ON)
;;
--enable-static)
FLAGS+=(-DBUILD_SHARED_LIBS=OFF)
FLAGS+=(-DBUILD_STATIC_LIBS=ON)
;;
--disable-Bsymbolic)
FLAGS+=(-DDISABLE_BSYMBOLIC=ON)