Skip to content

Commit cf0adb2

Browse files
authored
Fix build on Windows with CUDA (#1787)
This commit fixes the local build on Windows with CUDA.
1 parent 8d83a2f commit cf0adb2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

CMakeLists.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,18 @@ if(env_cxx_standard GREATER -1)
2626
WARNING "C++ standard version definition detected in environment variable."
2727
"PyTorch requires -std=c++14. Please remove -std=c++ settings in your environment.")
2828
endif()
29+
2930
set(CMAKE_CXX_STANDARD 14)
3031
set(CMAKE_C_STANDARD 11)
3132

33+
# https://developercommunity.visualstudio.com/t/VS-16100-isnt-compatible-with-CUDA-11/1433342
34+
if(MSVC)
35+
if(USE_CUDA)
36+
set(CMAKE_CXX_STANDARD 17)
37+
endif()
38+
endif()
39+
40+
3241
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
3342
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
3443

@@ -74,6 +83,45 @@ endif()
7483

7584
find_package(Torch REQUIRED)
7685

86+
# https://github.com/pytorch/pytorch/issues/54174
87+
function(CUDA_CONVERT_FLAGS EXISTING_TARGET)
88+
get_property(old_flags TARGET ${EXISTING_TARGET} PROPERTY INTERFACE_COMPILE_OPTIONS)
89+
if(NOT "${old_flags}" STREQUAL "")
90+
string(REPLACE ";" "," CUDA_flags "${old_flags}")
91+
set_property(TARGET ${EXISTING_TARGET} PROPERTY INTERFACE_COMPILE_OPTIONS
92+
"$<$<BUILD_INTERFACE:$<COMPILE_LANGUAGE:CXX>>:${old_flags}>$<$<BUILD_INTERFACE:$<COMPILE_LANGUAGE:CUDA>>:-Xcompiler=${CUDA_flags}>"
93+
)
94+
endif()
95+
endfunction()
96+
97+
if(MSVC)
98+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4819")
99+
if(USE_CUDA)
100+
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler=/wd4819")
101+
foreach(diag cc_clobber_ignored integer_sign_change useless_using_declaration
102+
set_but_not_used field_without_dll_interface
103+
base_class_has_different_dll_interface
104+
dll_interface_conflict_none_assumed
105+
dll_interface_conflict_dllexport_assumed
106+
implicit_return_from_non_void_function
107+
unsigned_compare_with_zero
108+
declared_but_not_referenced
109+
bad_friend_decl)
110+
string(APPEND CMAKE_CUDA_FLAGS " -Xcudafe --diag_suppress=${diag}")
111+
endforeach()
112+
CUDA_CONVERT_FLAGS(torch_cpu)
113+
if(TARGET torch_cuda)
114+
CUDA_CONVERT_FLAGS(torch_cuda)
115+
endif()
116+
if(TARGET torch_cuda_cu)
117+
CUDA_CONVERT_FLAGS(torch_cuda_cu)
118+
endif()
119+
if(TARGET torch_cuda_cpp)
120+
CUDA_CONVERT_FLAGS(torch_cuda_cpp)
121+
endif()
122+
endif()
123+
endif()
124+
77125
# TORCH_CXX_FLAGS contains the same -D_GLIBCXX_USE_CXX11_ABI value as PyTorch
78126
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall ${TORCH_CXX_FLAGS}")
79127

0 commit comments

Comments
 (0)