From 372ca4f4566ead35c0bf8ef293c4087095460eac Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 23 Jun 2019 12:44:28 -0700 Subject: [PATCH] cmake: Enable googletest unit tests (#1720) Also don't inherit compile warnings from wpiutil There's not a good way to disable inherited compiler flags. --- CMakeLists.txt | 7 +++++++ cameraserver/CMakeLists.txt | 10 ++++++++++ cmake/modules/AddTest.cmake | 14 ++++++++++++++ cmake/modules/CompileWarnings.cmake | 7 +++++++ cscore/CMakeLists.txt | 14 ++++++++++++-- googletest/CMakeLists.txt | 24 ++++++++++++++++++++++++ googletest/CMakeLists.txt.in | 15 +++++++++++++++ hal/CMakeLists.txt | 9 +++++++++ ntcore/CMakeLists.txt | 11 +++++++++++ wpilibc/CMakeLists.txt | 15 +++++++++++++++ wpiutil/CMakeLists.txt | 25 ++++++++++++++++--------- 11 files changed, 140 insertions(+), 11 deletions(-) create mode 100644 cmake/modules/AddTest.cmake create mode 100644 cmake/modules/CompileWarnings.cmake create mode 100644 googletest/CMakeLists.txt create mode 100644 googletest/CMakeLists.txt.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 20557d78ef..c8b8ac40e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ option(WITHOUT_JAVA "don't include java and JNI in the build" OFF) option(BUILD_SHARED_LIBS "build with shared libs (needed for JNI)" ON) option(WITHOUT_CSCORE "Don't build cscore (removes OpenCV requirement)" OFF) option(WITHOUT_ALLWPILIB "Don't build allwpilib (removes OpenCV requirement)" ON) +option(WITH_TESTS "build unit tests (requires internet connection)" OFF) option(USE_EXTERNAL_HAL "Use a separately built HAL" OFF) set(EXTERNAL_HAL_FILE "" CACHE FILEPATH "Location to look for an external HAL CMake File") option(USE_VCPKG_LIBUV "Use vcpkg libuv" OFF) @@ -96,6 +97,12 @@ endif() set(FILENAME_DEP_REPLACE "get_filename_component(SELF_DIR \"$\{CMAKE_CURRENT_LIST_FILE\}\" PATH)") set(SELF_DIR "$\{SELF_DIR\}") +if (WITH_TESTS) + enable_testing() + add_subdirectory(googletest) + include(GoogleTest) +endif() + add_subdirectory(wpiutil) add_subdirectory(ntcore) diff --git a/cameraserver/CMakeLists.txt b/cameraserver/CMakeLists.txt index 769426a2e3..d77cb4e84d 100644 --- a/cameraserver/CMakeLists.txt +++ b/cameraserver/CMakeLists.txt @@ -1,5 +1,8 @@ project(cameraserver) +include(CompileWarnings) +include(AddTest) + find_package( OpenCV REQUIRED ) # Java bindings @@ -32,6 +35,7 @@ set_target_properties(cameraserver PROPERTIES DEBUG_POSTFIX "d") target_include_directories(cameraserver PUBLIC $ $) +wpilib_target_warnings(cameraserver) target_link_libraries(cameraserver PUBLIC ntcore cscore wpiutil ${OpenCV_LIBS}) set_property(TARGET cameraserver PROPERTY FOLDER "libraries") @@ -55,6 +59,12 @@ install(EXPORT cameraserver DESTINATION ${cameraserver_config_dir}) file(GLOB multiCameraServer_src multiCameraServer/src/main/native/cpp/*.cpp) add_executable(multiCameraServer ${multiCameraServer_src}) +wpilib_target_warnings(multiCameraServer) target_link_libraries(multiCameraServer cameraserver) set_property(TARGET multiCameraServer PROPERTY FOLDER "examples") + +if (WITH_TESTS) + wpilib_add_test(cameraserver src/test/native/cpp) + target_link_libraries(cameraserver_test cameraserver gtest) +endif() diff --git a/cmake/modules/AddTest.cmake b/cmake/modules/AddTest.cmake new file mode 100644 index 0000000000..c8ef579faa --- /dev/null +++ b/cmake/modules/AddTest.cmake @@ -0,0 +1,14 @@ +include(CompileWarnings) + +macro(wpilib_add_test name srcdir) + file(GLOB_RECURSE test_src ${srcdir}/*.cpp) + add_executable(${name}_test ${test_src}) + wpilib_target_warnings(${name}_test) + if (BUILD_SHARED_LIBS) + target_compile_definitions(${name}_test PRIVATE -DGTEST_LINKED_AS_SHARED_LIBRARY) + endif() + if (MSVC) + target_compile_options(${name}_test PRIVATE /wd4251 /wd4101) + endif() + add_test(NAME ${name} COMMAND ${name}_test) +endmacro() diff --git a/cmake/modules/CompileWarnings.cmake b/cmake/modules/CompileWarnings.cmake new file mode 100644 index 0000000000..b2a3172001 --- /dev/null +++ b/cmake/modules/CompileWarnings.cmake @@ -0,0 +1,7 @@ +macro(wpilib_target_warnings target) + if(NOT MSVC) + target_compile_options(${target} PRIVATE -Wall -pedantic -Wextra -Werror -Wno-unused-parameter) + else() + target_compile_options(${target} PRIVATE /wd4244 /wd4267 /wd4146 /WX /wd4996) + endif() +endmacro() diff --git a/cscore/CMakeLists.txt b/cscore/CMakeLists.txt index 1a1ac229fa..a87ab7d539 100644 --- a/cscore/CMakeLists.txt +++ b/cscore/CMakeLists.txt @@ -1,6 +1,8 @@ project(cscore) include(SubDirList) +include(CompileWarnings) +include(AddTest) find_package( OpenCV REQUIRED ) @@ -21,14 +23,15 @@ if(NOT MSVC) endif() else() target_sources(cscore PRIVATE ${cscore_windows_src}) - target_compile_options(cscore PUBLIC -DNOMINMAX) - target_compile_options(cscore PRIVATE -D_CRT_SECURE_NO_WARNINGS) + target_compile_definitions(cscore PUBLIC -DNOMINMAX) + target_compile_definitions(cscore PRIVATE -D_CRT_SECURE_NO_WARNINGS) endif() target_include_directories(cscore PUBLIC $ $) target_include_directories(cscore PRIVATE src/main/native/cpp) +wpilib_target_warnings(cscore) target_link_libraries(cscore PUBLIC wpiutil ${OpenCV_LIBS}) set_property(TARGET cscore PROPERTY FOLDER "libraries") @@ -51,6 +54,7 @@ foreach(example ${cscore_examples}) file(GLOB cscore_example_src examples/${example}/*.cpp) if(cscore_example_src) add_executable(cscore_${example} ${cscore_example_src}) + wpilib_target_warnings(cscore_${example}) target_link_libraries(cscore_${example} cscore) set_property(TARGET cscore_${example} PROPERTY FOLDER "examples") endif() @@ -103,6 +107,7 @@ if (NOT WITHOUT_JAVA) set_property(TARGET cscore_jar PROPERTY FOLDER "java") add_library(cscorejni ${cscore_jni_src}) + wpilib_target_warnings(cscorejni) target_link_libraries(cscorejni PUBLIC cscore wpiutil ${OpenCV_LIBS}) set_property(TARGET cscorejni PROPERTY FOLDER "libraries") @@ -122,3 +127,8 @@ if (NOT WITHOUT_JAVA) install(TARGETS cscorejni EXPORT cscorejni DESTINATION "${main_lib_dest}") endif() + +if (WITH_TESTS) + wpilib_add_test(cscore src/test/native/cpp) + target_link_libraries(cscore_test cscore gmock) +endif() diff --git a/googletest/CMakeLists.txt b/googletest/CMakeLists.txt new file mode 100644 index 0000000000..c792212954 --- /dev/null +++ b/googletest/CMakeLists.txt @@ -0,0 +1,24 @@ +# Download and unpack googletest at configure time +configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) +execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) +if(result) + message(FATAL_ERROR "CMake step for googletest failed: ${result}") +endif() +execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) +if(result) + message(FATAL_ERROR "Build step for googletest failed: ${result}") +endif() + +# Prevent overriding the parent project's compiler/linker +# settings on Windows +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + +# Add googletest directly to our build. This defines +# the gtest and gtest_main targets. +add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src + ${CMAKE_CURRENT_BINARY_DIR}/googletest-build + EXCLUDE_FROM_ALL) diff --git a/googletest/CMakeLists.txt.in b/googletest/CMakeLists.txt.in new file mode 100644 index 0000000000..9378f44b33 --- /dev/null +++ b/googletest/CMakeLists.txt.in @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 2.8.2) + +project(googletest-download NONE) + +include(ExternalProject) +ExternalProject_Add(googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG b4676595c03a26bb84f68542c8b74d3d89b38b68 + SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) diff --git a/hal/CMakeLists.txt b/hal/CMakeLists.txt index 7394495db3..03c2696efa 100644 --- a/hal/CMakeLists.txt +++ b/hal/CMakeLists.txt @@ -1,5 +1,7 @@ project(hal) +include(CompileWarnings) +include(AddTest) file(STRINGS src/generate/Instances.txt RAW_INSTANCES) file(STRINGS src/generate/ResourceType.txt RAW_RESOURCE_TYPES) @@ -28,6 +30,7 @@ file(GLOB hal_sim_native_src src/main/native/sim/*.cpp hal_sim_native_src src/main/native/sim/mockdata/*.cpp) add_library(hal ${hal_shared_native_src}) +wpilib_target_warnings(hal) set_target_properties(hal PROPERTIES DEBUG_POSTFIX "d") if(USE_EXTERNAL_HAL) @@ -105,6 +108,7 @@ if (NOT WITHOUT_JAVA) set_target_properties(haljni PROPERTIES OUTPUT_NAME "wpiHaljni") + wpilib_target_warnings(haljni) target_link_libraries(haljni PUBLIC hal wpiutil) set_property(TARGET haljni PROPERTY FOLDER "libraries") @@ -124,3 +128,8 @@ if (NOT WITHOUT_JAVA) install(TARGETS haljni EXPORT haljni DESTINATION "${main_lib_dest}") endif() + +if (WITH_TESTS) + wpilib_add_test(hal src/test/native/cpp) + target_link_libraries(hal_test hal gtest) +endif() diff --git a/ntcore/CMakeLists.txt b/ntcore/CMakeLists.txt index 1c00da1388..1852702581 100644 --- a/ntcore/CMakeLists.txt +++ b/ntcore/CMakeLists.txt @@ -1,5 +1,8 @@ project(ntcore) +include(CompileWarnings) +include(AddTest) + file(GLOB ntcore_native_src src/main/native/cpp/*.cpp ntcore_native_src src/main/native/cpp/networktables/*.cpp @@ -9,6 +12,7 @@ set_target_properties(ntcore PROPERTIES DEBUG_POSTFIX "d") target_include_directories(ntcore PUBLIC $ $) +wpilib_target_warnings(ntcore) target_link_libraries(ntcore PUBLIC wpiutil) set_property(TARGET ntcore PROPERTY FOLDER "libraries") @@ -52,6 +56,7 @@ if (NOT WITHOUT_JAVA) set_property(TARGET ntcore_jar PROPERTY FOLDER "java") add_library(ntcorejni ${ntcore_jni_src}) + wpilib_target_warnings(ntcorejni) target_link_libraries(ntcorejni PUBLIC ntcore wpiutil) set_property(TARGET ntcorejni PROPERTY FOLDER "libraries") @@ -71,3 +76,9 @@ if (NOT WITHOUT_JAVA) install(TARGETS ntcorejni EXPORT ntcorejni DESTINATION "${main_lib_dest}") endif() + +if (WITH_TESTS) + wpilib_add_test(ntcore src/test/native/cpp) + target_include_directories(ntcore_test PRIVATE src/main/native/cpp) + target_link_libraries(ntcore_test ntcore gmock_main) +endif() diff --git a/wpilibc/CMakeLists.txt b/wpilibc/CMakeLists.txt index 7b131838b4..fcbb11a558 100644 --- a/wpilibc/CMakeLists.txt +++ b/wpilibc/CMakeLists.txt @@ -1,5 +1,8 @@ project(wpilibc) +include(CompileWarnings) +include(AddTest) + find_package( OpenCV REQUIRED ) configure_file(src/generate/WPILibVersion.cpp.in WPILibVersion.cpp) @@ -13,6 +16,7 @@ set_target_properties(wpilibc PROPERTIES DEBUG_POSTFIX "d") target_include_directories(wpilibc PUBLIC $ $) +wpilib_target_warnings(wpilibc) target_link_libraries(wpilibc PUBLIC cameraserver hal ntcore cscore wpiutil ${OpenCV_LIBS}) set_property(TARGET wpilibc PROPERTY FOLDER "libraries") @@ -29,3 +33,14 @@ endif() configure_file(wpilibc-config.cmake.in ${CMAKE_BINARY_DIR}/wpilibc-config.cmake ) install(FILES ${CMAKE_BINARY_DIR}/wpilibc-config.cmake DESTINATION ${wpilibc_config_dir}) install(EXPORT wpilibc DESTINATION ${wpilibc_config_dir}) + +if (WITH_TESTS) + wpilib_add_test(wpilibc src/test/native/cpp) + target_include_directories(wpilibc_test PRIVATE src/test/native/include) + target_link_libraries(wpilibc_test wpilibc gmock_main) + if (NOT MSVC) + target_compile_options(wpilibc_test PRIVATE -Wno-error) + else() + target_compile_options(wpilibc_test PRIVATE /WX-) + endif() +endif() diff --git a/wpiutil/CMakeLists.txt b/wpiutil/CMakeLists.txt index 1f379895a2..a904cc05b9 100644 --- a/wpiutil/CMakeLists.txt +++ b/wpiutil/CMakeLists.txt @@ -2,6 +2,8 @@ project(wpiutil) include(SubDirList) include(GenResources) +include(CompileWarnings) +include(AddTest) # Java bindings if (NOT WITHOUT_JAVA) @@ -78,14 +80,11 @@ set_target_properties(wpiutil PROPERTIES DEBUG_POSTFIX "d") set_property(TARGET wpiutil PROPERTY FOLDER "libraries") target_compile_features(wpiutil PUBLIC cxx_std_17) -if (NOT MSVC) - target_compile_options(wpiutil PUBLIC $) -else() - target_compile_options(wpiutil PUBLIC /wd4244 /wd4267 /wd4146) - target_compile_options(wpiutil PUBLIC $) - target_compile_options(wpiutil PRIVATE -D_CRT_SECURE_NO_WARNINGS) +if (MSVC) + target_compile_options(wpiutil PUBLIC /permissive- /Zc:throwingNew) + target_compile_definitions(wpiutil PRIVATE -D_CRT_SECURE_NO_WARNINGS) endif() - +wpilib_target_warnings(wpiutil) target_link_libraries(wpiutil Threads::Threads ${CMAKE_DL_LIBS}) if (NOT USE_VCPKG_LIBUV) @@ -103,11 +102,11 @@ if (NOT USE_VCPKG_LIBUV) else() target_sources(wpiutil PRIVATE ${uv_linux_src}) endif() - target_compile_options(wpiutil PRIVATE -D_GNU_SOURCE) + target_compile_definitions(wpiutil PRIVATE -D_GNU_SOURCE) else() target_sources(wpiutil PRIVATE ${uv_windows_src}) if(BUILD_SHARED_LIBS) - target_compile_options(wpiutil PRIVATE -DBUILDING_UV_SHARED) + target_compile_definitions(wpiutil PRIVATE -DBUILDING_UV_SHARED) endif() endif() else() @@ -141,6 +140,7 @@ foreach(example ${wpiutil_examples}) file(GLOB wpiutil_example_src examples/${example}/*.cpp) if(wpiutil_example_src) add_executable(wpiutil_${example} ${wpiutil_example_src}) + wpilib_target_warnings(wpiutil_${example}) target_link_libraries(wpiutil_${example} wpiutil) set_property(TARGET wpiutil_${example} PROPERTY FOLDER "examples") endif() @@ -154,11 +154,18 @@ endif() file(GLOB netconsoleServer_src src/netconsoleServer/native/cpp/*.cpp) add_executable(netconsoleServer ${netconsoleServer_src}) +wpilib_target_warnings(netconsoleServer) target_link_libraries(netconsoleServer wpiutil ${LIBUTIL}) file(GLOB netconsoleTee_src src/netconsoleTee/native/cpp/*.cpp) add_executable(netconsoleTee ${netconsoleTee_src}) +wpilib_target_warnings(netconsoleTee) target_link_libraries(netconsoleTee wpiutil) set_property(TARGET netconsoleServer PROPERTY FOLDER "examples") set_property(TARGET netconsoleTee PROPERTY FOLDER "examples") + +if (WITH_TESTS) + wpilib_add_test(wpiutil src/test/native/cpp) + target_link_libraries(wpiutil_test wpiutil ${LIBUTIL} gmock_main) +endif()