diff options
author | Alexander Neundorf <neundorf@kde.org> | 2007-06-04 21:08:46 (GMT) |
---|---|---|
committer | Alexander Neundorf <neundorf@kde.org> | 2007-06-04 21:08:46 (GMT) |
commit | 74750610cf29e5c0b92feb1f7b8c030e1f0e8448 (patch) | |
tree | bb1e7eedaf39b41cb4de3545a521eaa325d22689 /Modules/CheckTypeSize.cmake | |
parent | e40c51dddf7428d25543c0f05d6a6cc16454c97f (diff) | |
download | CMake-74750610cf29e5c0b92feb1f7b8c030e1f0e8448.zip CMake-74750610cf29e5c0b92feb1f7b8c030e1f0e8448.tar.gz CMake-74750610cf29e5c0b92feb1f7b8c030e1f0e8448.tar.bz2 |
ENH: determine typesize by compiling a file and reading strings from the compiled output.
Tested with various gcc, XCode, MSVC7, sdcc
For OSX when doing TRY_COMPILE() CMAKE_OSX_ARCHITECTURES is used, if there are different results an error is generated. CMAKE_OSX_ARCHITECTURES can be overwritten for the TRY_COMPILES with CMAKE_TRY_COMPILE_OSX_ARCHITECTURES.
Alex
Diffstat (limited to 'Modules/CheckTypeSize.cmake')
-rw-r--r-- | Modules/CheckTypeSize.cmake | 126 |
1 files changed, 72 insertions, 54 deletions
diff --git a/Modules/CheckTypeSize.cmake b/Modules/CheckTypeSize.cmake index a4869a6..7bbc6d2 100644 --- a/Modules/CheckTypeSize.cmake +++ b/Modules/CheckTypeSize.cmake @@ -13,58 +13,76 @@ # CMAKE_REQUIRED_LIBRARIES = list of libraries to link MACRO(CHECK_TYPE_SIZE TYPE VARIABLE) - SET(CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS 1) - IF(NOT DEFINED ${VARIABLE}) - IF("HAVE_${VARIABLE}" MATCHES "^HAVE_${VARIABLE}$") - SET(CHECK_TYPE_SIZE_TYPE "${TYPE}") - SET(MACRO_CHECK_TYPE_SIZE_FLAGS - "${CMAKE_REQUIRED_FLAGS}") - FOREACH(def HAVE_SYS_TYPES_H HAVE_STDINT_H HAVE_STDDEF_H) - IF("${def}") - SET(MACRO_CHECK_TYPE_SIZE_FLAGS - "${MACRO_CHECK_TYPE_SIZE_FLAGS} -D${def}") - ENDIF("${def}") - ENDFOREACH(def) - SET(CHECK_TYPE_SIZE_PREINCLUDE) - SET(CHECK_TYPE_SIZE_PREMAIN) - FOREACH(def ${CMAKE_EXTRA_INCLUDE_FILES}) - SET(CHECK_TYPE_SIZE_PREMAIN "${CHECK_TYPE_SIZE_PREMAIN}#include \"${def}\"\n") - ENDFOREACH(def) - CONFIGURE_FILE("${CMAKE_ROOT}/Modules/CheckTypeSize.c.in" - "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckTypeSize.c" IMMEDIATE @ONLY) - FILE(READ "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckTypeSize.c" - CHECK_TYPE_SIZE_FILE_CONTENT) - MESSAGE(STATUS "Check size of ${TYPE}") - IF(CMAKE_REQUIRED_LIBRARIES) - SET(CHECK_TYPE_SIZE_ADD_LIBRARIES - "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}") - ELSE(CMAKE_REQUIRED_LIBRARIES) - SET(CHECK_TYPE_SIZE_ADD_LIBRARIES) - ENDIF(CMAKE_REQUIRED_LIBRARIES) - IF(CMAKE_REQUIRED_INCLUDES) - SET(CHECK_TYPE_SIZE_ADD_INCLUDES - "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}") - ELSE(CMAKE_REQUIRED_INCLUDES) - SET(CHECK_TYPE_SIZE_ADD_INCLUDES) - ENDIF(CMAKE_REQUIRED_INCLUDES) - TRY_RUN(${VARIABLE} HAVE_${VARIABLE} - ${CMAKE_BINARY_DIR} - "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckTypeSize.c" - COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} - CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_TYPE_SIZE_FLAGS} - "${CHECK_TYPE_SIZE_ADD_LIBRARIES}" - "${CHECK_TYPE_SIZE_ADD_INCLUDES}" - OUTPUT_VARIABLE OUTPUT) - IF(HAVE_${VARIABLE}) - MESSAGE(STATUS "Check size of ${TYPE} - done") - FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log - "Determining size of ${TYPE} passed with the following output:\n${OUTPUT}\n\n") - ELSE(HAVE_${VARIABLE}) - MESSAGE(STATUS "Check size of ${TYPE} - failed") - FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log - "Determining size of ${TYPE} failed with the following output:\n${OUTPUT}\nCheckTypeSize.c:\n${CHECK_TYPE_SIZE_FILE_CONTENT}\n\n") - ENDIF(HAVE_${VARIABLE}) - ENDIF("HAVE_${VARIABLE}" MATCHES "^HAVE_${VARIABLE}$") - ENDIF(NOT DEFINED ${VARIABLE}) - SET(CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS ) + IF("HAVE_${VARIABLE}" MATCHES "^HAVE_${VARIABLE}$") + MESSAGE(STATUS "Check size of ${TYPE}") + SET(CHECK_TYPE_SIZE_TYPE "${TYPE}") + SET(MACRO_CHECK_TYPE_SIZE_FLAGS + "${CMAKE_REQUIRED_FLAGS}") + FOREACH(def HAVE_SYS_TYPES_H HAVE_STDINT_H HAVE_STDDEF_H) + IF("${def}") + SET(MACRO_CHECK_TYPE_SIZE_FLAGS + "${MACRO_CHECK_TYPE_SIZE_FLAGS} -D${def}") + ENDIF("${def}") + ENDFOREACH(def) + SET(CHECK_TYPE_SIZE_PREINCLUDE) + SET(CHECK_TYPE_SIZE_PREMAIN) + SET(CHECK_TYPE_SIZE_ADD_LIBRARIES) + SET(CHECK_TYPE_SIZE_ADD_INCLUDES) + + FOREACH(def ${CMAKE_EXTRA_INCLUDE_FILES}) + SET(CHECK_TYPE_SIZE_PREMAIN "${CHECK_TYPE_SIZE_PREMAIN}#include \"${def}\"\n") + ENDFOREACH(def) + IF(CMAKE_REQUIRED_LIBRARIES) + SET(CHECK_TYPE_SIZE_ADD_LIBRARIES + "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}") + ENDIF(CMAKE_REQUIRED_LIBRARIES) + IF(CMAKE_REQUIRED_INCLUDES) + SET(CHECK_TYPE_SIZE_ADD_INCLUDES + "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}") + ENDIF(CMAKE_REQUIRED_INCLUDES) + + CONFIGURE_FILE("${CMAKE_ROOT}/Modules/CheckTypeSizeC.c.in" + "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckTypeSizeC.c" IMMEDIATE @ONLY) + FILE(READ "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckTypeSizeC.c" + CHECK_TYPE_SIZE_FILE_CONTENT) + TRY_COMPILE(HAVE_${VARIABLE} + ${CMAKE_BINARY_DIR} + "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckTypeSizeC.c" + COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} + CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_TYPE_SIZE_FLAGS} + "${CHECK_TYPE_SIZE_ADD_LIBRARIES}" + "${CHECK_TYPE_SIZE_ADD_INCLUDES}" + OUTPUT_VARIABLE OUTPUT + COPY_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize.bin" ) + + IF(HAVE_${VARIABLE}) +# FILE(HEX_TO_BIN "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize.bin" +# "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize.even_more_bin") + FILE(STRINGS "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize.bin" + CMAKE_CHECKTYPESIZE_STRINGS LIMIT_COUNT 2 REGEX "INFO:sizeof") + + SET(CMAKE_CHECKTYPESIZE_FIRST_RESULT "FIRST_LOOP") + FOREACH(info ${CMAKE_CHECKTYPESIZE_STRINGS}) + IF("${info}" MATCHES ".*INFO:sizeof\\[0*([^]]*)\\].*") + STRING(REGEX REPLACE ".*INFO:sizeof\\[0*([^]]*)\\].*" "\\1" ${VARIABLE} "${info}") + ENDIF("${info}" MATCHES ".*INFO:sizeof\\[0*([^]]*)\\].*") + IF("${CMAKE_CHECKTYPESIZE_FIRST_RESULT}" STREQUAL "FIRST_LOOP") + SET(CMAKE_CHECKTYPESIZE_FIRST_RESULT "${${VARIABLE}}") + ENDIF("${CMAKE_CHECKTYPESIZE_FIRST_RESULT}" STREQUAL "FIRST_LOOP") + IF(NOT "${CMAKE_CHECKTYPESIZE_FIRST_RESULT}" STREQUAL "${${VARIABLE}}") + MESSAGE(SEND_ERROR "CHECK_TYPE_SIZE found different results, consider setting CMAKE_OSX_ARCHITECTURES or CMAKE_TRY_COMPILE_OSX_ARCHITECTURES to one or no architecture !") + ENDIF(NOT "${CMAKE_CHECKTYPESIZE_FIRST_RESULT}" STREQUAL "${${VARIABLE}}") + + ENDFOREACH(info ${CMAKE_CHECKTYPESIZE_STRINGS}) + MESSAGE(STATUS "Check size of ${TYPE} - done") + FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log + "Determining size of ${TYPE} passed with the following output:\n${OUTPUT}\n\n") + ELSE(HAVE_${VARIABLE}) + MESSAGE(STATUS "Check size of ${TYPE} - failed") + FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log + "Determining size of ${TYPE} failed with the following output:\n${OUTPUT}\nCheckTypeSize.c:\n${CHECK_TYPE_SIZE_FILE_CONTENT}\n\n") + SET(${VARIABLE}) + ENDIF(HAVE_${VARIABLE}) + SET(${VARIABLE} "${${VARIABLE}}" CACHE INTERNAL "Result of CHECK_TYPE_SIZE" FORCE) + ENDIF("HAVE_${VARIABLE}" MATCHES "^HAVE_${VARIABLE}$") ENDMACRO(CHECK_TYPE_SIZE) |