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