Make CUDA working directory unique for each target.

This allows you to have more than source file with the same name but different
directories.  The intermediate and configuration files are now in this same directory.
This commit is contained in:
James Bigler 2011-12-05 19:00:00 -07:00
parent e0bc42aa4f
commit 80e279d37c
1 changed files with 54 additions and 10 deletions

View File

@ -808,6 +808,43 @@ function(CUDA_BUILD_SHARED_LIBRARY shared_flag)
set(${shared_flag} ${_cuda_build_shared_libs} PARENT_SCOPE) set(${shared_flag} ${_cuda_build_shared_libs} PARENT_SCOPE)
endfunction() endfunction()
##############################################################################
# Helper to avoid clashes of files with the same basename but different paths.
# This doesn't attempt to do exactly what CMake internals do, which is to only
# add this path when there is a conflict, since by the time a second collision
# in names is detected it's already too late to fix the first one. For
# consistency sake the relative path will be added to all files.
function(CUDA_COMPUTE_BUILD_PATH path build_path)
#message("CUDA_COMPUTE_BUILD_PATH([${path}] ${build_path})")
# Only deal with CMake style paths from here on out
file(TO_CMAKE_PATH "${path}" bpath)
if (IS_ABSOLUTE "${bpath}")
# Absolute paths are generally unnessary, especially if something like
# FILE(GLOB_RECURSE) is used to pick up the files.
file(RELATIVE_PATH bpath "${CMAKE_CURRENT_SOURCE_DIR}" "${bpath}")
endif()
# This recipie is from cmLocalGenerator::CreateSafeUniqueObjectFileName in the
# CMake source.
# Remove leading /
string(REGEX REPLACE "^[/]+" "" bpath "${bpath}")
# Avoid absolute paths by removing ':'
string(REPLACE ":" "_" bpath "${bpath}")
# Avoid relative paths that go up the tree
string(REPLACE "../" "__/" bpath "${bpath}")
# Avoid spaces
string(REPLACE " " "_" bpath "${bpath}")
# Strip off the filename. I wait until here to do it, since removin the
# basename can make a path that looked like path/../basename turn into
# path/.. (notice the trailing slash).
get_filename_component(bpath "${bpath}" PATH)
set(${build_path} "${bpath}" PARENT_SCOPE)
#message("${build_path} = ${bpath}")
endfunction()
############################################################################## ##############################################################################
# This helper macro populates the following variables and setups up custom # This helper macro populates the following variables and setups up custom
# commands and targets to invoke the nvcc compiler to generate C or PTX source # commands and targets to invoke the nvcc compiler to generate C or PTX source
@ -977,13 +1014,6 @@ macro(CUDA_WRAP_SRCS cuda_target format generated_files)
list(APPEND nvcc_flags "-D${cuda_target}_EXPORTS") list(APPEND nvcc_flags "-D${cuda_target}_EXPORTS")
endif() endif()
# Determine output directory
if(CUDA_GENERATED_OUTPUT_DIR)
set(cuda_compile_output_dir "${CUDA_GENERATED_OUTPUT_DIR}")
else()
set(cuda_compile_output_dir "${CMAKE_CURRENT_BINARY_DIR}")
endif()
# Reset the output variable # Reset the output variable
set(_cuda_wrap_generated_files "") set(_cuda_wrap_generated_files "")
@ -994,6 +1024,19 @@ macro(CUDA_WRAP_SRCS cuda_target format generated_files)
get_source_file_property(_is_header ${file} HEADER_FILE_ONLY) get_source_file_property(_is_header ${file} HEADER_FILE_ONLY)
if(${file} MATCHES ".*\\.cu$" AND NOT _is_header) if(${file} MATCHES ".*\\.cu$" AND NOT _is_header)
# Determine output directory
cuda_compute_build_path("${file}" cuda_build_path)
set(cuda_compile_intermediate_directory "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${cuda_target}.dir/${cuda_build_path}")
if(CUDA_GENERATED_OUTPUT_DIR)
set(cuda_compile_output_dir "${CUDA_GENERATED_OUTPUT_DIR}")
else()
if ( compile_to_ptx )
set(cuda_compile_output_dir "${CMAKE_CURRENT_BINARY_DIR}")
else()
set(cuda_compile_output_dir "${cuda_compile_intermediate_directory}")
endif()
endif()
# Add a custom target to generate a c or ptx file. ###################### # Add a custom target to generate a c or ptx file. ######################
get_filename_component( basename ${file} NAME ) get_filename_component( basename ${file} NAME )
@ -1013,10 +1056,10 @@ macro(CUDA_WRAP_SRCS cuda_target format generated_files)
# argument, so that the ${CMAKE_CFG_INTDIR} gets expanded at run time # argument, so that the ${CMAKE_CFG_INTDIR} gets expanded at run time
# instead of configure time. # instead of configure time.
set(generated_file "${generated_file_path}/${generated_file_basename}") set(generated_file "${generated_file_path}/${generated_file_basename}")
set(cmake_dependency_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${generated_file_basename}.depend") set(cmake_dependency_file "${cuda_compile_intermediate_directory}/${generated_file_basename}.depend")
set(NVCC_generated_dependency_file "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${generated_file_basename}.NVCC-depend") set(NVCC_generated_dependency_file "${cuda_compile_intermediate_directory}/${generated_file_basename}.NVCC-depend")
set(generated_cubin_file "${generated_file_path}/${generated_file_basename}.cubin.txt") set(generated_cubin_file "${generated_file_path}/${generated_file_basename}.cubin.txt")
set(custom_target_script "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${generated_file_basename}.cmake") set(custom_target_script "${cuda_compile_intermediate_directory}/${generated_file_basename}.cmake")
# Setup properties for obj files: # Setup properties for obj files:
if( NOT compile_to_ptx ) if( NOT compile_to_ptx )
@ -1096,6 +1139,7 @@ macro(CUDA_WRAP_SRCS cuda_target format generated_files)
-D "generated_file:STRING=${generated_file}" -D "generated_file:STRING=${generated_file}"
-D "generated_cubin_file:STRING=${generated_cubin_file}" -D "generated_cubin_file:STRING=${generated_cubin_file}"
-P "${custom_target_script}" -P "${custom_target_script}"
WORKING_DIRECTORY "${cuda_compile_intermediate_directory}"
COMMENT "${cuda_build_comment_string}" COMMENT "${cuda_build_comment_string}"
) )