CheckTypeSize: Use C++-style headers to check for std:: types

In order to check for types like `std::uint8_t` in C++, we need to
include `<cstdint>` instead of `<stdint.h>`.
This commit is contained in:
Aiden Woodruff
2020-07-12 21:36:03 -04:00
committed by Brad King
parent 32c000823e
commit 371072e9e1

View File

@@ -89,25 +89,7 @@ function(__check_type_size_impl type var map builtin language)
message(CHECK_START "Check size of ${type}")
endif()
# Include header files.
set(headers)
if(builtin)
if(HAVE_SYS_TYPES_H)
string(APPEND headers "#include <sys/types.h>\n")
endif()
if(HAVE_STDINT_H)
string(APPEND headers "#include <stdint.h>\n")
endif()
if(HAVE_STDDEF_H)
string(APPEND headers "#include <stddef.h>\n")
endif()
endif()
foreach(h ${CMAKE_EXTRA_INCLUDE_FILES})
string(APPEND headers "#include \"${h}\"\n")
endforeach()
# Perform the check.
# Perform language check
if(language STREQUAL "C")
set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.c)
elseif(language STREQUAL "CXX")
@@ -115,6 +97,37 @@ function(__check_type_size_impl type var map builtin language)
else()
message(FATAL_ERROR "Unknown language:\n ${language}\nSupported languages: C, CXX.\n")
endif()
# Include header files.
set(headers)
if(builtin)
if(language STREQUAL "CXX" AND type MATCHES "^std::")
if(HAVE_SYS_TYPES_H)
string(APPEND headers "#include <sys/types.h>\n")
endif()
if(HAVE_CSTDINT)
string(APPEND headers "#include <cstdint>\n")
endif()
if(HAVE_CSTDDEF)
string(APPEND headers "#include <cstddef>\n")
endif()
else()
if(HAVE_SYS_TYPES_H)
string(APPEND headers "#include <sys/types.h>\n")
endif()
if(HAVE_STDINT_H)
string(APPEND headers "#include <stdint.h>\n")
endif()
if(HAVE_STDDEF_H)
string(APPEND headers "#include <stddef.h>\n")
endif()
endif()
endif()
foreach(h ${CMAKE_EXTRA_INCLUDE_FILES})
string(APPEND headers "#include \"${h}\"\n")
endforeach()
# Perform the check.
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}
@@ -232,8 +245,13 @@ macro(CHECK_TYPE_SIZE TYPE VARIABLE)
check_include_file(stddef.h HAVE_STDDEF_H)
elseif(_language STREQUAL "CXX")
check_include_file_cxx(sys/types.h HAVE_SYS_TYPES_H)
check_include_file_cxx(stdint.h HAVE_STDINT_H)
check_include_file_cxx(stddef.h HAVE_STDDEF_H)
if("${TYPE}" MATCHES "^std::")
check_include_file_cxx(cstdint HAVE_CSTDINT)
check_include_file_cxx(cstddef HAVE_CSTDDEF)
else()
check_include_file_cxx(stdint.h HAVE_STDINT_H)
check_include_file_cxx(stddef.h HAVE_STDDEF_H)
endif()
endif()
endif()
unset(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)