From d85fe40f5978642d82ebda9b9f4d57a6beb47d3f Mon Sep 17 00:00:00 2001 From: Hans-Christian Ebke Date: Mon, 9 Nov 2015 18:04:43 +0100 Subject: [PATCH] Added build support for C++11. FindCXX11.cmake copied from OpenFlipper. --- CMakeLists.txt | 10 +++++ cmake/FindCXX11.cmake | 71 ++++++++++++++++++++++++++++++++++++ src/Unittests/CMakeLists.txt | 10 +++++ 3 files changed, 91 insertions(+) create mode 100644 cmake/FindCXX11.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index b8eb0f39..b1d57c19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,16 @@ include (ACGOutput) # Definitions # ======================================================================== +set(INHIBIT_CXX11_SUPPORT OFF CACHE BOOL "Do not use C++11 even if it is available. (This is mainly useful for unit testing.)") + +if (NOT INHIBIT_CXX11_SUPPORT) +find_package (CXX11) + +if (CXX11_FOUND) + add_definitions(${CXX11_FLAGS} -DCPP11_ENABLED) +endif (CXX11_FOUND) +endif (NOT INHIBIT_CXX11_SUPPORT) + if (WIN32) add_definitions( -D_USE_MATH_DEFINES -DNOMINMAX diff --git a/cmake/FindCXX11.cmake b/cmake/FindCXX11.cmake new file mode 100644 index 00000000..a1fe935c --- /dev/null +++ b/cmake/FindCXX11.cmake @@ -0,0 +1,71 @@ +IF (NOT CXX11_FLAGS OR NOT CXX11_FOUND) +include(CheckCXXSourceCompiles) +include(FindPackageHandleStandardArgs) + +set(CXX11_FLAG_CANDIDATES + #Gnu and Intel Linux + "-std=c++11" + #Microsoft Visual Studio, and everything that automatically accepts C++11 + " " + #Intel windows + "/Qstd=c++0x" + ) + +set(CXX11_TEST_SOURCE +" +#include +#include +#include + +class Matrix { +public: + // Initializer lists + Matrix(int a, int b, int c, int d) + : data {a, b, c, d} + { + // Lambda functions + std::transform(data, data+4, data, + [](int x) { return x+1; }); + } + + // rvalue references + Matrix(Matrix &&rhs) { + } + +private: + int data[4]; +}; + +int main() +{ + int n[] {4,7,6,1,2}; + // auto + for (auto i : n) { + Matrix mat (3,5,1,2); + + // std::move, unique_ptr + std::unique_ptr m2( + new Matrix(std::move(mat))); + } + return 0; +} +") + +foreach(FLAG ${CXX11_FLAG_CANDIDATES}) + set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") + set(CMAKE_REQUIRED_FLAGS "${FLAG}") + unset(CXX11_FLAG_DETECTED CACHE) + + check_cxx_source_compiles("${CXX11_TEST_SOURCE}" CXX11_FLAG_DETECTED) + set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}") + if(CXX11_FLAG_DETECTED) + set(CXX11_FLAGS_INTERNAL "${FLAG}") + break() + endif(CXX11_FLAG_DETECTED) +endforeach(FLAG ${CXX11_FLAG_CANDIDATES}) + +set(CXX11_FLAGS "${CXX11_FLAGS_INTERNAL}") + +find_package_handle_standard_args(CXX11 DEFAULT_MSG CXX11_FLAGS) +mark_as_advanced(CXX11_FLAGS) +ENDIF() \ No newline at end of file diff --git a/src/Unittests/CMakeLists.txt b/src/Unittests/CMakeLists.txt index 270f4242..a629979d 100644 --- a/src/Unittests/CMakeLists.txt +++ b/src/Unittests/CMakeLists.txt @@ -22,6 +22,16 @@ if ( OPENMESH_BUILD_UNIT_TESTS ) # set additional link directories link_directories(${GTEST_LIBRARY_DIR} ) + set(INHIBIT_CXX11_SUPPORT OFF CACHE BOOL "Do not use C++11 even if it is available. (This is mainly useful for unit testing.)") + + if (NOT INHIBIT_CXX11_SUPPORT) + find_package (CXX11) + + if (CXX11_FOUND) + add_definitions(${CXX11_FLAGS} -DCPP11_ENABLED) + endif (CXX11_FOUND) + endif (NOT INHIBIT_CXX11_SUPPORT) + if ( CMAKE_GENERATOR MATCHES "^Visual Studio 11.*" ) add_definitions( /D _VARIADIC_MAX=10 ) endif()