summaryrefslogtreecommitdiffstats
path: root/Tests/CudaOnly/All/CMakeLists.txt
blob: ba32e9a128063499673d5962da7c477cdd0380f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
cmake_minimum_required(VERSION 3.20)
project(CudaOnlyAll CUDA)

if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA" AND
   CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0)
  set(compile_options -Wno-deprecated-gpu-targets)
endif()

function(verify_output flag)
  string(REPLACE "-" "_" architectures "${flag}")
  string(TOUPPER "${architectures}" architectures)
  set(architectures "${CMAKE_CUDA_ARCHITECTURES_${architectures}}")

  if(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
    set(match_regex "-target-cpu sm_([0-9]+)")
  elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
    set(match_regex "-arch compute_([0-9]+)")
  endif()

  string(REGEX MATCHALL "${match_regex}" target_cpus "${output}")

  foreach(cpu ${target_cpus})
    string(REGEX MATCH "${match_regex}" dont_care "${cpu}")
    list(APPEND command_archs "${CMAKE_MATCH_1}")
  endforeach()

  list(SORT command_archs)
  if(NOT "${command_archs}" STREQUAL "${architectures}")
    message(FATAL_ERROR "Architectures used for \"${flag}\" don't match the reference (\"${command_archs}\" != \"${architectures}\").")
  endif()
endfunction()

set(try_compile_flags -v ${compile_options})

set(CMAKE_CUDA_ARCHITECTURES all)
try_compile(all_archs_compiles
  ${CMAKE_CURRENT_BINARY_DIR}/try_compile/all_archs_compiles
  ${CMAKE_CURRENT_SOURCE_DIR}/main.cu
  COMPILE_DEFINITIONS ${try_compile_flags}
  OUTPUT_VARIABLE output
  )
verify_output(all)

set(CMAKE_CUDA_ARCHITECTURES all-major)
try_compile(all_major_archs_compiles
  ${CMAKE_CURRENT_BINARY_DIR}/try_compile/all_major_archs_compiles
  ${CMAKE_CURRENT_SOURCE_DIR}/main.cu
  COMPILE_DEFINITIONS ${try_compile_flags}
  OUTPUT_VARIABLE output
  )
verify_output(all-major)

if(all_archs_compiles AND all_major_archs_compiles)
  add_executable(CudaOnlyAll main.cu)
  target_compile_options(CudaOnlyAll PRIVATE ${compile_options})
endif()