• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Test if compile errors are produced where necessary.
2
3cmake_minimum_required(VERSION 3.1...3.18)
4
5include(CheckCXXSourceCompiles)
6include(CheckCXXCompilerFlag)
7
8set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/../../include)
9set(CMAKE_REQUIRED_FLAGS ${CXX_STANDARD_FLAG} ${PEDANTIC_COMPILE_FLAGS})
10
11function (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)
19endfunction ()
20
21function (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 ()
33endfunction ()
34
35function (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 ()
47endfunction ()
48
49# check if the source file skeleton compiles
50expect_compile("")
51
52# Formatting a wide character with a narrow format string is forbidden.
53expect_compile_error("fmt::format(\"{}\", L'a');")
54
55# Formatting a wide string with a narrow format string is forbidden.
56expect_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.
60expect_compile_error("fmt::format(L\"{}\", \"foo\");")
61
62# Formatting a wide string with a narrow format string is forbidden.
63expect_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.
72if (SUPPORTS_USER_DEFINED_LITERALS)
73  set(supports_udl 1)
74else ()
75  set(supports_udl 0)
76endif ()
77expect_compile("#if FMT_USE_USER_DEFINED_LITERALS != ${supports_udl}
78                # error
79                #endif")
80