]> git.lyx.org Git - lyx.git/blob - development/cmake/modules/FindCXX11Compiler.cmake
Fix screen display of parts and chapters in default classes
[lyx.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 # get cmake-known std numbers
38 # Detection of c++20 works well, but our code is not ready for it yet.
39 # We currently get errors with internal boost and also from our code.
40 set(_max_std_num 17)
41 lyxgetknowncmakestd(${_max_std_num} tmpnums)
42
43 if (CMAKE_CXX_COMPILER_ID MATCHES "^([cC]lang|AppleClang)$")
44   foreach(_num ${tmpnums})
45     list(APPEND CXX11_FLAG_CANDIDATES "--std=c++${_num} -Wno-deprecated-register")
46   endforeach()
47 else()
48   if (CYGWIN)
49     foreach(_num ${tmpnums})
50       list(APPEND CXX11_FLAG_CANDIDATES "--std=gnu++${_num}")
51     endforeach()
52   else()
53     if (MSVC)
54       # MSVC does not have a general C++11 flag, one can only switch off
55       # MS extensions with /Za in general or by extension with /Zc.
56       # Use an empty flag to ensure that CXX11_STD_REGEX is correctly set.
57       if (MSVC_VERSION LESS 1926)
58         set(CXX11_FLAG_CANDIDATES "noflagneeded")
59       else()
60         foreach(_num ${tmpnums})
61           list(APPEND CXX11_FLAG_CANDIDATES "/std:c++${_num}")
62         endforeach()
63         list(APPEND CXX11_FLAG_CANDIDATES "noflagneeded")
64       endif()
65     else()
66       set(CXX11_FLAG_CANDIDATES)
67       foreach(_num ${tmpnums})
68         list(APPEND CXX11_FLAG_CANDIDATES "--std=c++${_num}")
69       endforeach()
70     endif()
71   endif()
72 endif()
73
74 # sample openmp source code to test
75 SET(CXX11_TEST_SOURCE 
76 "
77 template <typename T>
78 struct check
79 {
80     static_assert(sizeof(int) <= sizeof(T), \"not big enough\");
81 };
82
83 typedef check<check<bool>> right_angle_brackets;
84
85 class TestDeleted
86 {
87 public:
88     TestDeleted() = delete;
89 };
90
91
92 int a;
93 decltype(a) b;
94
95 typedef check<int> check_type;
96 check_type c;
97 check_type&& cr = static_cast<check_type&&>(c);
98
99 auto d = a;
100
101 int main() {
102   return 0;
103 };
104 ")
105
106 # The following code snipped taken from example in http://stackoverflow.com/questions/8561850/compile-stdregex-iterator-with-gcc
107 set(REGEX_TEST_SOURCE
108 "
109 #include <regex>
110 #include <iostream>
111
112 #include <string.h>
113
114 typedef std::regex_iterator<const char *> Myiter;
115 int main()
116 {
117     const char *pat = \"axayaz\";
118     Myiter::regex_type rx(\"a\");
119     Myiter next(pat, pat + strlen(pat), rx);
120     Myiter end;
121
122     return (0);
123 }
124 ")
125
126 # check c compiler
127 set(SAFE_CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET})
128 set(CMAKE_REQUIRED_QUIET ON)
129 SET(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
130 FOREACH(FLAG ${CXX11_FLAG_CANDIDATES})
131   IF("${FLAG}" STREQUAL "noflagneeded")
132     UNSET(CMAKE_REQUIRED_FLAGS)
133   ELSE()
134     SET(CMAKE_REQUIRED_FLAGS "${FLAG}")
135   ENDIF()
136   UNSET(CXX11_FLAG_DETECTED CACHE)
137   CHECK_CXX_SOURCE_COMPILES("${CXX11_TEST_SOURCE}" CXX11_FLAG_DETECTED)
138   IF(CXX11_FLAG_DETECTED)
139     SET(CXX11_FLAG "${FLAG}")
140     message(STATUS "CXX11_FLAG_DETECTED = \"${FLAG}\"")
141     set(LYX_USE_CXX11 1)
142       check_cxx_source_compiles("${REGEX_TEST_SOURCE}" CXX_STD_REGEX_DETECTED)
143       if (CXX_STD_REGEX_DETECTED)
144         message(STATUS "Compiler supports std_regex")
145         set(CXX11_STD_REGEX ON)
146       else()
147         message(STATUS "Compiler does not support std_regex")
148         set(CXX11_STD_REGEX OFF)
149       endif()
150     break()
151   ENDIF()
152 ENDFOREACH()
153 SET(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
154 set(CMAKE_REQUIRED_QUIET ${SAFE_CMAKE_REQUIRED_QUIET})
155
156 # handle the standard arguments for find_package
157 FIND_PACKAGE_HANDLE_STANDARD_ARGS(CXX11Compiler DEFAULT_MSG CXX11_FLAG)
158 IF("${CXX11_FLAG}" STREQUAL "noflagneeded")
159   SET(CXX11_FLAG "")
160 ENDIF()
161
162 MARK_AS_ADVANCED(CXX11_FLAG CXX11_STD_REGEX)