function(mnn_status text)
  set(status_cond)
  set(status_then)
  set(status_else)

  set(status_current_name "cond")
  foreach(arg ${ARGN})
    if(arg STREQUAL "THEN")
      set(status_current_name "then")
    elseif(arg STREQUAL "ELSE")
      set(status_current_name "else")
    else()
      list(APPEND status_${status_current_name} ${arg})
    endif()
  endforeach()

  if(DEFINED status_cond)
    set(status_placeholder_length 23)
    string(RANDOM LENGTH ${status_placeholder_length} ALPHABET " " status_placeholder)
    string(LENGTH "${text}" status_text_length)
    if(status_text_length LESS status_placeholder_length)
      string(SUBSTRING "${text}${status_placeholder}" 0 ${status_placeholder_length} status_text)
    elseif(DEFINED status_then OR DEFINED status_else)
      message(STATUS "${text}")
      set(status_text "${status_placeholder}")
    else()
      set(status_text "${text}")
    endif()

    if(DEFINED status_then OR DEFINED status_else)
      if(${status_cond})
        string(REPLACE ";" " " status_then "${status_then}")
        string(REGEX REPLACE "^[ \t]+" "" status_then "${status_then}")
        message(STATUS "${status_text} ${status_then}")
      else()
        string(REPLACE ";" " " status_else "${status_else}")
        string(REGEX REPLACE "^[ \t]+" "" status_else "${status_else}")
        message(STATUS "${status_text} ${status_else}")
      endif()
    else()
      string(REPLACE ";" " " status_cond "${status_cond}")
      string(REGEX REPLACE "^[ \t]+" "" status_cond "${status_cond}")
      message(STATUS "${status_text} ${status_cond}")
    endif()
  else()
    message(STATUS "${text}")
  endif()
endfunction()

# Function merging lists of compiler flags to single string.
# Usage:
#   mnn_merge_flag_lists(out_variable <list1> [<list2>] [<list3>] ...)
function(mnn_merge_flag_lists out_var)
  set(__result "")
  foreach(__list ${ARGN})
    foreach(__flag ${${__list}})
      string(STRIP ${__flag} __flag)
      set(__result "${__result} ${__flag}")
    endforeach()
  endforeach()
  string(STRIP ${__result} __result)
  set(${out_var} ${__result} PARENT_SCOPE)
endfunction()

# Prints accumulated mnn configuration summary
function(mnn_print_configuration_summary)
    mnn_merge_flag_lists(__flags_rel CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS)
    mnn_merge_flag_lists(__flags_deb CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS)

    mnn_status("")
    mnn_status("******************* MNN Summary *******************")
    mnn_status("General:")
    mnn_status("  Version           :   ${PROJECT_VERSION}")
    mnn_status("  System            :   ${CMAKE_SYSTEM_NAME}")
    mnn_status("  C++ compiler      :   ${CMAKE_CXX_COMPILER}")
    mnn_status("  Release CXX flags :   ${__flags_rel}")
    mnn_status("  Debug CXX flags   :   ${__flags_deb}")
    mnn_status("  Build type        :   ${CMAKE_BUILD_TYPE}")
    mnn_status("")
    mnn_status("  BUILD_EXAMPLE    :    ${BUILD_EXAMPLE}")
    mnn_status("  BUILD_TEST       :    ${BUILD_TEST}")
    mnn_status("")
    mnn_status("Multithread Backend:")
    mnn_status("  Pthread           : " USE_PTHREAD THEN "Yes" ELSE "No")
    mnn_status("  TBB               : " USE_TBB AND TBB_FOUND THEN "Yes (ver. ${TBB_INTERFACE_VERSION})" ELSE "No")
    mnn_status("  OMP               : " USE_OMP AND OMP_FOUND THEN "Yes" ELSE "No")
    mnn_status("")
endfunction()
