Skip to content

Commit

Permalink
feat: Use BUILD_SHARED_LIBS to control lib type
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
tomghuang committed Jun 7, 2021
1 parent c295740 commit 1f73484
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 41 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
# <[email protected]>
# All rights reserved.
#
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 2 additions & 6 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
# <[email protected]>
# All rights reserved.
#
Expand Down Expand Up @@ -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()
47 changes: 32 additions & 15 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
# <[email protected]>
# All rights reserved.
#
Expand Down Expand Up @@ -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}")
Expand All @@ -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$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>"
DEBUG_POSTFIX "d"
)

include(GNUInstallDirs)
if(UNIX OR MSYS OR MINGW)
Expand All @@ -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}
)
4 changes: 2 additions & 2 deletions test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
# <[email protected]>
# All rights reserved.
#
Expand Down Expand Up @@ -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
"$<TARGET_FILE:argtable3>"
Expand Down
35 changes: 19 additions & 16 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
# <[email protected]>
# All rights reserved.
#
Expand Down Expand Up @@ -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
"$<TARGET_FILE:argtable3>"
"$<TARGET_FILE_DIR:test_shared>"
)
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
"$<TARGET_FILE:argtable3>"
"$<TARGET_FILE_DIR:test_shared>"
)

add_test(NAME test_shared COMMAND "$<TARGET_FILE:test_shared>")
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 "$<TARGET_FILE:test_static>")
endif()

add_executable(test_src ${TEST_SRC_FILES} ${ARGTABLE3_SRC_FILES})
target_include_directories(test_src PRIVATE ${PROJECT_SOURCE_DIR}/src)
Expand All @@ -95,7 +100,5 @@ add_custom_command(TARGET test_amalgamation PRE_BUILD
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_amalgamation COMMAND "$<TARGET_FILE:test_amalgamation>")

0 comments on commit 1f73484

Please sign in to comment.