function(mitkFunctionGetLibrarySearchPaths search_path intermediate_dir)

  set(_dir_candidates ${MITK_VTK_LIBRARY_DIRS} ${MITK_ITK_LIBRARY_DIRS} ${QT_LIBRARY_DIR}
                      ${QT_LIBRARY_DIR}/../bin ${MITK_BINARY_DIR}/bin ${MITK_BINARY_DIR}/bin/plugins)

  get_property(_additional_paths GLOBAL PROPERTY MITK_ADDITIONAL_LIBRARY_SEARCH_PATHS)
  if(_additional_paths)
    list(APPEND _dir_candidates ${_additional_paths})
  endif()

  if(WIN32)
    if(DCMTK_DIR)
      list(APPEND _dir_candidates "${DCMTK_DIR}/bin")
    endif()
   list(APPEND _dir_candidates "${ITK_DIR}/bin")
  else()
    if(DCMTK_DIR)
      list(APPEND _dir_candidates "${DCMTK_DIR}/lib")
    endif()
    list(APPEND _dir_candidates "${ITK_DIR}/lib")
  endif()

  if(MITK_USE_Python AND CTK_PYTHONQT_INSTALL_DIR)
    list(APPEND _dir_candidates ${CTK_PYTHONQT_INSTALL_DIR}/bin)
  endif()
  if(MITK_USE_Boost AND MITK_USE_Boost_LIBRARIES AND NOT MITK_USE_SYSTEM_Boost)
    list(APPEND _dir_candidates ${Boost_LIBRARY_DIR})
  endif()
  if(ACVD_DIR)
    list(APPEND _dir_candidates ${ACVD_DIR}/bin)
  endif()
  if(GDCM_DIR)
    list(APPEND _dir_candidates ${GDCM_DIR}/bin)
  endif()
  if(OpenCV_DIR)
    list(APPEND _dir_candidates ${OpenCV_DIR}/bin)
  endif()
  if(SOFA_DIR)
    list(APPEND _dir_candidates ${SOFA_DIR}/bin)
  endif()
  if(MITK_USE_TOF_PMDO3 OR MITK_USE_TOF_PMDCAMCUBE OR MITK_USE_TOF_PMDCAMBOARD)
    list(APPEND _dir_candidates ${MITK_PMD_SDK_DIR}/plugins)
  endif()

  if(MITK_USE_BLUEBERRY)
    list(APPEND _dir_candidates ${CTK_RUNTIME_LIBRARY_DIRS})
    if(DEFINED CTK_PLUGIN_RUNTIME_OUTPUT_DIRECTORY)
      if(IS_ABSOLUTE "${CTK_PLUGIN_RUNTIME_OUTPUT_DIRECTORY}")
        list(APPEND _dir_candidates "${CTK_PLUGIN_RUNTIME_OUTPUT_DIRECTORY}")
      else()
        list(APPEND _dir_candidates "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CTK_PLUGIN_RUNTIME_OUTPUT_DIRECTORY}")
      endif()
    endif()
  endif()

  if(MITK_LIBRARY_DIRS)
    list(APPEND _dir_candidates ${MITK_LIBRARY_DIRS})
  endif()

  list(REMOVE_DUPLICATES _dir_candidates)

  set(_search_dirs )
  foreach(_dir ${_dir_candidates})
    if(EXISTS "${_dir}/${intermediate_dir}")
      list(APPEND _search_dirs "${_dir}/${intermediate_dir}")
    else()
      list(APPEND _search_dirs ${_dir})
    endif()
  endforeach()

  # Special handling for "internal" search dirs. The intermediate directory
  # might not have been created yet, so we can't check for its existence.
  # Hence we just add it for Windows without checking.
  set(_internal_search_dirs ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/plugins)
  if(WIN32)
    foreach(_dir ${_internal_search_dirs})
      set(_search_dirs ${_dir}/${intermediate_dir} ${_search_dirs})
    endforeach()
  else()
    set(_search_dirs ${_internal_search_dirs} ${_search_dirs})
  endif()
  list(REMOVE_DUPLICATES _search_dirs)

  set(${search_path} ${_search_dirs} PARENT_SCOPE)
endfunction()
