view Resources/PostgreSQL/CheckTypeAlignment.cmake @ 232:4e15eace9b90

reorganization in DatabaseBackendAdapterV3
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 06 Apr 2021 12:50:06 +0200
parents 8c7bb94adff7
children
line wrap: on
line source

macro(CHECK_TYPE_ALIGNMENT TYPE NAME)
  if(NOT ${NAME})
    message(STATUS "Check alignment of ${TYPE}")
    
    set(INCLUDE_HEADERS "#include <stddef.h>
      #include <stdio.h>
      #include <stdlib.h>")

    if(HAVE_STDINT_H)
      set(INCLUDE_HEADERS "${INCLUDE_HEADERS}\n#include <stdint.h>\n")
    endif(HAVE_STDINT_H)

    file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/check_${NAME}_alignment.c"
      "${INCLUDE_HEADERS}
      int main(){
        char diff;
        struct foo {char a; ${TYPE} b;};
        struct foo *p = (struct foo *) malloc(sizeof(struct foo));
        diff = ((char *)&p->b) - ((char *)&p->a);
        return diff;}
    ")

    try_run(${NAME} COMPILE_RESULT "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/"
      "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/check_${NAME}_alignment.c")

    message(STATUS "Check alignment of ${TYPE} - ${${NAME}}")

  endif(NOT ${NAME})
endmacro()