diff options
author | Daniele E. Domenichelli <daniele.domenichelli@iit.it> | 2013-09-20 13:10:40 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2013-10-08 13:43:06 (GMT) |
commit | 73d28d2177a8b014f8807a61d14d3064c5d5c033 (patch) | |
tree | 055b3078b3988b33a71a6fd7bea29a5500e709b1 /Tests/CMakeOnly | |
parent | 951a158c8a89512f52779ea29731234fa25b90e5 (diff) | |
download | CMake-73d28d2177a8b014f8807a61d14d3064c5d5c033.zip CMake-73d28d2177a8b014f8807a61d14d3064c5d5c033.tar.gz CMake-73d28d2177a8b014f8807a61d14d3064c5d5c033.tar.bz2 |
CheckStructHasMember: Add support for C++
Previously if headers required to check if a struct has a member can be
compiled with C++ compiler only, the check would fail because the C
compiler fails. As a consequence, the result variable would be set to
false, even if the struct has that particular member.
Teach CHECK_STRUCT_HAS_MEMBER to accept a new optional argument LANGUAGE
that allows one to explicitly set the compiler to use. The new
signature is therefore:
CHECK_STRUCT_HAS_MEMBER (<struct> <member> <header> <variable>
[LANGUAGE <language>])
Diffstat (limited to 'Tests/CMakeOnly')
-rw-r--r-- | Tests/CMakeOnly/CMakeLists.txt | 2 | ||||
-rw-r--r-- | Tests/CMakeOnly/CheckStructHasMember/CMakeLists.txt | 93 | ||||
-rw-r--r-- | Tests/CMakeOnly/CheckStructHasMember/cm_cshm.h | 9 | ||||
-rw-r--r-- | Tests/CMakeOnly/CheckStructHasMember/cm_cshm.hxx | 16 |
4 files changed, 120 insertions, 0 deletions
diff --git a/Tests/CMakeOnly/CMakeLists.txt b/Tests/CMakeOnly/CMakeLists.txt index be7ddbc..7586de6 100644 --- a/Tests/CMakeOnly/CMakeLists.txt +++ b/Tests/CMakeOnly/CMakeLists.txt @@ -19,6 +19,8 @@ add_CMakeOnly_test(CheckCXXCompilerFlag) add_CMakeOnly_test(CheckLanguage) +add_CMakeOnly_test(CheckStructHasMember) + add_CMakeOnly_test(CompilerIdC) add_CMakeOnly_test(CompilerIdCXX) if(CMAKE_Fortran_COMPILER) diff --git a/Tests/CMakeOnly/CheckStructHasMember/CMakeLists.txt b/Tests/CMakeOnly/CheckStructHasMember/CMakeLists.txt new file mode 100644 index 0000000..f06d5c3 --- /dev/null +++ b/Tests/CMakeOnly/CheckStructHasMember/CMakeLists.txt @@ -0,0 +1,93 @@ +cmake_minimum_required(VERSION 2.8) + +project(CheckStructHasMember) + +set(CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}") + +include(CheckStructHasMember) + +foreach(_config_type Release RelWithDebInfo MinSizeRel Debug) + set(CMAKE_TRY_COMPILE_CONFIGURATION ${_config_type}) + unset(CSHM_RESULT_S1_${_config_type} CACHE) + unset(CSHM_RESULT_S2_${_config_type} CACHE) + unset(CSHM_RESULT_S3_${_config_type} CACHE) + message(STATUS "Testing configuration ${_config_type}") + + check_struct_has_member("struct non_existent_struct" "foo" "cm_cshm.h" CSHM_RESULT_S1_${_config_type}) + check_struct_has_member("struct struct_with_member" "non_existent_member" "cm_cshm.h" CSHM_RESULT_S2_${_config_type}) + check_struct_has_member("struct struct_with_member" "member" "cm_cshm.h" CSHM_RESULT_S3_${_config_type}) + + if(CSHM_RESULT_S1_${_config_type} OR CSHM_RESULT_S2_${_config_type}) + message(SEND_ERROR "CheckStructHasMember reported a nonexistent member as existing in configuration ${_config_type}") + endif() + + if(NOT CSHM_RESULT_S3_${_config_type}) + message(SEND_ERROR "CheckStructHasMember did not report an existent member as existing in configuration ${_config_type}") + endif() +endforeach() + +foreach(_config_type Release RelWithDebInfo MinSizeRel Debug) + set(CMAKE_TRY_COMPILE_CONFIGURATION ${_config_type}) + unset(CSHM_RESULT_S1_${_config_type}_C CACHE) + unset(CSHM_RESULT_S2_${_config_type}_C CACHE) + unset(CSHM_RESULT_S3_${_config_type}_C CACHE) + message(STATUS "Testing configuration ${_config_type}") + + check_struct_has_member("struct non_existent_struct" "foo" "cm_cshm.h" CSHM_RESULT_S1_${_config_type}_C LANGUAGE C) + check_struct_has_member("struct struct_with_member" "non_existent_member" "cm_cshm.h" CSHM_RESULT_S2_${_config_type}_C LANGUAGE C) + check_struct_has_member("struct struct_with_member" "member" "cm_cshm.h" CSHM_RESULT_S3_${_config_type}_C LANGUAGE C) + + if(CSHM_RESULT_S1_${_config_type}_C OR CSHM_RESULT_S2_${_config_type}_C) + message(SEND_ERROR "CheckStructHasMember reported a nonexistent member as existing in configuration ${_config_type}") + endif() + + if(NOT CSHM_RESULT_S3_${_config_type}_C) + message(SEND_ERROR "CheckStructHasMember did not report an existent member as existing in configuration ${_config_type}") + endif() +endforeach() + +foreach(_config_type Release RelWithDebInfo MinSizeRel Debug) + set(CMAKE_TRY_COMPILE_CONFIGURATION ${_config_type}) + unset(CSHM_RESULT_S1_${_config_type}_CXX CACHE) + unset(CSHM_RESULT_S2_${_config_type}_CXX CACHE) + unset(CSHM_RESULT_S3_${_config_type}_CXX CACHE) + unset(CSHM_RESULT_C1_${_config_type}_CXX CACHE) + unset(CSHM_RESULT_C2_${_config_type}_CXX CACHE) + unset(CSHM_RESULT_C3_${_config_type}_CXX CACHE) + + message(STATUS "Testing configuration ${_config_type}") + + check_struct_has_member("non_existent_struct" "foo" "cm_cshm.h" CSHM_RESULT_S1_${_config_type}_CXX LANGUAGE CXX) + check_struct_has_member("struct_with_non_existent_members" "non_existent_member" "cm_cshm.h" CSHM_RESULT_S2_${_config_type}_CXX LANGUAGE CXX) + check_struct_has_member("struct struct_with_member" "member" "cm_cshm.h" CSHM_RESULT_S3_${_config_type}_CXX LANGUAGE CXX) + check_struct_has_member("ns::non_existent_class" "foo" "cm_cshm.hxx" CSHM_RESULT_C1_${_config_type}_CXX LANGUAGE CXX) + check_struct_has_member("ns::class_with_non_existent_members" "foo" "cm_cshm.hxx" CSHM_RESULT_C2_${_config_type}_CXX LANGUAGE CXX) + check_struct_has_member("ns::class_with_member" "foo" "cm_cshm.hxx" CSHM_RESULT_C3_${_config_type}_CXX LANGUAGE CXX) + + if(CSHM_RESULT_S1_${_config_type}_CXX OR CSHM_RESULT_S2_${_config_type}_CXX OR CSHM_RESULT_C1_${_config_type}_CXX OR CSHM_RESULT_C2_${_config_type}_CXX) + message(SEND_ERROR "CheckStructHasMember reported a nonexistent member as existing in configuration ${_config_type}") + endif() + + if(NOT CSHM_RESULT_S3_${_config_type}_CXX OR NOT CSHM_RESULT_C3_${_config_type}_CXX) + message(SEND_ERROR "CheckStructHasMember did not report an existent member as existing in configuration ${_config_type}") + endif() +endforeach() + + +set(CMAKE_TRY_COMPILE_CONFIGURATION ${CMAKE_BUILD_TYPE}) + +if (CMAKE_COMPILER_IS_GNUCC) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") + unset(CSHM_RESULT_O3 CACHE) + unset(CSHM_RESULT_O3_C CACHE) + unset(CSHM_RESULT_O3_CXX CACHE) + message(STATUS "Testing with optimization -O3") + + check_struct_has_member("class_with_non_existent_members" foo "cm_cshm.h" CSHM_RESULT_O3) + check_struct_has_member("class_with_non_existent_members" foo "cm_cshm.h" CSHM_RESULT_O3_C LANGUAGE C) + check_struct_has_member("class_with_non_existent_members" foo "cm_cshm.h" CSHM_RESULT_O3_CXX LANGUAGE CXX) + + if (CSE_RESULT_O3 OR CSHM_RESULT_O3_C OR CSHM_RESULT_O3_CXX) + message(SEND_ERROR "CheckSymbolExists reported a nonexistent symbol as existing with optimization -O3") + endif () +endif () diff --git a/Tests/CMakeOnly/CheckStructHasMember/cm_cshm.h b/Tests/CMakeOnly/CheckStructHasMember/cm_cshm.h new file mode 100644 index 0000000..82bb049 --- /dev/null +++ b/Tests/CMakeOnly/CheckStructHasMember/cm_cshm.h @@ -0,0 +1,9 @@ +#ifndef _CSHM_DUMMY_H +#define _CSHM_DUMMY_H + +struct non_existent_struct; +struct struct_with_member{ + int member; +}; + +#endif diff --git a/Tests/CMakeOnly/CheckStructHasMember/cm_cshm.hxx b/Tests/CMakeOnly/CheckStructHasMember/cm_cshm.hxx new file mode 100644 index 0000000..458a99b --- /dev/null +++ b/Tests/CMakeOnly/CheckStructHasMember/cm_cshm.hxx @@ -0,0 +1,16 @@ +#ifndef _CSHM_DUMMY_HXX +#define _CSHM_DUMMY_HXX + +namespace ns { + +class non_existent_class; +class class_with_non_existent_members { +}; +class class_with_member { +public: + int foo; +}; + +} + +#endif |