]> code.bitgloo.com Git - clyne/entityx.git/commitdiff
Add C++11 feature checking to cmake.
authorAlec Thomas <alec@swapoff.org>
Thu, 18 Oct 2012 17:21:06 +0000 (13:21 -0400)
committerAlec Thomas <alec@swapoff.org>
Thu, 18 Oct 2012 17:21:06 +0000 (13:21 -0400)
17 files changed:
CMakeLists.txt
CheckCXX11Features.cmake [new file with mode: 0644]
c++11/c++11-test-__func__-N2340.cpp [new file with mode: 0644]
c++11/c++11-test-auto-N2546.cpp [new file with mode: 0644]
c++11/c++11-test-constexpr-N2235.cpp [new file with mode: 0644]
c++11/c++11-test-cstdint.cpp [new file with mode: 0644]
c++11/c++11-test-decltype-N2343.cpp [new file with mode: 0644]
c++11/c++11-test-lambda-N2927.cpp [new file with mode: 0644]
c++11/c++11-test-long_long-N1811.cpp [new file with mode: 0644]
c++11/c++11-test-nullptr-N2431.cpp [new file with mode: 0644]
c++11/c++11-test-nullptr-N2431_fail_compile.cpp [new file with mode: 0644]
c++11/c++11-test-rvalue_references-N2118.cpp [new file with mode: 0644]
c++11/c++11-test-sizeof_member-N2253.cpp [new file with mode: 0644]
c++11/c++11-test-static_assert-N1720.cpp [new file with mode: 0644]
c++11/c++11-test-static_assert-N1720_fail_compile.cpp [new file with mode: 0644]
c++11/c++11-test-variadic_templates-N2555.cpp [new file with mode: 0644]
c++11/demo.cpp [new file with mode: 0644]

index bb94909f95496ff1c9b325416bfeb14bcbe6f76c..8075f237b5a7c38557f17d0108a4e58514b85e7a 100644 (file)
@@ -1,6 +1,22 @@
 cmake_minimum_required(VERSION 2.8)
 project(EntityX)
-include_directories(.)
+include_directories(. ./c++11)
+
+# C++11 feature checks
+include(CheckCXX11Features.cmake)
+
+MACRO(REQUIRE FEATURE_NAME MESSAGE_STRING)
+    if (NOT DEFINED ${FEATURE_NAME})
+        message(FATAL_ERROR ${MESSAGE_STRING})
+    endif()
+ENDMACRO(REQUIRE)
+
+require(HAS_CXX11_AUTO "C++11 auto support is required")
+require(HAS_CXX11_NULLPTR "C++11 nullptr support is required")
+require(HAS_CXX11_RVALUE_REFERENCES "C++11 rvalue reference support is required")
+require(HAS_CXX11_CSTDINT_H "C++11 stdint support is required")
+require(HAS_CXX11_VARIADIC_TEMPLATES "C++11 variadic templates required")
+
 enable_testing()
 find_package(GTest REQUIRED)
 set(Boost_USE_STATIC_LIBS OFF)
@@ -9,13 +25,17 @@ set(Boost_USE_STATIC_RUNTIME OFF)
 #find_package(Boost REQUIRED COMPONENTS)
 find_package(Boost 1.36.0 REQUIRED COMPONENTS signals)
 
+set(CMAKE_CXX_FLAGS "-ansi -pedantic -Werror -Wall -Wextra -Wno-unused-parameter -Wno-error=unused-variable -std=c++11")
+set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
+set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
+set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
+set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
+
+
 include_directories(
     ${Boost_INCLUDE_DIR}
     ${GTest_INCLUDE_DIR}
     )
