From b3a5424a4e6e1c09d9b38ed1aa2b9cc1644bf3ce Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Tue, 19 Oct 2021 18:37:04 +0100 Subject: [PATCH] Add function to remove last filename extension Add a new function that takes a string and removes the portion following the last '.' character, usually a file extension. This would transform: * "a.b.c" into "a.b" * "name." into "name" * ".name" into "" * "no_dot" into "no_dot" (i.e. no change) CMake's existing file-extension-removal command removes the largest possible extension which would make "a.b.c" into "a", which is incorrect for handling tests that have '.'s within their names. The desired behaviour was added in CMake 3.14, but we support CMake >= 3.5.1 (for 3.0) and >= 2.8.12.2 (for 2.x) at the time of writing. Signed-off-by: David Horstmann --- CMakeLists.txt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2731d727c4..9c34da9228 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -136,6 +136,22 @@ function(link_to_source base_name) endif() endfunction(link_to_source) +# Get the filename without the final extension (i.e. convert "a.b.c" to "a.b") +function(get_name_without_last_ext dest_var full_name) + # Split into a list on '.' (but a cmake list is just a ';'-separated string) + string(REPLACE "." ";" ext_parts "${full_name}") + # Remove the last item if there are more than one + list(LENGTH ext_parts ext_parts_len) + if (${ext_parts_len} GREATER "1") + math(EXPR ext_parts_last_item "${ext_parts_len} - 1") + list(REMOVE_AT ext_parts ${ext_parts_last_item}) + endif() + # Convert back to a string by replacing separators with '.' + string(REPLACE ";" "." no_ext_name "${ext_parts}") + # Copy into the desired variable + set(${dest_var} ${no_ext_name} PARENT_SCOPE) +endfunction(get_name_without_last_ext) + string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}") include(CheckCCompilerFlag)