]> git.lyx.org Git - lyx.git/blob - development/cmake/modules/FindCXX11Compiler.cmake
Amend 2d48072e: Get rid of Qt resources
[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 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++14"
58         "--std=c++11"
59         "--std=gnu++11"
60         "--std=gnu++0x"
61       )
62     endif()
63   endif()
64 endif()
65
66 # sample openmp source code to test
67 SET(CXX11_TEST_SOURCE 
68 "
69 template <typename T>
70 struct check
71 {
72     static_assert(sizeof(int) <= sizeof(T), \"not big enough\");
73 };
74
75 typedef check<check<bool>> right_angle_brackets;
76
77 class TestDeleted
78 {
79 public:
80     TestDeleted() = delete;
81 };
82
83
84 int a;
85 decltype(a) b;
86
87 typedef check<int> check_type;
88 check_type c;
89 check_type&& cr = static_cast<check_type&&>(c);
90
91 auto d = a;
92
93 int main() {
94   return 0;
95 };
96 ")
97
98 # The following code snipped taken from example in http://stackoverflow.com/questions/8561850/compile-stdregex-iterator-with-gcc
99 set(REGEX_TEST_SOURCE
100 "
101 #include <regex>
102 #include <iostream>
103
104 #include <string.h>
105
106 typedef std::regex_iterator<const char *> Myiter;
107 int main()
108 {
109     const char *pat = \"axayaz\";
110     Myiter::regex_type rx(\"a\");
111     Myiter next(pat, pat + strlen(pat), rx);
112     Myiter end;
113
114     return (0);
115 }
116 ")
117
118 # check c compiler
119 set(SAFE_CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET})
120 set(CMAKE_REQUIRED_QUIET ON)
121 SET(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
122 FOREACH(FLAG ${CXX11_FLAG_CANDIDATES})
123   IF("${FLAG}" STREQUAL "noflagneeded")
124     UNSET(CMAKE_REQUIRED_FLAGS)
125   ELSE()
126     SET(CMAKE_REQUIRED_FLAGS "${FLAG}")
127   ENDIF()
128   UNSET(CXX11_FLAG_DETECTED CACHE)
129   CHECK_CXX_SOURCE_COMPILES("${CXX11_TEST_SOURCE}" CXX11_FLAG_DETECTED)
130   IF(CXX11_FLAG_DETECTED)
131     SET(CXX11_FLAG "${FLAG}")
132     message(STATUS "CXX11_FLAG_DETECTED = \"${FLAG}\"")
133     set(LYX_USE_CXX11 1)
134       check_cxx_source_compiles("${REGEX_TEST_SOURCE}" CXX_STD_REGEX_DETECTED)
135       if (CXX_STD_REGEX_DETECTED)
136         message(STATUS "Compiler supports std_regex")
137         set(CXX11_STD_REGEX ON)
138       else()
139         message(STATUS "Compiler does not support std_regex")
140         set(CXX11_STD_REGEX OFF)
141       endif()
142     break()
143   ENDIF()
144 ENDFOREACH()
145 SET(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}")
146 set(CMAKE_REQUIRED_QUIET ${SAFE_CMAKE_REQUIRED_QUIET})
147
148 # handle the standard arguments for find_package
149 FIND_PACKAGE_HANDLE_STANDARD_ARGS(CXX11Compiler DEFAULT_MSG CXX11_FLAG)
150 IF("${CXX11_FLAG}" STREQUAL "noflagneeded")
151   SET(CXX11_FLAG "")
152 ENDIF()
153
154 MARK_AS_ADVANCED(CXX11_FLAG CXX11_STD_REGEX)