CUDA: Factor out helper to validate CMAKE_CUDA_ARCHITECTURES

Prepare to use it for other languages.
This commit is contained in:
Brad King
2023-09-18 13:38:25 -04:00
parent 8617c28221
commit b3a1f17567
4 changed files with 28 additions and 19 deletions

View File

@@ -111,23 +111,9 @@ if(NOT CMAKE_CUDA_COMPILER_ID_RUN)
endif()
endif()
# Rest of the code treats an empty value as equivalent to "use the defaults".
# Error out early to prevent confusing errors as a result of this.
# Note that this also catches invalid non-numerical values such as "a".
if(DEFINED CMAKE_CUDA_ARCHITECTURES)
if(CMAKE_CUDA_ARCHITECTURES STREQUAL "")
message(FATAL_ERROR "CMAKE_CUDA_ARCHITECTURES must be non-empty if set.")
elseif(CMAKE_CUDA_ARCHITECTURES AND NOT CMAKE_CUDA_ARCHITECTURES MATCHES "^([0-9]+a?(-real|-virtual)?(;[0-9]+a?(-real|-virtual)?|;)*|all|all-major|native)$")
message(FATAL_ERROR
"CMAKE_CUDA_ARCHITECTURES:\n"
" ${CMAKE_CUDA_ARCHITECTURES}\n"
"is not one of the following:\n"
" * a semicolon-separated list of integers, each optionally\n"
" followed by '-real' or '-virtual'\n"
" * a special value: all, all-major, native\n"
)
endif()
endif()
# If the user set CMAKE_CUDA_ARCHITECTURES, validate its value.
include(Internal/CMakeCUDAArchitecturesValidate)
cmake_cuda_architectures_validate(CUDA)
if(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang")
# Clang doesn't automatically select an architecture supported by the SDK.

View File

@@ -0,0 +1,19 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
function(cmake_cuda_architectures_validate lang)
if(DEFINED CMAKE_${lang}_ARCHITECTURES)
if(CMAKE_${lang}_ARCHITECTURES STREQUAL "")
message(FATAL_ERROR "CMAKE_${lang}_ARCHITECTURES must be non-empty if set.")
elseif(CMAKE_${lang}_ARCHITECTURES AND NOT CMAKE_${lang}_ARCHITECTURES MATCHES "^([0-9]+a?(-real|-virtual)?(;[0-9]+a?(-real|-virtual)?|;)*|all|all-major|native)$")
message(FATAL_ERROR
"CMAKE_${lang}_ARCHITECTURES:\n"
" ${CMAKE_${lang}_ARCHITECTURES}\n"
"is not one of the following:\n"
" * a semicolon-separated list of integers, each optionally\n"
" followed by '-real' or '-virtual'\n"
" * a special value: all, all-major, native\n"
)
endif()
endif()
endfunction()

View File

@@ -1,5 +1,7 @@
^CMake Error at .*/Modules/CMakeDetermineCUDACompiler\.cmake:[0-9]+ \(message\):
^CMake Error at .*/Internal/CMakeCUDAArchitecturesValidate\.cmake:[0-9]+ \(message\):
CMAKE_CUDA_ARCHITECTURES must be non-empty if set\.
Call Stack \(most recent call first\):
[^
]*/Modules/CMakeDetermineCUDACompiler.cmake:[0-9]+ \(cmake_cuda_architectures_validate\)
architectures-empty\.cmake:2 \(enable_language\)
CMakeLists\.txt:3 \(include\)

View File

@@ -1,4 +1,4 @@
^CMake Error at .*/Modules/CMakeDetermineCUDACompiler\.cmake:[0-9]+ \(message\):
^CMake Error at .*/Internal/CMakeCUDAArchitecturesValidate\.cmake:[0-9]+ \(message\):
CMAKE_CUDA_ARCHITECTURES:
invalid
@@ -10,5 +10,7 @@
\* a special value: all, all-major, native
Call Stack \(most recent call first\):
[^
]*/Modules/CMakeDetermineCUDACompiler.cmake:[0-9]+ \(cmake_cuda_architectures_validate\)
architectures-invalid\.cmake:2 \(enable_language\)
CMakeLists\.txt:3 \(include\)$