# ===========================================================================
#
#                            PUBLIC DOMAIN NOTICE
#               National Center for Biotechnology Information
#
#  This software/database is a "United States Government Work" under the
#  terms of the United States Copyright Act.  It was written as part of
#  the author's official duties as a United States Government employee and
#  thus cannot be copyrighted.  This software/database is freely available
#  to the public for use. The National Library of Medicine and the U.S.
#  Government have not placed any restriction on its use or reproduction.
#
#  Although all reasonable efforts have been taken to ensure the accuracy
#  and reliability of the software and data, the NLM and the U.S.
#  Government do not and cannot warrant the performance or results that
#  may be obtained by using this software or data. The NLM and the U.S.
#  Government disclaim all warranties, express or implied, including
#  warranties of performance, merchantability or fitness for any particular
#  purpose.
#
#  Please cite the author in any work or product based on this material.
#
# ===========================================================================

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE})

function( GetTargetSources targets out_name_src_files out_name_incl_dirs out_name_compile_definitions )
    set( out_src_files "" )
    set( out_incl_dirs "" )
    set( out_compile_defs "" )

    foreach( target ${targets})
        get_target_property(TARGET_SRC_DIR ${target} SOURCE_DIR)
        get_target_property(TARGET_SRC ${target} SOURCES)
        get_target_property(TARGET_INCLUDE_DIRS ${target} INCLUDE_DIRECTORIES)
        get_target_property(TARGET_COMPILE_DEFS ${target} COMPILE_DEFINITIONS)

        foreach( src_file ${TARGET_SRC})
            list( APPEND out_src_files "${TARGET_SRC_DIR}/${src_file}" )
        endforeach()

        foreach( i ${TARGET_INCLUDE_DIRS})
            list( APPEND out_incl_dirs "${i}" )
        endforeach()

        if(NOT "TARGET_COMPILE_DEFS-NOTFOUND" STREQUAL ${TARGET_COMPILE_DEFS})
            foreach( i ${TARGET_COMPILE_DEFS})
                list( APPEND out_compile_defs "${i}" )
            endforeach()
        endif()
    endforeach()

    list( REMOVE_DUPLICATES out_src_files )
    list( REMOVE_DUPLICATES out_incl_dirs )
    list( REMOVE_DUPLICATES out_compile_defs )

    set( ${out_name_src_files} "${out_src_files}" PARENT_SCOPE )
    set( ${out_name_incl_dirs} "${out_incl_dirs}" PARENT_SCOPE )
    set( ${out_name_compile_definitions} "${out_compile_defs}" PARENT_SCOPE )
endfunction()

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE})

if(__NCBIVDB_CFG_PACKAGING)
    if(BUILD_SHARED_LIBS)
        set(ncbi-vdb-static OFF)
        set(ncbi-wvdb-static OFF)
        set(ncbi-vdb-shared "ncbi-vdb")
        set(ncbi-wvdb-shared "ncbi-wvdb")
    else()
        set(ncbi-vdb-static "ncbi-vdb")
        set(ncbi-wvdb-static "ncbi-wvdb")
        set(ncbi-vdb-shared OFF)
        set(ncbi-wvdb-shared OFF)
    endif()
    set(extra_libs ${CONAN_LIBS})
else()
    set(ncbi-vdb-static "ncbi-vdb")
    set(ncbi-vdb-shared "ncbi-vdb-shared")
    set(ncbi-wvdb-static "ncbi-wvdb")
    set(ncbi-wvdb-shared "ncbi-wvdb-shared")
    set(extra_libs OFF)
endif()

list(REMOVE_ITEM VDB_LIB_RD_TARGETS vdb)
GetTargetSources( vdb vdb_src vdb_incl_dirs vdb_compile_defs )

if(ncbi-vdb-static)
    add_library(${ncbi-vdb-static} STATIC ${vdb_src})
    if (vdb_compile_defs)
        target_compile_definitions(${ncbi-vdb-static} PRIVATE ${vdb_compile_defs})
    endif()
    target_include_directories(${ncbi-vdb-static} PUBLIC ${vdb_incl_dirs})
    target_link_libraries(${ncbi-vdb-static} PUBLIC ${VDB_LIB_RD_TARGETS})
    if(extra_libs)
        target_link_libraries(${ncbi-vdb-static} ${extra_libs})
    endif()
    target_link_libraries(${ncbi-vdb-static} PRIVATE ${MBEDTLS_LIBS})
    target_link_libraries(${ncbi-vdb-static} PRIVATE z bz2)
endif()
if(ncbi-vdb-shared)
    add_library(${ncbi-vdb-shared} SHARED ${vdb_src})
    if (vdb_compile_defs)
        target_compile_definitions(${ncbi-vdb-shared} PRIVATE ${vdb_compile_defs})
    endif()
    target_include_directories(${ncbi-vdb-shared} PUBLIC ${vdb_incl_dirs})
    target_link_libraries(${ncbi-vdb-shared} PUBLIC ${VDB_LIB_RD_TARGETS})
    if(extra_libs)
        target_link_libraries(${ncbi-vdb-shared} ${extra_libs})
    endif()
    target_link_libraries(${ncbi-vdb-shared} PRIVATE ${MBEDTLS_LIBS})
    target_link_libraries(${ncbi-vdb-shared} PRIVATE z bz2)
endif()

list(REMOVE_ITEM VDB_LIB_RDWR_TARGETS wvdb)
GetTargetSources( wvdb vdb_src vdb_incl_dirs vdb_compile_defs )

if(ncbi-wvdb-static)
    add_library(${ncbi-wvdb-static} STATIC ${vdb_src})
    target_compile_definitions(${ncbi-wvdb-static} PRIVATE ${vdb_compile_defs})
    target_include_directories(${ncbi-wvdb-static} PUBLIC ${vdb_incl_dirs})
    target_link_libraries(${ncbi-wvdb-static} PUBLIC ${VDB_LIB_RDWR_TARGETS})
    target_link_libraries(${ncbi-wvdb-static} PRIVATE ${MBEDTLS_LIBS})
    target_link_libraries(${ncbi-wvdb-static} PRIVATE z bz2)
endif()
if(ncbi-wvdb-shared)
    add_library(${ncbi-wvdb-shared} SHARED ${vdb_src})
    target_compile_definitions(${ncbi-wvdb-shared} PRIVATE ${vdb_compile_defs})
    target_include_directories(${ncbi-wvdb-shared} PUBLIC ${vdb_incl_dirs})
    target_link_libraries(${ncbi-wvdb-shared} PUBLIC ${VDB_LIB_RDWR_TARGETS})
    target_link_libraries(${ncbi-wvdb-shared} PRIVATE ${MBEDTLS_LIBS})
    target_link_libraries(${ncbi-wvdb-shared} PRIVATE z bz2)
endif()

if(_NCBIVDB_CFG_PACKAGING)
    install(TARGETS ncbi-vdb ncbi-wvdb)
endif()

