]> git.lyx.org Git - features.git/blob - development/cmake/modules/FindCXX11Compiler.cmake
Amend 39996524: Satisfy older cmake versions
[features.git] / development / cmake / modules / FindCXX11Compiler.cmake
1 # This file is part of lyx.
2 #
3 # The module comes from the enblend project, slightly modified.
4 #
5 # Check if compiler supports C++11 features
6 # and which compiler switches are necessary
7 # CXX11_FLAG : contains the necessary compiler flag
8
9 #
10 # Copyright (c) 2013 Thomas Modes <tmodes@@users.sourceforge.net>
11 #
12 #  Redistribution and use in source and binary forms, with or without
13 #  modification, are permitted provided that the following conditions
14 #  are met:
15 #
16 #  1. Redistributions of source code must retain the copyright
17 #         notice, this list of conditions and the following disclaimer.
18 #  2. Redistributions in binary form must reproduce the copyright
19 #         notice, this list of conditions and the following disclaimer in the
20 #         documentation and/or other materials provided with the distribution.
21 #  3. The name of the author may not be used to endorse or promote products
22 #         derived from this software without specific prior written permission.
23 #
24 # This file is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 # GNU General Public License for more details.
28 #
29 # You should have received a copy of the GNU General Public License
30 # along with lyx; if not, write to the Free Software
31 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
32 #
33
34 INCLUDE(CheckCXXSourceCompiles)
35 INCLUDE(FindPackageHandleStandardArgs)
36
37 if (CMAKE_CXX_COMPILER_ID MATCHES "^([cC]lang|AppleClang)$")
38   set(CXX11_FLAG_CANDIDATES "--std=c++11 -Wno-deprecated-register")
39 else()
40   if (CYGWIN)
41     set(CXX11_FLAG_CANDIDATES "--std=gnu++11")
42   else()
43     if (MSVC)
44       # MSVC does not have a general C++11 flag, one can only switch off
45       # MS extensions with /Za in general or by extension with /Zc.
46       # Use an empty flag to ensure that CXX11_STD_REGEX is correctly set.
47       if (MSVC_VERSION LESS 1926)
48         set(CXX11_FLAG_CANDIDATES "noflagneeded")
49       else()
50         set(CXX11_FLAG_CANDIDATES
51           "/std:c++17"
52           "/std:c++14"
53           "noflagneeded")
54       endif()
55     else()
56       set(CXX11_FLAG_CANDIDATES
57         "--std=c++20"
58         "--std=c++17"
59         "--std=c++14"
60         "--std=c++11"
61         "--std=gnu++11"
62         "--std=gnu++0x"
63       )
64     endif()
65   endif()
66 endif()
67
68 # sample openmp source code to test
69 SET(CXX11_TEST_SOURCE 
70 "
71 template <typename T>
72 struct check
73 {
74     static_assert(sizeof(int) <= sizeof(T), \"not big enough\");
75 };
76
77 typedef check<check<bool>> right_angle_brackets;
78
79 class TestDeleted
80 {
81 public:
82     TestDeleted() = delete;
83 };
84
85
86 int a;
87 decltype(a) b;
88
89 typedef check<int> check_type;
90 check_type c;
91 check_type&& cr = static_cast<check_type&&>(c);
92
93 auto d = a;
94
95 int main() {
96   return 0;
97 };
98 ")
99
100 # The following code snipped taken from example in http://stackoverflow.com/questions/8561850/compile-stdregex-iterator-with-gcc
101 set(REGEX_TEST_SOURCE
102 "
103 #include <regex>
104 #include <iostream>
105
106 #include <string.h>
107
108 typedef std::regex_iterator<const char *> Myiter;
109 int main()
110 {
111     const char *pat = \"axayaz\";
112     Myiter::regex_type rx(\"a\");
113     Myiter next(pat, pat + strlen(pat), rx);
114     Myiter end;
115
116     return (0);
117 }
118 ")
119
120 # check c compiler
121 set(SAFE_CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET})
122 set(CMAKE_REQUIRED_QUIET ON)
123 SET(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
124 FOREACH(FLAG ${CXX11_FLAG_CANDIDATES})
125   IF("${FLAG}" STREQUAL "noflagneeded")
126     UNSET(CMAKE_REQUIRED_FLAGS)
127   ELSE()
128     SET(CMAKE_REQUIRED_FLAGS "${FLAG}")
129   ENDIF()
130   UNSET(CXX11_FLAG_DETECTED CACHE)
131   CHECK_CXX_SOURCE_COMPILES("${CXX11_TEST_SOURCE}" CXX11_FLAG_DETECTED)
132   IF(CXX11_FLAG_DETECTED)
133     SET(CXX11_FLAG "${FLAG}")
134     message(STATUS "CXX11_FLAG_DETECTED = \"${FLAG}\"")
135     set(LYX_USE_CXX11 1)
136       check_cxx_source_compiles("${REGEX_TEST_SOURCE}" CXX_STD_REGEX_DETECTED)
137       if (CXX_STD_REGEX_DETECTED)
138         message(STATUS "Compiler supports std_regex")
139         set(CXX11_STD_REGEX ON)
140       else()
141         message(STATUS "Compiler does not support std_regex")
142         set(CXX11_STD_REGEX OFF)
143       endif()
144     break()
145   ENDIF()
146 ENDFOREACH()
147 SET(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
148 set(CMAKE_REQUIRED_QUIET ${SAFE_CMAKE_REQUIRED_QUIET})
149
150 # handle the standard arguments for find_package
151 FIND_PACKAGE_HANDLE_STANDARD_ARGS(CXX11Compiler DEFAULT_MSG CXX11_FLAG)
152 IF("${CXX11_FLAG}" STREQUAL "noflagneeded")
153   SET(CXX11_FLAG "")
154 ENDIF()
155
156 MARK_AS_ADVANCED(CXX11_FLAG CXX11_STD_REGEX)