1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-24 19:59:47 +08:00

GenEx $<PATH>: Add NATIVE_PATH sub-command.

Implements the generator expression $<PATH:MATIVE_PATH> which convert
path(s) into a native format with platform-specific slashes (``\`` on
Windows hosts and ``/`` elsewhere).

Fixes: #26515
This commit is contained in:
Timothy Finnegan
2024-12-10 19:51:49 -08:00
committed by Marc Chevrier
parent f60fb11e13
commit 427be46424
5 changed files with 87 additions and 2 deletions

View File

@@ -795,6 +795,16 @@ in cmake-style format.
When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
<Normalization>` after the conversion.
.. genex:: $<PATH:NATIVE_PATH[,NORMALIZE],path...>
.. versionadded:: 3.32
Returns ``path`` converted into a native format with platform-specific
slashes (``\`` on Windows hosts and ``/`` elsewhere).
When the ``NORMALIZE`` option is specified, the path is :ref:`normalized
<Normalization>` before the conversion.
.. genex:: $<PATH:APPEND,path...,input,...>
.. versionadded:: 3.24

View File

@@ -0,0 +1,5 @@
GenEx-PATH-NATIVE_PATH
----------------------
* The :genex:`$<PATH>` generator expression gains the sub-command
``NATIVE_PATH`` to convert a CMake path into a native one.

View File

@@ -982,6 +982,27 @@ static const struct PathNode : public cmGeneratorExpressionNode
}
return std::string{};
} },
{ "NATIVE_PATH"_s,
[](cmGeneratorExpressionContext* ctx,
const GeneratorExpressionContent* cnt,
Arguments& args) -> std::string {
bool normalize = args.front() == "NORMALIZE"_s;
if (normalize) {
args.advance(1);
}
if (CheckPathParametersEx(ctx, cnt,
normalize ? "NATIVE_PATH,NORMALIZE"_s
: "NATIVE_PATH"_s,
args.size(), 1)) {
return processList(
args.front(), [normalize](std::string& value) {
auto path = cmCMakePath{ value };
value = normalize ? path.Normal().NativeString()
: path.NativeString();
});
}
return std::string{};
} },
{ "APPEND"_s,
[](cmGeneratorExpressionContext* ctx,
const GeneratorExpressionContent* cnt,

View File

@@ -0,0 +1,48 @@
include ("${RunCMake_SOURCE_DIR}/check_errors.cmake")
unset (errors)
cmake_path(SET test_path "/x/y/z/../../a/d")
cmake_path(NATIVE_PATH test_path reference)
set(output [=[$<PATH:NATIVE_PATH,/x/y/z/../../a/d>]=])
if (NOT output STREQUAL reference)
list (APPEND errors "'${output}' instead of '${reference}'")
endif()
cmake_path(SET test_path "/x/y/z/../../a/d")
cmake_path(NATIVE_PATH test_path NORMALIZE reference)
set(output [=[$<PATH:NATIVE_PATH,NORMALIZE,/x/y/z/../../a/d>]=])
if (NOT output STREQUAL reference)
list (APPEND errors "'${output}' instead of '${reference}'")
endif()
######################################
## tests with list of paths
######################################
unset(reference)
foreach(path IN ITEMS "/x/y/z/../../a/d" "/x/y/z/../../b/e")
cmake_path(SET test_path "${path}")
cmake_path(NATIVE_PATH test_path result)
list(APPEND reference "${result}")
endforeach()
set(output [=[$<PATH:NATIVE_PATH,/x/y/z/../../a/d;/x/y/z/../../b/e>]=])
if (NOT output STREQUAL reference)
list (APPEND errors "'${output}' instead of '${reference}'")
endif()
unset(reference)
foreach(path IN ITEMS "/x/y/z/../../a/d" "/x/y/z/../../b/e")
cmake_path(SET test_path "${path}")
cmake_path(NATIVE_PATH test_path NORMALIZE result)
list(APPEND reference "${result}")
endforeach()
set(output [=[$<PATH:NATIVE_PATH,NORMALIZE,/x/y/z/../../a/d;/x/y/z/../../b/e>]=])
if (NOT output STREQUAL reference)
list (APPEND errors "'${output}' instead of '${reference}'")
endif()
check_errors("PATH:NATIVE_PATH" ${errors})

View File

@@ -16,7 +16,7 @@ foreach (subcommand IN ITEMS GET_ROOT_NAME GET_ROOT_DIRECTORY GET_ROOT_PATH GET_
GET_EXTENSION GET_STEM GET_RELATIVE_PART GET_PARENT_PATH
HAS_ROOT_NAME HAS_ROOT_DIRECTORY HAS_ROOT_PATH HAS_FILENAME
HAS_EXTENSION HAS_STEM HAS_RELATIVE_PART HAS_PARENT_PATH
IS_ABSOLUTE IS_RELATIVE CMAKE_PATH REMOVE_FILENAME REMOVE_EXTENSION
IS_ABSOLUTE IS_RELATIVE CMAKE_PATH NATIVE_PATH REMOVE_FILENAME REMOVE_EXTENSION
NORMAL_PATH)
check_path_syntax (${subcommand} unexpected-arg "-DPATH_ARGUMENTS=${subcommand},ARG1,ARG2")
endforeach()
@@ -27,7 +27,7 @@ foreach (subcommand IN ITEMS GET_EXTENSION GET_STEM REMOVE_EXTENSION)
check_path_syntax ("${subcommand}[LAST_ONLY]" unexpected-arg "-DPATH_ARGUMENTS=${subcommand},LAST_ONLY,ARG1,ARG2")
unset(RunCMake-stderr-file)
endforeach()
foreach (subcommand IN ITEMS CMAKE_PATH)
foreach (subcommand IN ITEMS CMAKE_PATH NATIVE_PATH)
check_path_syntax ("${subcommand}[NORMALIZE]" unexpected-arg "-DPATH_ARGUMENTS=${subcommand},NORMALIZE,ARG1,ARG2")
endforeach()
@@ -57,6 +57,7 @@ endfunction()
check_path_execution (GET_ITEM)
check_path_execution (HAS_ITEM)
check_path_execution (CMAKE_PATH)
check_path_execution (NATIVE_PATH)
check_path_execution (APPEND)
check_path_execution (REMOVE_ITEM)
check_path_execution (REPLACE_ITEM)