From 1f73484b3b38fabee1d66f24ee30e16f152c0911 Mon Sep 17 00:00:00 2001 From: "Tom G. Huang" Date: Sun, 6 Jun 2021 20:46:39 -0700 Subject: [PATCH] feat: Use BUILD_SHARED_LIBS to control lib type Currently CMake scripts will generate both dynamic and static libraries at the same time. However, CMake recommends using BUILD_SHARED_LIBS to build either dynamic or static library, which is enforced by vcpkg. Use BUILD_SHARED_LIBS to build one library type at a time, so that developers can use vcpkg manifest mechanism to install Argtable3. We also add namespace (argtable3::) to the package and generate CMake config version file (Argtable3ConfigVersion.cmake), which can be used to determine the library version in find_package(). --- CMakeLists.txt | 4 ++-- examples/CMakeLists.txt | 8 ++----- src/CMakeLists.txt | 47 +++++++++++++++++++++++++------------ test_package/CMakeLists.txt | 4 ++-- tests/CMakeLists.txt | 35 ++++++++++++++------------- 5 files changed, 57 insertions(+), 41 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a3c9e16..0247753 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ ################################################################################ # This file is part of the argtable3 library. # -# Copyright (C) 2016-2019 Tom G. Huang +# Copyright (C) 2016-2021 Tom G. Huang # # All rights reserved. # @@ -35,10 +35,10 @@ set(ARGTABLE3_PACKAGE_NAME "Argtable3") project(${ARGTABLE3_PROJECT_NAME} "C") +option(BUILD_SHARED_LIBS "Build shared library" OFF) option(ARGTABLE3_ENABLE_CONAN "Enable Conan dependency manager" OFF) option(ARGTABLE3_ENABLE_TESTS "Enable unit tests" ON) option(ARGTABLE3_ENABLE_ARG_REX_DEBUG "Enable arg_rex debug output" OFF) -option(ARGTABLE3_BUILD_STATIC_EXAMPLES "Build examples with the static argtable3 library" ON) option(ARGTABLE3_REPLACE_GETOPT "Replace getopt in the system C library" ON) option(ARGTABLE3_LONG_ONLY "Use getopt_long_only instead of getopt_long" OFF) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 5e52a3d..67e2221 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,7 +1,7 @@ ################################################################################ # This file is part of the argtable3 library. # -# Copyright (C) 2016-2019 Tom G. Huang +# Copyright (C) 2016-2021 Tom G. Huang # # All rights reserved. # @@ -50,9 +50,5 @@ foreach(examples_src ${EXAMPLES_SOURCES}) string(REPLACE ".c" "" examplename ${examples_src}) add_executable(${examplename} ${PROJECT_SOURCE_DIR}/examples/${examples_src}) target_include_directories(${examplename} PRIVATE ${PROJECT_SOURCE_DIR}/src) - if(ARGTABLE3_BUILD_STATIC_EXAMPLES) - target_link_libraries(${examplename} argtable3_static ${ARGTABLE3_EXTRA_LIBS}) - else() - target_link_libraries(${examplename} argtable3 ${ARGTABLE3_EXTRA_LIBS}) - endif() + target_link_libraries(${examplename} argtable3 ${ARGTABLE3_EXTRA_LIBS}) endforeach() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 55c435f..07531c0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,7 @@ ################################################################################ # This file is part of the argtable3 library. # -# Copyright (C) 2016-2019 Tom G. Huang +# Copyright (C) 2016-2021 Tom G. Huang # # All rights reserved. # @@ -58,7 +58,7 @@ endif() add_definitions(-D_XOPEN_SOURCE=700) -if(WIN32) +if(BUILD_SHARED_LIBS AND WIN32) set(COMPANY_NAME "The Argtable3 Project") set(FILE_DESC "ANSI C command-line parsing library") set(INTERNAL_NAME "${PROJECT_NAME}") @@ -69,19 +69,21 @@ if(WIN32) "${PROJECT_SOURCE_DIR}/src/version.rc.in" "${PROJECT_BINARY_DIR}/src/version.rc" ) - add_library(argtable3 SHARED ${ARGTABLE3_SRC_FILES} "${PROJECT_BINARY_DIR}/src/version.rc") + add_library(argtable3 ${ARGTABLE3_SRC_FILES} "${PROJECT_BINARY_DIR}/src/version.rc") target_compile_definitions(argtable3 INTERFACE argtable3_IMPORTS) else() - add_library(argtable3 SHARED ${ARGTABLE3_SRC_FILES}) + add_library(argtable3 ${ARGTABLE3_SRC_FILES}) endif() -target_include_directories(argtable3 PRIVATE ${PROJECT_SOURCE_DIR}/src) -add_library(argtable3_static STATIC ${ARGTABLE3_SRC_FILES}) -target_include_directories(argtable3_static PRIVATE ${PROJECT_SOURCE_DIR}/src) +add_library(${ARGTABLE3_PROJECT_NAME}::argtable3 ALIAS argtable3) +target_include_directories(argtable3 PRIVATE ${PROJECT_SOURCE_DIR}/src) -set_target_properties(argtable3 argtable3_static PROPERTIES +set_target_properties(argtable3 PROPERTIES VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH} - SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}) + SOVERSION ${PROJECT_VERSION_MAJOR} + OUTPUT_NAME "argtable3$<$>:_static>" + DEBUG_POSTFIX "d" +) include(GNUInstallDirs) if(UNIX OR MSYS OR MINGW) @@ -94,9 +96,24 @@ install(TARGETS argtable3 EXPORT ${ARGTABLE3_PACKAGE_NAME}Config RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) -install(TARGETS argtable3_static - EXPORT ${ARGTABLE3_PACKAGE_NAME}Config - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) -install(FILES "${PROJECT_SOURCE_DIR}/src/argtable3.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) -install(EXPORT ${ARGTABLE3_PACKAGE_NAME}Config DESTINATION ${ARGTABLE3_INSTALL_CMAKEDIR}) + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +install(FILES "${PROJECT_SOURCE_DIR}/src/argtable3.h" + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) + +install(EXPORT ${ARGTABLE3_PACKAGE_NAME}Config + NAMESPACE ${ARGTABLE3_PROJECT_NAME}:: + DESTINATION ${ARGTABLE3_INSTALL_CMAKEDIR} +) + +include(CMakePackageConfigHelpers) +write_basic_package_version_file("${PROJECT_BINARY_DIR}/${ARGTABLE3_PACKAGE_NAME}ConfigVersion.cmake" + VERSION ${ARGTABLE3_VERSION} + COMPATIBILITY SameMajorVersion +) + +install(FILES "${PROJECT_BINARY_DIR}/${ARGTABLE3_PACKAGE_NAME}ConfigVersion.cmake" + DESTINATION ${ARGTABLE3_INSTALL_CMAKEDIR} +) diff --git a/test_package/CMakeLists.txt b/test_package/CMakeLists.txt index 8face7f..eeaba02 100644 --- a/test_package/CMakeLists.txt +++ b/test_package/CMakeLists.txt @@ -1,7 +1,7 @@ ################################################################################ # This file is part of the argtable3 library. # -# Copyright (C) 2016-2019 Tom G. Huang +# Copyright (C) 2016-2021 Tom G. Huang # # All rights reserved. # @@ -44,7 +44,7 @@ enable_testing() if (CONAN_SETTINGS_OS) find_package(Argtable3 REQUIRED) add_executable(${PROJECT_NAME} testargtable3.c) - target_link_libraries(${PROJECT_NAME} PUBLIC argtable3 ${ARGTABLE3_EXTRA_LIBS}) + target_link_libraries(${PROJECT_NAME} PUBLIC argtable3::argtable3 ${ARGTABLE3_EXTRA_LIBS}) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "$" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e29c0f0..97c6b59 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,7 +1,7 @@ ################################################################################ # This file is part of the argtable3 library. # -# Copyright (C) 2016-2019 Tom G. Huang +# Copyright (C) 2016-2021 Tom G. Huang # # All rights reserved. # @@ -63,20 +63,25 @@ if(UNIX) set(ARGTABLE3_EXTRA_LIBS m) endif() -add_executable(test_shared ${TEST_PUBLIC_SRC_FILES}) -target_compile_definitions(test_shared PRIVATE -DARGTABLE3_TEST_PUBLIC_ONLY) -target_include_directories(test_shared PRIVATE ${PROJECT_SOURCE_DIR}/src) -target_link_libraries(test_shared argtable3 ${ARGTABLE3_EXTRA_LIBS}) -add_custom_command(TARGET test_shared POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - "$" - "$" -) +if(BUILD_SHARED_LIBS) + add_executable(test_shared ${TEST_PUBLIC_SRC_FILES}) + target_compile_definitions(test_shared PRIVATE -DARGTABLE3_TEST_PUBLIC_ONLY) + target_include_directories(test_shared PRIVATE ${PROJECT_SOURCE_DIR}/src) + target_link_libraries(test_shared argtable3 ${ARGTABLE3_EXTRA_LIBS}) + add_custom_command(TARGET test_shared POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "$" + "$" + ) + + add_test(NAME test_shared COMMAND "$") +else() + add_executable(test_static ${TEST_SRC_FILES}) + target_include_directories(test_static PRIVATE ${PROJECT_SOURCE_DIR}/src) + target_link_libraries(test_static argtable3 ${ARGTABLE3_EXTRA_LIBS}) -add_executable(test_static ${TEST_SRC_FILES}) -target_compile_definitions(test_static PRIVATE -DARGTABLE3_TEST_PUBLIC_ONLY) -target_include_directories(test_static PRIVATE ${PROJECT_SOURCE_DIR}/src) -target_link_libraries(test_static argtable3_static ${ARGTABLE3_EXTRA_LIBS}) + add_test(NAME test_static COMMAND "$") +endif() add_executable(test_src ${TEST_SRC_FILES} ${ARGTABLE3_SRC_FILES}) target_include_directories(test_src PRIVATE ${PROJECT_SOURCE_DIR}/src) @@ -95,7 +100,5 @@ add_custom_command(TARGET test_amalgamation PRE_BUILD WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tools" ) -add_test(NAME test_shared COMMAND "$") -add_test(NAME test_static COMMAND "$") add_test(NAME test_src COMMAND "$") add_test(NAME test_amalgamation COMMAND "$")