Imported Upstream version 3.19.4
[platform/upstream/cmake.git] / Source / Checks / cm_cxx_features.cmake
1 include(${CMAKE_CURRENT_LIST_DIR}/cm_message_checks_compat.cmake)
2
3 function(cm_check_cxx_feature name)
4   set(TRY_RUN_FEATURE "${ARGN}")
5   string(TOUPPER ${name} FEATURE)
6   if(NOT DEFINED CMake_HAVE_CXX_${FEATURE})
7     cm_message_checks_compat(
8       "Checking if compiler supports C++ ${name}"
9       __checkStart __checkPass __checkFail)
10     message(${__checkStart})
11     if(CMAKE_CXX_STANDARD)
12       set(maybe_cxx_standard -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD})
13     else()
14       set(maybe_cxx_standard "")
15     endif()
16     if (TRY_RUN_FEATURE)
17       try_run(CMake_RUN_CXX_${FEATURE} CMake_COMPILE_CXX_${FEATURE}
18         ${CMAKE_CURRENT_BINARY_DIR}
19         ${CMAKE_CURRENT_LIST_DIR}/cm_cxx_${name}.cxx
20         CMAKE_FLAGS ${maybe_cxx_standard}
21         OUTPUT_VARIABLE OUTPUT
22         )
23       if (CMake_RUN_CXX_${FEATURE} EQUAL "0" AND CMake_COMPILE_CXX_${FEATURE})
24         set(CMake_HAVE_CXX_${FEATURE} ON CACHE INTERNAL "TRY_RUN" FORCE)
25       else()
26         set(CMake_HAVE_CXX_${FEATURE} OFF CACHE INTERNAL "TRY_RUN" FORCE)
27       endif()
28     else()
29       try_compile(CMake_HAVE_CXX_${FEATURE}
30         ${CMAKE_CURRENT_BINARY_DIR}
31         ${CMAKE_CURRENT_LIST_DIR}/cm_cxx_${name}.cxx
32         CMAKE_FLAGS ${maybe_cxx_standard}
33         OUTPUT_VARIABLE OUTPUT
34         )
35     endif()
36     set(check_output "${OUTPUT}")
37     # Filter out MSBuild output that looks like a warning.
38     string(REGEX REPLACE " +0 Warning\\(s\\)" "" check_output "${check_output}")
39     # Filter out MSBuild output that looks like a warning.
40     string(REGEX REPLACE "[^\n]*warning MSB[0-9][0-9][0-9][0-9][^\n]*" "" check_output "${check_output}")
41     # Filter out warnings caused by user flags.
42     string(REGEX REPLACE "[^\n]*warning:[^\n]*-Winvalid-command-line-argument[^\n]*" "" check_output "${check_output}")
43     # Filter out warnings caused by local configuration.
44     string(REGEX REPLACE "[^\n]*warning:[^\n]*directory not found for option[^\n]*" "" check_output "${check_output}")
45     string(REGEX REPLACE "[^\n]*warning:[^\n]*object file compiled with -mlong-branch which is no longer needed[^\n]*" "" check_output "${check_output}")
46     # Filter out other warnings unrelated to feature checks.
47     string(REGEX REPLACE "[^\n]*warning:[^\n]*sprintf\\(\\) is often misused, please use snprintf[^\n]*" "" check_output "${check_output}")
48     # Filter out libhugetlbfs warnings.
49     string(REGEX REPLACE "[^\n]*libhugetlbfs [^\n]*: WARNING[^\n]*" "" check_output "${check_output}")
50     # Filter out xcodebuild warnings.
51     string(REGEX REPLACE "[^\n]* xcodebuild\\[[0-9]*:[0-9]*\\] warning: [^\n]*" "" check_output "${check_output}")
52     # Filter out icpc warnings
53     string(REGEX REPLACE "[^\n]*icpc: command line warning #10121: overriding [^\n]*" "" check_output "${check_output}")
54     # Filter out ld warnings.
55     string(REGEX REPLACE "[^\n]*ld: warning: [^\n]*" "" check_output "${check_output}")
56     # If using the feature causes warnings, treat it as broken/unavailable.
57     if(check_output MATCHES "(^|[ :])[Ww][Aa][Rr][Nn][Ii][Nn][Gg]")
58       set(CMake_HAVE_CXX_${FEATURE} OFF CACHE INTERNAL "TRY_COMPILE" FORCE)
59     endif()
60     if(CMake_HAVE_CXX_${FEATURE})
61       message(${__checkPass} "yes")
62       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
63         "Determining if compiler supports C++ ${name} passed with the following output:\n"
64         "${OUTPUT}\n"
65         "\n"
66         )
67     else()
68       message(${__checkFail} "no")
69       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
70         "Determining if compiler supports C++ ${name} failed with the following output:\n"
71         "${OUTPUT}\n"
72         "\n"
73         )
74     endif()
75   endif()
76 endfunction()
77
78 cm_check_cxx_feature(make_unique)
79 if(CMake_HAVE_CXX_MAKE_UNIQUE)
80   set(CMake_HAVE_CXX_UNIQUE_PTR 1)
81 endif()
82 cm_check_cxx_feature(unique_ptr)
83 if (NOT CMAKE_CXX_STANDARD LESS "17")
84   if (NOT CMAKE_CROSSCOMPILING OR CMAKE_CROSSCOMPILING_EMULATOR)
85     cm_check_cxx_feature(filesystem TRY_RUN)
86   else()
87     # In cross-compiling mode, it is not possible to check implementation bugs
88     # so rely only on conformance done by compilation
89     cm_check_cxx_feature(filesystem)
90   endif()
91 else()
92   set(CMake_HAVE_CXX_FILESYSTEM FALSE)
93 endif()