CMakeLists.txt 3.3 KB

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