# Macro for adding files close to the executable
macro(_copy_files_to_target target thefiles)
    if(WIN32)	    
        foreach (FFF ${thefiles} )          
            add_custom_command(
              TARGET ${target} POST_BUILD
              COMMAND ${CMAKE_COMMAND} -E copy ${FFF} $<TARGET_FILE_DIR:${target}>                
            )          
        endforeach()
    endif()
endmacro()

#####################################################################################
# BOOST
#
MACRO(_add_package_BOOST)
  SET(Boost_NO_SYSTEM_PATHS true)
  SET(Boost_USE_STATIC_LIBS ON CACHE BOOL "use static libraries from Boost")
  SET(Boost_USE_MULTITHREAD ON) #SET a flag
  SET(BOOST_LIB_LIST)
  IF(NOT WIN32)
   #  LIST(APPEND BOOST_LIB_LIST program_options log log_setup thread regex mpi serialization iostreams filesystem system)
	IF (NOT Boost_USE_STATIC_LIBS)
		MESSAGE("add boost all dynamica link definition")
		ADD_DEFINITIONS( -DBOOST_ALL_DYN_LINK )
	ENDIF()
	LIST(APPEND BOOST_LIB_LIST program_options log_setup log thread regex serialization iostreams filesystem system locale)
	FIND_PACKAGE(ICU REQUIRED COMPONENTS i18n uc)
	INCLUDE_DIRECTORIES(${ICU_INCLUDE_DIRS})
	LINK_DIRECTORIES(${ICU_LIBRARY_DIRS})
	COLLECT_DDL_FROM_FILES(ICU_LIBRARIES)
	FIND_PACKAGE(ZLIB REQUIRED)
	INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
	LINK_DIRECTORIES(${ZLIB_LIBRARY_DIRS})
	ADD_DEFINITIONS( -DBOOST_UBLAS_NDEBUG )
	COLLECT_DDL_FROM_FILES(ZLIB_LIBRARIES)
	FIND_PACKAGE(Threads REQUIRED)
  ELSE()
  #  LIST(APPEND BOOST_LIB_LIST regex mpi serialization iostreams system filesystem )
	LIST(APPEND BOOST_LIB_LIST regex serialization iostreams system filesystem )
  ENDIF()
  FIND_PACKAGE(Boost REQUIRED COMPONENTS ${BOOST_LIB_LIST})
  INCLUDE_DIRECTORIES(SYSTEM ${Boost_INCLUDE_DIRS})
  LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
  SET(BOOST_DLLS)
  IF(NOT Boost_USE_STATIC_LIBS)
	IF(WIN32)    
	  ADD_DEFINITIONS(-DBOOST_ALL_NO_LIB)
	  ADD_DEFINITIONS(-DBOOST_ALL_DYN_LINK)
	ENDIF()
	FOREACH(l ${BOOST_LIB_LIST})
	  STRING(TOUPPER ${l} v)
	  LIST(APPEND BOOST_DLLS ${Boost_${v}_LIBRARY_RELEASE})
	ENDFOREACH()
	MESSAGE( " BOOST_DLLS :-> ${BOOST_DLLS}")
	COLLECT_DDL_FROM_FILES(BOOST_DLLS)
  ENDIF()
ENDMACRO()

#####################################################################################
# Optional CUDA package
#
macro(_set_cuda_suffix)
	#------ CUDA VERSION
	if ( CUDA_VERSION ) 	  
	  if ( ${CUDA_VERSION} EQUAL "8.0" )
		SET ( CUDA_SUFFIX "cu8" )
	  endif ()
	  if ( ${CUDA_VERSION} EQUAL "9.0" )
		SET ( CUDA_SUFFIX "cu9" )
	  endif ()
	  if ( ${CUDA_VERSION} EQUAL "9.1" )
		SET ( CUDA_SUFFIX "cu9" )
	  endif ()
	  if ( ${CUDA_VERSION} EQUAL "10.0" )
		SET ( CUDA_SUFFIX "cu10" )
	  endif ()
	else()
	  message ( FATAL_ERROR "\nNVIDIA CUDA not found.\n" )
	endif()
endmacro()

