Merge topic 'CheckTypeSize_CXX'
e6cec64
CheckTypeSize: Add unit tests07a2342
CheckTypeSize: Add support for C++
This commit is contained in:
commit
6403a53f0e
|
@ -6,7 +6,8 @@
|
|||
#
|
||||
# ::
|
||||
#
|
||||
# CHECK_TYPE_SIZE(TYPE VARIABLE [BUILTIN_TYPES_ONLY])
|
||||
# CHECK_TYPE_SIZE(TYPE VARIABLE [BUILTIN_TYPES_ONLY]
|
||||
# [LANGUAGE <language>])
|
||||
#
|
||||
# Check if the type exists and determine its size. On return,
|
||||
# "HAVE_${VARIABLE}" holds the existence of the type, and "${VARIABLE}"
|
||||
|
@ -36,6 +37,9 @@
|
|||
# check automatically includes the available headers, thus supporting
|
||||
# checks of types defined in the headers.
|
||||
#
|
||||
# If LANGUAGE is set, the specified compiler will be used to perform the
|
||||
# check. Acceptable values are C and CXX
|
||||
#
|
||||
# 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:
|
||||
|
@ -79,7 +83,7 @@ get_filename_component(__check_type_size_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
|
|||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Helper function. DO NOT CALL DIRECTLY.
|
||||
function(__check_type_size_impl type var map builtin)
|
||||
function(__check_type_size_impl type var map builtin language)
|
||||
message(STATUS "Check size of ${type}")
|
||||
|
||||
# Include header files.
|
||||
|
@ -101,8 +105,13 @@ function(__check_type_size_impl type var map builtin)
|
|||
|
||||
# Perform the check.
|
||||
|
||||
|
||||
set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.c)
|
||||
if("${language}" STREQUAL "C")
|
||||
set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.c)
|
||||
elseif("${language}" STREQUAL "CXX")
|
||||
set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.cpp)
|
||||
else()
|
||||
message(FATAL_ERROR "Unknown language:\n ${language}\nSupported languages: C, CXX.\n")
|
||||
endif()
|
||||
set(bin ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.bin)
|
||||
configure_file(${__check_type_size_dir}/CheckTypeSize.c.in ${src} @ONLY)
|
||||
try_compile(HAVE_${var} ${CMAKE_BINARY_DIR} ${src}
|
||||
|
@ -176,8 +185,36 @@ endfunction()
|
|||
|
||||
#-----------------------------------------------------------------------------
|
||||
macro(CHECK_TYPE_SIZE TYPE VARIABLE)
|
||||
# parse arguments
|
||||
unset(doing)
|
||||
foreach(arg ${ARGN})
|
||||
if("x${arg}" STREQUAL "xBUILTIN_TYPES_ONLY")
|
||||
set(_CHECK_TYPE_SIZE_${arg} 1)
|
||||
unset(doing)
|
||||
elseif("x${arg}" STREQUAL "xLANGUAGE") # change to MATCHES for more keys
|
||||
set(doing "${arg}")
|
||||
set(_CHECK_TYPE_SIZE_${doing} "")
|
||||
elseif("x${doing}" STREQUAL "xLANGUAGE")
|
||||
set(_CHECK_TYPE_SIZE_${doing} "${arg}")
|
||||
unset(doing)
|
||||
else()
|
||||
message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
|
||||
endif()
|
||||
endforeach()
|
||||
if("x${doing}" MATCHES "^x(LANGUAGE)$")
|
||||
message(FATAL_ERROR "Missing argument:\n ${doing} arguments requires a value\n")
|
||||
endif()
|
||||
if(DEFINED _CHECK_TYPE_SIZE_LANGUAGE)
|
||||
if(NOT "x${_CHECK_TYPE_SIZE_LANGUAGE}" MATCHES "^x(C|CXX)$")
|
||||
message(FATAL_ERROR "Unknown language:\n ${_CHECK_TYPE_SIZE_LANGUAGE}.\nSupported languages: C, CXX.\n")
|
||||
endif()
|
||||
set(_language ${_CHECK_TYPE_SIZE_LANGUAGE})
|
||||
else()
|
||||
set(_language C)
|
||||
endif()
|
||||
|
||||
# Optionally check for standard headers.
|
||||
if("${ARGV2}" STREQUAL "BUILTIN_TYPES_ONLY")
|
||||
if(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
|
||||
set(_builtin 0)
|
||||
else()
|
||||
set(_builtin 1)
|
||||
|
@ -190,7 +227,7 @@ macro(CHECK_TYPE_SIZE TYPE VARIABLE)
|
|||
set(${VARIABLE}_KEYS)
|
||||
set(_map_file ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${VARIABLE}.cmake)
|
||||
if(NOT DEFINED HAVE_${VARIABLE})
|
||||
__check_type_size_impl(${TYPE} ${VARIABLE} ${_map_file} ${_builtin})
|
||||
__check_type_size_impl(${TYPE} ${VARIABLE} ${_map_file} ${_builtin} ${_language})
|
||||
endif()
|
||||
include(${_map_file} OPTIONAL)
|
||||
set(_map_file)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
cmake_minimum_required(VERSION 2.8.1 FATAL_ERROR)
|
||||
project(CheckTypeSize C)
|
||||
project(CheckTypeSize)
|
||||
|
||||
# Check C types
|
||||
include(CheckTypeSize)
|
||||
check_type_size("void*" SIZEOF_DATA_PTR)
|
||||
check_type_size(char SIZEOF_CHAR)
|
||||
|
@ -18,7 +19,19 @@ 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)
|
||||
|
||||
# Check CXX types
|
||||
check_type_size(bool SIZEOF_BOOL LANGUAGE CXX)
|
||||
|
||||
set(CMAKE_EXTRA_INCLUDE_FILES someclass.hxx)
|
||||
check_type_size("((ns::someclass*)0)->someint" SIZEOF_NS_CLASSMEMBER_INT LANGUAGE CXX)
|
||||
check_type_size("((ns::someclass*)0)->someptr" SIZEOF_NS_CLASSMEMBER_PTR LANGUAGE CXX)
|
||||
check_type_size("((ns::someclass*)0)->somechar" SIZEOF_NS_CLASSMEMBER_CHAR LANGUAGE CXX)
|
||||
check_type_size("((ns::someclass*)0)->somebool" SIZEOF_NS_CLASSMEMBER_BOOL LANGUAGE CXX)
|
||||
|
||||
configure_file(config.h.in config.h)
|
||||
configure_file(config.hxx.in config.hxx)
|
||||
|
||||
include_directories("${CheckTypeSize_BINARY_DIR}")
|
||||
|
||||
add_executable(CheckTypeSize CheckTypeSize.c)
|
||||
add_executable(CheckTypeSizeCXX CheckTypeSize.cxx)
|
||||
|
|
|
@ -0,0 +1,172 @@
|
|||
#include "config.h"
|
||||
#include "config.hxx"
|
||||
#include "someclass.hxx"
|
||||
|
||||
#ifdef HAVE_SYS_TYPES_H
|
||||
# include <sys/types.h>
|
||||
#endif
|
||||
#ifdef HAVE_STDINT_H
|
||||
# include <stdint.h>
|
||||
#endif
|
||||
#ifdef HAVE_STDDEF_H
|
||||
# include <stddef.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define CHECK(t,m) do { \
|
||||
if(sizeof(t) != m) \
|
||||
{ \
|
||||
printf(#m ": expected %d, got %d (line %d)\n", \
|
||||
(int)sizeof(t), (int)m, __LINE__); \
|
||||
result = 1; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define NODEF(m) do { \
|
||||
printf(#m": not defined (line %d)\n", __LINE__); \
|
||||
result = 1; \
|
||||
} while(0)
|
||||
|
||||
int main()
|
||||
{
|
||||
int result = 0;
|
||||
ns::someclass y;
|
||||
|
||||
/* void* */
|
||||
#if !defined(HAVE_SIZEOF_DATA_PTR)
|
||||
NODEF(HAVE_SIZEOF_DATA_PTR);
|
||||
#endif
|
||||
#if defined(SIZEOF_DATA_PTR)
|
||||
CHECK(void*, SIZEOF_DATA_PTR);
|
||||
#else
|
||||
NODEF(SIZEOF_DATA_PTR);
|
||||
#endif
|
||||
|
||||
/* char */
|
||||
#if !defined(HAVE_SIZEOF_CHAR)
|
||||
NODEF(HAVE_SIZEOF_CHAR);
|
||||
#endif
|
||||
#if defined(SIZEOF_CHAR)
|
||||
CHECK(char, SIZEOF_CHAR);
|
||||
#else
|
||||
NODEF(SIZEOF_CHAR);
|
||||
#endif
|
||||
|
||||
/* short */
|
||||
#if !defined(HAVE_SIZEOF_SHORT)
|
||||
NODEF(HAVE_SIZEOF_SHORT);
|
||||
#endif
|
||||
#if defined(SIZEOF_SHORT)
|
||||
CHECK(short, SIZEOF_SHORT);
|
||||
#else
|
||||
NODEF(SIZEOF_SHORT);
|
||||
#endif
|
||||
|
||||
/* int */
|
||||
#if !defined(HAVE_SIZEOF_INT)
|
||||
NODEF(HAVE_SIZEOF_INT);
|
||||
#endif
|
||||
#if defined(SIZEOF_INT)
|
||||
CHECK(int, SIZEOF_INT);
|
||||
#else
|
||||
NODEF(SIZEOF_INT);
|
||||
#endif
|
||||
|
||||
/* long */
|
||||
#if !defined(HAVE_SIZEOF_LONG)
|
||||
NODEF(HAVE_SIZEOF_LONG);
|
||||
#endif
|
||||
#if defined(SIZEOF_LONG)
|
||||
CHECK(long, SIZEOF_LONG);
|
||||
#else
|
||||
NODEF(SIZEOF_LONG);
|
||||
#endif
|
||||
|
||||
/* long long */
|
||||
#if defined(SIZEOF_LONG_LONG)
|
||||
CHECK(long long, SIZEOF_LONG_LONG);
|
||||
# if !defined(HAVE_SIZEOF_LONG_LONG)
|
||||
NODEF(HAVE_SIZEOF_LONG_LONG);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/* __int64 */
|
||||
#if defined(SIZEOF___INT64)
|
||||
CHECK(__int64, SIZEOF___INT64);
|
||||
# if !defined(HAVE_SIZEOF___INT64)
|
||||
NODEF(HAVE_SIZEOF___INT64);
|
||||
# endif
|
||||
#elif defined(HAVE_SIZEOF___INT64)
|
||||
NODEF(SIZEOF___INT64);
|
||||
#endif
|
||||
|
||||
/* size_t */
|
||||
#if !defined(HAVE_SIZEOF_SIZE_T)
|
||||
NODEF(HAVE_SIZEOF_SIZE_T);
|
||||
#endif
|
||||
#if defined(SIZEOF_SIZE_T)
|
||||
CHECK(size_t, SIZEOF_SIZE_T);
|
||||
#else
|
||||
NODEF(SIZEOF_SIZE_T);
|
||||
#endif
|
||||
|
||||
/* ssize_t */
|
||||
#if defined(SIZEOF_SSIZE_T)
|
||||
CHECK(ssize_t, SIZEOF_SSIZE_T);
|
||||
# if !defined(HAVE_SIZEOF_SSIZE_T)
|
||||
NODEF(HAVE_SIZEOF_SSIZE_T);
|
||||
# endif
|
||||
#elif defined(HAVE_SIZEOF_SSIZE_T)
|
||||
NODEF(SIZEOF_SSIZE_T);
|
||||
#endif
|
||||
|
||||
/* ns::someclass::someint */
|
||||
#if defined(SIZEOF_NS_CLASSMEMBER_INT)
|
||||
CHECK(y.someint, SIZEOF_NS_CLASSMEMBER_INT);
|
||||
CHECK(y.someint, SIZEOF_INT);
|
||||
# if !defined(HAVE_SIZEOF_NS_CLASSMEMBER_INT)
|
||||
NODEF(HAVE_SIZEOF_STRUCTMEMBER_INT);
|
||||
# endif
|
||||
#elif defined(HAVE_SIZEOF_STRUCTMEMBER_INT)
|
||||
NODEF(SIZEOF_STRUCTMEMBER_INT);
|
||||
#endif
|
||||
|
||||
/* ns::someclass::someptr */
|
||||
#if defined(SIZEOF_NS_CLASSMEMBER_PTR)
|
||||
CHECK(y.someptr, SIZEOF_NS_CLASSMEMBER_PTR);
|
||||
CHECK(y.someptr, SIZEOF_DATA_PTR);
|
||||
# if !defined(HAVE_SIZEOF_NS_CLASSMEMBER_PTR)
|
||||
NODEF(HAVE_SIZEOF_NS_CLASSMEMBER_PTR);
|
||||
# endif
|
||||
#elif defined(HAVE_SIZEOF_NS_CLASSMEMBER_PTR)
|
||||
NODEF(SIZEOF_NS_CLASSMEMBER_PTR);
|
||||
#endif
|
||||
|
||||
/* ns::someclass::somechar */
|
||||
#if defined(SIZEOF_NS_CLASSMEMBER_CHAR)
|
||||
CHECK(y.somechar, SIZEOF_NS_CLASSMEMBER_CHAR);
|
||||
CHECK(y.somechar, SIZEOF_CHAR);
|
||||
# if !defined(HAVE_SIZEOF_NS_CLASSMEMBER_CHAR)
|
||||
NODEF(HAVE_SIZEOF_NS_CLASSMEMBER_CHAR);
|
||||
# endif
|
||||
#elif defined(HAVE_SIZEOF_NS_CLASSMEMBER_CHAR)
|
||||
NODEF(SIZEOF_NS_CLASSMEMBER_CHAR);
|
||||
#endif
|
||||
|
||||
/* ns::someclass::somebool */
|
||||
#if defined(SIZEOF_NS_CLASSMEMBER_BOOL)
|
||||
CHECK(y.somechar, SIZEOF_NS_CLASSMEMBER_BOOL);
|
||||
CHECK(y.somechar, SIZEOF_BOOL);
|
||||
# if !defined(HAVE_SIZEOF_NS_CLASSMEMBER_BOOL)
|
||||
NODEF(HAVE_SIZEOF_NS_CLASSMEMBER_BOOL);
|
||||
# endif
|
||||
#elif defined(HAVE_SIZEOF_NS_CLASSMEMBER_BOOL)
|
||||
NODEF(SIZEOF_NS_CLASSMEMBER_BOOL);
|
||||
#endif
|
||||
|
||||
/* to avoid possible warnings about unused or write-only variable */
|
||||
y.someint = result;
|
||||
|
||||
return y.someint;
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
#cmakedefine HAVE_SYS_TYPES_H
|
||||
#cmakedefine HAVE_STDINT_H
|
||||
#cmakedefine HAVE_STDDEF_H
|
||||
|
||||
/* bool */
|
||||
#cmakedefine HAVE_SIZEOF_BOOL
|
||||
@SIZEOF_BOOL_CODE@
|
||||
|
||||
/* struct ns::somestruct::someint */
|
||||
#cmakedefine HAVE_SIZEOF_NS_STRUCTMEMBER_INT
|
||||
@SIZEOF_NS_STRUCTMEMBER_INT_CODE@
|
||||
|
||||
/* struct ns::somestruct::someptr */
|
||||
#cmakedefine HAVE_SIZEOF_NS_STRUCTMEMBER_PTR
|
||||
@SIZEOF_NS_STRUCTMEMBER_PTR_CODE@
|
||||
|
||||
/* struct ns::somestruct::somechar */
|
||||
#cmakedefine HAVE_SIZEOF_NS_STRUCTMEMBER_CHAR
|
||||
@SIZEOF_NS_STRUCTMEMBER_CHAR_CODE@
|
||||
|
||||
/* struct ns::somestruct::somebool */
|
||||
#cmakedefine HAVE_SIZEOF_NS_STRUCTMEMBER_BOOL
|
||||
@SIZEOF_NS_STRUCTMEMBER_BOOL_CODE@
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef _CMAKE_SOMECLASS_HXX
|
||||
#define _CMAKE_SOMECLASS_HXX
|
||||
|
||||
namespace ns {
|
||||
class someclass {
|
||||
public:
|
||||
int someint;
|
||||
void *someptr;
|
||||
char somechar;
|
||||
bool somebool;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,8 @@
|
|||
CMake Error at .*/Modules/CheckTypeSize.cmake:[0-9]+ \(message\):
|
||||
Missing argument:
|
||||
|
||||
LANGUAGE arguments requires a value
|
||||
|
||||
Call Stack \(most recent call first\):
|
||||
CheckTypeSizeMissingLanguage.cmake:[0-9]+ \(check_type_size\)
|
||||
CMakeLists.txt:[0-9]+ \(include\)
|
|
@ -0,0 +1,2 @@
|
|||
include(CheckTypeSize)
|
||||
check_type_size(int SIZEOF_INT LANGUAGE)
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,10 @@
|
|||
CMake Error at .*/Modules/CheckTypeSize.cmake:[0-9]+. \(message\):
|
||||
Unknown language:
|
||||
|
||||
.
|
||||
|
||||
Supported languages: C, CXX.
|
||||
|
||||
Call Stack \(most recent call first\):
|
||||
CheckTypeSizeMixedArgs.cmake:[0-9]+ \(check_type_size\)
|
||||
CMakeLists.txt:[0-9]+ \(include\)
|
|
@ -0,0 +1,2 @@
|
|||
include(CheckTypeSize)
|
||||
check_type_size(int SIZEOF_INT LANGUAGE BUILTIN_TYPES_ONLY)
|
|
@ -0,0 +1,10 @@
|
|||
include(CheckTypeSize)
|
||||
check_type_size(int SIZEOF_INT)
|
||||
check_type_size(int SIZEOF_INT BUILTIN_TYPES_ONLY)
|
||||
check_type_size(int SIZEOF_INT LANGUAGE C)
|
||||
check_type_size(int SIZEOF_INT LANGUAGE CXX)
|
||||
check_type_size(int SIZEOF_INT BUILTIN_TYPES_ONLY LANGUAGE C)
|
||||
|
||||
# Weird but ok... only last value is considered
|
||||
check_type_size(int SIZEOF_INT BUILTIN_TYPES_ONLY BUILTIN_TYPES_ONLY)
|
||||
check_type_size(int SIZEOF_INT LANGUAGE C LANGUAGE CXX)
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,8 @@
|
|||
CMake Error at .*/Modules/CheckTypeSize.cmake:[0-9]+. \(message\):
|
||||
Unknown argument:
|
||||
|
||||
LANGUAG
|
||||
|
||||
Call Stack \(most recent call first\):
|
||||
CheckTypeSizeUnknownArgument.cmake:[0-9]+ \(check_type_size\)
|
||||
CMakeLists.txt:[0-9]+ \(include\)
|
|
@ -0,0 +1,2 @@
|
|||
include(CheckTypeSize)
|
||||
check_type_size(int SIZEOF_INT BUILTIN_TYPES_ONLY LANGUAG CXX)
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,10 @@
|
|||
CMake Error at .*/Modules/CheckTypeSize.cmake:[0-9]+. \(message\):
|
||||
Unknown language:
|
||||
|
||||
FORTRAN.
|
||||
|
||||
Supported languages: C, CXX.
|
||||
|
||||
Call Stack \(most recent call first\):
|
||||
CheckTypeSizeUnknownLanguage.cmake:[0-9]+ \(check_type_size\)
|
||||
CMakeLists.txt:[0-9]+ \(include\)
|
|
@ -0,0 +1,2 @@
|
|||
include(CheckTypeSize)
|
||||
check_type_size(int SIZEOF_INT LANGUAGE FORTRAN)
|
|
@ -6,3 +6,9 @@ run_cmake(CheckStructHasMemberMissingLanguage)
|
|||
run_cmake(CheckStructHasMemberMissingKey)
|
||||
run_cmake(CheckStructHasMemberTooManyArguments)
|
||||
run_cmake(CheckStructHasMemberWrongKey)
|
||||
|
||||
run_cmake(CheckTypeSizeOk)
|
||||
run_cmake(CheckTypeSizeUnknownLanguage)
|
||||
run_cmake(CheckTypeSizeMissingLanguage)
|
||||
run_cmake(CheckTypeSizeUnknownArgument)
|
||||
run_cmake(CheckTypeSizeMixedArgs)
|
||||
|
|
Loading…
Reference in New Issue