# ===========================================================================
#
#                            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})

# TARGET_OBJECTS doesn't work for XCode, this is why we build ngs & ncbi-vdb from sources
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})


####################################
GetTargetSources( "${VDB_LIB_RD_TARGETS}" all_src all_incl_dirs all_compile_defs )
include_directories( ${all_incl_dirs} )
add_library( ncbi-vdb STATIC ${all_src} )
add_compile_definitions( ${all_compile_defs} )

ExportStatic( ncbi-vdb true )
ExportShared( ncbi-vdb true "${LIBXML2};m;dl;pthread" )


###################################
GetTargetSources( "${VDB_LIB_RDWR_TARGETS}" all_src all_incl_dirs all_compile_defs )
include_directories( ${all_incl_dirs} )
add_library( ncbi-wvdb STATIC ${all_src} )
add_compile_definitions( ${all_compile_defs} )

ExportStatic( ncbi-wvdb true )
ExportShared( ncbi-wvdb true "${LIBXML2};m;dl;pthread" )