macro ( _FIND_LOCALPACKAGE_CUDA ) 
  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../../../../LocalPackages/CUDA)
    Message(STATUS "Local CUDA detected. Using it")
    set(CUDA_LOCALPACK_VER "9.0" CACHE STRING "CUDA Version")
    set(CUDA_TOOLKIT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../LocalPackages/CUDA/v${CUDA_LOCALPACK_VER}_win")
  endif()
endmacro()

macro ( _FIND_LOCALPACKAGE_OPENVDB ) 
  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../../../../LocalPackages/OpenVDB)
    Message(STATUS "Local OpenVDB detected. Using it")    
	set(OPENVDB_LOCALPACK_VER "4.0.1" CACHE STRING "OpenVDB Version")
    set(OPENVDB_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../LocalPackages/OpenVDB/OpenVDB_${OPENVDB_LOCALPACK_VER}_vs2015" )	
  endif()
endmacro()

macro(_add_package_CUDA)
	Message(STATUS "\n<-- Searching for CUDA")		
	_FIND_LOCALPACKAGE_CUDA ()
	find_package(CUDA REQUIRED)
	if ( CUDA_FOUND )
		_set_cuda_suffix()
		message( STATUS "--> Using package CUDA (ver ${CUDA_VERSION})") 
		add_definitions(-DUSECUDA)    
		INCLUDE_DIRECTORIES(SYSTEM ${CUDA_INCLUDE_DIRS})
		LIST(APPEND CUDA_LIBRARIES ${CUDA_CUDA_LIBRARY} ${CUDA_CUDART_LIBRARY} ${CUDA_cudadevrt_LIBRARY})
		source_group(CUDA FILES ${CUDA_TOOLKIT_INCLUDE} ) 
	else()
		message ( FATAL_ERROR "---> Unable to find package CUDA")
	endif()
endmacro()

#####################################################################################
# Optional HDF5 package
#
macro(_add_package_HDF5)
  IF(WIN32)  
    SET (LIB_TYPE SHARED) # or SHARED  STATIC
	STRING(TOLOWER ${LIB_TYPE} SEARCH_TYPE)
	FIND_PACKAGE(HDF5 NAMES hdf5 COMPONENTS CXX ${SEARCH_TYPE} REQUIRED)  #FIND_PACKAGE(HDF5 COMPONENTS C CXX HL REQUIRED)
	INCLUDE_DIRECTORIES(SYSTEM ${HDF5_INCLUDE_DIR})
	SET(HDF5_LINK_DIR ${HDF5_ROOT}/lib)
	LIST(APPEND DefinitionFlags "H5_BUILT_AS_DYNAMIC_LIB")
	IF(${SEARCH_TYPE} STREQUAL "shared")
      SET(HDF5_LINK_RUNTIME_LIBS)
	  LIST(APPEND HDF5_LINK_RUNTIME_LIBS ${HDF5_ROOT}/bin/hdf5_cpp.dll)
	  LIST(APPEND HDF5_LINK_RUNTIME_LIBS ${HDF5_ROOT}/bin/hdf5.dll)
	  LIST(APPEND HDF5_LINK_RUNTIME_LIBS ${HDF5_ROOT}/bin/szip.dll)
	  LIST(APPEND HDF5_LINK_RUNTIME_LIBS ${HDF5_ROOT}/bin/zlib.dll)
	  COLLECT_DDL_FROM_FILES(HDF5_LINK_RUNTIME_LIBS)  
	ENDIF()
    SET(HDF5_LINK_LIBS ${HDF5_LINK_LIBS} ${HDF5_CXX_${LIB_TYPE}_LIBRARY}) 
  ELSE()
    FIND_PACKAGE(HDF5 COMPONENTS CXX REQUIRED)
	INCLUDE_DIRECTORIES(SYSTEM ${HDF5_INCLUDE_DIRS})
	LINK_DIRECTORIES(${HDF5_LIBRARY_DIRS}) 
	SET(HDF5_LINK_LIBS ${HDF5_CXX_LIBRARIES})
	COLLECT_DDL_FROM_FILES(HDF5_LINK_LIBS)
  ENDIF()
endmacro()
