Home | History | Annotate | Download | only in test
      1 #------------------------------------------------------------------------------
      2 # Build the google test library
      3 
      4 # We compile Google Test ourselves instead of using pre-compiled libraries.
      5 # See the Google Test FAQ "Why is it not recommended to install a
      6 # pre-compiled copy of Google Test (for example, into /usr/local)?"
      7 # at http://code.google.com/p/googletest/wiki/FAQ for more details.
      8 add_library(gmock STATIC
      9   gmock-gtest-all.cc gmock/gmock.h gtest/gtest.h gtest/gtest-spi.h)
     10 target_compile_options(gmock PUBLIC ${CPP11_FLAG})
     11 target_compile_definitions(gmock PUBLIC GTEST_HAS_STD_WSTRING=1)
     12 target_include_directories(gmock PUBLIC .)
     13 
     14 find_package(Threads)
     15 if (Threads_FOUND)
     16   target_link_libraries(gmock ${CMAKE_THREAD_LIBS_INIT})
     17 else ()
     18   target_compile_definitions(gmock PUBLIC GTEST_HAS_PTHREAD=0)
     19 endif ()
     20 
     21 if (NOT SUPPORTS_VARIADIC_TEMPLATES OR NOT SUPPORTS_INITIALIZER_LIST)
     22   target_compile_definitions(gmock PUBLIC GTEST_LANG_CXX11=0)
     23 endif ()
     24 
     25 # Workaround a bug in implementation of variadic templates in MSVC11.
     26 if (MSVC)
     27   target_compile_definitions(gmock PUBLIC _VARIADIC_MAX=10)
     28 endif ()
     29 
     30 # GTest doesn't detect <tuple> with clang.
     31 if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
     32   target_compile_definitions(gmock PUBLIC GTEST_USE_OWN_TR1_TUPLE=1)
     33 endif ()
     34 
     35 #------------------------------------------------------------------------------
     36 # Build the actual library tests
     37 
     38 set(TEST_MAIN_SRC test-main.cc gtest-extra.cc gtest-extra.h util.cc)
     39 add_library(test-main STATIC ${TEST_MAIN_SRC})
     40 target_compile_definitions(test-main PUBLIC
     41   FMT_USE_FILE_DESCRIPTORS=$<BOOL:${HAVE_OPEN}>)
     42 target_link_libraries(test-main gmock fmt)
     43 
     44 include(CheckCXXCompilerFlag)
     45 
     46 # Workaround GTest bug https://github.com/google/googletest/issues/705.
     47 check_cxx_compiler_flag(
     48   -fno-delete-null-pointer-checks HAVE_FNO_DELETE_NULL_POINTER_CHECKS)
     49 if (HAVE_FNO_DELETE_NULL_POINTER_CHECKS)
     50   target_compile_options(test-main PUBLIC -fno-delete-null-pointer-checks)
     51 endif ()
     52 
     53 # Use less strict pedantic flags for the tests because GMock doesn't compile
     54 # cleanly with -pedantic and -std=c++98.
     55 if (CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
     56   set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -Wno-long-long -Wno-variadic-macros)
     57 endif ()
     58 
     59 function(add_fmt_executable name)
     60   add_executable(${name} ${ARGN})
     61   if (MINGW)
     62     target_link_libraries(${name} -static-libgcc -static-libstdc++)
     63   endif ()
     64 endfunction()
     65 
     66 # Adds a test.
     67 # Usage: add_fmt_test(name srcs...)
     68 function(add_fmt_test name)
     69   add_fmt_executable(${name} ${name}.cc ${ARGN})
     70   target_link_libraries(${name} test-main)
     71 
     72   # define if certain c++ features can be used
     73   target_compile_definitions(${name} PRIVATE
     74     FMT_USE_TYPE_TRAITS=$<BOOL:${SUPPORTS_TYPE_TRAITS}>
     75     FMT_USE_ENUM_BASE=$<BOOL:${SUPPORTS_ENUM_BASE}>)
     76   if (FMT_PEDANTIC)
     77     target_compile_options(${name} PRIVATE ${PEDANTIC_COMPILE_FLAGS})
     78   endif ()
     79   add_test(NAME ${name} COMMAND ${name})
     80 endfunction()
     81 
     82 add_fmt_test(assert-test)
     83 add_fmt_test(gtest-extra-test)
     84 add_fmt_test(format-test)
     85 add_fmt_test(format-impl-test)
     86 add_fmt_test(ostream-test)
     87 add_fmt_test(printf-test)
     88 add_fmt_test(string-test)
     89 add_fmt_test(time-test)
     90 add_fmt_test(util-test mock-allocator.h)
     91 add_fmt_test(macro-test)
     92 add_fmt_test(custom-formatter-test)
     93 
     94 # Enable stricter options for one test to make sure that the header is free of
     95 # warnings.
     96 if (FMT_PEDANTIC AND MSVC)
     97   target_compile_options(format-test PRIVATE /W4)
     98 endif ()
     99 
    100 if (HAVE_OPEN)
    101   add_fmt_executable(posix-mock-test
    102     posix-mock-test.cc ../fmt/format.cc ../fmt/printf.cc ${TEST_MAIN_SRC})
    103   target_include_directories(posix-mock-test PRIVATE ${PROJECT_SOURCE_DIR})
    104   target_compile_definitions(posix-mock-test PRIVATE FMT_USE_FILE_DESCRIPTORS=1)
    105   target_link_libraries(posix-mock-test gmock)
    106   add_test(NAME posix-mock-test COMMAND posix-mock-test)
    107   add_fmt_test(posix-test)
    108 endif ()
    109 
    110 add_fmt_executable(header-only-test
    111   header-only-test.cc header-only-test2.cc test-main.cc)
    112 target_link_libraries(header-only-test gmock)
    113 if (TARGET fmt-header-only)
    114   target_link_libraries(header-only-test fmt-header-only)
    115 else ()
    116   target_include_directories(header-only-test PRIVATE ${PROJECT_SOURCE_DIR})
    117   target_compile_definitions(header-only-test PRIVATE FMT_HEADER_ONLY=1)
    118 endif ()
    119 
    120 # Test that the library can be compiled with exceptions disabled.
    121 check_cxx_compiler_flag(-fno-exceptions HAVE_FNO_EXCEPTIONS_FLAG)
    122 if (HAVE_FNO_EXCEPTIONS_FLAG)
    123   add_library(noexception-test ../fmt/format.cc)
    124   target_include_directories(noexception-test PRIVATE ${PROJECT_SOURCE_DIR})
    125   target_compile_options(noexception-test PRIVATE -fno-exceptions)
    126 endif ()
    127 
    128 if (FMT_PEDANTIC)
    129   # Test that the library compiles without windows.h.
    130   if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
    131     add_library(no-windows-h-test ../fmt/format.cc)
    132     target_include_directories(no-windows-h-test PRIVATE ${PROJECT_SOURCE_DIR})
    133     target_compile_definitions(no-windows-h-test PRIVATE FMT_USE_WINDOWS_H=0)
    134   endif ()
    135 
    136   add_test(compile-test ${CMAKE_CTEST_COMMAND}
    137     --build-and-test
    138     "${CMAKE_CURRENT_SOURCE_DIR}/compile-test"
    139     "${CMAKE_CURRENT_BINARY_DIR}/compile-test"
    140     --build-generator ${CMAKE_GENERATOR}
    141     --build-makeprogram ${CMAKE_MAKE_PROGRAM}
    142     --build-options 
    143     "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
    144     "-DCPP11_FLAG=${CPP11_FLAG}"
    145     "-DSUPPORTS_USER_DEFINED_LITERALS=${SUPPORTS_USER_DEFINED_LITERALS}")
    146 
    147   # test if the targets are findable from the build directory
    148   add_test(find-package-test ${CMAKE_CTEST_COMMAND}
    149     -C ${CMAKE_BUILD_TYPE}
    150     --build-and-test
    151     "${CMAKE_CURRENT_SOURCE_DIR}/find-package-test"
    152     "${CMAKE_CURRENT_BINARY_DIR}/find-package-test"
    153     --build-generator ${CMAKE_GENERATOR}
    154     --build-makeprogram ${CMAKE_MAKE_PROGRAM}
    155     --build-options
    156     "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
    157     "-DFMT_DIR=${PROJECT_BINARY_DIR}"
    158     "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
    159 
    160   # test if the targets are findable when add_subdirectory is used
    161   add_test(add-subdirectory-test ${CMAKE_CTEST_COMMAND}
    162     -C ${CMAKE_BUILD_TYPE}
    163     --build-and-test
    164     "${CMAKE_CURRENT_SOURCE_DIR}/add-subdirectory-test"
    165     "${CMAKE_CURRENT_BINARY_DIR}/add-subdirectory-test"
    166     --build-generator ${CMAKE_GENERATOR}
    167     --build-makeprogram ${CMAKE_MAKE_PROGRAM}
    168     --build-options
    169     "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
    170     "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
    171 endif ()
    172