add support for cmake's find_package() (refs #52)
This rewrite of the main CMakeLists.txt cleans up the way ICU flags are attached to the library target and adds the cmake helper files (cxxopts-config.cmake, cxxopts-config-version.cmake, cxxopts-targets.cmake) which are needed for exporting the cxxopts target. Cmake's find_package command uses these files when the library is consumed by another project. Additionally, two new tests have been added which build the example application via add_subdirectory and find_package.
This commit is contained in:
parent
38708cb91d
commit
0a5c7331ef
@ -36,34 +36,57 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES
|
|||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set(CXXOPTS_LINKER_LIBRARIES "")
|
add_library(cxxopts INTERFACE)
|
||||||
|
|
||||||
|
# optionally, enable unicode support using the ICU library
|
||||||
set(CXXOPTS_USE_UNICODE_HELP FALSE CACHE BOOL "Use ICU Unicode library")
|
set(CXXOPTS_USE_UNICODE_HELP FALSE CACHE BOOL "Use ICU Unicode library")
|
||||||
if(CXXOPTS_USE_UNICODE_HELP)
|
if(CXXOPTS_USE_UNICODE_HELP)
|
||||||
|
|
||||||
find_package(PkgConfig)
|
find_package(PkgConfig)
|
||||||
|
|
||||||
pkg_check_modules(ICU REQUIRED icu-uc)
|
pkg_check_modules(ICU REQUIRED icu-uc)
|
||||||
|
|
||||||
set(CXXOPTS_LINKER_LIBRARIES "${ICU_LDFLAGS}")
|
target_link_libraries(cxxopts INTERFACE ${ICU_LIBRARIES})
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ICU_CFLAGS} -DCXXOPTS_USE_UNICODE")
|
target_compile_options(cxxopts INTERFACE ${ICU_CFLAGS})
|
||||||
|
target_compile_definitions(cxxopts INTERFACE CXXOPTS_USE_UNICODE)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
target_sources(cxxopts INTERFACE
|
||||||
add_library(cxxopts INTERFACE)
|
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/cxxopts.hpp>
|
||||||
target_sources(
|
$<INSTALL_INTERFACE:include/cxxopts.hpp>
|
||||||
cxxopts INTERFACE
|
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/include/cxxopts.hpp
|
|
||||||
)
|
)
|
||||||
target_include_directories(
|
target_include_directories(cxxopts INTERFACE
|
||||||
cxxopts INTERFACE
|
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
|
||||||
include/
|
$<INSTALL_INTERFACE:include>
|
||||||
)
|
|
||||||
target_link_libraries(
|
|
||||||
cxxopts
|
|
||||||
INTERFACE ${CXXOPTS_LINKER_LIBRARIES}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION include)
|
include(CMakePackageConfigHelpers)
|
||||||
|
set(CXXOPTS_CMAKE_DIR "lib/cmake/cxxopts" CACHE STRING
|
||||||
|
"Installation directory for cmake files, relative to ${CMAKE_INSTALL_PREFIX}.")
|
||||||
|
set(version_config "${PROJECT_BINARY_DIR}/cxxopts-config-version.cmake")
|
||||||
|
set(project_config "${PROJECT_BINARY_DIR}/cxxopts-config.cmake")
|
||||||
|
set(targets_export_name cxxopts-targets)
|
||||||
|
|
||||||
|
# Generate the version, config and target files into the build directory.
|
||||||
|
write_basic_package_version_file(
|
||||||
|
${version_config}
|
||||||
|
VERSION ${VERSION}
|
||||||
|
COMPATIBILITY AnyNewerVersion)
|
||||||
|
configure_package_config_file(
|
||||||
|
${PROJECT_SOURCE_DIR}/cxxopts-config.cmake.in
|
||||||
|
${project_config}
|
||||||
|
INSTALL_DESTINATION ${CXXOPTS_CMAKE_DIR})
|
||||||
|
export(TARGETS cxxopts NAMESPACE cxxopts::
|
||||||
|
FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake)
|
||||||
|
|
||||||
|
# Install version, config and target files.
|
||||||
|
install(
|
||||||
|
FILES ${project_config} ${version_config}
|
||||||
|
DESTINATION ${CXXOPTS_CMAKE_DIR})
|
||||||
|
install(EXPORT ${targets_export_name} DESTINATION ${CXXOPTS_CMAKE_DIR}
|
||||||
|
NAMESPACE cxxopts::)
|
||||||
|
|
||||||
|
# Install the header file and export the target
|
||||||
|
install(TARGETS cxxopts EXPORT ${targets_export_name} DESTINATION lib)
|
||||||
|
install(FILES ${PROJECT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION include)
|
||||||
|
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
||||||
add_subdirectory(test)
|
add_subdirectory(test)
|
||||||
|
|||||||
4
cxxopts-config.cmake.in
Normal file
4
cxxopts-config.cmake.in
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
@PACKAGE_INIT@
|
||||||
|
|
||||||
|
include(${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake)
|
||||||
|
check_required_components(cxxopts)
|
||||||
@ -1,8 +1,32 @@
|
|||||||
if (CXXOPTS_BUILD_TESTS)
|
if (CXXOPTS_BUILD_TESTS)
|
||||||
add_executable(options_test main.cpp options.cpp)
|
add_executable(options_test main.cpp options.cpp)
|
||||||
target_link_libraries(options_test cxxopts)
|
target_link_libraries(options_test cxxopts)
|
||||||
|
|
||||||
|
|
||||||
add_test(options options_test)
|
add_test(options options_test)
|
||||||
endif()
|
|
||||||
|
|
||||||
|
# test if the targets are findable from the build directory
|
||||||
|
add_test(find-package-test ${CMAKE_CTEST_COMMAND}
|
||||||
|
-C ${CMAKE_BUILD_TYPE}
|
||||||
|
--build-and-test
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/find-package-test"
|
||||||
|
"${CMAKE_CURRENT_BINARY_DIR}/find-package-test"
|
||||||
|
--build-generator ${CMAKE_GENERATOR}
|
||||||
|
--build-makeprogram ${CMAKE_MAKE_PROGRAM}
|
||||||
|
--build-options
|
||||||
|
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
|
||||||
|
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
|
||||||
|
"-Dcxxopts_DIR=${PROJECT_BINARY_DIR}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# test if the targets are findable when add_subdirectory is used
|
||||||
|
add_test(add-subdirectory-test ${CMAKE_CTEST_COMMAND}
|
||||||
|
-C ${CMAKE_BUILD_TYPE}
|
||||||
|
--build-and-test
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/add-subdirectory-test"
|
||||||
|
"${CMAKE_CURRENT_BINARY_DIR}/add-subdirectory-test"
|
||||||
|
--build-generator ${CMAKE_GENERATOR}
|
||||||
|
--build-makeprogram ${CMAKE_MAKE_PROGRAM}
|
||||||
|
--build-options
|
||||||
|
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
|
||||||
|
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|||||||
11
test/add-subdirectory-test/CMakeLists.txt
Normal file
11
test/add-subdirectory-test/CMakeLists.txt
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.1)
|
||||||
|
|
||||||
|
project(cxxopts-test)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
add_subdirectory(../.. cxxopts EXCLUDE_FROM_ALL)
|
||||||
|
|
||||||
|
add_executable(library-test "../../src/example.cpp")
|
||||||
|
target_link_libraries(library-test cxxopts)
|
||||||
11
test/find-package-test/CMakeLists.txt
Normal file
11
test/find-package-test/CMakeLists.txt
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.1)
|
||||||
|
|
||||||
|
project(cxxopts-test)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
find_package(cxxopts REQUIRED)
|
||||||
|
|
||||||
|
add_executable(library-test "../../src/example.cpp")
|
||||||
|
target_link_libraries(library-test cxxopts::cxxopts)
|
||||||
Loading…
Reference in New Issue
Block a user