Skip to content

Commit

Permalink
Merge pull request #595 from dota17/static_shared
Browse files Browse the repository at this point in the history
Support to build both static and shared libraries
  • Loading branch information
hawicz committed May 8, 2020
2 parents 31243e4 + a100573 commit 22870ac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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
Expand All @@ -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}
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmake-configure
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 22870ac

Please sign in to comment.