Merge topic 'CheckTypeSize-doc-10579'

c6fed68 CheckTypeSize: add a test for size of struct members
48783b7 CheckTypeSize: show in documentation how to get struct member size (#10579)
This commit is contained in:
David Cole 2012-08-20 15:38:33 -04:00 committed by CMake Topic Stage
commit 5cf828c028
5 changed files with 73 additions and 2 deletions

View File

@ -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:
#

View File

@ -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)

View File

@ -1,4 +1,5 @@
#include "config.h"
#include "somestruct.h"
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
@ -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;
}

View File

@ -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@

View File

@ -0,0 +1,10 @@
#ifndef _CMAKE_SOMESTRUCT_H
#define _CMAKE_SOMESTRUCT_H
struct somestruct {
int someint;
void *someptr;
char somechar;
};
#endif