macro(WRAP_SOURCELIST)
    foreach (_file ${ARGN})
        get_filename_component(_basename ${_file} NAME_WE)
        get_filename_component(_path ${_file} PATH)

        if (NOT _file MATCHES "${CMAKE_CURRENT_BINARY_DIR}/GeneratedFiles")
            string(REGEX REPLACE "/" "\\\\\\\\" _sourcegroup "${_path}")
            source_group("${_sourcegroup}" FILES ${_file})
        endif ()
    endforeach ()

    source_group("GeneratedFiles" REGULAR_EXPRESSION "${CMAKE_CURRENT_BINARY_DIR}/GeneratedFiles")
endmacro()

macro(ADD_PREFIX_HEADER _target _header)
    get_target_property(_old_compile_flags ${_target} COMPILE_FLAGS)
    if (${_old_compile_flags} STREQUAL "_old_compile_flags-NOTFOUND")
        set(_old_compile_flags "")
    endif ()
    if (MSVC)
        set_target_properties(${_target} PROPERTIES COMPILE_FLAGS "/FI\"${_header}\" ${_old_compile_flags}")
    else ()
        set_target_properties(${_target} PROPERTIES COMPILE_FLAGS "-include ${_header} ${_old_compile_flags}")
    endif ()
endmacro()

macro(ADD_POST_BUILD_COMMAND _target)
    if (CMAKE_HOST_WIN32)
        set(${_target}_POST_BUILD_COMMAND "${CMAKE_BINARY_DIR}/${_target}/postBuild.cmd")
        file(WRITE "${${_target}_POST_BUILD_COMMAND}" "@echo Running ${_target} post build tasks...\n")
        add_custom_command(TARGET ${_target} POST_BUILD COMMAND ${${_target}_POST_BUILD_COMMAND} VERBATIM)
    else ()
        add_custom_command(TARGET ${_target} POST_BUILD COMMAND echo Running ${_target} post build tasks... VERBATIM)
    endif ()
endmacro()

macro(COPY_LIBRARY_HEADERS _target _list _destination)
    if (CMAKE_HOST_WIN32)
        file(APPEND "${${_target}_POST_BUILD_COMMAND}" "@mkdir \"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/../${_destination}\" >nul 2>nul\n")
        foreach (_file ${_list})
            get_filename_component(_absolute "${_file}" ABSOLUTE)
            file(APPEND "${${_target}_POST_BUILD_COMMAND}" "@xcopy /y /d /f \"${_absolute}\" \"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/../${_destination}\" >nul 2>nul\n")
        endforeach ()
    else ()
        add_custom_command(TARGET ${_target} POST_BUILD COMMAND echo Copying ${_target} library headers... VERBATIM)
        foreach (_file ${_list})
            get_filename_component(_absolute "${_file}" ABSOLUTE)
            add_custom_command(TARGET ${_target} POST_BUILD COMMAND mkdir -p ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/../${_destination} && cp -u ${_absolute} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/../${_destination} VERBATIM)
        endforeach ()
    endif ()
endmacro()

macro(COPY_LIBRARY_HEADERS_DIRECTORY _target _source _destination)
    get_filename_component(_absolute "${_source}" ABSOLUTE)
    if (CMAKE_HOST_WIN32)
        file(APPEND "${${_target}_POST_BUILD_COMMAND}" "@robocopy /s /e \"${_absolute}/\" \"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/../${_destination}/\" \"*.h\" \"*.hpp\" >nul 2>nul\n")
        file(APPEND "${${_target}_POST_BUILD_COMMAND}" "@IF %ERRORLEVEL% LEQ 3 set ERRORLEVEL=0\n")
    else ()
        add_custom_command(TARGET ${_target} POST_BUILD COMMAND echo Copying ${_target} library headers... VERBATIM)
        add_custom_command(TARGET ${_target} POST_BUILD COMMAND mkdir -p ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/../${_destination} && rsync -rpuL --include='*.h' --include='*.hpp' --exclude='*.*' ${_absolute}/* ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/../${_destination})
    endif ()
endmacro()

macro(IMPORT_LIBRARIES _target _list)
    get_target_property(_target_dir ${_target} SOURCE_DIR)
    string(REPLACE "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" _target_dir ${_target_dir})
    set(_target_object_dir ${_target_dir}/${_target}.dir/$<CONFIG>)
    foreach (_library ${_list})
        set(_library_name ${CMAKE_SHARED_LIBRARY_PREFIX}${_library}${CMAKE_SHARED_LIBRARY_SUFFIX})
        set(_library_path ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${_library_name})
        add_custom_command(TARGET ${_target} PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${_library_path} ${_target_object_dir}/libs/${ANDROID_NDK_ABI_NAME}/${_library_name})
    endforeach ()
endmacro()
