view Resources/CMake/PostgreSQLConfiguration.cmake @ 138:4968c70ba17e

preparing for auto-configuration using cmake
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 23 Aug 2017 16:53:45 +0200 (2017-08-23)
parents e221494b8ab9
children 171da992b0a5
line wrap: on
line source
# Orthanc - A Lightweight, RESTful DICOM Store
# Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
# Department, University Hospital of Liege, Belgium
# Copyright (C) 2017 Osimis, Belgium
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


#####################################################################
## PostgreSQL
#####################################################################

INCLUDE(CheckTypeSize)
INCLUDE(CheckCSourceCompiles)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckStructHasMember)


if (STATIC_BUILD OR NOT USE_SYSTEM_LIBPQ)
  add_definitions(-DORTHANC_POSTGRESQL_STATIC=1)

  SET(LIBPQ_MAJOR 9)
  SET(LIBPQ_MINOR 6)
  SET(LIBPQ_REVISION 1)
  SET(LIBPQ_VERSION ${LIBPQ_MAJOR}.${LIBPQ_MINOR}.${LIBPQ_REVISION})

  SET(LIBPQ_SOURCES_DIR ${CMAKE_BINARY_DIR}/postgresql-${LIBPQ_VERSION})
  DownloadPackage(
    "eaa7e267e89ea1ed2693d2b88d3cd290"
    "http://www.orthanc-server.com/downloads/third-party/postgresql-${LIBPQ_VERSION}.tar.gz"
    "${LIBPQ_SOURCES_DIR}")

  if (MSVC)
    configure_file(
      ${LIBPQ_SOURCES_DIR}/src/include/pg_config.h.win32
      ${AUTOGENERATED_DIR}/pg_config.h
      COPYONLY)
    
    configure_file(
      ${LIBPQ_SOURCES_DIR}/src/include/pg_config_ext.h.win32
      ${AUTOGENERATED_DIR}/pg_config_ext.h
      COPYONLY)
  else()
    
  endif()

  if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    set(LIBPQ_CONFIG_H ${LIBPQ_SOURCES_DIR}/src/include/pg_config.h.win32)

    else()
      if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
        set(LIBPQ_CONFIG_H ${CMAKE_SOURCE_DIR}/Resources/Platforms/pg_config-windows64.h)
      else()
        set(LIBPQ_CONFIG_H ${CMAKE_SOURCE_DIR}/Resources/Platforms/pg_config-windows32.h)
      endif()
    endif()

    add_definitions(
      -DEXEC_BACKEND
      )

    configure_file(
      ${LIBPQ_SOURCES_DIR}/src/include/port/win32.h
      ${AUTOGENERATED_DIR}/pg_config_os.h
      COPYONLY)

  elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
    add_definitions(
      -D_GNU_SOURCE
      -D_THREAD_SAFE
      -D_POSIX_PTHREAD_SEMANTICS
      )

    configure_file(
      ${LIBPQ_SOURCES_DIR}/src/include/port/linux.h
      ${AUTOGENERATED_DIR}/pg_config_os.h
      COPYONLY)

  elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
    if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
      set(LIBPQ_CONFIG_H ${CMAKE_SOURCE_DIR}/Resources/Platforms/pg_config-darwin64.h)
    else()
      message(FATAL_ERROR "Support your platform here")
    endif()

    add_definitions(
      -D_GNU_SOURCE
      -D_THREAD_SAFE
      -D_POSIX_PTHREAD_SEMANTICS
      )

    configure_file(
      ${LIBPQ_SOURCES_DIR}/src/include/port/darwin.h
      ${AUTOGENERATED_DIR}/pg_config_os.h
      COPYONLY)

  elseif (${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
    if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
      set(LIBPQ_CONFIG_H ${CMAKE_SOURCE_DIR}/Resources/Platforms/pg_config-openbsd64.h)
    else()
      message(FATAL_ERROR "Support your platform here")
    endif()

    configure_file(
      ${LIBPQ_SOURCES_DIR}/src/include/port/openbsd.h
      ${AUTOGENERATED_DIR}/pg_config_os.h
      COPYONLY)

  else()
    message(FATAL_ERROR "Support your platform here")
  endif()


  configure_file(
    ${LIBPQ_CONFIG_H}
    ${AUTOGENERATED_DIR}/pg_config.h
    COPYONLY
    )


  
  set(CMAKE_EXTRA_INCLUDE_FILES "sys/socket.h;netdb.h;sys/types.h")
  
  check_type_size("long int" SIZE_LONG_INT)
  if (SIZE_LONG_INT EQUAL 8)
    set(HAVE_LONG_INT_64 1)
  endif()

  file(READ ${CMAKE_SOURCE_DIR}/Resources/Platforms/c_flexmember.c SOURCE)
  check_c_source_compiles("${SOURCE}" c_flexmember)
  if (c_flexmember)
    set(FLEXIBLE_ARRAY_MEMBER 1)
  endif()

  file(READ ${CMAKE_SOURCE_DIR}/Resources/Platforms/printf_archetype.c SOURCE)
  check_c_source_compiles("${SOURCE}" printf_archetype)
  if (printf_archetype)
    set(PG_PRINTF_ATTRIBUTE "gnu_printf")
  else()
    set(PG_PRINTF_ATTRIBUTE "printf")
  endif()

  check_function_exists("unsetenv" HAVE_UNSETENV)
  check_function_exists("getaddrinfo" HAVE_GETADDRINFO)

  check_type_size("struct addrinfo" SIZE_STRUCT_ADDRINFO)
  if (HAVE_SIZE_STRUCT_ADDRINFO)
    set(HAVE_STRUCT_ADDRINFO 1)
  endif()

  check_type_size("struct sockaddr_storage" SIZE_STRUCT_SOCKADDR_STORAGE)
  if (HAVE_SIZE_STRUCT_SOCKADDR_STORAGE)
    set(HAVE_STRUCT_SOCKADDR_STORAGE 1)
  endif()

  set(MEMSET_LOOP_LIMIT 1024)            # This is hardcoded in "postgresql-9.6.1/configure"
  set(DEF_PGPORT 5432)                   # Default port number of PostgreSQL
  set(DEF_PGPORT_STR "\"5432\"")         # Same as above, as a string
  set(PG_VERSION "\"${LIBPQ_VERSION}\"") # Version of PostgreSQL, as a string

  # Version of PostgreSQL, as a number
  math(EXPR PG_VERSION_NUM "${LIBPQ_MAJOR} * 10000 + ${LIBPQ_MINOR} * 100 + ${LIBPQ_REVISION}")
  
  set(HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY 1)   # TODO
  set(HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN 1)    # TODO
  set(ACCEPT_TYPE_ARG3 socklen_t)  # TODO

  # Compute maximum alignment of any basic type.
  # We assume long's alignment is at least as strong as char, short, or int;
  # but we must check long long (if it exists) and double.
  check_type_size("long" SIZE_LONG)
  check_type_size("long long" SIZE_LONG_LONG)
  check_type_size("double" SIZE_DOUBLE)
  set(MAXIMUM_ALIGNOF ${SIZE_LONG})
  if(SIZE_LONG_LONG AND SIZE_LONG_LONG GREATER MAXIMUM_ALIGNOF)
    set(MAXIMUM_ALIGNOF ${SIZE_LONG_LONG})
  endif()
  if(SIZE_DOUBLE GREATER MAXIMUM_ALIGNOF)
    set(MAXIMUM_ALIGNOF ${SIZE_DOUBLE})
  endif()
  
  
  configure_file(
    /tmp/toto.h
    ${AUTOGENERATED_DIR}/pg_config.h)




  
  configure_file(
    ${CMAKE_SOURCE_DIR}/Resources/Platforms/pg_config_ext.h
    ${AUTOGENERATED_DIR}/pg_config_ext.h
    COPYONLY
    )

  file(WRITE
    ${AUTOGENERATED_DIR}/pg_config_paths.h
    "")

  add_definitions(
    -D_REENTRANT
    -DFRONTEND
    -DUNSAFE_STAT_OK
    -DSYSCONFDIR=""
    )

  include_directories(
    ${LIBPQ_SOURCES_DIR}/src/include
    ${LIBPQ_SOURCES_DIR}/src/include/libpq
    ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq
    )

  set(LIBPQ_SOURCES
    ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-auth.c 
    ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-connect.c
    ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-exec.c
    ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-lobj.c
    ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-misc.c
    ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-print.c
    ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-protocol2.c
    ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-protocol3.c
    ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/fe-secure.c
    ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/libpq-events.c
    ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/pqexpbuffer.c

    # libpgport C files we always use
    ${LIBPQ_SOURCES_DIR}/src/port/chklocale.c
    ${LIBPQ_SOURCES_DIR}/src/port/inet_net_ntop.c
    ${LIBPQ_SOURCES_DIR}/src/port/noblock.c
    ${LIBPQ_SOURCES_DIR}/src/port/pgstrcasecmp.c
    ${LIBPQ_SOURCES_DIR}/src/port/pqsignal.c
    ${LIBPQ_SOURCES_DIR}/src/port/thread.c

    ${LIBPQ_SOURCES_DIR}/src/backend/libpq/ip.c
    ${LIBPQ_SOURCES_DIR}/src/backend/libpq/md5.c
    ${LIBPQ_SOURCES_DIR}/src/backend/utils/mb/encnames.c
    ${LIBPQ_SOURCES_DIR}/src/backend/utils/mb/wchar.c
    )


  if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
    LIST(APPEND LIBPQ_SOURCES
      ${LIBPQ_SOURCES_DIR}/src/port/strlcpy.c
      )      

  elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    include_directories(
      ${LIBPQ_SOURCES_DIR}/src/include/port/win32
      ${LIBPQ_SOURCES_DIR}/src/port
      )
    
    LIST(APPEND LIBPQ_SOURCES
      # libpgport C files that are needed if identified by configure
      ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/win32.c
      ${LIBPQ_SOURCES_DIR}/src/port/crypt.c 
      ${LIBPQ_SOURCES_DIR}/src/port/inet_aton.c
      ${LIBPQ_SOURCES_DIR}/src/port/open.c
      ${LIBPQ_SOURCES_DIR}/src/port/pgsleep.c
      ${LIBPQ_SOURCES_DIR}/src/port/snprintf.c
      ${LIBPQ_SOURCES_DIR}/src/port/system.c 
      ${LIBPQ_SOURCES_DIR}/src/port/win32setlocale.c
      ${LIBPQ_SOURCES_DIR}/src/port/getaddrinfo.c
      ${LIBPQ_SOURCES_DIR}/src/port/strlcpy.c
      )
      
    if (CMAKE_COMPILER_IS_GNUCXX OR 
        (MSVC AND MSVC_VERSION GREATER 1800))
      # Starting Visual Studio 2013 (version 1800), it is necessary to also add "win32error.c"
      LIST(APPEND LIBPQ_SOURCES ${LIBPQ_SOURCES_DIR}/src/port/win32error.c)
    endif()

    if (MSVC)
      LIST(APPEND LIBPQ_SOURCES ${LIBPQ_SOURCES_DIR}/src/interfaces/libpq/pthread-win32.c)
    endif()
  endif()

  if (${CMAKE_COMPILER_IS_GNUCXX})
    if (NOT CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
      LIST(APPEND LIBPQ_SOURCES
        ${LIBPQ_SOURCES_DIR}/src/port/getpeereid.c
        )
    endif()
  elseif (${MSVC})
    include_directories(
      ${LIBPQ_SOURCES_DIR}/src/include/port/win32_msvc
      )
    
    LIST(APPEND LIBPQ_SOURCES
      ${LIBPQ_SOURCES_DIR}/src/port/dirent.c 
      ${LIBPQ_SOURCES_DIR}/src/port/dirmod.c 
      )
  endif()

  source_group(ThirdParty\\PostgreSQL REGULAR_EXPRESSION ${LIBPQ_SOURCES_DIR}/.*)

else()
  include(${CMAKE_SOURCE_DIR}/Resources/CMake/FindPostgreSQL.cmake)
  include_directories(
    ${PostgreSQL_INCLUDE_DIR}
    ${PostgreSQL_TYPE_INCLUDE_DIR}
    )
  link_libraries(${PostgreSQL_LIBRARY})
endif()