From 8f01fe7bf139b019eaff5594b558b17862cfab37 Mon Sep 17 00:00:00 2001 From: Raul Tambre Date: Wed, 10 Jun 2020 20:08:17 +0300 Subject: FindCUDAToolkit: Use list(SORT) to sort in natural order We had a custom loop to sort in the natural order. Since CMake 3.18 this is supported natively by CMake and simplifies the code significantly. --- Modules/CMakeDetermineCUDACompiler.cmake | 40 ++++---------------------------- Modules/FindCUDAToolkit.cmake | 40 ++++---------------------------- 2 files changed, 8 insertions(+), 72 deletions(-) diff --git a/Modules/CMakeDetermineCUDACompiler.cmake b/Modules/CMakeDetermineCUDACompiler.cmake index f31e2e8..2c1a40e 100644 --- a/Modules/CMakeDetermineCUDACompiler.cmake +++ b/Modules/CMakeDetermineCUDACompiler.cmake @@ -210,46 +210,17 @@ elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang") # Build out a descending list of possible cuda installations, e.g. file(GLOB possible_paths "${platform_base}*") # Iterate the glob results and create a descending list. - set(possible_versions) + set(versions) foreach(p ${possible_paths}) # Extract version number from end of string string(REGEX MATCH "[0-9][0-9]?\\.[0-9]$" p_version ${p}) if(IS_DIRECTORY ${p} AND p_version) - list(APPEND possible_versions ${p_version}) + list(APPEND versions ${p_version}) endif() endforeach() - # Cannot use list(SORT) because that is alphabetical, we need numerical. - # NOTE: this is not an efficient sorting strategy. But even if a user had - # every possible version of CUDA installed, this wouldn't create any - # significant overhead. - set(versions) - foreach (v ${possible_versions}) - list(LENGTH versions num_versions) - # First version, nothing to compare with so just append. - if(num_versions EQUAL 0) - list(APPEND versions ${v}) - else() - # Loop through list. Insert at an index when comparison is - # VERSION_GREATER since we want a descending list. Duplicates will not - # happen since this came from a glob list of directories. - set(i 0) - set(early_terminate FALSE) - while (i LESS num_versions) - list(GET versions ${i} curr) - if(v VERSION_GREATER curr) - list(INSERT versions ${i} ${v}) - set(early_terminate TRUE) - break() - endif() - math(EXPR i "${i} + 1") - endwhile() - # If it did not get inserted, place it at the end. - if(NOT early_terminate) - list(APPEND versions ${v}) - endif() - endif() - endforeach() + # Sort numerically in descending order, so we try the newest versions first. + list(SORT versions COMPARE NATURAL ORDER DESCENDING) # With a descending list of versions, populate possible paths to search. set(search_paths) @@ -272,10 +243,7 @@ elseif(CMAKE_CUDA_COMPILER_ID STREQUAL "Clang") # We are done with these variables now, cleanup. unset(platform_base) unset(possible_paths) - unset(possible_versions) unset(versions) - unset(i) - unset(early_terminate) unset(search_paths) if(NOT _CUDA_NVCC_EXECUTABLE) diff --git a/Modules/FindCUDAToolkit.cmake b/Modules/FindCUDAToolkit.cmake index 7ec9cbe..84bcf34 100644 --- a/Modules/FindCUDAToolkit.cmake +++ b/Modules/FindCUDAToolkit.cmake @@ -553,46 +553,17 @@ if(NOT CUDAToolkit_NVCC_EXECUTABLE) # Build out a descending list of possible cuda installations, e.g. file(GLOB possible_paths "${platform_base}*") # Iterate the glob results and create a descending list. - set(possible_versions) + set(versions) foreach(p ${possible_paths}) # Extract version number from end of string string(REGEX MATCH "[0-9][0-9]?\\.[0-9]$" p_version ${p}) if(IS_DIRECTORY ${p} AND p_version) - list(APPEND possible_versions ${p_version}) + list(APPEND versions ${p_version}) endif() endforeach() - # Cannot use list(SORT) because that is alphabetical, we need numerical. - # NOTE: this is not an efficient sorting strategy. But even if a user had - # every possible version of CUDA installed, this wouldn't create any - # significant overhead. - set(versions) - foreach(v ${possible_versions}) - list(LENGTH versions num_versions) - # First version, nothing to compare with so just append. - if(num_versions EQUAL 0) - list(APPEND versions ${v}) - else() - # Loop through list. Insert at an index when comparison is - # VERSION_GREATER since we want a descending list. Duplicates will not - # happen since this came from a glob list of directories. - set(i 0) - set(early_terminate FALSE) - while(i LESS num_versions) - list(GET versions ${i} curr) - if(v VERSION_GREATER curr) - list(INSERT versions ${i} ${v}) - set(early_terminate TRUE) - break() - endif() - math(EXPR i "${i} + 1") - endwhile() - # If it did not get inserted, place it at the end. - if(NOT early_terminate) - list(APPEND versions ${v}) - endif() - endif() - endforeach() + # Sort numerically in descending order, so we try the newest versions first. + list(SORT versions COMPARE NATURAL ORDER DESCENDING) # With a descending list of versions, populate possible paths to search. set(search_paths) @@ -615,10 +586,7 @@ if(NOT CUDAToolkit_NVCC_EXECUTABLE) # We are done with these variables now, cleanup for caller. unset(platform_base) unset(possible_paths) - unset(possible_versions) unset(versions) - unset(i) - unset(early_terminate) unset(search_paths) if(NOT CUDAToolkit_NVCC_EXECUTABLE) -- cgit v0.12