[CUDA][MPI][CMAKE] Compiling .cu with Mpi, Gcc and Cmake

I was working on a project where MPI and OpenMP were used and where everything about compilation was done.
And I had to include some Cuda code to this.

There are plenty of ways to do that, but in my case I wanted to change a few things in the compilation steps.
So one way is to use nvcc to link everything (and add the MPI dependencies manually) the other way is to use mpi wrapper mpicxx (and add the Cuda dependencies manually),
etc., etc…

In my case, I compile the Cuda file with nvcc manually and link them to my library as object files.

Here is how it work:

# CUDA dependency & support
OPTION( MYPROJECT_ENABLE_CUDA 	"Set to ON to compile with CUDA support" 	OFF )
MESSAGE( STATUS "MYPROJECT_ENABLE_CUDA          = ${MYPROJECT_ENABLE_CUDA}" )
if(MYPROJECT_ENABLE_CUDA)
	# We try nvcc in the terminal
	execute_process(COMMAND nvcc --version ERROR_VARIABLE cuda_error_output OUTPUT_QUIET)
	if(cuda_error_output)
		message( FATAL_ERROR "nvcc is needed with CUDA." )
	endif()
	# If the flags were not already defined
	if(NOT DEFINED CUSTOM_CUDA_FLAGS)
		SET( CUSTOM_CUDA_FLAGS "-arch=sm_20" CACHE STRING "Set your CUDA flags, for example : -arch=sm_20;-ptxas-options=-v;-use_fast_math")
	endif()
	# This is needed to remove backslash after space in ADD_CUSTOM_COMMAND
	SEPARATE_ARGUMENTS(CUSTOM_CUDA_FLAGS)
	MESSAGE( STATUS "CUSTOM_CUDA_FLAGS             = ${CUSTOM_CUDA_FLAGS}" )

	# Add libcudart and cuda.h
	link_directories($ENV{CUDA_LIB})
	include_directories($ENV{CUDA_INC})
	list(APPEND ${PROJECT_NAME}_LIBRARIES "cudart")

	# Find all the CU files in my project
	file(GLOB_RECURSE source_cu_files ${PROJECT_SOURCE_DIR}/MYPROJECT/Src/*.cu)

	# Iterate and add builind command for each file
	set( MYPROJECT_CUDA_SOURCES "" )
	FOREACH (_file ${source_cu_files})

		GET_FILENAME_COMPONENT (_filewe ${_file} NAME_WE)
		SET (_filehpp_output ${CMAKE_CURRENT_BINARY_DIR}/${_filewe}.o)

		ADD_CUSTOM_COMMAND (OUTPUT  ${_filehpp_output}
			      DEPENDS ${_file}
			      COMMAND echo ARGS Compiling ${_filewe}
			      COMMAND nvcc ARGS -c ${_file} -o ${_filehpp_output} ${CUSTOM_CUDA_FLAGS})

		LIST (APPEND MYPROJECT_CUDA_SOURCES ${_filehpp_output})
	ENDFOREACH ()

	MESSAGE( STATUS "MYPROJECT_CUDA_SOURCES          = ${MYPROJECT_CUDA_SOURCES}" )
endif()

# Later ....

# Add the dependency to my lib in order to link statically
add_library(myproject ${LIBRARY_TYPE} ${MYPROJECT_SOURCES} ${MYPROJECT_CUDA_SOURCES})

Please, notice that I have some environment variable in order to add the link and include directories to find libcudart and cuda.h,
I have something like:

export CUDA_HOME=/usr/local/cuda-5.5
export LD_LIBRARY_PATH=${CUDA_HOME}/lib64:${LD_LIBRARY_PATH}
export PATH=${CUDA_HOME}/bin:${PATH}
export CUDA_LIB=${CUDA_HOME}/lib64
export CUDA_INC=${CUDA_HOME}/include