Home | History | Annotate | Download | only in compile-test
      1 # Test if compile errors are produced where necessary.
      2 
      3 cmake_minimum_required(VERSION 2.8)
      4 
      5 include(CheckCXXSourceCompiles)
      6 set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/../..)
      7 set(CMAKE_REQUIRED_FLAGS ${CPP11_FLAG})
      8 
      9 function (generate_source result fragment)
     10   set(${result} "
     11   #define FMT_HEADER_ONLY 1
     12   #include \"fmt/posix.h\"
     13   int main() {
     14     ${fragment}
     15   }
     16   " PARENT_SCOPE)
     17 endfunction ()
     18 
     19 function (expect_compile code)
     20   generate_source(source "${code}")
     21   check_cxx_source_compiles("${source}" compiles)
     22   if (NOT compiles)
     23     set(error_msg "Compile error for: ${code}")
     24   endif ()
     25   # Unset the CMake cache variable compiles. Otherwise the compile test will
     26   # just use cached information next time it runs.
     27   unset(compiles CACHE)
     28   if (error_msg)
     29     message(FATAL_ERROR ${error_msg})
     30   endif ()
     31 endfunction ()
     32 
     33 function (expect_compile_error code)
     34   generate_source(source "${code}")
     35   check_cxx_source_compiles("${source}" compiles)
     36   if (compiles)
     37     set(error_msg "No compile error for: ${code}")
     38   endif ()
     39   # Unset the CMake cache variable compiles. Otherwise the compile test will
     40   # just use cached information next time it runs.
     41   unset(compiles CACHE)
     42   if (error_msg)
     43     message(FATAL_ERROR ${error_msg})
     44   endif ()
     45 endfunction ()
     46 
     47 # check if the source file skeleton compiles
     48 expect_compile("")
     49 
     50 # MakeArg doesn't accept [const] volatile char *.
     51 expect_compile_error("volatile char s[] = \"test\"; (fmt::internal::MakeArg<char>)(s);")
     52 expect_compile_error("const volatile char s[] = \"test\"; (fmt::internal::MakeArg<char>)(s);")
     53 
     54 # MakeArg<char> doesn't accept wchar_t.
     55 expect_compile_error("fmt::internal::MakeValue<char>(L'a');")
     56 expect_compile_error("fmt::internal::MakeValue<char>(L\"test\");")
     57 
     58 # Writing a wide character to a character stream Writer is forbidden.
     59 expect_compile_error("fmt::MemoryWriter() << L'a';")
     60 expect_compile_error("fmt::MemoryWriter() << fmt::pad(\"abc\", 5, L' ');")
     61 expect_compile_error("fmt::MemoryWriter() << fmt::pad(42, 5, L' ');")
     62 
     63 # Formatting a wide character with a narrow format string is forbidden.
     64 expect_compile_error("fmt::format(\"{}\", L'a';")
     65 
     66 expect_compile("FMT_STATIC_ASSERT(true, \"this should never happen\");")
     67 expect_compile_error("FMT_STATIC_ASSERT(0 > 1, \"oops\");")
     68 
     69 # Make sure that compiler features detected in the header
     70 # match the features detected in CMake.
     71 if (SUPPORTS_USER_DEFINED_LITERALS)
     72   set(supports_udl 1)
     73 else ()
     74   set(supports_udl 0)
     75 endif ()
     76 expect_compile("#if FMT_USE_USER_DEFINED_LITERALS != ${supports_udl}
     77                 # error
     78                 #endif")
     79