1 # We can find some usecases which follow the guide of CMake which uses
2 # `enable_language(CUDA)` instead of `find_package(CUDA)` and let the CMake
3 # built-in functions use NVCC.
5 # See: https://cmake.org/cmake/help/latest/module/FindCUDA.html#replacement
7 # However, this requires CMake version 3.10 or higher and we can't be sure most
8 # of the CUDA projects are using those.
10 # This test relies on `find_package(CUDA)` in the parent CMake config.
12 # These can be updated when NVCC becomes ready for C++ 17 features
13 # https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#cpp14-language-features
14 set(CMAKE_CUDA_STANDARD 14)
15 set(CMAKE_CUDA_STANDARD_REQUIRED 14)
17 # In this test, we assume that the user is going to compile CUDA source code
18 # with some libraries (fmt in this case).
20 # In addition to that, this test invokes both the C++ host compiler and NVCC
21 # by providing another (non-CUDA) C++ source code.
22 if (${CMAKE_VERSION} VERSION_LESS 3.15)
23 # https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html
24 list(APPEND CUDA_NVCC_FLAGS "-std=c++14")
26 # This is the solution of pytorch:
27 # https://github.com/pytorch/pytorch/pull/7118
28 list(APPEND CUDA_NVCC_FLAGS "-Xcompiler" "/std:c++14")
29 list(APPEND CUDA_NVCC_FLAGS "-Xcompiler" "/Zc:__cplusplus")
30 # for the reason of this -Xcompiler options, see below.
32 cuda_add_executable(fmt-in-cuda-test cuda-cpp14.cu cpp14.cc)
33 target_compile_features(fmt-in-cuda-test PRIVATE cxx_std_14)
35 # This part is for (non-CUDA) C++ code. MSVC can define incorrect
36 # `__cplusplus` macro. Fix for the issue is to use additional compiler flag.
39 # https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
40 # https://github.com/Microsoft/vscode-cpptools/issues/2595
41 target_compile_options(fmt-in-cuda-test PRIVATE /Zc:__cplusplus /permissive-)
44 # now using a "new" way of handling CUDA
45 add_executable(fmt-in-cuda-test cuda-cpp14.cu cpp14.cc)
46 set_target_properties(fmt-in-cuda-test PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
47 target_compile_features(fmt-in-cuda-test PRIVATE cxx_std_14)
49 # with MSVC, 'cxx_std_14' will only propagate to the host code (MSVC), but will
50 # not set __cplusplus correctly anyway, while nvcc will ignore it.
51 # If specified for nvcc on the command line as '-std=c++14' nvcc will emit this
53 # nvcc warning : The -std=c++14 flag is not supported with the configured host
54 # compiler. Flag will be ignored.
55 set_property(SOURCE cuda-cpp14.cu APPEND PROPERTY
56 COMPILE_OPTIONS -Xcompiler /std:c++14 -Xcompiler /Zc:__cplusplus)
57 set_property(SOURCE cpp14.cc APPEND PROPERTY
58 COMPILE_OPTIONS /std:c++14 /Zc:__cplusplus)
62 get_target_property(IN_USE_CUDA_STANDARD fmt-in-cuda-test CUDA_STANDARD)
63 message(STATUS "cuda_standard: ${IN_USE_CUDA_STANDARD}")
65 get_target_property(IN_USE_CUDA_STANDARD_REQUIRED
66 fmt-in-cuda-test CUDA_STANDARD_REQUIRED)
67 message(STATUS "cuda_standard_required: ${IN_USE_CUDA_STANDARD_REQUIRED}")
69 # We don't use PUBLIC or other keyword for reasons explained in the
70 # CUDA_LINK_LIBRARIES_KEYWORD section in
71 # https://cmake.org/cmake/help/latest/module/FindCUDA.html
72 target_link_libraries(fmt-in-cuda-test fmt::fmt)