mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-14 02:08:27 +08:00
CheckTypeSize: Add check_type_size(RESULT_VARIABLE)
Changes: - Added new `RESULT_VARIABLE` keyword to enable customizing the name of the internal cache variable, which contains the boolean result of the check. - The macro check_type_size() changed to function for easier arguments handling. - Documentation synced and extended to better understand the check_type_size() command. Some typos fixed in the initial example. - CheckTypeSize tests adjusted so also C++ is tested. - Error messages slightly adjusted when checking the LANGUAGE argument. Closes: #27202
This commit is contained in:
6
Help/release/dev/CheckTypeSize-RESULT_VARIABLE.rst
Normal file
6
Help/release/dev/CheckTypeSize-RESULT_VARIABLE.rst
Normal file
@@ -0,0 +1,6 @@
|
||||
CheckTypeSize
|
||||
-------------
|
||||
|
||||
* The :module:`CheckTypeSize` module's command :command:`check_type_size`
|
||||
gained a new argument ``RESULT_VARIABLE`` to customize the result variable
|
||||
name instead of the default ``HAVE_<size-var>``.
|
@@ -25,23 +25,42 @@ This module provides the following command:
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
check_type_size(<type> <variable> [BUILTIN_TYPES_ONLY] [LANGUAGE <language>])
|
||||
check_type_size(
|
||||
<type>
|
||||
<size-var>
|
||||
[RESULT_VARIABLE <result-var>]
|
||||
[BUILTIN_TYPES_ONLY]
|
||||
[LANGUAGE <language>]
|
||||
)
|
||||
|
||||
The arguments are:
|
||||
|
||||
``<type>``
|
||||
The type or expression being checked.
|
||||
|
||||
``<variable>``
|
||||
The name of the variable and a prefix used for storing the check results.
|
||||
``<size-var>``
|
||||
The name of the internal cache variable for storing the size of the type
|
||||
or expression ``<type>``. This name is also used as a prefix as
|
||||
explained below.
|
||||
|
||||
``RESULT_VARIABLE <result-var>``
|
||||
.. versionadded:: 4.2
|
||||
|
||||
The name of the internal cache variable that holds a boolean value
|
||||
indicating whether the type or expression ``<type>`` exists. If *not*
|
||||
given, the command will by default define an internal cache variable
|
||||
named ``HAVE_<size-var>`` instead.
|
||||
|
||||
``BUILTIN_TYPES_ONLY``
|
||||
If given, only compiler-builtin types will be supported in the check.
|
||||
If *not* given, the command checks for common headers ``<sys/types.h>``,
|
||||
``<stdint.h>``, and ``<stddef.h>``, and saves results in
|
||||
``HAVE_SYS_TYPES_H``, ``HAVE_STDINT_H``, and ``HAVE_STDDEF_H`` internal
|
||||
cache variables. The type size check automatically includes the available
|
||||
headers, thus supporting checks of types defined in the headers.
|
||||
cache variables. For C++ ``std::`` types, ``<cstdint>`` and
|
||||
``<cstddef>`` are also checked with ``HAVE_CSTDINT`` and
|
||||
``HAVE_CSTDDEF`` defined respectively. The command automatically
|
||||
includes the available headers in the type size check, thus supporting
|
||||
checks of types defined in the headers.
|
||||
|
||||
``LANGUAGE <language>``
|
||||
Uses the ``<language>`` compiler to perform the check.
|
||||
@@ -52,40 +71,50 @@ This module provides the following command:
|
||||
|
||||
Results are reported in the following variables:
|
||||
|
||||
``HAVE_<variable>``
|
||||
Internal cache variable that holds a boolean true or false value
|
||||
indicating whether the type or expression ``<type>`` exists.
|
||||
|
||||
``<variable>``
|
||||
``<size-var>``
|
||||
Internal cache variable that holds one of the following values:
|
||||
|
||||
``<size>``
|
||||
If the type or expression exists, it will have a non-zero size
|
||||
``<size>`` in bytes.
|
||||
If the type or expression ``<type>`` exists, it will have a non-zero
|
||||
size ``<size>`` in bytes.
|
||||
|
||||
``0``
|
||||
When type has architecture-dependent size; This may occur when
|
||||
:variable:`CMAKE_OSX_ARCHITECTURES` has multiple architectures.
|
||||
In this case ``<variable>_CODE`` contains preprocessor tests
|
||||
mapping from each architecture macro to the corresponding type size.
|
||||
The list of architecture macros is stored in ``<variable>_KEYS``,
|
||||
and the value for each key is stored in ``<variable>-<key>``.
|
||||
When the type has an architecture-dependent size; This may occur when
|
||||
:variable:`CMAKE_OSX_ARCHITECTURES` has multiple architectures. In
|
||||
this case also the ``<size-var>_KEYS`` variable is defined and the
|
||||
``<size-var>_CODE`` variable contains preprocessor tests mapping as
|
||||
explained below.
|
||||
|
||||
"" (empty string)
|
||||
When type or expression does not exist.
|
||||
When the type or expression ``<type>`` does not exist.
|
||||
|
||||
``<variable>_CODE``
|
||||
``HAVE_<size-var>``
|
||||
Internal cache variable that holds a boolean value indicating whether
|
||||
the type or expression ``<type>`` exists. This variable is defined
|
||||
when the ``RESULT_VARIABLE`` argument is not used.
|
||||
|
||||
``<result-var>``
|
||||
.. versionadded:: 4.2
|
||||
|
||||
Internal cache variable defined when the ``RESULT_VARIABLE`` argument
|
||||
is used. It holds a boolean value indicating whether the type or
|
||||
expression ``<type>`` exists (same value as ``HAVE_<size-var>``). In
|
||||
this case, the ``HAVE_<size-var>`` variable is not defined.
|
||||
|
||||
``<size-var>_CODE``
|
||||
CMake variable that holds preprocessor code to define the macro
|
||||
``<variable>`` to the size of the type, or to leave the macro undefined
|
||||
``<size-var>`` to the size of the type, or to leave the macro undefined
|
||||
if the type does not exist.
|
||||
|
||||
Despite the name of this command, it may also be used to determine the size
|
||||
of more complex expressions. For example, to check the size of a struct
|
||||
member:
|
||||
When the type has an architecture-dependent size (``<size-var>`` value
|
||||
is ``0``) this variable contains preprocessor tests mapping from each
|
||||
architecture macro to the corresponding type size.
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
check_type_size("((struct something*)0)->member" SIZEOF_MEMBER)
|
||||
``<size-var>_KEYS``
|
||||
CMake variable that is defined only when the type has an
|
||||
architecture-dependent size (``<size-var>`` value is ``0``) and contains
|
||||
a list of architecture macros. The value for each key is stored in
|
||||
``<size-var>-<key>`` variables.
|
||||
|
||||
.. rubric:: Variables Affecting the Check
|
||||
|
||||
@@ -121,6 +150,7 @@ Consider the code:
|
||||
|
||||
# Check for size of long.
|
||||
check_type_size(long SIZEOF_LONG)
|
||||
|
||||
message("HAVE_SIZEOF_LONG: ${HAVE_SIZEOF_LONG}")
|
||||
message("SIZEOF_LONG: ${SIZEOF_LONG}")
|
||||
message("SIZEOF_LONG_CODE: ${SIZEOF_LONG_CODE}")
|
||||
@@ -133,21 +163,21 @@ On a 64-bit architecture, the output may look something like this::
|
||||
|
||||
On Apple platforms, when :variable:`CMAKE_OSX_ARCHITECTURES` has multiple
|
||||
architectures, types may have architecture-dependent sizes.
|
||||
For example, with the code
|
||||
For example, with the code:
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
include(CheckTypeSize)
|
||||
|
||||
check_type_size(long SIZEOF_LONG)
|
||||
|
||||
message("HAVE_SIZEOF_LONG: ${HAVE_SIZEOF_LONG}")
|
||||
message("SIZEOF_LONG: ${SIZEOF_LONG}")
|
||||
foreach(key IN LISTS SIZE_OF_LONG_KEYS)
|
||||
foreach(key IN LISTS SIZEOF_LONG_KEYS)
|
||||
message("key: ${key}")
|
||||
message("value: ${SIZE_OF_LONG-${key}}")
|
||||
message("value: ${SIZEOF_LONG-${key}}")
|
||||
endforeach()
|
||||
message("SIZEOF_LONG_CODE:
|
||||
${SIZEOF_LONG_CODE}")
|
||||
message("SIZEOF_LONG_CODE:\n${SIZEOF_LONG_CODE}")
|
||||
|
||||
the result may be::
|
||||
|
||||
@@ -159,12 +189,93 @@ the result may be::
|
||||
value: 8
|
||||
SIZEOF_LONG_CODE:
|
||||
#if defined(__i386)
|
||||
# define SIZE_OF_LONG 4
|
||||
# define SIZEOF_LONG 4
|
||||
#elif defined(__x86_64)
|
||||
# define SIZE_OF_LONG 8
|
||||
# define SIZEOF_LONG 8
|
||||
#else
|
||||
# error SIZE_OF_LONG unknown
|
||||
# error SIZEOF_LONG unknown
|
||||
#endif
|
||||
|
||||
Example: Configuration Header
|
||||
"""""""""""""""""""""""""""""
|
||||
|
||||
The next example demonstrates how the result variables can be used in a
|
||||
configuration header:
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
include(CheckTypeSize)
|
||||
check_type_size(long SIZEOF_LONG)
|
||||
|
||||
configure_file(config.h.in config.h @ONLY)
|
||||
|
||||
.. code-block:: c
|
||||
:caption: ``config.h.in``
|
||||
:force:
|
||||
|
||||
/* Define whether the type 'long' exists. */
|
||||
#cmakedefine HAVE_SIZEOF_LONG
|
||||
|
||||
/* The size of 'long', as computed by sizeof. */
|
||||
@SIZEOF_LONG_CODE@
|
||||
|
||||
Example: Checking Complex Expressions
|
||||
"""""""""""""""""""""""""""""""""""""
|
||||
|
||||
Despite the name of this module, it may also be used to determine the size
|
||||
of more complex expressions. For example, to check the size of a struct
|
||||
member:
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
include(CheckTypeSize)
|
||||
check_type_size("((struct something*)0)->member" SIZEOF_MEMBER)
|
||||
|
||||
Example: Isolated Check
|
||||
"""""""""""""""""""""""
|
||||
|
||||
In the following example, the check is performed with temporarily modified
|
||||
additional headers using the ``CMAKE_EXTRA_INCLUDE_FILES`` variable and
|
||||
:module:`CMakePushCheckState` module. The result of the check is stored in
|
||||
``HAVE_SIZEOF_UNION_SEMUN``, and size is stored in ``SIZEOF_UNION_SEMUN``
|
||||
internal cache variables.
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
include(CheckTypeSize)
|
||||
include(CMakePushCheckState)
|
||||
|
||||
cmake_push_check_state(RESET)
|
||||
set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h sys/ipc.h sys/sem.h)
|
||||
check_type_size("union semun" SIZEOF_UNION_SEMUN)
|
||||
cmake_pop_check_state()
|
||||
|
||||
Example: Customizing Result Variable
|
||||
""""""""""""""""""""""""""""""""""""
|
||||
|
||||
Since CMake 4.2, the ``HAVE_<size-var>`` variable name can be customized
|
||||
using the ``RESULT_VARIABLE`` argument. In the following example, this
|
||||
module is used to check whether the ``struct flock`` exists, and the result
|
||||
is stored in the ``MyProj_HAVE_STRUCT_FLOCK`` internal cache variable:
|
||||
|
||||
.. code-block:: cmake
|
||||
|
||||
cmake_minimum_required(VERSION 4.2)
|
||||
|
||||
# ...
|
||||
|
||||
include(CheckTypeSize)
|
||||
include(CMakePushCheckState)
|
||||
|
||||
cmake_push_check_state(RESET)
|
||||
set(CMAKE_EXTRA_INCLUDE_FILES "fcntl.h")
|
||||
|
||||
check_type_size(
|
||||
"struct flock"
|
||||
MyProj_SIZEOF_STRUCT_FLOCK
|
||||
RESULT_VARIABLE MyProj_HAVE_STRUCT_FLOCK
|
||||
)
|
||||
cmake_pop_check_state()
|
||||
#]=======================================================================]
|
||||
|
||||
include(CheckIncludeFile)
|
||||
@@ -173,11 +284,13 @@ include(CheckIncludeFileCXX)
|
||||
include_guard(GLOBAL)
|
||||
|
||||
block(SCOPE_FOR POLICIES)
|
||||
cmake_policy(SET CMP0140 NEW)
|
||||
cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
|
||||
cmake_policy(SET CMP0174 NEW)
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Helper function. DO NOT CALL DIRECTLY.
|
||||
function(__check_type_size_impl type var map builtin language)
|
||||
function(__check_type_size_impl type var result_var map builtin language)
|
||||
if(NOT CMAKE_REQUIRED_QUIET)
|
||||
message(CHECK_START "Check size of ${type}")
|
||||
endif()
|
||||
@@ -194,7 +307,7 @@ function(__check_type_size_impl type var map builtin language)
|
||||
|
||||
# Include header files.
|
||||
set(headers)
|
||||
if(builtin)
|
||||
if(NOT builtin)
|
||||
if(language STREQUAL "CXX" AND type MATCHES "^std::")
|
||||
if(HAVE_SYS_TYPES_H)
|
||||
string(APPEND headers "#include <sys/types.h>\n")
|
||||
@@ -232,7 +345,7 @@ function(__check_type_size_impl type var map builtin language)
|
||||
set(bin ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.bin)
|
||||
file(READ ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/CheckTypeSize.c.in src_content)
|
||||
string(CONFIGURE "${src_content}" src_content @ONLY)
|
||||
try_compile(HAVE_${var} SOURCE_FROM_VAR "${src}" src_content
|
||||
try_compile(${result_var} SOURCE_FROM_VAR "${src}" src_content
|
||||
COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
|
||||
LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS}
|
||||
LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}
|
||||
@@ -244,7 +357,7 @@ function(__check_type_size_impl type var map builtin language)
|
||||
)
|
||||
unset(_CTS_LINK_DIRECTORIES)
|
||||
|
||||
if(HAVE_${var})
|
||||
if(${result_var})
|
||||
# The check compiled. Load information from the binary.
|
||||
file(STRINGS ${bin} strings LIMIT_COUNT 10 REGEX "INFO:size")
|
||||
|
||||
@@ -303,45 +416,53 @@ function(__check_type_size_impl type var map builtin language)
|
||||
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")
|
||||
function(CHECK_TYPE_SIZE TYPE VARIABLE)
|
||||
cmake_parse_arguments(
|
||||
PARSE_ARGV
|
||||
2
|
||||
_CHECK_TYPE_SIZE
|
||||
"BUILTIN_TYPES_ONLY" # Options
|
||||
"RESULT_VARIABLE;LANGUAGE" # One-value arguments
|
||||
"" # Multi-value arguments
|
||||
)
|
||||
|
||||
if(_CHECK_TYPE_SIZE_UNPARSED_ARGUMENTS)
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"Unknown arguments:\n ${_CHECK_TYPE_SIZE_UNPARSED_ARGUMENTS}\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)
|
||||
|
||||
if(NOT DEFINED _CHECK_TYPE_SIZE_RESULT_VARIABLE)
|
||||
set(_CHECK_TYPE_SIZE_RESULT_VARIABLE HAVE_${VARIABLE})
|
||||
elseif(_CHECK_TYPE_SIZE_RESULT_VARIABLE STREQUAL "")
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"Missing argument:\n RESULT_VARIABLE argument requires a value\n"
|
||||
)
|
||||
endif()
|
||||
|
||||
if(NOT DEFINED _CHECK_TYPE_SIZE_LANGUAGE)
|
||||
set(_CHECK_TYPE_SIZE_LANGUAGE C)
|
||||
elseif(_CHECK_TYPE_SIZE_LANGUAGE STREQUAL "")
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"Missing argument:\n LANGUAGE argument requires a value\n"
|
||||
)
|
||||
elseif(NOT _CHECK_TYPE_SIZE_LANGUAGE MATCHES "^(C|CXX)$")
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"Unknown language:\n ${_CHECK_TYPE_SIZE_LANGUAGE}.\n"
|
||||
"Supported languages: C, CXX.\n")
|
||||
endif()
|
||||
|
||||
# Optionally check for standard headers.
|
||||
if(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
|
||||
set(_builtin 0)
|
||||
else()
|
||||
set(_builtin 1)
|
||||
if(_language STREQUAL "C")
|
||||
if(NOT _CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
|
||||
if(_CHECK_TYPE_SIZE_LANGUAGE STREQUAL "C")
|
||||
check_include_file(sys/types.h HAVE_SYS_TYPES_H)
|
||||
check_include_file(stdint.h HAVE_STDINT_H)
|
||||
check_include_file(stddef.h HAVE_STDDEF_H)
|
||||
elseif(_language STREQUAL "CXX")
|
||||
elseif(_CHECK_TYPE_SIZE_LANGUAGE STREQUAL "CXX")
|
||||
check_include_file_cxx(sys/types.h HAVE_SYS_TYPES_H)
|
||||
if("${TYPE}" MATCHES "^std::")
|
||||
check_include_file_cxx(cstdint HAVE_CSTDINT)
|
||||
@@ -352,18 +473,23 @@ macro(CHECK_TYPE_SIZE TYPE VARIABLE)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
unset(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
|
||||
unset(_CHECK_TYPE_SIZE_LANGUAGE)
|
||||
|
||||
# Compute or load the size or size map.
|
||||
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} ${_language})
|
||||
if(NOT DEFINED ${_CHECK_TYPE_SIZE_RESULT_VARIABLE})
|
||||
__check_type_size_impl(
|
||||
${TYPE}
|
||||
${VARIABLE}
|
||||
${_CHECK_TYPE_SIZE_RESULT_VARIABLE}
|
||||
${_map_file}
|
||||
${_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY}
|
||||
${_CHECK_TYPE_SIZE_LANGUAGE}
|
||||
)
|
||||
endif()
|
||||
include(${_map_file} OPTIONAL)
|
||||
set(_map_file)
|
||||
set(_builtin)
|
||||
|
||||
set(_propagated_vars "")
|
||||
|
||||
# Create preprocessor code.
|
||||
if(${VARIABLE}_KEYS)
|
||||
@@ -372,15 +498,17 @@ macro(CHECK_TYPE_SIZE TYPE VARIABLE)
|
||||
foreach(key ${${VARIABLE}_KEYS})
|
||||
string(APPEND ${VARIABLE}_CODE "#${_if} defined(${key})\n# define ${VARIABLE} ${${VARIABLE}-${key}}\n")
|
||||
set(_if elif)
|
||||
list(APPEND _propagated_vars ${VARIABLE}-${key})
|
||||
endforeach()
|
||||
string(APPEND ${VARIABLE}_CODE "#else\n# error ${VARIABLE} unknown\n#endif")
|
||||
set(_if)
|
||||
elseif(${VARIABLE})
|
||||
set(${VARIABLE}_CODE "#define ${VARIABLE} ${${VARIABLE}}")
|
||||
else()
|
||||
set(${VARIABLE}_CODE "/* #undef ${VARIABLE} */")
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
return(PROPAGATE ${VARIABLE}_CODE ${VARIABLE}_KEYS ${_propagated_vars})
|
||||
endfunction()
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
endblock()
|
||||
|
@@ -17,17 +17,39 @@ 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)
|
||||
check_type_size(
|
||||
"((struct somestruct*)0)->somelong"
|
||||
SIZEOF_STRUCTMEMBER_LONG
|
||||
RESULT_VARIABLE HAS_SIZEOF_STRUCTMEMBER_LONG
|
||||
)
|
||||
|
||||
# Check CXX types
|
||||
check_type_size(bool SIZEOF_BOOL LANGUAGE CXX)
|
||||
check_type_size(uint8_t SIZEOF_UINT8_T LANGUAGE CXX)
|
||||
check_type_size(std::uint8_t SIZEOF_STD_UINT8_T LANGUAGE CXX)
|
||||
|
||||
set(CMAKE_EXTRA_INCLUDE_FILES somestruct.h)
|
||||
check_type_size("((struct ns::somestruct*)0)->someint" SIZEOF_NS_STRUCTMEMBER_INT LANGUAGE CXX)
|
||||
check_type_size("((struct ns::somestruct*)0)->someptr" SIZEOF_NS_STRUCTMEMBER_PTR LANGUAGE CXX)
|
||||
check_type_size("((struct ns::somestruct*)0)->somechar" SIZEOF_NS_STRUCTMEMBER_CHAR LANGUAGE CXX)
|
||||
check_type_size(
|
||||
"((struct ns::somestruct*)0)->somelong"
|
||||
SIZEOF_NS_STRUCTMEMBER_LONG
|
||||
RESULT_VARIABLE HAS_SIZEOF_NS_STRUCTMEMBER_LONG
|
||||
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)
|
||||
check_type_size(
|
||||
"((ns::someclass*)0)->somelong"
|
||||
SIZEOF_NS_CLASSMEMBER_LONG
|
||||
LANGUAGE CXX
|
||||
RESULT_VARIABLE HAS_SIZEOF_NS_CLASSMEMBER_LONG
|
||||
)
|
||||
|
||||
configure_file(config.h.in config.h)
|
||||
configure_file(config.hxx.in config.hxx)
|
||||
@@ -38,3 +60,4 @@ add_executable(CheckTypeSize CheckTypeSize.c)
|
||||
add_executable(CheckTypeSizeCXX CheckTypeSize.cxx)
|
||||
|
||||
add_test(NAME CheckTypeSize COMMAND CheckTypeSize)
|
||||
add_test(NAME CheckTypeSizeCXX COMMAND CheckTypeSizeCXX)
|
||||
|
@@ -28,6 +28,12 @@
|
||||
result = 1; \
|
||||
} while (0)
|
||||
|
||||
#define DEF(m) \
|
||||
do { \
|
||||
printf(#m ": expected undefined, got defined (line %d)\n", __LINE__); \
|
||||
result = 1; \
|
||||
} while (0)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int result = 0;
|
||||
@@ -154,6 +160,20 @@ int main(void)
|
||||
NODEF(SIZEOF_STRUCTMEMBER_CHAR);
|
||||
#endif
|
||||
|
||||
/* struct somestruct::somelong */
|
||||
#if defined(SIZEOF_STRUCTMEMBER_LONG)
|
||||
CHECK(x.somelong, SIZEOF_STRUCTMEMBER_LONG);
|
||||
CHECK(x.somelong, SIZEOF_LONG);
|
||||
# if !defined(HAS_SIZEOF_STRUCTMEMBER_LONG)
|
||||
NODEF(HAS_SIZEOF_STRUCTMEMBER_LONG);
|
||||
# endif
|
||||
#elif defined(HAS_SIZEOF_STRUCTMEMBER_LONG)
|
||||
NODEF(SIZEOF_STRUCTMEMBER_LONG);
|
||||
#endif
|
||||
#if defined(HAVE_SIZEOF_STRUCTMEMBER_LONG)
|
||||
DEF(HAVE_SIZEOF_STRUCTMEMBER_LONG);
|
||||
#endif
|
||||
|
||||
/* to avoid possible warnings about unused or write-only variable */
|
||||
x.someint = result;
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
#include "config.h"
|
||||
#include "config.hxx"
|
||||
#include "someclass.hxx"
|
||||
#include "somestruct.h"
|
||||
|
||||
#ifdef HAVE_SYS_TYPES_H
|
||||
# include <sys/types.h>
|
||||
@@ -35,9 +36,16 @@
|
||||
result = 1; \
|
||||
} while (0)
|
||||
|
||||
#define DEF(m) \
|
||||
do { \
|
||||
printf(#m ": expected undefined, got defined (line %d)\n", __LINE__); \
|
||||
result = 1; \
|
||||
} while (0)
|
||||
|
||||
int main()
|
||||
{
|
||||
int result = 0;
|
||||
ns::somestruct x;
|
||||
ns::someclass y;
|
||||
|
||||
/* void* */
|
||||
@@ -148,15 +156,62 @@ int main()
|
||||
NODEF(SIZEOF_STD_UINT8_T);
|
||||
#endif
|
||||
|
||||
/* struct ns::somestruct::someint */
|
||||
#if defined(SIZEOF_NS_STRUCTMEMBER_INT)
|
||||
CHECK(x.someint, SIZEOF_NS_STRUCTMEMBER_INT);
|
||||
CHECK(x.someint, SIZEOF_INT);
|
||||
# if !defined(HAVE_SIZEOF_NS_STRUCTMEMBER_INT)
|
||||
NODEF(HAVE_SIZEOF_NS_STRUCTMEMBER_INT);
|
||||
# endif
|
||||
#elif defined(HAVE_SIZEOF_NS_STRUCTMEMBER_INT)
|
||||
NODEF(SIZEOF_NS_STRUCTMEMBER_INT);
|
||||
#endif
|
||||
|
||||
/* struct ns::somestruct::someptr */
|
||||
#if defined(SIZEOF_NS_STRUCTMEMBER_PTR)
|
||||
CHECK(x.someptr, SIZEOF_NS_STRUCTMEMBER_PTR);
|
||||
CHECK(x.someptr, SIZEOF_DATA_PTR);
|
||||
# if !defined(HAVE_SIZEOF_NS_STRUCTMEMBER_PTR)
|
||||
NODEF(HAVE_SIZEOF_NS_STRUCTMEMBER_PTR);
|
||||
# endif
|
||||
#elif defined(HAVE_SIZEOF_NS_STRUCTMEMBER_PTR)
|
||||
NODEF(SIZEOF_NS_STRUCTMEMBER_PTR);
|
||||
#endif
|
||||
|
||||
/* struct ns::somestruct::somechar */
|
||||
#if defined(SIZEOF_NS_STRUCTMEMBER_CHAR)
|
||||
CHECK(x.somechar, SIZEOF_NS_STRUCTMEMBER_CHAR);
|
||||
CHECK(x.somechar, SIZEOF_CHAR);
|
||||
# if !defined(HAVE_SIZEOF_NS_STRUCTMEMBER_CHAR)
|
||||
NODEF(HAVE_SIZEOF_NS_STRUCTMEMBER_CHAR);
|
||||
# endif
|
||||
#elif defined(HAVE_SIZEOF_NS_STRUCTMEMBER_CHAR)
|
||||
NODEF(SIZEOF_NS_STRUCTMEMBER_CHAR);
|
||||
#endif
|
||||
|
||||
/* struct ns::somestruct::somelong */
|
||||
#if defined(SIZEOF_NS_STRUCTMEMBER_LONG)
|
||||
CHECK(x.somelong, SIZEOF_NS_STRUCTMEMBER_LONG);
|
||||
CHECK(x.somelong, SIZEOF_LONG);
|
||||
# if !defined(HAS_SIZEOF_NS_STRUCTMEMBER_LONG)
|
||||
NODEF(HAS_SIZEOF_NS_STRUCTMEMBER_LONG);
|
||||
# endif
|
||||
#elif defined(HAS_SIZEOF_NS_STRUCTMEMBER_LONG)
|
||||
NODEF(SIZEOF_NS_STRUCTMEMBER_LONG);
|
||||
#endif
|
||||
#if defined(HAVE_SIZEOF_NS_STRUCTMEMBER_LONG)
|
||||
DEF(HAVE_SIZEOF_NS_STRUCTMEMBER_LONG);
|
||||
#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);
|
||||
NODEF(HAVE_SIZEOF_NS_CLASSMEMBER_INT);
|
||||
# endif
|
||||
#elif defined(HAVE_SIZEOF_STRUCTMEMBER_INT)
|
||||
NODEF(SIZEOF_STRUCTMEMBER_INT);
|
||||
#elif defined(HAVE_SIZEOF_NS_CLASSMEMBER_INT)
|
||||
NODEF(SIZEOF_NS_CLASSMEMBER_INT);
|
||||
#endif
|
||||
|
||||
/* ns::someclass::someptr */
|
||||
@@ -183,8 +238,8 @@ int main()
|
||||
|
||||
/* ns::someclass::somebool */
|
||||
#if defined(SIZEOF_NS_CLASSMEMBER_BOOL)
|
||||
CHECK(y.somechar, SIZEOF_NS_CLASSMEMBER_BOOL);
|
||||
CHECK(y.somechar, SIZEOF_BOOL);
|
||||
CHECK(y.somebool, SIZEOF_NS_CLASSMEMBER_BOOL);
|
||||
CHECK(y.somebool, SIZEOF_BOOL);
|
||||
# if !defined(HAVE_SIZEOF_NS_CLASSMEMBER_BOOL)
|
||||
NODEF(HAVE_SIZEOF_NS_CLASSMEMBER_BOOL);
|
||||
# endif
|
||||
@@ -192,6 +247,20 @@ int main()
|
||||
NODEF(SIZEOF_NS_CLASSMEMBER_BOOL);
|
||||
#endif
|
||||
|
||||
/* ns::someclass::somelong */
|
||||
#if defined(SIZEOF_NS_CLASSMEMBER_LONG)
|
||||
CHECK(y.somelong, SIZEOF_NS_CLASSMEMBER_LONG);
|
||||
CHECK(y.somelong, SIZEOF_LONG);
|
||||
# if !defined(HAS_SIZEOF_NS_CLASSMEMBER_LONG)
|
||||
NODEF(HAS_SIZEOF_NS_CLASSMEMBER_LONG);
|
||||
# endif
|
||||
#elif defined(HAS_SIZEOF_NS_CLASSMEMBER_LONG)
|
||||
NODEF(SIZEOF_NS_CLASSMEMBER_LONG);
|
||||
#endif
|
||||
#if defined(HAVE_SIZEOF_NS_CLASSMEMBER_LONG)
|
||||
DEF(HAVE_SIZEOF_NS_CLASSMEMBER_LONG);
|
||||
#endif
|
||||
|
||||
/* to avoid possible warnings about unused or write-only variable */
|
||||
y.someint = result;
|
||||
|
||||
|
@@ -49,3 +49,8 @@
|
||||
/* struct somestruct::somechar */
|
||||
#cmakedefine HAVE_SIZEOF_STRUCTMEMBER_CHAR
|
||||
@SIZEOF_STRUCTMEMBER_CHAR_CODE@
|
||||
|
||||
/* struct somestruct::somelong */
|
||||
#cmakedefine HAS_SIZEOF_STRUCTMEMBER_LONG
|
||||
#cmakedefine HAVE_SIZEOF_STRUCTMEMBER_LONG
|
||||
@SIZEOF_STRUCTMEMBER_LONG_CODE@
|
||||
|
@@ -28,6 +28,28 @@
|
||||
#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@
|
||||
/* struct ns::somestruct::somelong */
|
||||
#cmakedefine HAS_SIZEOF_NS_STRUCTMEMBER_LONG
|
||||
#cmakedefine HAVE_SIZEOF_NS_STRUCTMEMBER_LONG
|
||||
@SIZEOF_NS_STRUCTMEMBER_LONG_CODE@
|
||||
|
||||
/* ns::someclass::someint */
|
||||
#cmakedefine HAVE_SIZEOF_NS_CLASSMEMBER_INT
|
||||
@SIZEOF_NS_CLASSMEMBER_INT_CODE@
|
||||
|
||||
/* ns::someclass::someptr */
|
||||
#cmakedefine HAVE_SIZEOF_NS_CLASSMEMBER_PTR
|
||||
@SIZEOF_NS_CLASSMEMBER_PTR_CODE@
|
||||
|
||||
/* ns::someclass::somechar */
|
||||
#cmakedefine HAVE_SIZEOF_NS_CLASSMEMBER_CHAR
|
||||
@SIZEOF_NS_CLASSMEMBER_CHAR_CODE@
|
||||
|
||||
/* ns::someclass::somebool */
|
||||
#cmakedefine HAVE_SIZEOF_NS_CLASSMEMBER_BOOL
|
||||
@SIZEOF_NS_CLASSMEMBER_BOOL_CODE@
|
||||
|
||||
/* ns::someclass::somelong */
|
||||
#cmakedefine HAS_SIZEOF_NS_CLASSMEMBER_LONG
|
||||
#cmakedefine HAVE_SIZEOF_NS_CLASSMEMBER_LONG
|
||||
@SIZEOF_NS_CLASSMEMBER_LONG_CODE@
|
||||
|
@@ -9,6 +9,7 @@ public:
|
||||
void* someptr;
|
||||
char somechar;
|
||||
bool somebool;
|
||||
long somelong;
|
||||
};
|
||||
}
|
||||
|
||||
|
@@ -1,11 +1,20 @@
|
||||
#ifndef _CMAKE_SOMESTRUCT_H
|
||||
#define _CMAKE_SOMESTRUCT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
namespace ns {
|
||||
#endif
|
||||
|
||||
struct somestruct
|
||||
{
|
||||
int someint;
|
||||
void* someptr;
|
||||
char somechar;
|
||||
long somelong;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* namespace ns { */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@@ -1,7 +1,7 @@
|
||||
CMake Error at .*/Modules/CheckTypeSize.cmake:[0-9]+ \(message\):
|
||||
Missing argument:
|
||||
|
||||
LANGUAGE arguments requires a value
|
||||
LANGUAGE argument requires a value
|
||||
|
||||
Call Stack \(most recent call first\):
|
||||
CheckTypeSizeMissingLanguage.cmake:[0-9]+ \(check_type_size\)
|
||||
|
@@ -0,0 +1 @@
|
||||
1
|
@@ -0,0 +1,8 @@
|
||||
CMake Error at .*/Modules/CheckTypeSize.cmake:[0-9]+ \(message\):
|
||||
Missing argument:
|
||||
|
||||
RESULT_VARIABLE argument requires a value
|
||||
|
||||
Call Stack \(most recent call first\):
|
||||
CheckTypeSizeMissingResultVariable.cmake:[0-9]+ \(check_type_size\)
|
||||
CMakeLists.txt:[0-9]+ \(include\)
|
@@ -0,0 +1,2 @@
|
||||
include(CheckTypeSize)
|
||||
check_type_size(int SIZEOF_INT RESULT_VARIABLE)
|
@@ -1,9 +1,7 @@
|
||||
CMake Error at .*/Modules/CheckTypeSize.cmake:[0-9]+. \(message\):
|
||||
Unknown language:
|
||||
Missing argument:
|
||||
|
||||
.
|
||||
|
||||
Supported languages: C, CXX.
|
||||
LANGUAGE argument requires a value
|
||||
|
||||
Call Stack \(most recent call first\):
|
||||
CheckTypeSizeMixedArgs.cmake:[0-9]+ \(check_type_size\)
|
||||
|
@@ -6,7 +6,10 @@ 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)
|
||||
check_type_size(int SIZEOF_INT RESULT_VARIABLE HAVE_INT LANGUAGE C)
|
||||
check_type_size(int SIZEOF_INT BUILTIN_TYPES_ONLY RESULT_VARIABLE HAVE_INT 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)
|
||||
check_type_size(int SIZEOF_INT RESULT_VARIABLE HAVE_INT RESULT_VARIABLE HAS_INT)
|
||||
|
@@ -2,3 +2,4 @@ enable_language(CXX)
|
||||
include(CheckTypeSize)
|
||||
check_type_size(int SIZEOF_INT LANGUAGE CXX)
|
||||
check_type_size(int SIZEOF_INT BUILTIN_TYPES_ONLY LANGUAGE CXX)
|
||||
check_type_size(int SIZEOF_INT RESULT_VARIABLE HAS_INT LANGUAGE CXX)
|
||||
|
@@ -1,7 +1,7 @@
|
||||
CMake Error at .*/Modules/CheckTypeSize.cmake:[0-9]+. \(message\):
|
||||
Unknown argument:
|
||||
Unknown arguments:
|
||||
|
||||
LANGUAG_
|
||||
LANGUAG_;CXX
|
||||
|
||||
Call Stack \(most recent call first\):
|
||||
CheckTypeSizeUnknownArgument.cmake:[0-9]+ \(check_type_size\)
|
||||
|
@@ -12,6 +12,7 @@ run_cmake(CheckStructHasMemberWrongKey)
|
||||
run_cmake(CheckTypeSizeOk)
|
||||
run_cmake(CheckTypeSizeUnknownLanguage)
|
||||
run_cmake(CheckTypeSizeMissingLanguage)
|
||||
run_cmake(CheckTypeSizeMissingResultVariable)
|
||||
run_cmake(CheckTypeSizeUnknownArgument)
|
||||
run_cmake(CheckTypeSizeMixedArgs)
|
||||
|
||||
|
Reference in New Issue
Block a user