Browse Source

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().
pull/75/head
Tom G. Huang 3 years ago
parent
commit
1f73484b3b
  1. 4
      CMakeLists.txt
  2. 6
      examples/CMakeLists.txt
  3. 47
      src/CMakeLists.txt
  4. 4
      test_package/CMakeLists.txt
  5. 13
      tests/CMakeLists.txt

4
CMakeLists.txt

@ -1,7 +1,7 @@
################################################################################ ################################################################################
# This file is part of the argtable3 library. # This file is part of the argtable3 library.
# #
# Copyright (C) 2016-2019 Tom G. Huang # Copyright (C) 2016-2021 Tom G. Huang
# <tomghuang@gmail.com> # <tomghuang@gmail.com>
# All rights reserved. # All rights reserved.
# #
@ -35,10 +35,10 @@ set(ARGTABLE3_PACKAGE_NAME "Argtable3")
project(${ARGTABLE3_PROJECT_NAME} "C") 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_CONAN "Enable Conan dependency manager" OFF)
option(ARGTABLE3_ENABLE_TESTS "Enable unit tests" ON) option(ARGTABLE3_ENABLE_TESTS "Enable unit tests" ON)
option(ARGTABLE3_ENABLE_ARG_REX_DEBUG "Enable arg_rex debug output" OFF) 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_REPLACE_GETOPT "Replace getopt in the system C library" ON)
option(ARGTABLE3_LONG_ONLY "Use getopt_long_only instead of getopt_long" OFF) option(ARGTABLE3_LONG_ONLY "Use getopt_long_only instead of getopt_long" OFF)

6
examples/CMakeLists.txt

@ -1,7 +1,7 @@
################################################################################ ################################################################################
# This file is part of the argtable3 library. # This file is part of the argtable3 library.
# #
# Copyright (C) 2016-2019 Tom G. Huang # Copyright (C) 2016-2021 Tom G. Huang
# <tomghuang@gmail.com> # <tomghuang@gmail.com>
# All rights reserved. # All rights reserved.
# #
@ -50,9 +50,5 @@ foreach(examples_src ${EXAMPLES_SOURCES})
string(REPLACE ".c" "" examplename ${examples_src}) string(REPLACE ".c" "" examplename ${examples_src})
add_executable(${examplename} ${PROJECT_SOURCE_DIR}/examples/${examples_src}) add_executable(${examplename} ${PROJECT_SOURCE_DIR}/examples/${examples_src})
target_include_directories(${examplename} PRIVATE ${PROJECT_SOURCE_DIR}/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}) target_link_libraries(${examplename} argtable3 ${ARGTABLE3_EXTRA_LIBS})
endif()
endforeach() endforeach()

47
src/CMakeLists.txt

@ -1,7 +1,7 @@
################################################################################ ################################################################################
# This file is part of the argtable3 library. # This file is part of the argtable3 library.
# #
# Copyright (C) 2016-2019 Tom G. Huang # Copyright (C) 2016-2021 Tom G. Huang
# <tomghuang@gmail.com> # <tomghuang@gmail.com>
# All rights reserved. # All rights reserved.
# #
@ -58,7 +58,7 @@ endif()
add_definitions(-D_XOPEN_SOURCE=700) add_definitions(-D_XOPEN_SOURCE=700)
if(WIN32) if(BUILD_SHARED_LIBS AND WIN32)
set(COMPANY_NAME "The Argtable3 Project") set(COMPANY_NAME "The Argtable3 Project")
set(FILE_DESC "ANSI C command-line parsing library") set(FILE_DESC "ANSI C command-line parsing library")
set(INTERNAL_NAME "${PROJECT_NAME}") set(INTERNAL_NAME "${PROJECT_NAME}")
@ -69,19 +69,21 @@ if(WIN32)
"${PROJECT_SOURCE_DIR}/src/version.rc.in" "${PROJECT_SOURCE_DIR}/src/version.rc.in"
"${PROJECT_BINARY_DIR}/src/version.rc" "${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) target_compile_definitions(argtable3 INTERFACE argtable3_IMPORTS)
else() else()
add_library(argtable3 SHARED ${ARGTABLE3_SRC_FILES}) add_library(argtable3 ${ARGTABLE3_SRC_FILES})
endif() endif()
target_include_directories(argtable3 PRIVATE ${PROJECT_SOURCE_DIR}/src)
add_library(argtable3_static STATIC ${ARGTABLE3_SRC_FILES}) add_library(${ARGTABLE3_PROJECT_NAME}::argtable3 ALIAS argtable3)
target_include_directories(argtable3_static PRIVATE ${PROJECT_SOURCE_DIR}/src) 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} VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}
SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}) SOVERSION ${PROJECT_VERSION_MAJOR}
OUTPUT_NAME "argtable3$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>"
DEBUG_POSTFIX "d"
)
include(GNUInstallDirs) include(GNUInstallDirs)
if(UNIX OR MSYS OR MINGW) if(UNIX OR MSYS OR MINGW)
@ -94,9 +96,24 @@ install(TARGETS argtable3
EXPORT ${ARGTABLE3_PACKAGE_NAME}Config EXPORT ${ARGTABLE3_PACKAGE_NAME}Config
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE 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"
install(FILES "${PROJECT_SOURCE_DIR}/src/argtable3.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
install(EXPORT ${ARGTABLE3_PACKAGE_NAME}Config DESTINATION ${ARGTABLE3_INSTALL_CMAKEDIR}) )
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}
)

