1 # Test if compile errors are produced where necessary.
3 cmake_minimum_required(VERSION 3.1.0)
5 include(CheckCXXSourceCompiles)
6 include(CheckCXXCompilerFlag)
8 set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/../../include)
9 set(CMAKE_REQUIRED_FLAGS ${CXX_STANDARD_FLAG} ${PEDANTIC_COMPILE_FLAGS})
11 function (generate_source result fragment)
13 #define FMT_HEADER_ONLY 1
14 #include \"fmt/format.h\"
21 function (expect_compile code)
22 generate_source(source "${code}")
23 check_cxx_source_compiles("${source}" compiles)
25 set(error_msg "Compile error for: ${code}")
27 # Unset the CMake cache variable compiles. Otherwise the compile test will
28 # just use cached information next time it runs.
31 message(FATAL_ERROR ${error_msg})
35 function (expect_compile_error code)
36 generate_source(source "${code}")
37 check_cxx_source_compiles("${source}" compiles)
39 set(error_msg "No compile error for: ${code}")
41 # Unset the CMake cache variable compiles. Otherwise the compile test will
42 # just use cached information next time it runs.
45 message(FATAL_ERROR ${error_msg})
49 # check if the source file skeleton compiles
52 # Formatting a wide character with a narrow format string is forbidden.
53 expect_compile_error("fmt::format(\"{}\", L'a');")
55 # Formatting a wide string with a narrow format string is forbidden.
56 expect_compile_error("fmt::format(\"{}\", L\"foo\");")
58 # Formatting a narrow string with a wide format string is forbidden because
59 # mixing UTF-8 with UTF-16/32 can result in an invalid output.
60 expect_compile_error("fmt::format(L\"{}\", \"foo\");")
62 # Formatting a wide string with a narrow format string is forbidden.
63 expect_compile_error("
65 operator std::string() const { return std::string(); }
67 fmt::format(\"{}\", S());
70 # Make sure that compiler features detected in the header
71 # match the features detected in CMake.
72 if (SUPPORTS_USER_DEFINED_LITERALS)
77 expect_compile("#if FMT_USE_USER_DEFINED_LITERALS != ${supports_udl}