Skip to content

Commit

Permalink
CUDA: add support for specifying an explicit host compiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Maynard committed Nov 14, 2016
1 parent a92f8d9 commit bbaf243
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
14 changes: 10 additions & 4 deletions Modules/CMakeCUDAInformation.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ include(CMakeCommonLanguageInclude)
# CMAKE_CUDA_COMPILE_OBJECT
# CMAKE_CUDA_LINK_EXECUTABLE

if(CMAKE_CUDA_HOST_COMPILER)
set(CMAKE_CUDA_HOST_FLAGS "-ccbin \"${CMAKE_CUDA_HOST_COMPILER}\" ")
else()
set(CMAKE_CUDA_HOST_FLAGS "")
endif()

# create a shared library
if(NOT CMAKE_CUDA_CREATE_SHARED_LIBRARY)
set(CMAKE_CUDA_CREATE_SHARED_LIBRARY
"<CMAKE_CUDA_COMPILER> <CMAKE_SHARED_LIBRARY_CUDA_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CUDA_FLAGS> <SONAME_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
"<CMAKE_CUDA_COMPILER> ${CMAKE_CUDA_HOST_FLAGS} <CMAKE_SHARED_LIBRARY_CUDA_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CUDA_FLAGS> <SONAME_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
endif()

# create a shared module copy the shared library rule by default
Expand All @@ -85,7 +91,7 @@ endif()
# compile a cu file into an object file
if(NOT CMAKE_CUDA_COMPILE_OBJECT)
set(CMAKE_CUDA_COMPILE_OBJECT
"<CMAKE_CUDA_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -x cu -c <SOURCE> -o <OBJECT>")
"<CMAKE_CUDA_COMPILER> ${CMAKE_CUDA_HOST_FLAGS} <DEFINES> <INCLUDES> <FLAGS> -x cu -c <SOURCE> -o <OBJECT>")

#The Ninja generator uses the make file dependency files to determine what
#files need to be recompiled. Unfortunately, nvcc doesn't support building
Expand All @@ -96,15 +102,15 @@ if(NOT CMAKE_CUDA_COMPILE_OBJECT)
#it is exempt from this logic.
if(CMAKE_GENERATOR STREQUAL "Ninja")
list(APPEND CMAKE_CUDA_COMPILE_OBJECT
"<CMAKE_CUDA_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -x cu -M <SOURCE> -MT <OBJECT> -o $DEP_FILE")
"<CMAKE_CUDA_COMPILER> ${CMAKE_CUDA_HOST_FLAGS} <DEFINES> <INCLUDES> <FLAGS> -x cu -M <SOURCE> -MT <OBJECT> -o $DEP_FILE")
endif()

endif()

# compile a cu file into an executable
if(NOT CMAKE_CUDA_LINK_EXECUTABLE)
set(CMAKE_CUDA_LINK_EXECUTABLE
"<CMAKE_CUDA_COMPILER> <FLAGS> <CMAKE_CUDA_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
"<CMAKE_CUDA_COMPILER> ${CMAKE_CUDA_HOST_FLAGS} <FLAGS> <CMAKE_CUDA_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
endif()


Expand Down
13 changes: 12 additions & 1 deletion Modules/CMakeDetermineCUDACompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ endif()

mark_as_advanced(CMAKE_CUDA_COMPILER)

#Allow the user to specify a host compiler
#todo: need to specify this to compiler test passes
set(CMAKE_CUDA_HOST_COMPILER "" CACHE FILEPATH "Host compiler to be used by nvcc")

# Build a small source file to identify the compiler.
if(NOT CMAKE_CUDA_COMPILER_ID_RUN)
set(CMAKE_CUDA_COMPILER_ID_RUN 1)
Expand All @@ -39,9 +43,16 @@ if(NOT CMAKE_CUDA_COMPILER_ID_RUN)
set(CMAKE_CXX_COMPILER_ID_TOOL_MATCH_REGEX "\nLd[^\n]*(\n[ \t]+[^\n]*)*\n[ \t]+([^ \t\r\n]+)[^\r\n]*-o[^\r\n]*CompilerIdCUDA/(\\./)?(CompilerIdCUDA.xctest/)?CompilerIdCUDA[ \t\n\\\"]")
set(CMAKE_CXX_COMPILER_ID_TOOL_MATCH_INDEX 2)

if(CMAKE_CUDA_HOST_COMPILER)
# Each entry in this list is a set of extra flags to try
# adding to the compile line to see if it helps produce
# a valid identification file. In our case this would just
# be the explicit host compiler
set(CMAKE_CUDA_COMPILER_ID_TEST_FLAGS_FIRST "-ccbin=${CMAKE_CUDA_HOST_COMPILER}")
endif()

include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
CMAKE_DETERMINE_COMPILER_ID(CUDA CUDAFLAGS CMakeCUDACompilerId.cu)

endif()

include(CMakeFindBinUtils)
Expand Down
3 changes: 3 additions & 0 deletions Source/cmCoreTryCompile.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ static std::string const kCMAKE_TRY_COMPILE_OSX_ARCHITECTURES =
"CMAKE_TRY_COMPILE_OSX_ARCHITECTURES";
static std::string const kCMAKE_TRY_COMPILE_PLATFORM_VARIABLES =
"CMAKE_TRY_COMPILE_PLATFORM_VARIABLES";
static std::string const kCMAKE_CUDA_HOST_COMPILER =
"CMAKE_CUDA_HOST_COMPILER";

int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
bool isTryRun)
Expand Down Expand Up @@ -453,6 +455,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
vars.insert(kCMAKE_OSX_SYSROOT);
vars.insert(kCMAKE_POSITION_INDEPENDENT_CODE);
vars.insert(kCMAKE_SYSROOT);
vars.insert(kCMAKE_CUDA_HOST_COMPILER);

if (const char* varListStr = this->Makefile->GetDefinition(
kCMAKE_TRY_COMPILE_PLATFORM_VARIABLES)) {
Expand Down

0 comments on commit bbaf243

Please sign in to comment.