From 952db0f3978b8c55648bcdee2291af4f54e36521 Mon Sep 17 00:00:00 2001 From: dota17 Date: Wed, 6 May 2020 10:48:53 +0800 Subject: [PATCH 1/5] support to build both static and shared libraries --- CMakeLists.txt | 20 ++++++++++++++++++++ README.md | 10 +++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d51dc4..dc84b52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -400,6 +400,26 @@ target_include_directories(${PROJECT_NAME} $ ) +# Allow to build static and shared libraries at the same time +if (BUILD_STATIC_LIBS) + set(ORIGINAL_STATIC_LIB_NAME ${PROJECT_NAME}-static) + add_library(${ORIGINAL_STATIC_LIB_NAME} STATIC + ${JSON_C_SOURCES} + ${JSON_C_HEADERS} + ) + + # rename the static library + set_target_properties(${ORIGINAL_STATIC_LIB_NAME} PROPERTIES + OUTPUT_NAME ${PROJECT_NAME} + ) + + target_include_directories(${PROJECT_NAME} + PUBLIC + $ + $ + ) +endif () + # Always create new install dirs with 0755 permissions, regardless of umask set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS OWNER_READ diff --git a/README.md b/README.md index 39ea0d6..909fd11 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ 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_STATIC_LIBS | Bool | This build generates a static (lib/a) library. 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,14 @@ 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 +cmake -DBUILD_SHARED_LIBS=OFF .. +``` + +Allow to build both static and shared libraries. + +```sh +cmake -DBUILD_STATIC_LIBS=ON .. ``` ### Building with partial threading support From e97fc20bfd99d341174dc61c30ce7f9540786a6f Mon Sep 17 00:00:00 2001 From: dota17 Date: Thu, 7 May 2020 14:50:43 +0800 Subject: [PATCH 2/5] update --- CMakeLists.txt | 7 +------ README.md | 12 +++--------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dc84b52..dc36d18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 @@ -412,12 +413,6 @@ if (BUILD_STATIC_LIBS) set_target_properties(${ORIGINAL_STATIC_LIB_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME} ) - - target_include_directories(${PROJECT_NAME} - PUBLIC - $ - $ - ) endif () # Always create new install dirs with 0755 permissions, regardless of umask diff --git a/README.md b/README.md index 909fd11..f5a7ee3 100644 --- a/README.md +++ b/README.md @@ -97,8 +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_STATIC_LIBS | Bool | This build generates a static (lib/a) library. +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 @@ -107,16 +107,10 @@ DISABLE_BSYMBOLIC | Bool | Disable use of -Bsymbolic-functions Pass these options as `-D` on CMake's command-line. ```sh -# build a static library +# build a static library only cmake -DBUILD_SHARED_LIBS=OFF .. ``` -Allow to build both static and shared libraries. - -```sh -cmake -DBUILD_STATIC_LIBS=ON .. -``` - ### Building with partial threading support Although json-c does not support fully multi-threaded access to From 929d74512a58412b06e5e3899016b248acf168f5 Mon Sep 17 00:00:00 2001 From: hofnarr Date: Fri, 8 May 2020 02:16:52 +0300 Subject: [PATCH 3/5] cmake: add list for build targets --- CMakeLists.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dc36d18..e7b4086 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -392,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} @@ -413,6 +413,7 @@ if (BUILD_STATIC_LIBS) set_target_properties(${ORIGINAL_STATIC_LIB_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME} ) + list(APPEND CMAKE_TARGETS ${STATIC_LIB}) endif () # Always create new install dirs with 0755 permissions, regardless of umask @@ -426,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} From 558ef8609cebbab37830e15d34a3716de8b37c73 Mon Sep 17 00:00:00 2001 From: hofnarr Date: Fri, 8 May 2020 02:19:38 +0300 Subject: [PATCH 4/5] cmake: change variable name --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e7b4086..5becd74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -403,14 +403,14 @@ target_include_directories(${PROJECT_NAME} # Allow to build static and shared libraries at the same time if (BUILD_STATIC_LIBS) - set(ORIGINAL_STATIC_LIB_NAME ${PROJECT_NAME}-static) - add_library(${ORIGINAL_STATIC_LIB_NAME} STATIC + set(STATIC_LIB ${PROJECT_NAME}-static) + add_library(${STATIC_LIB} STATIC ${JSON_C_SOURCES} ${JSON_C_HEADERS} ) # rename the static library - set_target_properties(${ORIGINAL_STATIC_LIB_NAME} PROPERTIES + set_target_properties(${STATIC_LIB} PROPERTIES OUTPUT_NAME ${PROJECT_NAME} ) list(APPEND CMAKE_TARGETS ${STATIC_LIB}) From a100573eecf66daa6b50113c016b8a2563b1504e Mon Sep 17 00:00:00 2001 From: hofnarr Date: Fri, 8 May 2020 02:27:06 +0300 Subject: [PATCH 5/5] cmake-configure: fix enable-static option --- cmake-configure | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake-configure b/cmake-configure index 2fcc39e..c8e44ae 100755 --- a/cmake-configure +++ b/cmake-configure @@ -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)