1# Test if compile errors are produced where necessary.
2
3cmake_minimum_required(VERSION 2.8)
4
5include(CheckCXXSourceCompiles)
6set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/../..)
7set(CMAKE_REQUIRED_FLAGS ${CPP11_FLAG})
8
9function (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)
17endfunction ()
18
19function (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 ()
31endfunction ()
32
33function (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 ()
45endfunction ()
46
47# check if the source file skeleton compiles
48expect_compile("")
49
50# MakeArg doesn't accept [const] volatile char *.
51expect_compile_error("volatile char s[] = \"test\"; (fmt::internal::MakeArg<char>)(s);")
52expect_compile_error("const volatile char s[] = \"test\"; (fmt::internal::MakeArg<char>)(s);")
53
54# MakeArg<char> doesn't accept wchar_t.
55expect_compile_error("fmt::internal::MakeValue<char>(L'a');")
56expect_compile_error("fmt::internal::MakeValue<char>(L\"test\");")
57
58# Writing a wide character to a character stream Writer is forbidden.
59expect_compile_error("fmt::MemoryWriter() << L'a';")
60expect_compile_error("fmt::MemoryWriter() << fmt::pad(\"abc\", 5, L' ');")
61expect_compile_error("fmt::MemoryWriter() << fmt::pad(42, 5, L' ');")
62
63# Formatting a wide character with a narrow format string is forbidden.
64expect_compile_error("fmt::format(\"{}\", L'a';")
65
66expect_compile("FMT_STATIC_ASSERT(true, \"this should never happen\");")
67expect_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.
71if (SUPPORTS_USER_DEFINED_LITERALS)
72  set(supports_udl 1)
73else ()
74  set(supports_udl 0)
75endif ()
76expect_compile("#if FMT_USE_USER_DEFINED_LITERALS != ${supports_udl}
77                # error
78                #endif")
79