Commit 11faadeba77d05a80c751e97142875c4b296fa87
Committed by
jarro2783
1 parent
a3a1363b
Cmake overhaul (#53)
* remove biicode support The company/startup died in 2015 and the open source project seems to be dead as well. * define compiler warning flags globally instead of locally for every binary * 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. * removed target_sources from interface library Adding target_sources to interface libraries and exporting them is not supported in CMake 3.1 and 3.2. Furthermore, since it is a header, it is not needed at all. * use the *_LDFLAGS instead of *_LIBARIES when linking ICU The LIBRARIES variable seems to contain only the name and not the full path.
Showing
7 changed files
with
100 additions
and
47 deletions
CMakeLists.txt
| ... | ... | @@ -17,15 +17,6 @@ |
| 17 | 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | 19 | # THE SOFTWARE. |
| 20 | - | |
| 21 | -IF(BIICODE) | |
| 22 | - include(biicode/cmake/tools) | |
| 23 | - ADD_BIICODE_TARGETS() | |
| 24 | - ACTIVATE_CPP11(INTERFACE ${BII_BLOCK_TARGET}) | |
| 25 | -RETURN() | |
| 26 | - | |
| 27 | -ENDIF() | |
| 28 | - | |
| 29 | 20 | cmake_minimum_required(VERSION 3.1) |
| 30 | 21 | project(cxxopts) |
| 31 | 22 | |
| ... | ... | @@ -36,35 +27,62 @@ set(VERSION "1.2.0") |
| 36 | 27 | option(CXXOPTS_BUILD_EXAMPLES "Set to ON to build examples" ON) |
| 37 | 28 | option(CXXOPTS_BUILD_TESTS "Set to ON to build tests" OFF) |
| 38 | 29 | |
| 39 | -set(CXXOPTS_LINKER_LIBRARIES "") | |
| 30 | +# request c++11 without gnu extension for the whole project and enable more warnings | |
| 31 | +set(CMAKE_CXX_STANDARD 11) | |
| 32 | +set(CMAKE_CXX_EXTENSIONS OFF) | |
| 33 | +if(MSVC) | |
| 34 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W2") | |
| 35 | +elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") | |
| 36 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow") | |
| 37 | +endif() | |
| 38 | + | |
| 39 | +add_library(cxxopts INTERFACE) | |
| 40 | + | |
| 41 | +# optionally, enable unicode support using the ICU library | |
| 40 | 42 | set(CXXOPTS_USE_UNICODE_HELP FALSE CACHE BOOL "Use ICU Unicode library") |
| 41 | 43 | if(CXXOPTS_USE_UNICODE_HELP) |
| 44 | + find_package(PkgConfig) | |
| 45 | + pkg_check_modules(ICU REQUIRED icu-uc) | |
| 42 | 46 | |
| 43 | - find_package(PkgConfig) | |
| 47 | + target_link_libraries(cxxopts INTERFACE ${ICU_LDFLAGS}) | |
| 48 | + target_compile_options(cxxopts INTERFACE ${ICU_CFLAGS}) | |
| 49 | + target_compile_definitions(cxxopts INTERFACE CXXOPTS_USE_UNICODE) | |
| 50 | +endif() | |
| 44 | 51 | |
| 45 | - pkg_check_modules(ICU REQUIRED icu-uc) | |
| 52 | +target_include_directories(cxxopts INTERFACE | |
| 53 | + $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> | |
| 54 | + $<INSTALL_INTERFACE:include> | |
| 55 | + ) | |
| 46 | 56 | |
| 47 | - set(CXXOPTS_LINKER_LIBRARIES "${ICU_LDFLAGS}") | |
| 48 | - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ICU_CFLAGS} -DCXXOPTS_USE_UNICODE") | |
| 49 | -endif() | |
| 57 | +include(CMakePackageConfigHelpers) | |
| 58 | +set(CXXOPTS_CMAKE_DIR "lib/cmake/cxxopts" CACHE STRING | |
| 59 | + "Installation directory for cmake files, relative to ${CMAKE_INSTALL_PREFIX}.") | |
| 60 | +set(version_config "${PROJECT_BINARY_DIR}/cxxopts-config-version.cmake") | |
| 61 | +set(project_config "${PROJECT_BINARY_DIR}/cxxopts-config.cmake") | |
| 62 | +set(targets_export_name cxxopts-targets) | |
| 50 | 63 | |
| 51 | -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow") | |
| 64 | +# Generate the version, config and target files into the build directory. | |
| 65 | +write_basic_package_version_file( | |
| 66 | + ${version_config} | |
| 67 | + VERSION ${VERSION} | |
| 68 | + COMPATIBILITY AnyNewerVersion) | |
| 69 | +configure_package_config_file( | |
| 70 | + ${PROJECT_SOURCE_DIR}/cxxopts-config.cmake.in | |
| 71 | + ${project_config} | |
| 72 | + INSTALL_DESTINATION ${CXXOPTS_CMAKE_DIR}) | |
| 73 | +export(TARGETS cxxopts NAMESPACE cxxopts:: | |
| 74 | + FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake) | |
| 52 | 75 | |
| 53 | -add_library(cxxopts INTERFACE) | |
| 54 | -target_sources( | |
| 55 | - cxxopts INTERFACE | |
| 56 | - ${CMAKE_CURRENT_SOURCE_DIR}/include/cxxopts.hpp | |
| 57 | - ) | |
| 58 | -target_include_directories( | |
| 59 | - cxxopts INTERFACE | |
| 60 | - include/ | |
| 61 | - ) | |
| 62 | -target_link_libraries( | |
| 63 | - cxxopts | |
| 64 | - INTERFACE ${CXXOPTS_LINKER_LIBRARIES} | |
| 65 | - ) | |
| 76 | +# Install version, config and target files. | |
| 77 | +install( | |
| 78 | + FILES ${project_config} ${version_config} | |
| 79 | + DESTINATION ${CXXOPTS_CMAKE_DIR}) | |
| 80 | +install(EXPORT ${targets_export_name} DESTINATION ${CXXOPTS_CMAKE_DIR} | |
| 81 | + NAMESPACE cxxopts::) | |
| 66 | 82 | |
| 67 | -install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION include) | |
| 83 | +# Install the header file and export the target | |
| 84 | +install(TARGETS cxxopts EXPORT ${targets_export_name} DESTINATION lib) | |
| 85 | +install(FILES ${PROJECT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION include) | |
| 68 | 86 | |
| 69 | 87 | add_subdirectory(src) |
| 70 | 88 | add_subdirectory(test) | ... | ... |
biicode.conf deleted
cxxopts-config.cmake.in
0 โ 100644
src/CMakeLists.txt
| ... | ... | @@ -21,10 +21,4 @@ |
| 21 | 21 | if(CXXOPTS_BUILD_EXAMPLES) |
| 22 | 22 | add_executable(example example.cpp) |
| 23 | 23 | target_link_libraries(example cxxopts) |
| 24 | - | |
| 25 | - if (MSVC) | |
| 26 | - target_compile_options(example PUBLIC /W2) | |
| 27 | - elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") | |
| 28 | - target_compile_options(example PUBLIC -std=c++11 -Wall) | |
| 29 | - endif() | |
| 30 | 24 | endif() | ... | ... |
test/CMakeLists.txt
| 1 | 1 | if (CXXOPTS_BUILD_TESTS) |
| 2 | 2 | add_executable(options_test main.cpp options.cpp) |
| 3 | 3 | target_link_libraries(options_test cxxopts) |
| 4 | + add_test(options options_test) | |
| 4 | 5 | |
| 5 | - if (MSVC) | |
| 6 | - target_compile_options(options_test PUBLIC /W2) | |
| 7 | - elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") | |
| 8 | - target_compile_options(options_test PUBLIC -std=c++11 -Wall) | |
| 9 | - endif() | |
| 6 | + # test if the targets are findable from the build directory | |
| 7 | + add_test(find-package-test ${CMAKE_CTEST_COMMAND} | |
| 8 | + -C ${CMAKE_BUILD_TYPE} | |
| 9 | + --build-and-test | |
| 10 | + "${CMAKE_CURRENT_SOURCE_DIR}/find-package-test" | |
| 11 | + "${CMAKE_CURRENT_BINARY_DIR}/find-package-test" | |
| 12 | + --build-generator ${CMAKE_GENERATOR} | |
| 13 | + --build-makeprogram ${CMAKE_MAKE_PROGRAM} | |
| 14 | + --build-options | |
| 15 | + "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" | |
| 16 | + "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}" | |
| 17 | + "-Dcxxopts_DIR=${PROJECT_BINARY_DIR}" | |
| 18 | + ) | |
| 10 | 19 | |
| 11 | - add_test(options options_test) | |
| 20 | + # test if the targets are findable when add_subdirectory is used | |
| 21 | + add_test(add-subdirectory-test ${CMAKE_CTEST_COMMAND} | |
| 22 | + -C ${CMAKE_BUILD_TYPE} | |
| 23 | + --build-and-test | |
| 24 | + "${CMAKE_CURRENT_SOURCE_DIR}/add-subdirectory-test" | |
| 25 | + "${CMAKE_CURRENT_BINARY_DIR}/add-subdirectory-test" | |
| 26 | + --build-generator ${CMAKE_GENERATOR} | |
| 27 | + --build-makeprogram ${CMAKE_MAKE_PROGRAM} | |
| 28 | + --build-options | |
| 29 | + "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" | |
| 30 | + "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}" | |
| 31 | + ) | |
| 12 | 32 | endif() |
| 13 | - | ... | ... |
test/add-subdirectory-test/CMakeLists.txt
0 โ 100644
| 1 | +cmake_minimum_required(VERSION 3.1) | |
| 2 | + | |
| 3 | +project(cxxopts-test) | |
| 4 | + | |
| 5 | +set(CMAKE_CXX_STANDARD 11) | |
| 6 | +set(CMAKE_CXX_EXTENSIONS OFF) | |
| 7 | + | |
| 8 | +add_subdirectory(../.. cxxopts EXCLUDE_FROM_ALL) | |
| 9 | + | |
| 10 | +add_executable(library-test "../../src/example.cpp") | |
| 11 | +target_link_libraries(library-test cxxopts) | ... | ... |
test/find-package-test/CMakeLists.txt
0 โ 100644