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 /Modules/CheckStructHasMember.cmake | |
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 'Modules/CheckStructHasMember.cmake')
-rw-r--r-- | Modules/CheckStructHasMember.cmake | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/Modules/CheckStructHasMember.cmake b/Modules/CheckStructHasMember.cmake index ea2891c..d28cc2a 100644 --- a/Modules/CheckStructHasMember.cmake +++ b/Modules/CheckStructHasMember.cmake @@ -1,10 +1,12 @@ # - Check if the given struct or class has the specified member variable -# CHECK_STRUCT_HAS_MEMBER (STRUCT MEMBER HEADER VARIABLE) +# CHECK_STRUCT_HAS_MEMBER (<struct> <member> <header> <variable> +# [LANGUAGE <language>]) # -# STRUCT - the name of the struct or class you are interested in -# MEMBER - the member which existence you want to check -# HEADER - the header(s) where the prototype should be declared -# VARIABLE - variable to store the result +# <struct> - the name of the struct or class you are interested in +# <member> - the member which existence you want to check +# <header> - the header(s) where the prototype should be declared +# <variable> - variable to store the result +# <language> - the compiler to use (C or CXX) # # The following variables may be set before calling this macro to # modify the way the check is run: @@ -12,8 +14,9 @@ # CMAKE_REQUIRED_FLAGS = string of compile command line flags # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar) # CMAKE_REQUIRED_INCLUDES = list of include directories +# CMAKE_REQUIRED_LIBRARIES = list of libraries to link # -# Example: CHECK_STRUCT_HAS_MEMBER("struct timeval" tv_sec sys/select.h HAVE_TIMEVAL_TV_SEC) +# Example: CHECK_STRUCT_HAS_MEMBER("struct timeval" tv_sec sys/select.h HAVE_TIMEVAL_TV_SEC LANGUAGE C) #============================================================================= # Copyright 2007-2009 Kitware, Inc. @@ -29,6 +32,7 @@ # License text for the above reference.) include(CheckCSourceCompiles) +include(CheckCXXSourceCompiles) macro (CHECK_STRUCT_HAS_MEMBER _STRUCT _MEMBER _HEADER _RESULT) set(_INCLUDE_FILES) @@ -36,16 +40,29 @@ macro (CHECK_STRUCT_HAS_MEMBER _STRUCT _MEMBER _HEADER _RESULT) set(_INCLUDE_FILES "${_INCLUDE_FILES}#include <${it}>\n") endforeach () + if("x${ARGN}" STREQUAL "x") + set(_lang C) + elseif("x${ARGN}" MATCHES "^xLANGUAGE;([a-zA-Z]+)$") + set(_lang "${CMAKE_MATCH_1}") + else() + message(FATAL_ERROR "Unknown arguments:\n ${ARGN}\n") + endif() + set(_CHECK_STRUCT_MEMBER_SOURCE_CODE " ${_INCLUDE_FILES} int main() { ${_STRUCT}* tmp; tmp->${_MEMBER}; - return 0; + return 0; } ") - CHECK_C_SOURCE_COMPILES("${_CHECK_STRUCT_MEMBER_SOURCE_CODE}" ${_RESULT}) + if("${_lang}" STREQUAL "C") + CHECK_C_SOURCE_COMPILES("${_CHECK_STRUCT_MEMBER_SOURCE_CODE}" ${_RESULT}) + elseif("${_lang}" STREQUAL "CXX") + CHECK_CXX_SOURCE_COMPILES("${_CHECK_STRUCT_MEMBER_SOURCE_CODE}" ${_RESULT}) + else() + message(FATAL_ERROR "Unknown language:\n ${_lang}\nSupported languages: C, CXX.\n") + endif() endmacro () - |