cmake_minimum_required(VERSION 3.1)

function(get_ned_folders _target _output)
    set(_folders "")
    get_target_property(_target_type ${_target} TYPE)
    if(NOT ${_target_type} STREQUAL "INTERFACE_LIBRARY")
        get_target_property(_target_ned_folders ${_target} NED_FOLDERS)
        if(_target_ned_folders)
            list(APPEND _folders ${_target_ned_folders})
        endif()
    endif()

    get_target_property(_target_dependencies ${_target} INTERFACE_LINK_LIBRARIES)
    if(_target_dependencies)
        foreach(_target_dependency IN LISTS _target_dependencies)
            if(TARGET ${_target_dependency})
                get_ned_folders(${_target_dependency} _folders_dependency)
                list(APPEND _folders ${_folders_dependency})
            endif()
        endforeach()
    endif()

    list(REMOVE_DUPLICATES _folders)
    set(${_output} ${_folders} PARENT_SCOPE)
endfunction()
