Debian Codegraph @

cpp/cpp.cmake

raw
1 #
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
18 #
19
20 # Check C++ capabilities.
21
22 include(CheckCXXSourceCompiles)
23
24 macro (cxx_test prog name)
25 check_cxx_source_compiles("${prog}" HAS_${name})
26 if (HAS_${name})
27 list(APPEND CPP_DEFINITIONS "HAS_${name}")
28 else()
29 set(CPP_TEST_FAILED True)
30 endif()
31 endmacro()
32
33 set(CPP_DEFINITIONS "")
34 set(CMAKE_REQUIRED_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_STANDARD} ${CXX_WARNING_FLAGS}")
35 cxx_test("#if defined(__cplusplus) && __cplusplus >= 201103\nint main(int, char**) { return 0; }\n#endif" CPP11)
36 # Don't need to check individual flags if compiler claims to be C++11 or later as they will be set automatically
37 if (NOT HAS_CPP11)
38 set(CPP_TEST_FAILED False)
39 cxx_test("long long ll; int main(int, char**) { return 0; }" LONG_LONG_TYPE)
40 cxx_test("int* x = nullptr; int main(int, char**) { return 0; }" NULLPTR)
41 cxx_test("#include <string>\nvoid blah(std::string&&) {} int main(int, char**) { blah(\"hello\"); return 0; }" RVALUE_REFERENCES)
42 cxx_test("class x {explicit operator int(); }; int main(int, char**) { return 0; }" EXPLICIT_CONVERSIONS)
43 cxx_test("class x {x()=default; }; int main(int, char**) { return 0; }" DEFAULTED_FUNCTIONS)
44 cxx_test("class x {x(x&&)=default; }; int main(int, char**) { return 0; }" DEFAULTED_MOVE_INITIALIZERS)
45 cxx_test("class x {x()=delete; }; int main(int, char**) { return 0; }" DELETED_FUNCTIONS)
46 cxx_test("struct x {x() {}}; int main(int, char**) { static thread_local x foo; return 0; }" THREAD_LOCAL)
47 cxx_test("int main(int, char**) { int a=[](){return 42;}(); return a; }" LAMBDAS)
48 cxx_test("template <class... X> void x(X... a) {} int main(int, char**) { x(1); x(43, \"\"); return 0; }" VARIADIC_TEMPLATES)
49
50 cxx_test("#include <random>\nint main(int, char**) { return 0; }" HEADER_RANDOM)
51 cxx_test("#include <memory>\nstd::unique_ptr<int> u; int main(int, char**) { return 0; }" STD_UNIQUE_PTR)
52 cxx_test("#include <thread>\nstd::thread t; int main(int, char**) { return 0; }" STD_THREAD)
53 cxx_test("#include <mutex>\nstd::mutex m; int main(int, char**) { return 0; }" STD_MUTEX)
54 cxx_test("#include <atomic>\nstd::atomic<int> a; int main(int, char**) { return 0; }" STD_ATOMIC)
55
56 # If all the tests passed this is the same as if we have C++11 for the purposes of compilation
57 # (this shortens the compile command line for VS 2017 significantly)
58 if (NOT CPP_TEST_FAILED)
59 set(CPP_DEFINITIONS "HAS_CPP11")
60 endif()
61 endif()
62 unset(CMAKE_REQUIRED_FLAGS) # Don't contaminate later C tests with C++ flags