]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | # Test if compile errors are produced where necessary. |
2 | ||
20effc67 | 3 | cmake_minimum_required(VERSION 3.1...3.18) |
11fdf7f2 TL |
4 | |
5 | include(CheckCXXSourceCompiles) | |
6 | include(CheckCXXCompilerFlag) | |
7 | ||
8 | set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/../../include) | |
9 | set(CMAKE_REQUIRED_FLAGS ${CXX_STANDARD_FLAG} ${PEDANTIC_COMPILE_FLAGS}) | |
10 | ||
11 | function (generate_source result fragment) | |
12 | set(${result} " | |
13 | #define FMT_HEADER_ONLY 1 | |
14 | #include \"fmt/format.h\" | |
15 | int main() { | |
16 | ${fragment} | |
17 | } | |
18 | " PARENT_SCOPE) | |
19 | endfunction () | |
20 | ||
21 | function (expect_compile code) | |
22 | generate_source(source "${code}") | |
23 | check_cxx_source_compiles("${source}" compiles) | |
24 | if (NOT compiles) | |
25 | set(error_msg "Compile error for: ${code}") | |
26 | endif () | |
27 | # Unset the CMake cache variable compiles. Otherwise the compile test will | |
28 | # just use cached information next time it runs. | |
29 | unset(compiles CACHE) | |
30 | if (error_msg) | |
31 | message(FATAL_ERROR ${error_msg}) | |
32 | endif () | |
33 | endfunction () | |
34 | ||
35 | function (expect_compile_error code) | |
36 | generate_source(source "${code}") | |
37 | check_cxx_source_compiles("${source}" compiles) | |
38 | if (compiles) | |
39 | set(error_msg "No compile error for: ${code}") | |
40 | endif () | |
41 | # Unset the CMake cache variable compiles. Otherwise the compile test will | |
42 | # just use cached information next time it runs. | |
43 | unset(compiles CACHE) | |
44 | if (error_msg) | |
45 | message(FATAL_ERROR ${error_msg}) | |
46 | endif () | |
47 | endfunction () | |
48 | ||
49 | # check if the source file skeleton compiles | |
50 | expect_compile("") | |
51 | ||
52 | # Formatting a wide character with a narrow format string is forbidden. | |
53 | expect_compile_error("fmt::format(\"{}\", L'a');") | |
54 | ||
55 | # Formatting a wide string with a narrow format string is forbidden. | |
56 | expect_compile_error("fmt::format(\"{}\", L\"foo\");") | |
57 | ||
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\");") | |
61 | ||
62 | # Formatting a wide string with a narrow format string is forbidden. | |
63 | expect_compile_error(" | |
64 | struct S { | |
65 | operator std::string() const { return std::string(); } | |
66 | }; | |
67 | fmt::format(\"{}\", S()); | |
68 | ") | |
69 | ||
70 | # Make sure that compiler features detected in the header | |
71 | # match the features detected in CMake. | |
72 | if (SUPPORTS_USER_DEFINED_LITERALS) | |
73 | set(supports_udl 1) | |
74 | else () | |
75 | set(supports_udl 0) | |
76 | endif () | |
77 | expect_compile("#if FMT_USE_USER_DEFINED_LITERALS != ${supports_udl} | |
78 | # error | |
79 | #endif") |