4
test_package/CMakeLists.txt

@ -1,7 +1,7 @@
################################################################################ ################################################################################
# This file is part of the argtable3 library. # This file is part of the argtable3 library.
# #
# Copyright (C) 2016-2019 Tom G. Huang # Copyright (C) 2016-2021 Tom G. Huang
# <tomghuang@gmail.com> # <tomghuang@gmail.com>
# All rights reserved. # All rights reserved.
# #
@ -44,7 +44,7 @@ enable_testing()
if (CONAN_SETTINGS_OS) if (CONAN_SETTINGS_OS)
find_package(Argtable3 REQUIRED) find_package(Argtable3 REQUIRED)
add_executable(${PROJECT_NAME} testargtable3.c) 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 add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different COMMAND ${CMAKE_COMMAND} -E copy_if_different
"$<TARGET_FILE:argtable3>" "$<TARGET_FILE:argtable3>"

13
tests/CMakeLists.txt

@ -1,7 +1,7 @@
################################################################################ ################################################################################
# This file is part of the argtable3 library. # This file is part of the argtable3 library.
# #
# Copyright (C) 2016-2019 Tom G. Huang # Copyright (C) 2016-2021 Tom G. Huang
# <tomghuang@gmail.com> # <tomghuang@gmail.com>
# All rights reserved. # All rights reserved.
# #
@ -63,6 +63,7 @@ if(UNIX)
set(ARGTABLE3_EXTRA_LIBS m) set(ARGTABLE3_EXTRA_LIBS m)
endif() endif()
if(BUILD_SHARED_LIBS)
add_executable(test_shared ${TEST_PUBLIC_SRC_FILES}) add_executable(test_shared ${TEST_PUBLIC_SRC_FILES})
target_compile_definitions(test_shared PRIVATE -DARGTABLE3_TEST_PUBLIC_ONLY) target_compile_definitions(test_shared PRIVATE -DARGTABLE3_TEST_PUBLIC_ONLY)
target_include_directories(test_shared PRIVATE ${PROJECT_SOURCE_DIR}/src) target_include_directories(test_shared PRIVATE ${PROJECT_SOURCE_DIR}/src)
@ -73,10 +74,14 @@ add_custom_command(TARGET test_shared POST_BUILD
"$<TARGET_FILE_DIR:test_shared>" "$<TARGET_FILE_DIR:test_shared>"
) )
add_test(NAME test_shared COMMAND "$<TARGET_FILE:test_shared>")
else()
add_executable(test_static ${TEST_SRC_FILES}) 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_include_directories(test_static PRIVATE ${PROJECT_SOURCE_DIR}/src)
target_link_libraries(test_static argtable3_static ${ARGTABLE3_EXTRA_LIBS}) target_link_libraries(test_static argtable3 ${ARGTABLE3_EXTRA_LIBS})
add_test(NAME test_static COMMAND "$<TARGET_FILE:test_static>")
endif()
add_executable(test_src ${TEST_SRC_FILES} ${ARGTABLE3_SRC_FILES}) add_executable(test_src ${TEST_SRC_FILES} ${ARGTABLE3_SRC_FILES})
target_include_directories(test_src PRIVATE ${PROJECT_SOURCE_DIR}/src) 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" WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tools"
) )
add_test(NAME test_shared COMMAND "$<TARGET_FILE:test_shared>")
add_test(NAME test_static COMMAND "$<TARGET_FILE:test_static>")
add_test(NAME test_src COMMAND "$<TARGET_FILE:test_src>") add_test(NAME test_src COMMAND "$<TARGET_FILE:test_src>")
add_test(NAME test_amalgamation COMMAND "$<TARGET_FILE:test_amalgamation>") add_test(NAME test_amalgamation COMMAND "$<TARGET_FILE:test_amalgamation>")

Loading…
Cancel
Save