cmake_minimum_required(VERSION 2.6...3.29)
project(libpq)

set(LIBPQ_SERIES 9)
set(LIBPQ_MAJOR 1)
set(LIBPQ_MINOR 4)

if(WIN32)
    set(LIBPQ_SOURCES
        chklocale.c
        crypt.c
        encnames.c
        fe-auth.c
        fe-connect.c
        fe-exec.c
        fe-lobj.c
        fe-misc.c
        fe-print.c
        fe-protocol2.c
        fe-protocol3.c
        fe-secure.c
        getaddrinfo.c
        inet_aton.c
        inet_net_ntop.c
        ip.c
        libpq-events.c
        md5.c
        noblock.c
        open.c
        pgsleep.c
        pgstrcasecmp.c
        pqexpbuffer.c
        pqsignal.c
        pthread-win32.c
        snprintf.c
        strlcpy.c
        thread.c
        wchar.c
        win32.c
        win32error.c
        win32setlocale.c
    )

    include_directories(${LIBPQ_INCLUDE})
    add_definitions(-DFRONTEND -DUNSAFE_STAT_OK -DEXEC_BACKEND -DSO_MAJOR_VERSION=5)

    #### Static library (uncomment to build)
    #add_library(libpq_static STATIC ${LIBPQ_SOURCES})
    #set_target_properties(libpq_static PROPERTIES
    #    OUTPUT_NAME "pq"
    #    PREFIX "lib"
    #    IMPORT_PREFIX "lib"
    #    CLEAN_DIRECT_OUTPUT 1)

    add_library(libpq_shared SHARED ${LIBPQ_SOURCES})
    if(WIN32)
        set_target_properties(libpq_shared PROPERTIES OUTPUT_NAME "pq${LIBPQ_SERIES}")
    else()
        set_target_properties(libpq_shared PROPERTIES OUTPUT_NAME "pq")
    endif()
    set_target_properties(libpq_shared PROPERTIES
        VERSION ${LIBPQ_SERIES}.${LIBPQ_MAJOR}.${LIBPQ_MINOR}
        PREFIX "lib"
        IMPORT_PREFIX "lib"
        CLEAN_DIRECT_OUTPUT 1)
    target_link_libraries(libpq_shared ws2_32 secur32)
endif()