-add_definitions(
-    -ansi -pedantic -Werror -Wall -Wextra -Wno-unused-parameter -Wno-error=unused-variable -std=c++11
-    )
 add_executable(
     entityx_test
     entityx/Components_test.cc
diff --git a/CheckCXX11Features.cmake b/CheckCXX11Features.cmake
new file mode 100644 (file)
index 0000000..fc1243f
--- /dev/null
@@ -0,0 +1,103 @@
+# Checks for C++11 features\r
+#  CXX11_FEATURE_LIST - a list containing all supported features\r
+#  HAS_CXX11_AUTO               - auto keyword\r
+#  HAS_CXX11_NULLPTR            - nullptr\r
+#  HAS_CXX11_LAMBDA             - lambdas\r
+#  HAS_CXX11_STATIC_ASSERT      - static_assert()\r
+#  HAS_CXX11_RVALUE_REFERENCES  - rvalue references\r
+#  HAS_CXX11_DECLTYPE           - decltype keyword\r
+#  HAS_CXX11_CSTDINT_H          - cstdint header\r
+#  HAS_CXX11_LONG_LONG          - long long signed & unsigned types\r
+#  HAS_CXX11_VARIADIC_TEMPLATES - variadic templates\r
+#  HAS_CXX11_CONSTEXPR          - constexpr keyword\r
+#  HAS_CXX11_SIZEOF_MEMBER      - sizeof() non-static members\r
+#  HAS_CXX11_FUNC               - __func__ preprocessor constant\r
+#\r
+# Original script by Rolf Eike Beer\r
+# Modifications by Andreas Weis\r
+#\r
+CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3)\r
+\r
+SET(CHECK_CXX11_OLD_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})\r
+IF(CMAKE_COMPILER_IS_GNUCXX)\r
+       SET(CMAKE_CXX_FLAGS "-std=c++0x")\r
+endif()\r
+\r
+MACRO(CXX11_CHECK_FEATURE FEATURE_NAME FEATURE_NUMBER RESULT_VAR)\r
+       IF (NOT DEFINED ${RESULT_VAR})\r
+               SET(_bindir "${CMAKE_CURRENT_BINARY_DIR}/cxx11/cxx11_${FEATURE_NAME}")\r
+\r
+               IF (${FEATURE_NUMBER})\r
+                       SET(_SRCFILE_BASE ${CMAKE_CURRENT_LIST_DIR}/c++11-test-${FEATURE_NAME}-N${FEATURE_NUMBER})\r
+                       SET(_LOG_NAME "\"${FEATURE_NAME}\" (N${FEATURE_NUMBER})")\r
+               ELSE (${FEATURE_NUMBER})\r
+                       SET(_SRCFILE_BASE ${CMAKE_CURRENT_LIST_DIR}/c++11-test-${FEATURE_NAME})\r
+                       SET(_LOG_NAME "\"${FEATURE_NAME}\"")\r
+               ENDIF (${FEATURE_NUMBER})\r
+               MESSAGE(STATUS "Checking C++11 support for ${_LOG_NAME}")\r
+\r
+               SET(_SRCFILE "${_SRCFILE_BASE}.cpp")\r
+               SET(_SRCFILE_FAIL "${_SRCFILE_BASE}_fail.cpp")\r
+               SET(_SRCFILE_FAIL_COMPILE "${_SRCFILE_BASE}_fail_compile.cpp")\r
+\r
+               IF (CROSS_COMPILING)\r
+                       try_compile(${RESULT_VAR} "${_bindir}" "${_SRCFILE}")\r
+                       IF (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL})\r
+                               try_compile(${RESULT_VAR} "${_bindir}_fail" "${_SRCFILE_FAIL}")\r
+                       ENDIF (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL})\r
+               ELSE (CROSS_COMPILING)\r
+                       try_run(_RUN_RESULT_VAR _COMPILE_RESULT_VAR\r
+                                       "${_bindir}" "${_SRCFILE}")\r
+                       IF (_COMPILE_RESULT_VAR AND NOT _RUN_RESULT_VAR)\r
+                               SET(${RESULT_VAR} TRUE)\r
+                       ELSE (_COMPILE_RESULT_VAR AND NOT _RUN_RESULT_VAR)\r
+                               SET(${RESULT_VAR} FALSE)\r
+                       ENDIF (_COMPILE_RESULT_VAR AND NOT _RUN_RESULT_VAR)\r
+                       IF (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL})\r
+                               try_run(_RUN_RESULT_VAR _COMPILE_RESULT_VAR\r
+                                               "${_bindir}_fail" "${_SRCFILE_FAIL}")\r
+                               IF (_COMPILE_RESULT_VAR AND _RUN_RESULT_VAR)\r
+                                       SET(${RESULT_VAR} TRUE)\r
+                               ELSE (_COMPILE_RESULT_VAR AND _RUN_RESULT_VAR)\r
+                                       SET(${RESULT_VAR} FALSE)\r
+                               ENDIF (_COMPILE_RESULT_VAR AND _RUN_RESULT_VAR)\r
+                       ENDIF (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL})\r
+               ENDIF (CROSS_COMPILING)\r
+               IF (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL_COMPILE})\r
+                       try_compile(_TMP_RESULT "${_bindir}_fail_compile" "${_SRCFILE_FAIL_COMPILE}")\r
+                       IF (_TMP_RESULT)\r
+                               SET(${RESULT_VAR} FALSE)\r
+                       ELSE (_TMP_RESULT)\r
+                               SET(${RESULT_VAR} TRUE)\r
+                       ENDIF (_TMP_RESULT)\r
+               ENDIF (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL_COMPILE})\r
+\r
+               IF (${RESULT_VAR})\r
+                       MESSAGE(STATUS "Checking C++11 support for ${_LOG_NAME} -- works")\r
+                       LIST(APPEND CXX11_FEATURE_LIST ${RESULT_VAR})\r
+               ELSE (${RESULT_VAR})\r
+                       MESSAGE(STATUS "Checking C++11 support for ${_LOG_NAME} -- not supported")\r
+               ENDIF (${RESULT_VAR})\r
+               SET(${RESULT_VAR} ${${RESULT_VAR}} CACHE INTERNAL "C++11 support for ${_LOG_NAME}")\r
+       ENDIF (NOT DEFINED ${RESULT_VAR})\r
+ENDMACRO(CXX11_CHECK_FEATURE)\r
+\r
+CXX11_CHECK_FEATURE("auto"               2546 HAS_CXX11_AUTO)\r
+CXX11_CHECK_FEATURE("nullptr"            2431 HAS_CXX11_NULLPTR)\r
+CXX11_CHECK_FEATURE("lambda"             2927 HAS_CXX11_LAMBDA)\r
+CXX11_CHECK_FEATURE("static_assert"      1720 HAS_CXX11_STATIC_ASSERT)\r
+CXX11_CHECK_FEATURE("rvalue_references"  2118 HAS_CXX11_RVALUE_REFERENCES)\r
+CXX11_CHECK_FEATURE("decltype"           2343 HAS_CXX11_DECLTYPE)\r
+CXX11_CHECK_FEATURE("cstdint"            ""   HAS_CXX11_CSTDINT_H)\r
+CXX11_CHECK_FEATURE("long_long"          1811 HAS_CXX11_LONG_LONG)\r
+CXX11_CHECK_FEATURE("variadic_templates" 2555 HAS_CXX11_VARIADIC_TEMPLATES)\r
+CXX11_CHECK_FEATURE("constexpr"          2235 HAS_CXX11_CONSTEXPR)\r
+CXX11_CHECK_FEATURE("sizeof_member"      2253 HAS_CXX11_SIZEOF_MEMBER)\r
+CXX11_CHECK_FEATURE("__func__"           2340 HAS_CXX11_FUNC)\r
+\r
+SET(CXX11_FEATURE_LIST ${CXX11_FEATURE_LIST} CACHE STRING "C++11 feature support list")\r
+MARK_AS_ADVANCED(FORCE CXX11_FEATURE_LIST)\r
+\r
+SET(CMAKE_CXX_FLAGS ${CHECK_CXX11_OLD_CMAKE_CXX_FLAGS})\r
+UNSET(CHECK_CXX11_OLD_CMAKE_CXX_FLAGS)\r
+\r
diff --git a/c++11/c++11-test-__func__-N2340.cpp b/c++11/c++11-test-__func__-N2340.cpp
new file mode 100644 (file)
index 0000000..c10dd18
--- /dev/null
@@ -0,0 +1,8 @@
+#include <cstring>\r
+\r
+int main()\r
+{\r
+       if (!__func__) { return 1; }\r
+       if(std::strlen(__func__) <= 0) { return 1; }\r
+       return 0;\r
+}\r
diff --git a/c++11/c++11-test-auto-N2546.cpp b/c++11/c++11-test-auto-N2546.cpp
new file mode 100644 (file)
index 0000000..dbff414
--- /dev/null
@@ -0,0 +1,12 @@
+\r
+int main()\r
+{\r
+       auto i = 5;\r
+       auto f = 3.14159f;\r
+       auto d = 3.14159;\r
+       bool ret = (\r
+               (sizeof(f) < sizeof(d)) &&\r
+               (sizeof(i) == sizeof(int))\r
+       );\r
+       return ret ? 0 : 1;\r
+}\r
diff --git a/c++11/c++11-test-constexpr-N2235.cpp b/c++11/c++11-test-constexpr-N2235.cpp
new file mode 100644 (file)
index 0000000..9f969e4
--- /dev/null
@@ -0,0 +1,19 @@
+constexpr int square(int x)\r
+{\r
+       return x*x;\r
+}\r
+\r
+constexpr int the_answer()\r
+{\r
+       return 42;\r
+}\r
+\r
+int main()\r
+{\r
+       int test_arr[square(3)];\r
+       bool ret = (\r
+               (square(the_answer()) == 1764) &&\r
+               (sizeof(test_arr)/sizeof(test_arr[0]) == 9)\r
+       );\r
+       return ret ? 0 : 1;\r
+}\r
diff --git a/c++11/c++11-test-cstdint.cpp b/c++11/c++11-test-cstdint.cpp
new file mode 100644 (file)
index 0000000..58d4381
--- /dev/null
@@ -0,0 +1,10 @@
+#include <cstdint>\r
+int main()\r
+{\r
+       bool test = \r
+               (sizeof(int8_t) == 1) &&\r
+               (sizeof(int16_t) == 2) &&\r
+               (sizeof(int32_t) == 4) &&\r
+               (sizeof(int64_t) == 8);\r
+       return test ? 0 : 1;\r
+}\r
diff --git a/c++11/c++11-test-decltype-N2343.cpp b/c++11/c++11-test-decltype-N2343.cpp
new file mode 100644 (file)
index 0000000..d023885
--- /dev/null
@@ -0,0 +1,11 @@
+\r
+bool check_size(int i)\r
+{\r
+       return sizeof(int) == sizeof(decltype(i));\r
+}\r
+\r
+int main()\r
+{\r
+       bool ret = check_size(42);\r
+       return ret ? 0 : 1;\r
+}\r
diff --git a/c++11/c++11-test-lambda-N2927.cpp b/c++11/c++11-test-lambda-N2927.cpp
new file mode 100644 (file)
index 0000000..b86ad17
--- /dev/null
@@ -0,0 +1,5 @@
+int main()\r
+{\r
+       int ret = 0;\r
+       return ([&ret]() -> int { return ret; })();\r
+}\r
diff --git a/c++11/c++11-test-long_long-N1811.cpp b/c++11/c++11-test-long_long-N1811.cpp
new file mode 100644 (file)
index 0000000..2ae6988
--- /dev/null
@@ -0,0 +1,7 @@
+int main(void)\r
+{\r
+       long long l;\r
+       unsigned long long ul;\r
+\r
+       return ((sizeof(l) >= 8) && (sizeof(ul) >= 8)) ? 0 : 1;\r
+}\r
diff --git a/c++11/c++11-test-nullptr-N2431.cpp b/c++11/c++11-test-nullptr-N2431.cpp
new file mode 100644 (file)
index 0000000..6c5ae66
--- /dev/null
@@ -0,0 +1,5 @@
+int main()\r
+{\r
+       int* test = nullptr;\r
+       return test ? 1 : 0;\r
+}\r
diff --git a/c++11/c++11-test-nullptr-N2431_fail_compile.cpp b/c++11/c++11-test-nullptr-N2431_fail_compile.cpp
new file mode 100644 (file)
index 0000000..5747f1b
--- /dev/null
@@ -0,0 +1,5 @@
+int main()\r
+{\r
+       int i = nullptr;\r
+       return 1;\r
+}\r
diff --git a/c++11/c++11-test-rvalue_references-N2118.cpp b/c++11/c++11-test-rvalue_references-N2118.cpp
new file mode 100644 (file)
index 0000000..ef4e421
--- /dev/null
@@ -0,0 +1,15 @@
+int foo(int& lvalue)\r
+{\r
+       return 123;\r
+}\r
+\r
+int foo(int&& rvalue)\r
+{\r
+       return 321;\r
+}\r
+\r
+int main()\r
+{\r
+       int i = 42;\r
+       return ((foo(i) == 123) && (foo(42) == 321)) ? 0 : 1;\r
+}\r
diff --git a/c++11/c++11-test-sizeof_member-N2253.cpp b/c++11/c++11-test-sizeof_member-N2253.cpp
new file mode 100644 (file)
index 0000000..3049ed1
--- /dev/null
@@ -0,0 +1,14 @@
+struct foo {\r
+       char bar;\r
+       int baz;\r
+};\r
+\r
+int main(void)\r
+{\r
+       bool ret = (\r
+               (sizeof(foo::bar) == 1) &&\r
+               (sizeof(foo::baz) >= sizeof(foo::bar)) &&\r
+               (sizeof(foo) >= sizeof(foo::bar)+sizeof(foo::baz))\r
+       );\r
+       return ret ? 0 : 1;\r
+}\r
diff --git a/c++11/c++11-test-static_assert-N1720.cpp b/c++11/c++11-test-static_assert-N1720.cpp
new file mode 100644 (file)
index 0000000..eae3c9a
--- /dev/null
@@ -0,0 +1,5 @@
+int main()\r
+{\r
+       static_assert(0 < 1, "your ordering of integers is screwed");\r
+       return 0;\r
+}\r
diff --git a/c++11/c++11-test-static_assert-N1720_fail_compile.cpp b/c++11/c++11-test-static_assert-N1720_fail_compile.cpp
new file mode 100644 (file)
index 0000000..d97b679
--- /dev/null
@@ -0,0 +1,5 @@
+int main()\r
+{\r
+       static_assert(1 < 0, "this should fail");\r
+       return 0;\r
+}\r
diff --git a/c++11/c++11-test-variadic_templates-N2555.cpp b/c++11/c++11-test-variadic_templates-N2555.cpp
new file mode 100644 (file)
index 0000000..79fae84
--- /dev/null
@@ -0,0 +1,23 @@
+int Accumulate()\r
+{\r
+       return 0;\r
+}\r
+\r
+template<typename T, typename... Ts>\r
+int Accumulate(T v, Ts... vs)\r
+{\r
+       return v + Accumulate(vs...);\r
+}\r
+\r
+template<int... Is>\r
+int CountElements()\r
+{\r
+       return sizeof...(Is);\r
+}\r
+\r
+int main()\r
+{\r
+       int acc = Accumulate(1, 2, 3, 4, -5);\r
+       int count = CountElements<1,2,3,4,5>();\r
+       return ((acc == 5) && (count == 5)) ? 0 : 1;\r
+}\r
diff --git a/c++11/demo.cpp b/c++11/demo.cpp
new file mode 100644 (file)
index 0000000..782681b
--- /dev/null
@@ -0,0 +1,23 @@
+\r
+#include <iostream>\r
+\r
+int main()\r
+{\r
+       std::cout << "Testing\n";\r
+       std::cout << "Has static_assert: " <<\r
+#ifdef HAS_CXX11_STATIC_ASSERT\r
+               "yes :)"\r
+#else\r
+               "no"\r
+#endif\r
+               << "\n";\r
+       std::cout << "Has variadic templates: " <<\r
+#ifdef HAS_CXX11_VARIADIC_TEMPLATES\r
+               "yes :)"\r
+#else\r
+               "no"\r
+#endif\r
+               << "\n";\r
+       return 0;\r
+}\r
+\r