From 6d8215ecb69f8efd57eefd06c02bc59fe957b4d9 Mon Sep 17 00:00:00 2001 From: Azdul Date: Sun, 20 Jan 2019 14:36:48 +0100 Subject: [PATCH] Google Test / Google Mock / OpenMW unit tests on Windows --- CI/before_script.msvc.sh | 63 ++++++++++++++++++++ apps/openmw_test_suite/CMakeLists.txt | 9 +++ apps/openmw_test_suite/openmw_test_suite.cpp | 6 ++ 3 files changed, 78 insertions(+) diff --git a/CI/before_script.msvc.sh b/CI/before_script.msvc.sh index 4fd352f7d..b5bf0b632 100644 --- a/CI/before_script.msvc.sh +++ b/CI/before_script.msvc.sh @@ -34,6 +34,8 @@ VS_VERSION="" NMAKE="" PLATFORM="" CONFIGURATION="" +TEST_FRAMEWORK="" +GOOGLE_INSTALL_ROOT="" while [ $# -gt 0 ]; do ARGSTR=$1 @@ -78,6 +80,9 @@ while [ $# -gt 0 ]; do CONFIGURATION=$1 shift ;; + t ) + TEST_FRAMEWORK=true ;; + h ) cat < Set the build platform, can also be set with environment variable PLATFORM. + -t + Build unit tests / Google test -u Configure for unity builds. -v <2013/2015/2017> @@ -395,6 +402,16 @@ if [ -z $SKIP_DOWNLOAD ]; then download "SDL 2.0.7" \ "https://www.libsdl.org/release/SDL2-devel-2.0.7-VC.zip" \ "SDL2-2.0.7.zip" + + # Google test and mock + if [ ! -z $TEST_FRAMEWORK ]; then + echo "Google test 1.8.1..." + if [ -d googletest ]; then + printf " Google test exists, skipping." + else + git clone -b release-1.8.1 https://github.com/google/googletest.git + fi + fi fi cd .. #/.. @@ -670,6 +687,52 @@ printf "SDL 2.0.7... " add_runtime_dlls "$(pwd)/SDL2-2.0.7/lib/x${ARCHSUFFIX}/SDL2.dll" echo Done. } +cd $DEPS +echo +# Google Test and Google Mock +if [ ! -z $TEST_FRAMEWORK ]; then + printf "Google test 1.8.1 ..." + + cd googletest + if [ ! -d build ]; then + mkdir build + fi + + cd build + + GOOGLE_INSTALL_ROOT="${DEPS_INSTALL}/GoogleTest" + if [ $CONFIGURATION == "Debug" ]; then + DEBUG_SUFFIX="d" + else + DEBUG_SUFFIX="" + fi + + if [ ! -d $GOOGLE_INSTALL_ROOT ]; then + + cmake .. -DCMAKE_BUILD_TYPE="${CONFIGURATION}" -DCMAKE_INSTALL_PREFIX="${GOOGLE_INSTALL_ROOT}" -DCMAKE_USE_WIN32_THREADS_INIT=1 -G "${GENERATOR}" -DBUILD_SHARED_LIBS=1 + cmake --build . --config "${CONFIGURATION}" + cmake --build . --target install --config "${CONFIGURATION}" + + add_runtime_dlls "${GOOGLE_INSTALL_ROOT}\bin\gtest_main${DEBUG_SUFFIX}.dll" + add_runtime_dlls "${GOOGLE_INSTALL_ROOT}\bin\gtest${DEBUG_SUFFIX}.dll" + add_runtime_dlls "${GOOGLE_INSTALL_ROOT}\bin\gmock_main${DEBUG_SUFFIX}.dll" + add_runtime_dlls "${GOOGLE_INSTALL_ROOT}\bin\gmock${DEBUG_SUFFIX}.dll" + fi + + add_cmake_opts -DBUILD_UNITTESTS=yes + # FindGTest and FindGMock do not work perfectly on Windows + # but we can help them by telling them everything we know about installation + add_cmake_opts -DGMOCK_ROOT="$GOOGLE_INSTALL_ROOT" + add_cmake_opts -DGTEST_ROOT="$GOOGLE_INSTALL_ROOT" + add_cmake_opts -DGTEST_LIBRARY="$GOOGLE_INSTALL_ROOT/lib/gtest${DEBUG_SUFFIX}.lib" + add_cmake_opts -DGTEST_MAIN_LIBRARY="$GOOGLE_INSTALL_ROOT/lib/gtest_main${DEBUG_SUFFIX}.lib" + add_cmake_opts -DGMOCK_LIBRARY="$GOOGLE_INSTALL_ROOT/lib/gmock${DEBUG_SUFFIX}.lib" + add_cmake_opts -DGMOCK_MAIN_LIBRARY="$GOOGLE_INSTALL_ROOT/lib/gmock_main${DEBUG_SUFFIX}.lib" + add_cmake_opts -DGTEST_LINKED_AS_SHARED_LIBRARY=True + echo Done. + +fi + echo cd $DEPS_INSTALL/.. echo diff --git a/apps/openmw_test_suite/CMakeLists.txt b/apps/openmw_test_suite/CMakeLists.txt index 7f10ab2fb..e8b30815b 100644 --- a/apps/openmw_test_suite/CMakeLists.txt +++ b/apps/openmw_test_suite/CMakeLists.txt @@ -40,4 +40,13 @@ if (GTEST_FOUND AND GMOCK_FOUND) add_definitions(--coverage) target_link_libraries(openmw_test_suite gcov) endif() + + if (MSVC) + if (CMAKE_CL_64) + # Debug version of openmw_unit_tests needs increased number of sections beyond 2^16 + # just like openmw and openmw-cs + set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj") + endif (CMAKE_CL_64) + endif (MSVC) + endif() diff --git a/apps/openmw_test_suite/openmw_test_suite.cpp b/apps/openmw_test_suite/openmw_test_suite.cpp index 7cc76b25b..7364b20fd 100644 --- a/apps/openmw_test_suite/openmw_test_suite.cpp +++ b/apps/openmw_test_suite/openmw_test_suite.cpp @@ -1,6 +1,12 @@ #include +#ifdef WIN32 +//we cannot use GTEST_API_ before main if we're building standalone exe application, +//and we're linking GoogleTest / GoogleMock as DLLs and not linking gtest_main / gmock_main +int main(int argc, char **argv) { +#else GTEST_API_ int main(int argc, char **argv) { +#endif testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }