From 48783b71fb127f46860ed5bed605c85eda1303a2 Mon Sep 17 00:00:00 2001 From: Rolf Eike Beer Date: Mon, 13 Aug 2012 18:58:30 +0200 Subject: [PATCH 1/2] CheckTypeSize: show in documentation how to get struct member size (#10579) --- Modules/CheckTypeSize.cmake | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Modules/CheckTypeSize.cmake b/Modules/CheckTypeSize.cmake index 1b9dc9fbb..7285b8a6d 100644 --- a/Modules/CheckTypeSize.cmake +++ b/Modules/CheckTypeSize.cmake @@ -24,6 +24,11 @@ # size check automatically includes the available headers, thus # supporting checks of types defined in the headers. # +# Despite the name of the macro you may use it to check the size of +# more complex expressions, too. To check e.g. for the size of a struct +# member you can do something like this: +# check_type_size("((struct something*)0)->member" SIZEOF_MEMBER) +# # The following variables may be set before calling this macro to # modify the way the check is run: # From c6fed68ef87303aa9db32cdd7d830fe42659d769 Mon Sep 17 00:00:00 2001 From: Rolf Eike Beer Date: Mon, 13 Aug 2012 19:27:44 +0200 Subject: [PATCH 2/2] CheckTypeSize: add a test for size of struct members --- Tests/Module/CheckTypeSize/CMakeLists.txt | 8 ++++- Tests/Module/CheckTypeSize/CheckTypeSize.c | 40 +++++++++++++++++++++- Tests/Module/CheckTypeSize/config.h.in | 12 +++++++ Tests/Module/CheckTypeSize/somestruct.h | 10 ++++++ 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 Tests/Module/CheckTypeSize/somestruct.h diff --git a/Tests/Module/CheckTypeSize/CMakeLists.txt b/Tests/Module/CheckTypeSize/CMakeLists.txt index 45e9f6757..abe617a16 100644 --- a/Tests/Module/CheckTypeSize/CMakeLists.txt +++ b/Tests/Module/CheckTypeSize/CMakeLists.txt @@ -12,7 +12,13 @@ check_type_size(__int64 SIZEOF___INT64) check_type_size(size_t SIZEOF_SIZE_T) check_type_size(ssize_t SIZEOF_SSIZE_T) +set(CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}") +set(CMAKE_EXTRA_INCLUDE_FILES somestruct.h) +check_type_size("((struct somestruct*)0)->someint" SIZEOF_STRUCTMEMBER_INT) +check_type_size("((struct somestruct*)0)->someptr" SIZEOF_STRUCTMEMBER_PTR) +check_type_size("((struct somestruct*)0)->somechar" SIZEOF_STRUCTMEMBER_CHAR) + configure_file(config.h.in config.h) -include_directories(${CheckTypeSize_BINARY_DIR}) +include_directories("${CheckTypeSize_BINARY_DIR}") add_executable(CheckTypeSize CheckTypeSize.c) diff --git a/Tests/Module/CheckTypeSize/CheckTypeSize.c b/Tests/Module/CheckTypeSize/CheckTypeSize.c index 602c83494..32e395cca 100644 --- a/Tests/Module/CheckTypeSize/CheckTypeSize.c +++ b/Tests/Module/CheckTypeSize/CheckTypeSize.c @@ -1,4 +1,5 @@ #include "config.h" +#include "somestruct.h" #ifdef HAVE_SYS_TYPES_H # include @@ -29,6 +30,7 @@ int main() { int result = 0; + struct somestruct x; /* void* */ #if !defined(HAVE_SIZEOF_DATA_PTR) @@ -118,5 +120,41 @@ int main() NODEF(SIZEOF_SSIZE_T); #endif - return result; + /* struct somestruct::someint */ +#if defined(SIZEOF_STRUCTMEMBER_INT) + CHECK(x.someint, SIZEOF_STRUCTMEMBER_INT); + CHECK(x.someint, SIZEOF_INT); +# if !defined(HAVE_SIZEOF_STRUCTMEMBER_INT) + NODEF(HAVE_SIZEOF_STRUCTMEMBER_INT); +# endif +#elif defined(HAVE_SIZEOF_STRUCTMEMBER_INT) + NODEF(SIZEOF_STRUCTMEMBER_INT); +#endif + + /* struct somestruct::someptr */ +#if defined(SIZEOF_STRUCTMEMBER_PTR) + CHECK(x.someptr, SIZEOF_STRUCTMEMBER_PTR); + CHECK(x.someptr, SIZEOF_DATA_PTR); +# if !defined(HAVE_SIZEOF_STRUCTMEMBER_PTR) + NODEF(HAVE_SIZEOF_STRUCTMEMBER_PTR); +# endif +#elif defined(HAVE_SIZEOF_STRUCTMEMBER_PTR) + NODEF(SIZEOF_STRUCTMEMBER_PTR); +#endif + + /* struct somestruct::someint */ +#if defined(SIZEOF_STRUCTMEMBER_CHAR) + CHECK(x.somechar, SIZEOF_STRUCTMEMBER_CHAR); + CHECK(x.somechar, SIZEOF_CHAR); +# if !defined(HAVE_SIZEOF_STRUCTMEMBER_CHAR) + NODEF(HAVE_SIZEOF_STRUCTMEMBER_CHAR); +# endif +#elif defined(HAVE_SIZEOF_STRUCTMEMBER_CHAR) + NODEF(SIZEOF_STRUCTMEMBER_CHAR); +#endif + + /* to avoid possible warnings about unused or write-only variable */ + x.someint = result; + + return x.someint; } diff --git a/Tests/Module/CheckTypeSize/config.h.in b/Tests/Module/CheckTypeSize/config.h.in index b5bfbf6e5..c60107588 100644 --- a/Tests/Module/CheckTypeSize/config.h.in +++ b/Tests/Module/CheckTypeSize/config.h.in @@ -37,3 +37,15 @@ /* ssize_t */ #cmakedefine HAVE_SIZEOF_SSIZE_T @SIZEOF_SSIZE_T_CODE@ + +/* struct somestruct::someint */ +#cmakedefine HAVE_SIZEOF_STRUCTMEMBER_INT +@SIZEOF_STRUCTMEMBER_INT_CODE@ + +/* struct somestruct::someptr */ +#cmakedefine HAVE_SIZEOF_STRUCTMEMBER_PTR +@SIZEOF_STRUCTMEMBER_PTR_CODE@ + +/* struct somestruct::somechar */ +#cmakedefine HAVE_SIZEOF_STRUCTMEMBER_CHAR +@SIZEOF_STRUCTMEMBER_CHAR_CODE@ diff --git a/Tests/Module/CheckTypeSize/somestruct.h b/Tests/Module/CheckTypeSize/somestruct.h new file mode 100644 index 000000000..e08efc4ca --- /dev/null +++ b/Tests/Module/CheckTypeSize/somestruct.h @@ -0,0 +1,10 @@ +#ifndef _CMAKE_SOMESTRUCT_H +#define _CMAKE_SOMESTRUCT_H + +struct somestruct { + int someint; + void *someptr; + char somechar; +}; + +#endif