summaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorMarc Chevrier <marc.chevrier@gmail.com>2020-09-08 12:49:01 (GMT)
committerKitware Robot <kwrobot@kitware.com>2020-09-08 12:49:12 (GMT)
commit56134c1708bb227ebded52e66963b58acae7a958 (patch)
tree71fc4131b66a1bc5f9bec1dd855de1617418fc06 /Source
parent412cc37d357fbbaf5b67e54585dbdd1fbc0a6ae2 (diff)
parenteb583b0a660ba68e8e3b5f820301fde333619283 (diff)
downloadCMake-56134c1708bb227ebded52e66963b58acae7a958.zip
CMake-56134c1708bb227ebded52e66963b58acae7a958.tar.gz
CMake-56134c1708bb227ebded52e66963b58acae7a958.tar.bz2
Merge topic 'cmake_path'
eb583b0a66 cmake_path command: path management 212e953d35 cmCMakePath: Class for path handling Acked-by: Kitware Robot <kwrobot@kitware.com> Acked-by: Ben Boeckel <ben.boeckel@kitware.com> Acked-by: Raul Tambre <raul@tambre.ee> Merge-request: !5158
Diffstat (limited to 'Source')
-rw-r--r--Source/CMakeLists.txt4
-rw-r--r--Source/Checks/cm_cxx_features.cmake29
-rw-r--r--Source/Checks/cm_cxx_filesystem.cxx19
-rw-r--r--Source/cmCMakePath.cxx146
-rw-r--r--Source/cmCMakePath.h571
-rw-r--r--Source/cmCMakePathCommand.cxx1019
-rw-r--r--Source/cmCMakePathCommand.h14
-rw-r--r--Source/cmCommands.cxx2
8 files changed, 1796 insertions, 8 deletions
diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt
index ee8767f..310ffeb 100644
--- a/Source/CMakeLists.txt
+++ b/Source/CMakeLists.txt
@@ -182,6 +182,8 @@ set(SRCS
cmCheckCustomOutputs.cxx
cmCLocaleEnvironmentScope.h
cmCLocaleEnvironmentScope.cxx
+ cmCMakePath.h
+ cmCMakePath.cxx
cmCommandArgumentParserHelper.cxx
cmCommonTargetGenerator.cxx
cmCommonTargetGenerator.h
@@ -498,6 +500,8 @@ set(SRCS
cmCMakeLanguageCommand.h
cmCMakeMinimumRequired.cxx
cmCMakeMinimumRequired.h
+ cmCMakePathCommand.h
+ cmCMakePathCommand.cxx
cmCMakePolicyCommand.cxx
cmCMakePolicyCommand.h
cmConditionEvaluator.cxx
diff --git a/Source/Checks/cm_cxx_features.cmake b/Source/Checks/cm_cxx_features.cmake
index e726fc7..5c1593d 100644
--- a/Source/Checks/cm_cxx_features.cmake
+++ b/Source/Checks/cm_cxx_features.cmake
@@ -1,6 +1,7 @@
include(${CMAKE_CURRENT_LIST_DIR}/cm_message_checks_compat.cmake)
function(cm_check_cxx_feature name)
+ set(TRY_RUN_FEATURE "${ARGN}")
string(TOUPPER ${name} FEATURE)
if(NOT DEFINED CMake_HAVE_CXX_${FEATURE})
cm_message_checks_compat(
@@ -12,12 +13,26 @@ function(cm_check_cxx_feature name)
else()
set(maybe_cxx_standard "")
endif()
- try_compile(CMake_HAVE_CXX_${FEATURE}
- ${CMAKE_CURRENT_BINARY_DIR}
- ${CMAKE_CURRENT_LIST_DIR}/cm_cxx_${name}.cxx
- CMAKE_FLAGS ${maybe_cxx_standard}
- OUTPUT_VARIABLE OUTPUT
- )
+ if (TRY_RUN_FEATURE)
+ try_run(CMake_RUN_CXX_${FEATURE} CMake_COMPILE_CXX_${FEATURE}
+ ${CMAKE_CURRENT_BINARY_DIR}
+ ${CMAKE_CURRENT_LIST_DIR}/cm_cxx_${name}.cxx
+ CMAKE_FLAGS ${maybe_cxx_standard}
+ OUTPUT_VARIABLE OUTPUT
+ )
+ if (CMake_RUN_CXX_${FEATURE} EQUAL "0" AND CMake_COMPILE_CXX_${FEATURE})
+ set(CMake_HAVE_CXX_${FEATURE} ON CACHE INTERNAL "TRY_RUN" FORCE)
+ else()
+ set(CMake_HAVE_CXX_${FEATURE} OFF CACHE INTERNAL "TRY_RUN" FORCE)
+ endif()
+ else()
+ try_compile(CMake_HAVE_CXX_${FEATURE}
+ ${CMAKE_CURRENT_BINARY_DIR}
+ ${CMAKE_CURRENT_LIST_DIR}/cm_cxx_${name}.cxx
+ CMAKE_FLAGS ${maybe_cxx_standard}
+ OUTPUT_VARIABLE OUTPUT
+ )
+ endif()
set(check_output "${OUTPUT}")
# Filter out MSBuild output that looks like a warning.
string(REGEX REPLACE " +0 Warning\\(s\\)" "" check_output "${check_output}")
@@ -64,7 +79,7 @@ if(CMake_HAVE_CXX_MAKE_UNIQUE)
endif()
cm_check_cxx_feature(unique_ptr)
if (NOT CMAKE_CXX_STANDARD LESS "17")
- cm_check_cxx_feature(filesystem)
+ cm_check_cxx_feature(filesystem TRY_RUN)
else()
set(CMake_HAVE_CXX_FILESYSTEM FALSE)
endif()
diff --git a/Source/Checks/cm_cxx_filesystem.cxx b/Source/Checks/cm_cxx_filesystem.cxx
index e508d1c..ae8acc5 100644
--- a/Source/Checks/cm_cxx_filesystem.cxx
+++ b/Source/Checks/cm_cxx_filesystem.cxx
@@ -3,8 +3,25 @@
int main()
{
+ std::filesystem::path p0(L"/a/b/c");
+
std::filesystem::path p1("/a/b/c");
std::filesystem::path p2("/a/b/c");
+ if (p1 != p2) {
+ return 1;
+ }
+
+#if defined(_WIN32)
+ std::filesystem::path p3("//host/a/b/../c");
+ if (p3.lexically_normal().generic_string() != "//host/a/c") {
+ return 1;
+ }
+
+ std::filesystem::path p4("c://a/.///b/../");
+ if (p4.lexically_normal().generic_string() != "c:/a/") {
+ return 1;
+ }
+#endif
- return p1 == p2 ? 0 : 1;
+ return 0;
}
diff --git a/Source/cmCMakePath.cxx b/Source/cmCMakePath.cxx
new file mode 100644
index 0000000..b8215df
--- /dev/null
+++ b/Source/cmCMakePath.cxx
@@ -0,0 +1,146 @@
+/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
+ file Copyright.txt or https://cmake.org/licensing for details. */
+
+#include "cmConfigure.h" // IWYU pragma: keep
+
+#include "cmCMakePath.h"
+
+#include <string>
+
+#if defined(_WIN32)
+# include <cstdlib>
+#endif
+
+#include <cm/filesystem>
+#include <cm/string_view>
+
+#if defined(_WIN32)
+# include "cmStringAlgorithms.h"
+#endif
+
+cmCMakePath& cmCMakePath::ReplaceWideExtension(cm::string_view extension)
+{
+ auto file = this->Path.filename().string();
+ if (!file.empty() && file != "." && file != "..") {
+ auto pos = file.find('.', file[0] == '.' ? 1 : 0);
+ if (pos != std::string::npos) {
+ file.erase(pos);
+ }
+ }
+ if (!extension.empty()) {
+ if (extension[0] != '.') {
+ file += '.';
+ }
+ file.append(std::string(extension));
+ }
+ this->Path.replace_filename(file);
+ return *this;
+}
+
+cmCMakePath cmCMakePath::GetWideExtension() const
+{
+ auto file = this->Path.filename().string();
+ if (file.empty() || file == "." || file == "..") {
+ return cmCMakePath{};
+ }
+
+ auto pos = file.find('.', file[0] == '.' ? 1 : 0);
+ if (pos != std::string::npos) {
+ return cm::string_view(file.data() + pos, file.length() - pos);
+ }
+
+ return cmCMakePath{};
+}
+
+cmCMakePath cmCMakePath::GetNarrowStem() const
+{
+ auto stem = this->Path.stem().string();
+ if (!stem.empty()) {
+ auto pos = stem.find('.', stem[0] == '.' ? 1 : 0);
+ if (pos != std::string::npos) {
+ return stem.substr(0, pos);
+ }
+ }
+ return stem;
+}
+
+cmCMakePath cmCMakePath::Absolute(const cm::filesystem::path& base) const
+{
+ if (this->Path.is_relative()) {
+ auto path = base;
+ path /= this->Path;
+ // filesystem::path::operator/= use preferred_separator ('\' on Windows)
+ // so converts back to '/'
+ return path.generic_string();
+ }
+ return *this;
+}
+
+bool cmCMakePath::IsPrefix(const cmCMakePath& path) const
+{
+ auto prefix_it = this->Path.begin();
+ auto prefix_end = this->Path.end();
+ auto path_it = path.Path.begin();
+ auto path_end = path.Path.end();
+
+ while (prefix_it != prefix_end && path_it != path_end &&
+ *prefix_it == *path_it) {
+ ++prefix_it;
+ ++path_it;
+ }
+ return prefix_it == prefix_end;
+}
+
+std::string cmCMakePath::FormatPath(std::string path, format fmt)
+{
+#if defined(_WIN32)
+ if (fmt == auto_format || fmt == native_format) {
+ auto prefix = path.substr(0, 4);
+ for (auto& c : prefix) {
+ if (c == '\\') {
+ c = '/';
+ }
+ }
+ // remove Windows long filename marker
+ if (prefix == "//?/"_s) {
+ path.erase(0, 4);
+ }
+ if (cmHasPrefix(path, "UNC/"_s) || cmHasPrefix(path, "UNC\\"_s)) {
+ path.erase(0, 2);
+ path[0] = '/';
+ }
+ }
+#else
+ static_cast<void>(fmt);
+#endif
+ return path;
+}
+
+void cmCMakePath::GetNativePath(std::string& path) const
+{
+ cm::filesystem::path tmp(this->Path);
+ tmp.make_preferred();
+
+ path = tmp.string();
+}
+void cmCMakePath::GetNativePath(std::wstring& path) const
+{
+ cm::filesystem::path tmp(this->Path);
+ tmp.make_preferred();
+
+ path = tmp.wstring();
+
+#if defined(_WIN32)
+ // Windows long filename
+ static std::wstring UNC(L"\\\\?\\UNC");
+ static std::wstring PREFIX(L"\\\\?\\");
+
+ if (this->IsAbsolute() && path.length() > _MAX_PATH - 12) {
+ if (this->HasRootName() && path[0] == L'\\') {
+ path = UNC + path.substr(1);
+ } else {
+ path = PREFIX + path;
+ }
+ }
+#endif
+}
diff --git a/Source/cmCMakePath.h b/Source/cmCMakePath.h
new file mode 100644
index 0000000..15aa30c
--- /dev/null
+++ b/Source/cmCMakePath.h
@@ -0,0 +1,571 @@
+/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
+ file Copyright.txt or https://cmake.org/licensing for details. */
+
+#pragma once
+
+#include "cmConfigure.h" // IWYU pragma: keep
+
+#include <cstddef>
+#include <string>
+#include <utility>
+
+#include <cm/filesystem>
+#include <cm/string_view>
+#include <cm/type_traits>
+#include <cmext/string_view>
+
+namespace detail {
+#if defined(__SUNPRO_CC) && defined(__sparc)
+// Oracle DeveloperStudio C++ compiler on Solaris/Sparc fails to compile
+// the full 'is_pathable' and 'is_move_pathable' checks. We use it only to
+// improve error messages via 'enable_if' when calling methods with incorrect
+// types. Just pretend all types are allowed so we can at least compile valid
+// code.
+template <typename T>
+struct is_pathable : std::true_type
+{
+};
+
+template <typename T>
+struct is_move_pathable : std::true_type
+{
+};
+
+#else
+template <typename T, typename = void>
+struct is_pathable : std::false_type
+{
+};
+
+template <>
+struct is_pathable<cm::filesystem::path> : std::true_type
+{
+};
+template <>
+struct is_pathable<std::string> : std::true_type
+{
+};
+template <>
+struct is_pathable<cm::string_view> : std::true_type
+{
+};
+template <>
+struct is_pathable<cm::static_string_view> : std::true_type
+{
+};
+template <typename T>
+struct is_pathable<
+ T,
+ cm::enable_if_t<std::is_same<char*, typename std::decay<T>::type>::value,
+ void>>
+ : cm::bool_constant<std::is_same<char*, typename std::decay<T>::type>::value>
+{
+};
+
+template <typename T>
+struct is_move_pathable : std::false_type
+{
+};
+
+template <>
+struct is_move_pathable<cm::filesystem::path> : std::true_type
+{
+};
+template <>
+struct is_move_pathable<std::string> : std::true_type
+{
+};
+#endif
+}
+
+class cmCMakePath
+{
+private:
+ template <typename Source>
+ using enable_if_move_pathable =
+ cm::enable_if_t<detail::is_move_pathable<Source>::value, cmCMakePath&>;
+
+ template <typename Source>
+ using enable_if_pathable =
+ cm::enable_if_t<detail::is_pathable<Source>::value, cmCMakePath&>;
+
+public:
+ using value_type = cm::filesystem::path::value_type;
+ using string_type = cm::filesystem::path::string_type;
+
+ enum format : unsigned char
+ {
+ auto_format =
+ static_cast<unsigned char>(cm::filesystem::path::format::auto_format),
+ native_format =
+ static_cast<unsigned char>(cm::filesystem::path::format::native_format),
+ generic_format =
+ static_cast<unsigned char>(cm::filesystem::path::format::generic_format)
+ };
+
+ class iterator;
+ using const_iterator = iterator;
+
+ cmCMakePath() noexcept = default;
+
+ cmCMakePath(const cmCMakePath&) = default;
+
+ cmCMakePath(cmCMakePath&& path) noexcept
+ : Path(std::forward<cm::filesystem::path>(path.Path))
+ {
+ }
+
+ cmCMakePath(cm::filesystem::path path) noexcept
+ : Path(std::move(path))
+ {
+ }
+ cmCMakePath(cm::string_view source, format fmt = generic_format) noexcept
+ : Path(FormatPath(source, fmt))
+ {
+ }
+ template <typename Source, typename = enable_if_move_pathable<Source>>
+ cmCMakePath(Source source, format fmt = generic_format)
+ : Path(FormatPath(std::move(source), fmt))
+ {
+ }
+
+ template <typename Source, typename = enable_if_move_pathable<Source>>
+ cmCMakePath& Assign(Source&& source)
+ {
+ this->Path = std::forward<Source>(source);
+ return *this;
+ }
+ template <typename Source, typename = enable_if_pathable<Source>>
+ cmCMakePath& Assign(const Source& source)
+ {
+ this->Path = source;
+ return *this;
+ }
+
+ cmCMakePath& operator=(const cmCMakePath& path)
+ {
+ if (this != &path) {
+ this->Path = path.Path;
+ }
+ return *this;
+ }
+ cmCMakePath& operator=(cmCMakePath&& path) noexcept
+ {
+ if (this != &path) {
+ this->Path = std::move(path.Path);
+ }
+ return *this;
+ }
+ template <typename Source, typename = enable_if_move_pathable<Source>>
+ cmCMakePath& operator=(Source&& source)
+ {
+ this->Assign(std::forward<Source>(source));
+ return *this;
+ }
+ template <typename Source, typename = enable_if_pathable<Source>>
+ cmCMakePath& operator=(const Source& source)
+ {
+ this->Assign(source);
+ return *this;
+ }
+
+ // Concatenation
+ cmCMakePath& Append(const cmCMakePath& path)
+ {
+ return this->Append(path.Path);
+ }
+ cmCMakePath& Append(const cm::filesystem::path& path)
+ {
+ this->Path /= path;
+ // filesystem::path::append use preferred_separator ('\' on Windows)
+ // so convert back to '/'
+ this->Path = this->Path.generic_string();
+ return *this;
+ }
+
+ template <typename Source, typename = enable_if_pathable<Source>>
+ cmCMakePath& Append(const Source& source)
+ {
+ return this->Append(cm::filesystem::path(source));
+ }
+
+ cmCMakePath& operator/=(const cmCMakePath& path)
+ {
+ return this->Append(path);
+ }
+ template <typename Source, typename = enable_if_pathable<Source>>
+ cmCMakePath& operator/=(const Source& source)
+ {
+ return this->Append(source);
+ }
+
+ cmCMakePath& Concat(const cmCMakePath& path)
+ {
+ this->Path += path.Path;
+ return *this;
+ }
+ cmCMakePath& Concat(cm::static_string_view source)
+ {
+ this->Path.concat(std::string(source));
+ return *this;
+ }
+ template <typename Source, typename = enable_if_pathable<Source>>
+ cmCMakePath& Concat(const Source& source)
+ {
+ this->Path.concat(source);
+ return *this;
+ }
+
+ cmCMakePath& operator+=(const cmCMakePath& path)
+ {
+ return this->Concat(path);
+ }
+ template <typename Source, typename = enable_if_pathable<Source>>
+ cmCMakePath& operator+=(const Source& source)
+ {
+ return this->Concat(source);
+ }
+
+ // Manipulation
+ void Clear() noexcept { this->Path.clear(); }
+
+ cmCMakePath& RemoveFileName()
+ {
+ this->Path.remove_filename();
+ return *this;
+ }
+
+ cmCMakePath& ReplaceFileName(const cmCMakePath& filename)
+ {
+ if (this->Path.has_filename()) {
+ this->Path.replace_filename(filename.Path);
+ }
+ return *this;
+ }
+ template <typename Source, typename = enable_if_pathable<Source>>
+ cmCMakePath& ReplaceFileName(const Source& filename)
+ {
+ if (this->Path.has_filename()) {
+ this->Path.replace_filename(filename);
+ }
+ return *this;
+ }
+
+ cmCMakePath& ReplaceExtension(const cmCMakePath& extension = cmCMakePath())
+ {
+ this->Path.replace_extension(extension.Path);
+ return *this;
+ }
+ template <typename Source, typename = enable_if_pathable<Source>>
+ cmCMakePath& ReplaceExtension(const Source& extension)
+ {
+ this->Path.replace_extension(extension);
+ return *this;
+ }
+
+ cmCMakePath& ReplaceWideExtension(
+ const cmCMakePath& extension = cmCMakePath())
+ {
+ return this->ReplaceWideExtension(
+ static_cast<cm::string_view>(extension.Path.string()));
+ }
+ template <typename Source, typename = enable_if_pathable<Source>>
+ cmCMakePath& ReplaceWideExtension(const Source& extension)
+ {
+ return this->ReplaceWideExtension(cm::string_view(extension));
+ }
+ cmCMakePath& ReplaceWideExtension(cm::string_view extension);
+
+ cmCMakePath& RemoveExtension()
+ {
+ if (this->Path.has_extension()) {
+ this->ReplaceExtension(cm::string_view(""));
+ }
+ return *this;
+ }
+
+ cmCMakePath& RemoveWideExtension()
+ {
+ if (this->Path.has_extension()) {
+ this->ReplaceWideExtension(cm::string_view(""));
+ }
+ return *this;
+ }
+
+ void swap(cmCMakePath& other) noexcept { this->Path.swap(other.Path); }
+
+ // Observers
+ std::string String() const { return this->Path.string(); }
+ std::wstring WString() const { return this->Path.wstring(); }
+
+ string_type Native() const
+ {
+ string_type path;
+ this->GetNativePath(path);
+
+ return path;
+ }
+ std::string NativeString() const
+ {
+ std::string path;
+ this->GetNativePath(path);
+
+ return path;
+ }
+ std::wstring NativeWString() const
+ {
+ std::wstring path;
+ this->GetNativePath(path);
+
+ return path;
+ }
+ std::string GenericString() const { return this->Path.generic_string(); }
+ std::wstring GenericWString() const { return this->Path.generic_wstring(); }
+
+ // Decomposition
+ cmCMakePath GetRootName() const { return this->Path.root_name(); }
+ cmCMakePath GetRootDirectory() const { return this->Path.root_directory(); }
+ cmCMakePath GetRootPath() const { return this->Path.root_path(); }
+ cmCMakePath GetFileName() const { return this->Path.filename(); }
+ cmCMakePath GetExtension() const { return this->Path.extension(); }
+ cmCMakePath GetWideExtension() const;
+ cmCMakePath GetStem() const { return this->Path.stem(); }
+ cmCMakePath GetNarrowStem() const;
+
+ cmCMakePath GetRelativePath() const { return this->Path.relative_path(); }
+ cmCMakePath GetParentPath() const { return this->Path.parent_path(); }
+
+ // Generation
+ cmCMakePath Normal() const
+ {
+ auto path = this->Path.lexically_normal();
+ // filesystem::path:lexically_normal use preferred_separator ('\') on
+ // Windows) so convert back to '/'
+ return path.generic_string();
+ }
+
+ cmCMakePath Relative(const cmCMakePath& base) const
+ {
+ return this->Relative(base.Path);
+ }
+ cmCMakePath Relative(const cm::filesystem::path& base) const
+ {
+ auto path = this->Path.lexically_relative(base);
+ // filesystem::path:lexically_relative use preferred_separator ('\') on
+ // Windows) so convert back to '/'
+ return path.generic_string();
+ }
+ template <typename Source, typename = enable_if_pathable<Source>>
+ cmCMakePath Relative(const Source& base) const
+ {
+ return this->Relative(cm::filesystem::path(base));
+ }
+
+ cmCMakePath Proximate(const cmCMakePath& base) const
+ {
+ return this->Proximate(base.Path);
+ }
+ cmCMakePath Proximate(const cm::filesystem::path& base) const
+ {
+ auto path = this->Path.lexically_proximate(base);
+ // filesystem::path::lexically_proximate use preferred_separator ('\') on
+ // Windows) so convert back to '/'
+ return path.generic_string();
+ }
+ template <typename Source, typename = enable_if_pathable<Source>>
+ cmCMakePath Proximate(const Source& base) const
+ {
+ return this->Proximate(cm::filesystem::path(base));
+ }
+
+ cmCMakePath Absolute(const cmCMakePath& base) const
+ {
+ return this->Absolute(base.Path);
+ }
+ template <typename Source, typename = enable_if_pathable<Source>>
+ cmCMakePath Absolute(const Source& base) const
+ {
+ return this->Absolute(cm::filesystem::path(base));
+ }
+ cmCMakePath Absolute(const cm::filesystem::path& base) const;
+
+ // Comparison
+ int Compare(const cmCMakePath& path) const noexcept
+ {
+ return this->Path.compare(path.Path);
+ }
+
+ // Query
+ bool IsEmpty() const noexcept { return this->Path.empty(); }
+
+ bool HasRootPath() const { return this->Path.has_root_path(); }
+ bool HasRootName() const { return this->Path.has_root_name(); }
+ bool HasRootDirectory() const { return this->Path.has_root_directory(); }
+ bool HasRelativePath() const { return this->Path.has_relative_path(); }
+ bool HasParentPath() const { return this->Path.has_parent_path(); }
+ bool HasFileName() const { return this->Path.has_filename(); }
+ bool HasStem() const { return this->Path.has_stem(); }
+ bool HasExtension() const { return this->Path.has_extension(); }
+
+ bool IsAbsolute() const { return this->Path.is_absolute(); }
+ bool IsRelative() const { return this->Path.is_relative(); }
+ bool IsPrefix(const cmCMakePath& path) const;
+
+ // Iterators
+ // =========
+ inline iterator begin() const;
+ inline iterator end() const;
+
+ // Non-members
+ // ===========
+ friend inline bool operator==(const cmCMakePath& lhs,
+ const cmCMakePath& rhs) noexcept
+ {
+ return lhs.Compare(rhs) == 0;
+ }
+ friend inline bool operator!=(const cmCMakePath& lhs,
+ const cmCMakePath& rhs) noexcept
+ {
+ return lhs.Compare(rhs) != 0;
+ }
+
+ friend inline cmCMakePath operator/(const cmCMakePath& lhs,
+ const cmCMakePath& rhs)
+ {
+ cmCMakePath result(lhs);
+ result /= rhs;
+
+ return result;
+ }
+
+private:
+ friend std::size_t hash_value(const cmCMakePath& path) noexcept;
+
+ static std::string FormatPath(std::string path, format fmt = generic_format);
+ static std::string FormatPath(cm::string_view path,
+ format fmt = generic_format)
+ {
+ return FormatPath(std::string(path), fmt);
+ }
+
+ void GetNativePath(std::string& path) const;
+ void GetNativePath(std::wstring& path) const;
+
+ cm::filesystem::path Path;
+};
+
+class cmCMakePath::iterator
+{
+public:
+ using iterator_category = cm::filesystem::path::iterator::iterator_category;
+
+ using value_type = cmCMakePath;
+ using difference_type = cm::filesystem::path::iterator::difference_type;
+ using pointer = const cmCMakePath*;
+ using reference = const cmCMakePath&;
+
+ iterator() = default;
+
+ iterator(const iterator& other)
+ : Iterator(other.Iterator)
+ , Path(other.Path)
+ , PathElement(*this->Iterator)
+ {
+ }
+
+ ~iterator() = default;
+
+ iterator& operator=(const iterator& other)
+ {
+ if (this != &other) {
+ this->Iterator = other.Iterator;
+ this->Path = other.Path;
+ this->PathElement = *this->Iterator;
+ }
+
+ return *this;
+ }
+
+ reference operator*() const { return this->PathElement; }
+
+ pointer operator->() const { return &this->PathElement; }
+
+ iterator& operator++()
+ {
+ ++this->Iterator;
+ this->PathElement = *this->Iterator;
+
+ return *this;
+ }
+
+ iterator operator++(int)
+ {
+ iterator it(*this);
+ this->operator++();
+ return it;
+ }
+
+ iterator& operator--()
+ {
+ --this->Iterator;
+ this->PathElement = *this->Iterator;
+
+ return *this;
+ }
+
+ iterator operator--(int)
+ {
+ iterator it(*this);
+ this->operator--();
+ return it;
+ }
+
+private:
+ friend class cmCMakePath;
+ friend bool operator==(const iterator&, const iterator&);
+
+ iterator(const cmCMakePath* path, const cm::filesystem::path::iterator& it)
+ : Iterator(it)
+ , Path(path)
+ , PathElement(*this->Iterator)
+ {
+ }
+
+ cm::filesystem::path::iterator Iterator;
+ const cmCMakePath* Path = nullptr;
+ cmCMakePath PathElement;
+};
+
+inline cmCMakePath::iterator cmCMakePath::begin() const
+{
+ return iterator(this, this->Path.begin());
+}
+inline cmCMakePath::iterator cmCMakePath::end() const
+{
+ return iterator(this, this->Path.end());
+}
+
+// Non-member functions
+// ====================
+inline bool operator==(const cmCMakePath::iterator& lhs,
+ const cmCMakePath::iterator& rhs)
+{
+ return lhs.Path == rhs.Path && lhs.Path != nullptr &&
+ lhs.Iterator == rhs.Iterator;
+}
+
+inline bool operator!=(const cmCMakePath::iterator& lhs,
+ const cmCMakePath::iterator& rhs)
+{
+ return !(lhs == rhs);
+}
+
+inline void swap(cmCMakePath& lhs, cmCMakePath& rhs) noexcept
+{
+ lhs.swap(rhs);
+}
+
+inline std::size_t hash_value(const cmCMakePath& path) noexcept
+{
+ return cm::filesystem::hash_value(path.Path);
+}
diff --git a/Source/cmCMakePathCommand.cxx b/Source/cmCMakePathCommand.cxx
new file mode 100644
index 0000000..720f582
--- /dev/null
+++ b/Source/cmCMakePathCommand.cxx
@@ -0,0 +1,1019 @@
+/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
+ file Copyright.txt or https://cmake.org/licensing for details. */
+#include "cmCMakePathCommand.h"
+
+#include <algorithm>
+#include <functional>
+#include <iomanip>
+#include <map>
+#include <sstream>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include <cm/string_view>
+#include <cmext/string_view>
+
+#include "cmArgumentParser.h"
+#include "cmCMakePath.h"
+#include "cmExecutionStatus.h"
+#include "cmMakefile.h"
+#include "cmRange.h"
+#include "cmStringAlgorithms.h"
+#include "cmSubcommandTable.h"
+#include "cmSystemTools.h"
+
+namespace {
+// Helper classes for argument parsing
+template <typename Result>
+class CMakePathArgumentParser : public cmArgumentParser<Result>
+{
+public:
+ CMakePathArgumentParser()
+ : cmArgumentParser<Result>()
+ {
+ }
+
+ template <typename T>
+ CMakePathArgumentParser& Bind(cm::static_string_view name, T Result::*member)
+ {
+ cmArgumentParser<Result>::Bind(name, member);
+ return *this;
+ }
+
+ template <int Advance = 2>
+ Result Parse(std::vector<std::string> const& args,
+ std::vector<std::string>* keywordsMissingValue = nullptr,
+ std::vector<std::string>* parsedKeywords = nullptr) const
+ {
+ this->Inputs.clear();
+
+ return cmArgumentParser<Result>::Parse(cmMakeRange(args).advance(Advance),
+ &this->Inputs, keywordsMissingValue,
+ parsedKeywords);
+ }
+
+ const std::vector<std::string>& GetInputs() const { return Inputs; }
+
+protected:
+ mutable std::vector<std::string> Inputs;
+};
+
+// OUTPUT_VARIABLE is expected
+template <typename Result>
+class ArgumentParserWithOutputVariable : public CMakePathArgumentParser<Result>
+{
+public:
+ ArgumentParserWithOutputVariable()
+ : CMakePathArgumentParser<Result>()
+ {
+ this->Bind("OUTPUT_VARIABLE"_s, &Result::Output);
+ }
+
+ template <typename T>
+ ArgumentParserWithOutputVariable& Bind(cm::static_string_view name,
+ T Result::*member)
+ {
+ cmArgumentParser<Result>::Bind(name, member);
+ return *this;
+ }
+
+ template <int Advance = 2>
+ Result Parse(std::vector<std::string> const& args) const
+ {
+ this->KeywordsMissingValue.clear();
+ this->ParsedKeywords.clear();
+
+ return CMakePathArgumentParser<Result>::template Parse<Advance>(
+ args, &this->KeywordsMissingValue, &this->ParsedKeywords);
+ }
+
+ const std::vector<std::string>& GetKeywordsMissingValue() const
+ {
+ return this->KeywordsMissingValue;
+ }
+ const std::vector<std::string>& GetParsedKeywords() const
+ {
+ return this->ParsedKeywords;
+ }
+
+ bool checkOutputVariable(const Result& arguments,
+ cmExecutionStatus& status) const
+ {
+ if (std::find(this->GetKeywordsMissingValue().begin(),
+ this->GetKeywordsMissingValue().end(),
+ "OUTPUT_VARIABLE"_s) !=
+ this->GetKeywordsMissingValue().end()) {
+ status.SetError("OUTPUT_VARIABLE requires an argument.");
+ return false;
+ }
+
+ if (std::find(this->GetParsedKeywords().begin(),
+ this->GetParsedKeywords().end(),
+ "OUTPUT_VARIABLE"_s) != this->GetParsedKeywords().end() &&
+ arguments.Output.empty()) {
+ status.SetError("Invalid name for output variable.");
+ return false;
+ }
+
+ return true;
+ }
+
+private:
+ mutable std::vector<std::string> KeywordsMissingValue;
+ mutable std::vector<std::string> ParsedKeywords;
+};
+
+struct OutputVariable
+{
+ std::string Output;
+};
+// Usable when OUTPUT_VARIABLE is the only option
+class OutputVariableParser
+ : public ArgumentParserWithOutputVariable<OutputVariable>
+{
+};
+
+struct NormalizeOption
+{
+ bool Normalize = false;
+};
+// Usable when NORMALIZE is the only option
+class NormalizeParser : public CMakePathArgumentParser<NormalizeOption>
+{
+public:
+ NormalizeParser() { this->Bind("NORMALIZE"_s, &NormalizeOption::Normalize); }
+};
+
+// retrieve value of input path from specified variable
+bool getInputPath(const std::string& arg, cmExecutionStatus& status,
+ std::string& path)
+{
+ auto def = status.GetMakefile().GetDefinition(arg);
+ if (def == nullptr) {
+ status.SetError("undefined variable for input path.");
+ return false;
+ }
+
+ path = *def;
+ return true;
+}
+
+bool HandleGetCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ static std::map<cm::string_view,
+ std::function<cmCMakePath(const cmCMakePath&, bool)>> const
+ actions{ { "ROOT_NAME"_s,
+ [](const cmCMakePath& path, bool) -> cmCMakePath {
+ return path.GetRootName();
+ } },
+ { "ROOT_DIRECTORY"_s,
+ [](const cmCMakePath& path, bool) -> cmCMakePath {
+ return path.GetRootDirectory();
+ } },
+ { "ROOT_PATH"_s,
+ [](const cmCMakePath& path, bool) -> cmCMakePath {
+ return path.GetRootPath();
+ } },
+ { "FILENAME"_s,
+ [](const cmCMakePath& path, bool) -> cmCMakePath {
+ return path.GetFileName();
+ } },
+ { "EXTENSION"_s,
+ [](const cmCMakePath& path, bool last_only) -> cmCMakePath {
+ if (last_only) {
+ return path.GetExtension();
+ }
+ return path.GetWideExtension();
+ } },
+ { "STEM"_s,
+ [](const cmCMakePath& path, bool last_only) -> cmCMakePath {
+ if (last_only) {
+ return path.GetStem();
+ }
+ return path.GetNarrowStem();
+ } },
+ { "RELATIVE_PATH"_s,
+ [](const cmCMakePath& path, bool) -> cmCMakePath {
+ return path.GetRelativePath();
+ } },
+ { "PARENT_PATH"_s,
+ [](const cmCMakePath& path, bool) -> cmCMakePath {
+ return path.GetParentPath();
+ } } };
+
+ if (args.size() < 4) {
+ status.SetError("GET must be called with at least three arguments.");
+ return false;
+ }
+
+ const auto& action = args[2];
+
+ if (actions.find(action) == actions.end()) {
+ status.SetError(
+ cmStrCat("GET called with an unknown action: ", action, "."));
+ return false;
+ }
+
+ struct Arguments
+ {
+ bool LastOnly = false;
+ };
+
+ CMakePathArgumentParser<Arguments> parser;
+ if ((action == "EXTENSION"_s || action == "STEM"_s)) {
+ parser.Bind("LAST_ONLY"_s, &Arguments::LastOnly);
+ }
+
+ Arguments const arguments = parser.Parse<3>(args);
+
+ if (parser.GetInputs().size() != 1) {
+ status.SetError("GET called with unexpected arguments.");
+ return false;
+ }
+ if (parser.GetInputs().front().empty()) {
+ status.SetError("Invalid name for output variable.");
+ return false;
+ }
+
+ std::string path;
+ if (!getInputPath(args[1], status, path)) {
+ return false;
+ }
+
+ auto result = actions.at(action)(path, arguments.LastOnly);
+
+ status.GetMakefile().AddDefinition(parser.GetInputs().front(),
+ result.String());
+
+ return true;
+}
+
+bool HandleAppendCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ static OutputVariableParser const parser{};
+
+ const auto arguments = parser.Parse(args);
+
+ if (!parser.checkOutputVariable(arguments, status)) {
+ return false;
+ }
+
+ cmCMakePath path(status.GetMakefile().GetSafeDefinition(args[1]));
+ for (const auto& input : parser.GetInputs()) {
+ path /= input;
+ }
+
+ status.GetMakefile().AddDefinition(
+ arguments.Output.empty() ? args[1] : arguments.Output, path.String());
+
+ return true;
+}
+
+bool HandleConcatCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ static OutputVariableParser const parser{};
+
+ const auto arguments = parser.Parse(args);
+
+ if (!parser.checkOutputVariable(arguments, status)) {
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ cmCMakePath path(inputPath);
+ for (const auto& input : parser.GetInputs()) {
+ path += input;
+ }
+
+ status.GetMakefile().AddDefinition(
+ arguments.Output.empty() ? args[1] : arguments.Output, path.String());
+
+ return true;
+}
+
+bool HandleRemoveFilenameCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ static OutputVariableParser const parser{};
+
+ const auto arguments = parser.Parse(args);
+
+ if (!parser.checkOutputVariable(arguments, status)) {
+ return false;
+ }
+
+ if (!parser.GetInputs().empty()) {
+ status.SetError("REMOVE_FILENAME called with unexpected arguments.");
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ cmCMakePath path(inputPath);
+ path.RemoveFileName();
+
+ status.GetMakefile().AddDefinition(
+ arguments.Output.empty() ? args[1] : arguments.Output, path.String());
+
+ return true;
+}
+
+bool HandleReplaceFilenameCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ static OutputVariableParser const parser{};
+
+ const auto arguments = parser.Parse(args);
+
+ if (!parser.checkOutputVariable(arguments, status)) {
+ return false;
+ }
+
+ if (parser.GetInputs().size() > 1) {
+ status.SetError("REPLACE_FILENAME called with unexpected arguments.");
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ cmCMakePath path(inputPath);
+ path.ReplaceFileName(
+ parser.GetInputs().empty() ? "" : parser.GetInputs().front());
+
+ status.GetMakefile().AddDefinition(
+ arguments.Output.empty() ? args[1] : arguments.Output, path.String());
+
+ return true;
+}
+
+bool HandleRemoveExtensionCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ struct Arguments
+ {
+ std::string Output;
+ bool LastOnly = false;
+ };
+
+ static auto const parser =
+ ArgumentParserWithOutputVariable<Arguments>{}.Bind("LAST_ONLY"_s,
+ &Arguments::LastOnly);
+
+ Arguments const arguments = parser.Parse(args);
+
+ if (!parser.checkOutputVariable(arguments, status)) {
+ return false;
+ }
+
+ if (!parser.GetInputs().empty()) {
+ status.SetError("REMOVE_EXTENSION called with unexpected arguments.");
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ cmCMakePath path(inputPath);
+
+ if (arguments.LastOnly) {
+ path.RemoveExtension();
+ } else {
+ path.RemoveWideExtension();
+ }
+
+ status.GetMakefile().AddDefinition(
+ arguments.Output.empty() ? args[1] : arguments.Output, path.String());
+
+ return true;
+}
+
+bool HandleReplaceExtensionCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ struct Arguments
+ {
+ std::string Output;
+ bool LastOnly = false;
+ };
+
+ static auto const parser =
+ ArgumentParserWithOutputVariable<Arguments>{}.Bind("LAST_ONLY"_s,
+ &Arguments::LastOnly);
+
+ Arguments const arguments = parser.Parse(args);
+
+ if (!parser.checkOutputVariable(arguments, status)) {
+ return false;
+ }
+
+ if (parser.GetInputs().size() > 1) {
+ status.SetError("REPLACE_EXTENSION called with unexpected arguments.");
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ cmCMakePath path(inputPath);
+ cmCMakePath extension(
+ parser.GetInputs().empty() ? "" : parser.GetInputs().front());
+
+ if (arguments.LastOnly) {
+ path.ReplaceExtension(extension);
+ } else {
+ path.ReplaceWideExtension(extension);
+ }
+
+ status.GetMakefile().AddDefinition(
+ arguments.Output.empty() ? args[1] : arguments.Output, path.String());
+
+ return true;
+}
+
+bool HandleNormalPathCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ static OutputVariableParser const parser{};
+
+ const auto arguments = parser.Parse(args);
+
+ if (!parser.checkOutputVariable(arguments, status)) {
+ return false;
+ }
+
+ if (!parser.GetInputs().empty()) {
+ status.SetError("NORMAL_PATH called with unexpected arguments.");
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ auto path = cmCMakePath(inputPath).Normal();
+
+ status.GetMakefile().AddDefinition(
+ arguments.Output.empty() ? args[1] : arguments.Output, path.String());
+
+ return true;
+}
+
+bool HandleTransformPathCommand(
+ std::vector<std::string> const& args, cmExecutionStatus& status,
+ const std::function<cmCMakePath(const cmCMakePath&,
+ const std::string& base)>& transform,
+ bool normalizeOption = false)
+{
+ struct Arguments
+ {
+ std::string Output;
+ std::string BaseDirectory;
+ bool Normalize = false;
+ };
+
+ auto parser = ArgumentParserWithOutputVariable<Arguments>{}.Bind(
+ "BASE_DIRECTORY"_s, &Arguments::BaseDirectory);
+ if (normalizeOption) {
+ parser.Bind("NORMALIZE"_s, &Arguments::Normalize);
+ }
+
+ Arguments arguments = parser.Parse(args);
+
+ if (!parser.checkOutputVariable(arguments, status)) {
+ return false;
+ }
+
+ if (!parser.GetInputs().empty()) {
+ status.SetError(cmStrCat(args[0], " called with unexpected arguments."));
+ return false;
+ }
+
+ if (std::find(parser.GetKeywordsMissingValue().begin(),
+ parser.GetKeywordsMissingValue().end(), "BASE_DIRECTORY"_s) !=
+ parser.GetKeywordsMissingValue().end()) {
+ status.SetError("BASE_DIRECTORY requires an argument.");
+ return false;
+ }
+
+ if (std::find(parser.GetParsedKeywords().begin(),
+ parser.GetParsedKeywords().end(),
+ "BASE_DIRECTORY"_s) == parser.GetParsedKeywords().end()) {
+ arguments.BaseDirectory = status.GetMakefile().GetCurrentSourceDirectory();
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ auto path = transform(cmCMakePath(inputPath), arguments.BaseDirectory);
+ if (arguments.Normalize) {
+ path = path.Normal();
+ }
+
+ status.GetMakefile().AddDefinition(
+ arguments.Output.empty() ? args[1] : arguments.Output, path.String());
+
+ return true;
+}
+
+bool HandleRelativePathCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ return HandleTransformPathCommand(
+ args, status,
+ [](const cmCMakePath& path, const std::string& base) -> cmCMakePath {
+ return path.Relative(base);
+ });
+}
+
+bool HandleProximatePathCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ return HandleTransformPathCommand(
+ args, status,
+ [](const cmCMakePath& path, const std::string& base) -> cmCMakePath {
+ return path.Proximate(base);
+ });
+}
+
+bool HandleAbsolutePathCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ return HandleTransformPathCommand(
+ args, status,
+ [](const cmCMakePath& path, const std::string& base) -> cmCMakePath {
+ return path.Absolute(base);
+ },
+ true);
+}
+
+bool HandleCMakePathCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ if (args.size() < 3 || args.size() > 4) {
+ status.SetError("CMAKE_PATH must be called with two or three arguments.");
+ return false;
+ }
+
+ static NormalizeParser const parser;
+
+ const auto arguments = parser.Parse(args);
+
+ if (parser.GetInputs().size() != 1) {
+ status.SetError("CMAKE_PATH called with unexpected arguments.");
+ return false;
+ }
+
+ if (args[1].empty()) {
+ status.SetError("Invalid name for output variable.");
+ return false;
+ }
+
+ auto path =
+ cmCMakePath(parser.GetInputs().front(), cmCMakePath::native_format);
+
+ if (arguments.Normalize) {
+ path = path.Normal();
+ }
+
+ status.GetMakefile().AddDefinition(args[1], path.GenericString());
+
+ return true;
+}
+
+bool HandleNativePathCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ if (args.size() < 3 || args.size() > 4) {
+ status.SetError("NATIVE_PATH must be called with two or three arguments.");
+ return false;
+ }
+
+ static NormalizeParser const parser;
+
+ const auto arguments = parser.Parse(args);
+
+ if (parser.GetInputs().size() != 1) {
+ status.SetError("NATIVE_PATH called with unexpected arguments.");
+ return false;
+ }
+ if (parser.GetInputs().front().empty()) {
+ status.SetError("Invalid name for output variable.");
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ cmCMakePath path(inputPath);
+ if (arguments.Normalize) {
+ path = path.Normal();
+ }
+
+ status.GetMakefile().AddDefinition(parser.GetInputs().front(),
+ path.NativeString());
+
+ return true;
+}
+
+bool HandleConvertCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+#if defined(_WIN32) && !defined(__CYGWIN__)
+ const auto pathSep = ";"_s;
+#else
+ const auto pathSep = ":"_s;
+#endif
+ const auto cmakePath = "TO_CMAKE_PATH_LIST"_s;
+ const auto nativePath = "TO_NATIVE_PATH_LIST"_s;
+
+ if (args.size() < 4 || args.size() > 5) {
+ status.SetError("CONVERT must be called with three or four arguments.");
+ return false;
+ }
+
+ const auto& action = args[2];
+
+ if (action != cmakePath && action != nativePath) {
+ status.SetError(
+ cmStrCat("CONVERT called with an unknown action: ", action, "."));
+ return false;
+ }
+
+ if (args[3].empty()) {
+ status.SetError("Invalid name for output variable.");
+ return false;
+ }
+
+ static NormalizeParser const parser;
+
+ const auto arguments = parser.Parse<4>(args);
+
+ if (!parser.GetInputs().empty()) {
+ status.SetError("CONVERT called with unexpected arguments.");
+ return false;
+ }
+
+ std::vector<std::string> paths;
+
+ if (action == cmakePath) {
+ paths = cmSystemTools::SplitString(args[1], pathSep.front());
+ } else {
+ cmExpandList(args[1], paths);
+ }
+
+ for (auto& path : paths) {
+ auto p = cmCMakePath(path,
+ action == cmakePath ? cmCMakePath::native_format
+ : cmCMakePath::generic_format);
+ if (arguments.Normalize) {
+ p = p.Normal();
+ }
+ if (action == cmakePath) {
+ path = p.GenericString();
+ } else {
+ path = p.NativeString();
+ }
+ }
+
+ auto value = cmJoin(paths, action == cmakePath ? ";"_s : pathSep);
+ status.GetMakefile().AddDefinition(args[3], value);
+
+ return true;
+}
+
+bool HandleCompareCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ if (args.size() != 5) {
+ status.SetError("COMPARE must be called with four arguments.");
+ return false;
+ }
+
+ static std::map<cm::string_view,
+ std::function<bool(const cmCMakePath&,
+ const cmCMakePath&)>> const operators{
+ { "EQUAL"_s,
+ [](const cmCMakePath& path1, const cmCMakePath& path2) -> bool {
+ return path1 == path2;
+ } },
+ { "NOT_EQUAL"_s,
+ [](const cmCMakePath& path1, const cmCMakePath& path2) -> bool {
+ return path1 != path2;
+ } }
+ };
+
+ const auto op = operators.find(args[2]);
+ if (op == operators.end()) {
+ status.SetError(cmStrCat(
+ "COMPARE called with an unknown comparison operator: ", args[2], "."));
+ return false;
+ }
+
+ if (args[4].empty()) {
+ status.SetError("Invalid name for output variable.");
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ cmCMakePath path1(inputPath);
+ cmCMakePath path2(args[3]);
+ auto result = op->second(path1, path2);
+
+ status.GetMakefile().AddDefinitionBool(args[4], result);
+
+ return true;
+}
+
+bool HandleHasItemCommand(
+ std::vector<std::string> const& args, cmExecutionStatus& status,
+ const std::function<bool(const cmCMakePath&)>& has_item)
+{
+ if (args.size() != 3) {
+ status.SetError(
+ cmStrCat(args.front(), " must be called with two arguments."));
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ if (args[2].empty()) {
+ status.SetError("Invalid name for output variable.");
+ return false;
+ }
+
+ cmCMakePath path(inputPath);
+ auto result = has_item(path);
+
+ status.GetMakefile().AddDefinitionBool(args[2], result);
+
+ return true;
+}
+
+bool HandleHasRootNameCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ return HandleHasItemCommand(
+ args, status,
+ [](const cmCMakePath& path) -> bool { return path.HasRootName(); });
+}
+
+bool HandleHasRootDirectoryCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ return HandleHasItemCommand(
+ args, status,
+ [](const cmCMakePath& path) -> bool { return path.HasRootDirectory(); });
+}
+
+bool HandleHasRootPathCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ return HandleHasItemCommand(
+ args, status,
+ [](const cmCMakePath& path) -> bool { return path.HasRootPath(); });
+}
+
+bool HandleHasFilenameCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ return HandleHasItemCommand(
+ args, status,
+ [](const cmCMakePath& path) -> bool { return path.HasFileName(); });
+}
+
+bool HandleHasExtensionCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ return HandleHasItemCommand(
+ args, status,
+ [](const cmCMakePath& path) -> bool { return path.HasExtension(); });
+}
+
+bool HandleHasStemCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ return HandleHasItemCommand(
+ args, status,
+ [](const cmCMakePath& path) -> bool { return path.HasStem(); });
+}
+
+bool HandleHasRelativePathCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ return HandleHasItemCommand(
+ args, status,
+ [](const cmCMakePath& path) -> bool { return path.HasRelativePath(); });
+}
+
+bool HandleHasParentPathCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ return HandleHasItemCommand(
+ args, status,
+ [](const cmCMakePath& path) -> bool { return path.HasParentPath(); });
+}
+
+bool HandleIsAbsoluteCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ if (args.size() != 3) {
+ status.SetError("IS_ABSOLUTE must be called with two arguments.");
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ if (args[2].empty()) {
+ status.SetError("Invalid name for output variable.");
+ return false;
+ }
+
+ bool isAbsolute = cmCMakePath(inputPath).IsAbsolute();
+
+ status.GetMakefile().AddDefinitionBool(args[2], isAbsolute);
+
+ return true;
+}
+
+bool HandleIsRelativeCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ if (args.size() != 3) {
+ status.SetError("IS_RELATIVE must be called with two arguments.");
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ if (args[2].empty()) {
+ status.SetError("Invalid name for output variable.");
+ return false;
+ }
+
+ bool isRelative = cmCMakePath(inputPath).IsRelative();
+
+ status.GetMakefile().AddDefinitionBool(args[2], isRelative);
+
+ return true;
+}
+
+bool HandleIsPrefixCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ if (args.size() < 4 || args.size() > 5) {
+ status.SetError("IS_PREFIX must be called with three or four arguments.");
+ return false;
+ }
+
+ static NormalizeParser const parser;
+
+ const auto arguments = parser.Parse(args);
+
+ if (parser.GetInputs().size() != 2) {
+ status.SetError("IS_PREFIX called with unexpected arguments.");
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ const auto& input = parser.GetInputs().front();
+ const auto& output = parser.GetInputs().back();
+
+ if (output.empty()) {
+ status.SetError("Invalid name for output variable.");
+ return false;
+ }
+
+ bool isPrefix;
+ if (arguments.Normalize) {
+ isPrefix =
+ cmCMakePath(inputPath).Normal().IsPrefix(cmCMakePath(input).Normal());
+ } else {
+ isPrefix = cmCMakePath(inputPath).IsPrefix(input);
+ }
+
+ status.GetMakefile().AddDefinitionBool(output, isPrefix);
+
+ return true;
+}
+
+bool HandleHashCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ if (args.size() < 3 || args.size() > 4) {
+ status.SetError("HASH must be called with two or three arguments.");
+ return false;
+ }
+
+ static NormalizeParser const parser;
+
+ const auto arguments = parser.Parse(args);
+
+ if (parser.GetInputs().size() != 1) {
+ status.SetError("HASH called with unexpected arguments.");
+ return false;
+ }
+
+ std::string inputPath;
+ if (!getInputPath(args[1], status, inputPath)) {
+ return false;
+ }
+
+ const auto& output = parser.GetInputs().front();
+
+ if (output.empty()) {
+ status.SetError("Invalid name for output variable.");
+ return false;
+ }
+
+ auto hash = hash_value(arguments.Normalize ? cmCMakePath(inputPath).Normal()
+ : cmCMakePath(inputPath));
+
+ std::ostringstream out;
+ out << std::setbase(16) << hash;
+
+ status.GetMakefile().AddDefinition(output, out.str());
+
+ return true;
+}
+} // anonymous namespace
+
+bool cmCMakePathCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status)
+{
+ if (args.size() < 2) {
+ status.SetError("must be called with at least two arguments.");
+ return false;
+ }
+
+ static cmSubcommandTable const subcommand{
+ { "GET"_s, HandleGetCommand },
+ { "APPEND"_s, HandleAppendCommand },
+ { "CONCAT"_s, HandleConcatCommand },
+ { "REMOVE_FILENAME"_s, HandleRemoveFilenameCommand },
+ { "REPLACE_FILENAME"_s, HandleReplaceFilenameCommand },
+ { "REMOVE_EXTENSION"_s, HandleRemoveExtensionCommand },
+ { "REPLACE_EXTENSION"_s, HandleReplaceExtensionCommand },
+ { "NORMAL_PATH"_s, HandleNormalPathCommand },
+ { "RELATIVE_PATH"_s, HandleRelativePathCommand },
+ { "PROXIMATE_PATH"_s, HandleProximatePathCommand },
+ { "ABSOLUTE_PATH"_s, HandleAbsolutePathCommand },
+ { "CMAKE_PATH"_s, HandleCMakePathCommand },
+ { "NATIVE_PATH"_s, HandleNativePathCommand },
+ { "CONVERT"_s, HandleConvertCommand },
+ { "COMPARE"_s, HandleCompareCommand },
+ { "HAS_ROOT_NAME"_s, HandleHasRootNameCommand },
+ { "HAS_ROOT_DIRECTORY"_s, HandleHasRootDirectoryCommand },
+ { "HAS_ROOT_PATH"_s, HandleHasRootPathCommand },
+ { "HAS_FILENAME"_s, HandleHasFilenameCommand },
+ { "HAS_EXTENSION"_s, HandleHasExtensionCommand },
+ { "HAS_STEM"_s, HandleHasStemCommand },
+ { "HAS_RELATIVE_PATH"_s, HandleHasRelativePathCommand },
+ { "HAS_PARENT_PATH"_s, HandleHasParentPathCommand },
+ { "IS_ABSOLUTE"_s, HandleIsAbsoluteCommand },
+ { "IS_RELATIVE"_s, HandleIsRelativeCommand },
+ { "IS_PREFIX"_s, HandleIsPrefixCommand },
+ { "HASH"_s, HandleHashCommand }
+ };
+
+ return subcommand(args[0], args, status);
+}
diff --git a/Source/cmCMakePathCommand.h b/Source/cmCMakePathCommand.h
new file mode 100644
index 0000000..49e9380
--- /dev/null
+++ b/Source/cmCMakePathCommand.h
@@ -0,0 +1,14 @@
+/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
+ file Copyright.txt or https://cmake.org/licensing for details. */
+
+#pragma once
+
+#include "cmConfigure.h" // IWYU pragma: keep
+
+#include <string>
+#include <vector>
+
+class cmExecutionStatus;
+
+bool cmCMakePathCommand(std::vector<std::string> const& args,
+ cmExecutionStatus& status);
diff --git a/Source/cmCommands.cxx b/Source/cmCommands.cxx
index c94f128..37be542 100644
--- a/Source/cmCommands.cxx
+++ b/Source/cmCommands.cxx
@@ -17,6 +17,7 @@
#include "cmBreakCommand.h"
#include "cmBuildCommand.h"
#include "cmCMakeMinimumRequired.h"
+#include "cmCMakePathCommand.h"
#include "cmCMakePolicyCommand.h"
#include "cmCommand.h"
#include "cmConfigureFileCommand.h"
@@ -120,6 +121,7 @@ void GetScriptingCommands(cmState* state)
{
state->AddBuiltinCommand("break", cmBreakCommand);
state->AddBuiltinCommand("cmake_minimum_required", cmCMakeMinimumRequired);
+ state->AddBuiltinCommand("cmake_path", cmCMakePathCommand);
state->AddBuiltinCommand("cmake_policy", cmCMakePolicyCommand);
state->AddBuiltinCommand("configure_file", cmConfigureFileCommand);
state->AddBuiltinCommand("continue", cmContinueCommand);