From 65848d1e5f9096decf629d991351d26f0aa75d3b Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Sat, 14 Jan 2023 20:37:42 +0100 Subject: [PATCH] cmake: support gtest from the package manager (#552) Some developers would be happier with the gtest version provided from their package manager. Use it if it is installed the package provide cmake support. Fixed: https://github.com/ArthurSonzogni/FTXUI/issues/551 --- CHANGELOG.md | 3 ++ CMakeLists.txt | 23 ++++------- cmake/ftxui_benchmark.cmake | 48 +++++++++-------------- cmake/ftxui_find_google_benchmark.cmake | 32 +++++++++++++++ cmake/ftxui_find_google_test.cmake | 33 ++++++++++++++++ cmake/ftxui_fuzzer.cmake | 4 ++ cmake/ftxui_install.cmake | 4 ++ cmake/ftxui_package.cmake | 4 ++ cmake/ftxui_set_options.cmake | 20 +++++----- cmake/ftxui_test.cmake | 52 +++++++------------------ doc/CMakeLists.txt | 45 +++++++++++---------- examples/CMakeLists.txt | 4 ++ 12 files changed, 161 insertions(+), 111 deletions(-) create mode 100644 cmake/ftxui_find_google_benchmark.cmake create mode 100644 cmake/ftxui_find_google_test.cmake diff --git a/CHANGELOG.md b/CHANGELOG.md index b6fbdc9..6aa0880 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,9 @@ current (development) - Bugfix: Add unicode 13 support for full width characters. - Bugfix: Fix MSVC treating codecvt C++17 deprecated function as an error. +### Build +- Support using the google test version provided by the package manager. + 3.0.0 ----- diff --git a/CMakeLists.txt b/CMakeLists.txt index f457e48..dab2557 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,22 +145,13 @@ ftxui_check_coverage(screen) ftxui_check_coverage(dom) ftxui_check_coverage(component) -if (FTXUI_BUILD_TESTS AND ${CMAKE_VERSION} VERSION_GREATER "3.11.4") - include(cmake/ftxui_test.cmake) -endif() - -if(FTXUI_BUILD_EXAMPLES) - add_subdirectory(examples) -endif() - -if(FTXUI_BUILD_DOCS) - add_subdirectory(doc) -endif() - +include(cmake/ftxui_test.cmake) +include(cmake/ftxui_benchmark.cmake) +include(cmake/ftxui_fuzzer.cmake) include(cmake/iwyu.cmake) include(cmake/ftxui_export.cmake) +include(cmake/ftxui_install.cmake) +include(cmake/ftxui_package.cmake) -if(FTXUI_ENABLE_INSTALL) - include(cmake/ftxui_install.cmake) - include(cmake/ftxui_package.cmake) -endif() +add_subdirectory(examples) +add_subdirectory(doc) diff --git a/cmake/ftxui_benchmark.cmake b/cmake/ftxui_benchmark.cmake index 79670f0..c4082e1 100644 --- a/cmake/ftxui_benchmark.cmake +++ b/cmake/ftxui_benchmark.cmake @@ -1,31 +1,21 @@ -if (NOT WIN32) - FetchContent_Declare(googlebenchmark - GIT_REPOSITORY "https://github.com/google/benchmark" - GIT_TAG 62937f91b5c763a8e119d0c20c67b87bde8eff1c - GIT_PROGRESS TRUE - ) - - FetchContent_GetProperties(googlebenchmark) - set (BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE INTERNAL "") - set (BENCHMARK_ENABLE_TESTING OFF CACHE INTERNAL "") - if(NOT googlebenchmark_POPULATED) - FetchContent_Populate(googlebenchmark) - add_subdirectory( - ${googlebenchmark_SOURCE_DIR} - ${googlebenchmark_BINARY_DIR} - EXCLUDE_FROM_ALL +if (NOT FTXUI_BUILD_TESTS OR + NOT ${CMAKE_VERSION} VERSION_GREATER "3.11.4" OR + WIN32 ) - endif() - - add_executable(ftxui_benchmark - src/ftxui/dom/benchmark_test.cpp - ) - target_link_libraries(ftxui_benchmark - PRIVATE dom - PRIVATE benchmark::benchmark - PRIVATE benchmark::benchmark_main - ) - target_include_directories(ftxui_benchmark - PRIVATE src - ) + return() endif() + +include(cmake/ftxui_find_google_benchmark.cmake) + +add_executable(ftxui-benchmark + src/ftxui/dom/benchmark_test.cpp + ) +ftxui_set_options(ftxui-benchmark) +target_link_libraries(ftxui-benchmark + PRIVATE dom + PRIVATE benchmark::benchmark + PRIVATE benchmark::benchmark_main + ) +target_include_directories(ftxui-benchmark + PRIVATE src + ) diff --git a/cmake/ftxui_find_google_benchmark.cmake b/cmake/ftxui_find_google_benchmark.cmake new file mode 100644 index 0000000..3eb7e9e --- /dev/null +++ b/cmake/ftxui_find_google_benchmark.cmake @@ -0,0 +1,32 @@ +# Some developers would be happier with the google benchmark version provided +# from their package manager. Use it if it is installed the package provide +# cmake support. +# https://github.com/ArthurSonzogni/FTXUI/issues/551 +find_package(benchmark QUIET) +if (benchmark_FOUND) + return() +endif() + +option(FETCHCONTENT_UPDATES_DISCONNECTED TRUE) +option(FETCHCONTENT_QUIET FALSE) +include(FetchContent) + +FetchContent_Declare(googlebenchmark + GIT_REPOSITORY "https://github.com/google/benchmark" + GIT_TAG 62937f91b5c763a8e119d0c20c67b87bde8eff1c + GIT_PROGRESS TRUE +) + +FetchContent_GetProperties(googlebenchmark) +set (BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE INTERNAL "") +set (BENCHMARK_ENABLE_TESTING OFF CACHE INTERNAL "") +if(googlebenchmark_POPULATED) + return() +endif() + +FetchContent_Populate(googlebenchmark) +add_subdirectory( + ${googlebenchmark_SOURCE_DIR} + ${googlebenchmark_BINARY_DIR} + EXCLUDE_FROM_ALL +) diff --git a/cmake/ftxui_find_google_test.cmake b/cmake/ftxui_find_google_test.cmake new file mode 100644 index 0000000..293bd7b --- /dev/null +++ b/cmake/ftxui_find_google_test.cmake @@ -0,0 +1,33 @@ +# Some developers would be happier with the gtest version provided from their +# package manager. Use it if it is installed the package provide cmake support. +# https://github.com/ArthurSonzogni/FTXUI/issues/551 +find_package(GTest QUIET) + +if (GTest_FOUND) + return() +endif() + +option(FETCHCONTENT_UPDATES_DISCONNECTED TRUE) +option(FETCHCONTENT_QUIET FALSE) +include(FetchContent) + +FetchContent_Declare(googletest + GIT_REPOSITORY "https://github.com/google/googletest" + GIT_TAG 23ef29555ef4789f555f1ba8c51b4c52975f0907 + GIT_PROGRESS TRUE +) + +FetchContent_GetProperties(googletest) +if(googletest_POPULATED) + return() +endif() + +FetchContent_Populate(googletest) +set(BUILD_GMOCK OFF CACHE INTERNAL "") +set(INSTALL_GTEST OFF CACHE INTERNAL "") +set(gtest_force_shared_crt ON CACHE INTERNAL "") +add_subdirectory( + ${googletest_SOURCE_DIR} + ${googletest_BINARY_DIR} + EXCLUDE_FROM_ALL +) diff --git a/cmake/ftxui_fuzzer.cmake b/cmake/ftxui_fuzzer.cmake index a3df3ed..0377197 100644 --- a/cmake/ftxui_fuzzer.cmake +++ b/cmake/ftxui_fuzzer.cmake @@ -1,3 +1,7 @@ +if (FTXUI_BUILD_TESTS_FUZZER) + return() +endif() + set(CMAKE_C_COMPILER clang) set(CMAKE_CXX_COMPILER clang++) diff --git a/cmake/ftxui_install.cmake b/cmake/ftxui_install.cmake index a3aacee..19e68d0 100644 --- a/cmake/ftxui_install.cmake +++ b/cmake/ftxui_install.cmake @@ -1,3 +1,7 @@ +if(NOT FTXUI_ENABLE_INSTALL) + return() +endif() + include(GNUInstallDirs) install(TARGETS screen dom component EXPORT ftxui-export diff --git a/cmake/ftxui_package.cmake b/cmake/ftxui_package.cmake index cc20cb3..ded9829 100644 --- a/cmake/ftxui_package.cmake +++ b/cmake/ftxui_package.cmake @@ -1,3 +1,7 @@ +if(NOT FTXUI_ENABLE_INSTALL) + return() +endif() + if (UNIX AND NOT APPLE) set(CPACK_GENERATOR "DEB;External;RPM;STGZ;TBZ2;TGZ;TXZ;TZ;TZST;ZIP") elseif (UNIX AND APPLE) diff --git a/cmake/ftxui_set_options.cmake b/cmake/ftxui_set_options.cmake index b274df3..1294e7d 100644 --- a/cmake/ftxui_set_options.cmake +++ b/cmake/ftxui_set_options.cmake @@ -1,16 +1,16 @@ -find_program( CLANG_TIDY_EXE NAMES "clang-tidy" DOC "Path to clang-tidy executable" ) +find_program(CLANG_TIDY_EXE NAMES "clang-tidy" DOC "Path to clang-tidy executable" ) if(NOT CLANG_TIDY_EXE) message(STATUS "clang-tidy not found.") else() message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}") endif() - function(ftxui_set_options library) - set_target_properties(${library} PROPERTIES - VERSION ${PROJECT_VERSION} - OUTPUT_NAME "ftxui-${library}" - ) + message(STATUS "ftxui_set_options " ${library}) + set_target_properties(${library} PROPERTIES VERSION ${PROJECT_VERSION}) + if (NOT ${library} MATCHES "ftxui-*") + set_target_properties(${library} PROPERTIES OUTPUT_NAME "ftxui-${library}") + endif() if(CLANG_TIDY_EXE AND FTXUI_CLANG_TIDY) set_target_properties(${library} @@ -44,7 +44,11 @@ function(ftxui_set_options library) ) # C++17 is used. We require fold expression at least. - target_compile_features(${library} PUBLIC cxx_std_17) + target_compile_features(${library} PUBLIC cxx_std_20) + set_target_properties(${library} PROPERTIES + CXX_STANDARD 17 + CXX_EXTENSIONS OFF + ) # Force Microsoft Visual Studio to decode sources files in UTF-8. This applies # to the library and the library users. @@ -80,9 +84,7 @@ function(ftxui_set_options library) endfunction() if (EMSCRIPTEN) - #string(APPEND CMAKE_CXX_FLAGS " -s ASSERTIONS=1") string(APPEND CMAKE_CXX_FLAGS " -s USE_PTHREADS") string(APPEND CMAKE_EXE_LINKER_FLAGS " -s ASYNCIFY") string(APPEND CMAKE_EXE_LINKER_FLAGS " -s PROXY_TO_PTHREAD") endif() - diff --git a/cmake/ftxui_test.cmake b/cmake/ftxui_test.cmake index 4f82259..996e5c3 100644 --- a/cmake/ftxui_test.cmake +++ b/cmake/ftxui_test.cmake @@ -1,28 +1,13 @@ -enable_testing() - -option(FETCHCONTENT_UPDATES_DISCONNECTED TRUE) -option(FETCHCONTENT_QUIET FALSE) -include(FetchContent) - -FetchContent_Declare(googletest - GIT_REPOSITORY "https://github.com/google/googletest" - GIT_TAG 23ef29555ef4789f555f1ba8c51b4c52975f0907 - GIT_PROGRESS TRUE -) -FetchContent_GetProperties(googletest) -if(NOT googletest_POPULATED) - FetchContent_Populate(googletest) - set(BUILD_GMOCK OFF CACHE INTERNAL "") - set(INSTALL_GTEST OFF CACHE INTERNAL "") - set(gtest_force_shared_crt ON CACHE INTERNAL "") - add_subdirectory( - ${googletest_SOURCE_DIR} - ${googletest_BINARY_DIR} - EXCLUDE_FROM_ALL - ) +if (NOT FTXUI_BUILD_TESTS OR + NOT ${CMAKE_VERSION} VERSION_GREATER "3.11.4") + return() endif() -add_executable(tests +enable_testing() + +include(cmake/ftxui_find_google_test.cmake) + +add_executable(ftxui-tests src/ftxui/component/animation_test.cpp src/ftxui/component/button_test.cpp src/ftxui/component/collapsible_test.cpp @@ -63,25 +48,18 @@ add_executable(tests src/ftxui/screen/string_test.cpp ) -target_link_libraries(tests +target_link_libraries(ftxui-tests PRIVATE component - PRIVATE gtest - PRIVATE gtest_main + PRIVATE GTest::gtest + PRIVATE GTest::gtest_main ) -target_include_directories(tests +target_include_directories(ftxui-tests PRIVATE src ) -ftxui_set_options(tests) -target_compile_features(tests PUBLIC cxx_std_20) +ftxui_set_options(ftxui-tests) +target_compile_features(ftxui-tests PUBLIC cxx_std_20) include(GoogleTest) -gtest_discover_tests(tests +gtest_discover_tests(ftxui-tests DISCOVERY_TIMEOUT 600 ) - - -include(cmake/ftxui_benchmark.cmake) - -if (FTXUI_BUILD_TESTS_FUZZER) - include(cmake/ftxui_fuzzer.cmake) -endif() diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt index 3062e2a..4270d5c 100644 --- a/doc/CMakeLists.txt +++ b/doc/CMakeLists.txt @@ -1,22 +1,27 @@ +if(NOT FTXUI_BUILD_DOCS) + return() +endif() + find_package(Doxygen) -if (DOXYGEN_FOUND) - # Generate example list for documentation - set(EXAMPLE_LIST "${CMAKE_CURRENT_BINARY_DIR}/example_list.md") - file(WRITE ${EXAMPLE_LIST} "# Examples") - get_property(EXAMPLES GLOBAL PROPERTY FTXUI::EXAMPLES) - foreach(EXAMPLE IN LISTS EXAMPLES) - file(APPEND ${EXAMPLE_LIST} "\n@example examples/${EXAMPLE}.cpp") - endforeach(EXAMPLE IN LISTS EXAMPLES) - - configure_file(Doxyfile.in Doxyfile @ONLY) - - # note the option ALL which allows to build the docs together with the application - add_custom_target(doc - COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - COMMENT "Generating API documentation with Doxygen" - VERBATIM - ) -else (DOXYGEN_FOUND) +if (NOT DOXYGEN_FOUND) message("Doxygen need to be installed to generate the doxygen documentation") -endif (DOXYGEN_FOUND) + return() +endif() + +# Generate example list for documentation +set(EXAMPLE_LIST "${CMAKE_CURRENT_BINARY_DIR}/example_list.md") +file(WRITE ${EXAMPLE_LIST} "# Examples") +get_property(EXAMPLES GLOBAL PROPERTY FTXUI::EXAMPLES) +foreach(EXAMPLE IN LISTS EXAMPLES) + file(APPEND ${EXAMPLE_LIST} "\n@example examples/${EXAMPLE}.cpp") +endforeach(EXAMPLE IN LISTS EXAMPLES) + +configure_file(Doxyfile.in Doxyfile @ONLY) + +# note the option ALL which allows to build the docs together with the application +add_custom_target(doc + COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Generating API documentation with Doxygen" + VERBATIM + ) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 016aa73..ecfa932 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,3 +1,7 @@ +if(NOT FTXUI_BUILD_EXAMPLES) + return() +endif() + set(EXAMPLES_DIR ${CMAKE_CURRENT_SOURCE_DIR}) function(example name) add_executable(ftxui_example_${name} ${name}.cpp)