From 5a63fd68286ce0e1c8c0dae47fcd1276797b7af2 Mon Sep 17 00:00:00 2001
From: Steffen Roeber <steffen.roeber@cariad.technology>
Date: Thu, 24 Aug 2023 17:00:02 +0200
Subject: [PATCH] filesystem patch

---
 cmake/FindFilesystem.cmake                    | 247 ++++++++++++++++++
 ...QtConfigureTimeExecutableCMakeLists.txt.in |   4 +
 cmake/QtExecutableHelpers.cmake               |   4 +
 src/corelib/CMakeLists.txt                    |   2 +
 src/corelib/configure.cmake                   |  22 --
 src/corelib/global/qconfig-bootstrapped.h     |   1 -
 src/corelib/io/qdir.h                         |  35 +--
 src/corelib/io/qfileinfo.h                    |  32 +--
 src/tools/syncqt/CMakeLists.txt               |   6 +-
 tests/auto/corelib/io/qdir/tst_qdir.cpp       |   4 -
 tests/auto/corelib/io/qfile/tst_qfile.cpp     |   4 -
 .../corelib/io/qfileinfo/tst_qfileinfo.cpp    |   6 -
 .../corelib/io/qdiriterator/CMakeLists.txt    |  11 +-
 .../qdiriterator/tst_bench_qdiriterator.cpp   |   6 -
 14 files changed, 283 insertions(+), 101 deletions(-)
 create mode 100644 cmake/FindFilesystem.cmake

diff --git a/cmake/FindFilesystem.cmake b/cmake/FindFilesystem.cmake
new file mode 100644
index 0000000000..a152e52293
--- /dev/null
+++ b/cmake/FindFilesystem.cmake
@@ -0,0 +1,247 @@
+# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+# file Copyright.txt or https://cmake.org/licensing for details.
+
+#[=======================================================================[.rst:
+
+FindFilesystem
+##############
+
+This module supports the C++17 standard library's filesystem utilities. Use the
+:imp-target:`std::filesystem` imported target to
+
+Options
+*******
+
+The ``COMPONENTS`` argument to this module supports the following values:
+
+.. find-component:: Experimental
+    :name: fs.Experimental
+
+    Allows the module to find the "experimental" Filesystem TS version of the
+    Filesystem library. This is the library that should be used with the
+    ``std::experimental::filesystem`` namespace.
+
+.. find-component:: Final
+    :name: fs.Final
+
+    Finds the final C++17 standard version of the filesystem library.
+
+If no components are provided, behaves as if the
+:find-component:`fs.Final` component was specified.
+
+If both :find-component:`fs.Experimental` and :find-component:`fs.Final` are
+provided, first looks for ``Final``, and falls back to ``Experimental`` in case
+of failure. If ``Final`` is found, :imp-target:`std::filesystem` and all
+:ref:`variables <fs.variables>` will refer to the ``Final`` version.
+
+
+Imported Targets
+****************
+
+.. imp-target:: std::filesystem
+
+    The ``std::filesystem`` imported target is defined when any requested
+    version of the C++ filesystem library has been found, whether it is
+    *Experimental* or *Final*.
+
+    If no version of the filesystem library is available, this target will not
+    be defined.
+
+    .. note::
+        This target has ``cxx_std_17`` as an ``INTERFACE``
+        :ref:`compile language standard feature <req-lang-standards>`. Linking
+        to this target will automatically enable C++17 if no later standard
+        version is already required on the linking target.
+
+
+.. _fs.variables:
+
+Variables
+*********
+
+.. variable:: CXX_FILESYSTEM_IS_EXPERIMENTAL
+
+    Set to ``TRUE`` when the :find-component:`fs.Experimental` version of C++
+    filesystem library was found, otherwise ``FALSE``.
+
+.. variable:: CXX_FILESYSTEM_HAVE_FS
+
+    Set to ``TRUE`` when a filesystem header was found.
+
+.. variable:: CXX_FILESYSTEM_HEADER
+
+    Set to either ``filesystem`` or ``experimental/filesystem`` depending on
+    whether :find-component:`fs.Final` or :find-component:`fs.Experimental` was
+    found.
+
+.. variable:: CXX_FILESYSTEM_NAMESPACE
+
+    Set to either ``std::filesystem`` or ``std::experimental::filesystem``
+    depending on whether :find-component:`fs.Final` or
+    :find-component:`fs.Experimental` was found.
+
+
+Examples
+********
+
+Using `find_package(Filesystem)` with no component arguments:
+
+.. code-block:: cmake
+
+    find_package(Filesystem REQUIRED)
+
+    add_executable(my-program main.cpp)
+    target_link_libraries(my-program PRIVATE std::filesystem)
+
+
+#]=======================================================================]
+
+
+if(TARGET std::filesystem)
+    # This module has already been processed. Don't do it again.
+    return()
+endif()
+
+cmake_minimum_required(VERSION 3.10)
+
+include(CMakePushCheckState)
+include(CheckIncludeFileCXX)
+
+# If we're not cross-compiling, try to run test executables.
+# Otherwise, assume that compile + link is a sufficient check.
+if(CMAKE_CROSSCOMPILING)
+    include(CheckCXXSourceCompiles)
+    macro(_cmcm_check_cxx_source code var)
+        check_cxx_source_compiles("${code}" ${var})
+    endmacro()
+else()
+    include(CheckCXXSourceRuns)
+    macro(_cmcm_check_cxx_source code var)
+        check_cxx_source_runs("${code}" ${var})
+    endmacro()
+endif()
+
+cmake_push_check_state()
+
+set(CMAKE_REQUIRED_QUIET ${Filesystem_FIND_QUIETLY})
+
+# All of our tests required C++17 or later
+set(CMAKE_CXX_STANDARD 17)
+
+# Normalize and check the component list we were given
+set(want_components ${Filesystem_FIND_COMPONENTS})
+if(Filesystem_FIND_COMPONENTS STREQUAL "")
+    set(want_components Final)
+endif()
+
+# Warn on any unrecognized components
+set(extra_components ${want_components})
+list(REMOVE_ITEM extra_components Final Experimental)
+foreach(component IN LISTS extra_components)
+    message(WARNING "Extraneous find_package component for Filesystem: ${component}")
+endforeach()
+
+# Detect which of Experimental and Final we should look for
+set(find_experimental TRUE)
+set(find_final TRUE)
+if(NOT "Final" IN_LIST want_components)
+    set(find_final FALSE)
+endif()
+if(NOT "Experimental" IN_LIST want_components)
+    set(find_experimental FALSE)
+endif()
+
+if(find_final)
+    check_include_file_cxx("filesystem" _CXX_FILESYSTEM_HAVE_HEADER)
+    mark_as_advanced(_CXX_FILESYSTEM_HAVE_HEADER)
+    if(_CXX_FILESYSTEM_HAVE_HEADER)
+        # We found the non-experimental header. Don't bother looking for the
+        # experimental one.
+        set(find_experimental FALSE)
+    endif()
+else()
+    set(_CXX_FILESYSTEM_HAVE_HEADER FALSE)
+endif()
+
+if(find_experimental)
+    check_include_file_cxx("experimental/filesystem" _CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER)
+    mark_as_advanced(_CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER)
+else()
+    set(_CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER FALSE)
+endif()
+
+if(_CXX_FILESYSTEM_HAVE_HEADER)
+    set(_have_fs TRUE)
+    set(_fs_header filesystem)
+    set(_fs_namespace std::filesystem)
+    set(_is_experimental FALSE)
+elseif(_CXX_FILESYSTEM_HAVE_EXPERIMENTAL_HEADER)
+    set(_have_fs TRUE)
+    set(_fs_header experimental/filesystem)
+    set(_fs_namespace std::experimental::filesystem)
+    set(_is_experimental TRUE)
+else()
+    set(_have_fs FALSE)
+endif()
+
+set(CXX_FILESYSTEM_HAVE_FS ${_have_fs} CACHE BOOL "TRUE if we have the C++ filesystem headers")
+set(CXX_FILESYSTEM_HEADER ${_fs_header} CACHE STRING "The header that should be included to obtain the filesystem APIs")
+set(CXX_FILESYSTEM_NAMESPACE ${_fs_namespace} CACHE STRING "The C++ namespace that contains the filesystem APIs")
+set(CXX_FILESYSTEM_IS_EXPERIMENTAL ${_is_experimental} CACHE BOOL "TRUE if the C++ filesystem library is the experimental version")
+
+set(_found FALSE)
+
+if(CXX_FILESYSTEM_HAVE_FS)
+    # We have some filesystem library available. Do link checks
+    string(CONFIGURE [[
+        #include <cstdlib>
+        #include <@CXX_FILESYSTEM_HEADER@>
+
+        int main() {
+            auto cwd = @CXX_FILESYSTEM_NAMESPACE@::current_path();
+            printf("%s", cwd.c_str());
+            return EXIT_SUCCESS;
+        }
+    ]] code @ONLY)
+
+    # Check a simple filesystem program without any linker flags
+    _cmcm_check_cxx_source("${code}" CXX_FILESYSTEM_NO_LINK_NEEDED)
+
+    set(can_link ${CXX_FILESYSTEM_NO_LINK_NEEDED})
+
+    if(NOT CXX_FILESYSTEM_NO_LINK_NEEDED)
+        set(prev_libraries ${CMAKE_REQUIRED_LIBRARIES})
+        # Add the libstdc++ flag
+        set(CMAKE_REQUIRED_LIBRARIES ${prev_libraries} -lstdc++fs)
+        _cmcm_check_cxx_source("${code}" CXX_FILESYSTEM_STDCPPFS_NEEDED)
+        set(can_link ${CXX_FILESYSTEM_STDCPPFS_NEEDED})
+        if(NOT CXX_FILESYSTEM_STDCPPFS_NEEDED)
+            # Try the libc++ flag
+            set(CMAKE_REQUIRED_LIBRARIES ${prev_libraries} -lc++fs)
+            _cmcm_check_cxx_source("${code}" CXX_FILESYSTEM_CPPFS_NEEDED)
+            set(can_link ${CXX_FILESYSTEM_CPPFS_NEEDED})
+        endif()
+    endif()
+
+    if(can_link)
+        add_library(std::filesystem INTERFACE IMPORTED)
+        set_property(TARGET std::filesystem APPEND PROPERTY INTERFACE_COMPILE_FEATURES cxx_std_17)
+        set(_found TRUE)
+
+        if(CXX_FILESYSTEM_NO_LINK_NEEDED)
+            # Nothing to add...
+        elseif(CXX_FILESYSTEM_STDCPPFS_NEEDED)
+            set_property(TARGET std::filesystem APPEND PROPERTY INTERFACE_LINK_LIBRARIES -lstdc++fs)
+        elseif(CXX_FILESYSTEM_CPPFS_NEEDED)
+            set_property(TARGET std::filesystem APPEND PROPERTY INTERFACE_LINK_LIBRARIES -lc++fs)
+        endif()
+    endif()
+endif()
+
+cmake_pop_check_state()
+
+set(Filesystem_FOUND ${_found} CACHE BOOL "TRUE if we can run a program using std::filesystem" FORCE)
+
+if(Filesystem_FIND_REQUIRED AND NOT Filesystem_FOUND)
+    message(FATAL_ERROR "Cannot run simple program using std::filesystem")
+endif()
diff --git a/cmake/QtConfigureTimeExecutableCMakeLists.txt.in b/cmake/QtConfigureTimeExecutableCMakeLists.txt.in
index a36299706c..77b24f9947 100644
--- a/cmake/QtConfigureTimeExecutableCMakeLists.txt.in
+++ b/cmake/QtConfigureTimeExecutableCMakeLists.txt.in
@@ -7,6 +9,7 @@ set(defines @defines@)
 set(compile_options @compile_options@)
 set(link_options @link_options@)
 set(output_directory @output_directory@)
+set(libraries @libraries@)
 
 foreach(package IN LISTS packages)
     find_package(${package} REQUIRED)
@@ -18,6 +21,7 @@ set_target_properties(@configure_time_target@ PROPERTIES
     RUNTIME_OUTPUT_DIRECTORY "${output_directory}"
 )
 
+target_link_libraries(@configure_time_target@ PRIVATE @libraries@)
 target_compile_options(@configure_time_target@ PRIVATE ${compile_options})
 target_compile_definitions(@configure_time_target@ PRIVATE ${defines})
 target_link_options(@configure_time_target@ PRIVATE ${link_options})
diff --git a/cmake/QtExecutableHelpers.cmake b/cmake/QtExecutableHelpers.cmake
index 8ebcadf30e..e4b93bb6cb 100644
--- a/cmake/QtExecutableHelpers.cmake
+++ b/cmake/QtExecutableHelpers.cmake
@@ -339,6 +339,9 @@ endfunction()
 #     CMAKE_FLAGS
 #         specify flags of the form -DVAR:TYPE=VALUE to be passed to the cmake command-line used to
 #         drive the test build.
+#     LIBRARIES
+#         The list of libraries that the target requires. Note all libraries needs to be accessible
+#         in the external project scope.
 # Options:
 #     WIN32
 #         reflects the corresponding add_executable argument.
@@ -360,6 +363,7 @@ function(qt_internal_add_configure_time_executable target)
         COMPILE_OPTIONS
         LINK_OPTIONS
         SOURCES
+        LIBRARIES
         CMAKE_FLAGS
     )
     set(option_args WIN32 MACOSX_BUNDLE NO_INSTALL)
diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt
index d3f137847d..bc20e651a6 100644
--- a/src/corelib/CMakeLists.txt
+++ b/src/corelib/CMakeLists.txt
@@ -5,6 +5,7 @@ qt_find_package(Threads PROVIDED_TARGETS Threads::Threads)
 qt_find_package(WrapPCRE2 PROVIDED_TARGETS WrapPCRE2::WrapPCRE2)
 qt_find_package(WrapZLIB PROVIDED_TARGETS WrapZLIB::WrapZLIB)
 
+qt_find_package(Filesystem REQUIRED PROVIDED_TARGETS std::filesystem)
 
 # compute the reverse relative path from QtCoreConfigExtras to the install prefix
 # this is used in QtCoreConfigExtras to make its install paths relocatable
@@ -327,6 +328,7 @@ qt_internal_add_module(Core
     LIBRARIES
         Qt::GlobalConfigPrivate
         WrapZLIB::WrapZLIB
+        std::filesystem
     PRECOMPILED_HEADER
         "global/qt_pch.h"
     GENERATE_CPP_EXPORTS
diff --git a/src/corelib/configure.cmake b/src/corelib/configure.cmake
index 7d52f48f57..0857048a1e 100644
--- a/src/corelib/configure.cmake
+++ b/src/corelib/configure.cmake
@@ -163,24 +163,6 @@ std::mt19937 mt(0);
 }
 ")
 
-# cxx17_filesystem
-qt_config_compile_test(cxx17_filesystem
-    LABEL "C++17 <filesystem>"
-    CODE
-"#include <filesystem>
-
-int main(void)
-{
-    /* BEGIN TEST: */
-std::filesystem::copy(
-    std::filesystem::path(\"./file\"),
-    std::filesystem::path(\"./other\"));
-    /* END TEST: */
-    return 0;
-}
-"
-)
-
 # dladdr
 qt_config_compile_test(dladdr
     LABEL "dladdr"
@@ -545,10 +527,6 @@ qt_feature("cxx11_future" PUBLIC
     LABEL "C++11 <future>"
     CONDITION TEST_cxx11_future
 )
-qt_feature("cxx17_filesystem" PUBLIC
-    LABEL "C++17 <filesystem>"
-    CONDITION TEST_cxx17_filesystem
-)
 qt_feature("dladdr" PRIVATE
     LABEL "dladdr"
     CONDITION QT_FEATURE_dlopen AND TEST_dladdr
diff --git a/src/corelib/global/qconfig-bootstrapped.h b/src/corelib/global/qconfig-bootstrapped.h
index 78d622f5e9..87c5de1c88 100644
--- a/src/corelib/global/qconfig-bootstrapped.h
+++ b/src/corelib/global/qconfig-bootstrapped.h
@@ -44,7 +44,6 @@
 #define QT_FEATURE_cborstreamwriter 1
 #define QT_CRYPTOGRAPHICHASH_ONLY_SHA1
 #define QT_FEATURE_cxx11_random (__has_include(<random>) ? 1 : -1)
-#define QT_FEATURE_cxx17_filesystem -1
 #define QT_NO_DATASTREAM
 #define QT_FEATURE_datestring 1
 #define QT_FEATURE_datetimeparser -1
diff --git a/src/corelib/io/qdir.h b/src/corelib/io/qdir.h
index 7d5e940e84..7a299d07cb 100644
--- a/src/corelib/io/qdir.h
+++ b/src/corelib/io/qdir.h
@@ -66,22 +66,15 @@ public:
     QDir(const QString &path = QString());
     QDir(const QString &path, const QString &nameFilter,
          SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries);
-#ifdef Q_QDOC
-    QDir(const std::filesystem::path &path);
+    Q_WEAK_OVERLOAD
+    QDir(const std::filesystem::path &path)
+        : QDir(QtPrivate::fromFilesystemPath(path)) {}
+    Q_WEAK_OVERLOAD
     QDir(const std::filesystem::path &path, const QString &nameFilter,
-         SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries);
-#elif QT_CONFIG(cxx17_filesystem)
-    template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
-    QDir(const T &path) : QDir(QtPrivate::fromFilesystemPath(path))
-    {
-    }
-    template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
-    QDir(const T &path, const QString &nameFilter,
          SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries)
-         : QDir(QtPrivate::fromFilesystemPath(path), nameFilter, sort, filter)
+        : QDir(QtPrivate::fromFilesystemPath(path), nameFilter, sort, filter)
     {
     }
-#endif // QT_CONFIG(cxx17_filesystem)
     ~QDir();
 
     QDir &operator=(const QDir &);
@@ -91,39 +84,29 @@ public:
     { d_ptr.swap(other.d_ptr); }
 
     void setPath(const QString &path);
-#ifdef Q_QDOC
-    void setPath(const std::filesystem::path &path);
-#elif QT_CONFIG(cxx17_filesystem)
-    template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
-    void setPath(const T &path)
+    Q_WEAK_OVERLOAD
+    void setPath(const std::filesystem::path &path)
     {
         setPath(QtPrivate::fromFilesystemPath(path));
     }
-#endif // QT_CONFIG(cxx17_filesystem)
     QString path() const;
     QString absolutePath() const;
     QString canonicalPath() const;
-#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
     std::filesystem::path filesystemPath() const
     { return QtPrivate::toFilesystemPath(path()); }
     std::filesystem::path filesystemAbsolutePath() const
     { return QtPrivate::toFilesystemPath(absolutePath()); }
     std::filesystem::path filesystemCanonicalPath() const
     { return QtPrivate::toFilesystemPath(canonicalPath()); }
-#endif // QT_CONFIG(cxx17_filesystem)
 
 #ifndef QT_BOOTSTRAPPED
     static void setSearchPaths(const QString &prefix, const QStringList &searchPaths);
     static void addSearchPath(const QString &prefix, const QString &path);
-#ifdef Q_QDOC
-    static void addSearchPath(const QString &prefix, const std::filesystem::path &path);
-#elif QT_CONFIG(cxx17_filesystem)
-    template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
-    static void addSearchPath(const QString &prefix, const T &path)
+    Q_WEAK_OVERLOAD
+    static void addSearchPath(const QString &prefix, const std::filesystem::path &path)
     {
         addSearchPath(prefix, QtPrivate::fromFilesystemPath(path));
     }
-#endif // QT_CONFIG(cxx17_filesystem)
     static QStringList searchPaths(const QString &prefix);
 #endif // QT_BOOTSTRAPPED
 
diff --git a/src/corelib/io/qfileinfo.h b/src/corelib/io/qfileinfo.h
index d0ec231f89..beaf74268b 100644
--- a/src/corelib/io/qfileinfo.h
+++ b/src/corelib/io/qfileinfo.h
@@ -34,19 +34,14 @@ public:
     QFILEINFO_MAYBE_EXPLICIT QFileInfo(const QFileDevice &file);
     QFILEINFO_MAYBE_EXPLICIT QFileInfo(const QDir &dir, const QString &file);
     QFileInfo(const QFileInfo &fileinfo);
-#ifdef Q_QDOC
-    QFileInfo(const std::filesystem::path &file);
-    QFileInfo(const QDir &dir, const std::filesystem::path &file);
-#elif QT_CONFIG(cxx17_filesystem)
-    template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
-    QFILEINFO_MAYBE_EXPLICIT QFileInfo(const T &file) : QFileInfo(QtPrivate::fromFilesystemPath(file)) { }
-
-    template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
-    QFILEINFO_MAYBE_EXPLICIT QFileInfo(const QDir &dir, const T &file) : QFileInfo(dir, QtPrivate::fromFilesystemPath(file))
+    Q_WEAK_OVERLOAD
+    QFILEINFO_MAYBE_EXPLICIT QFileInfo(const std::filesystem::path &file)
+        : QFileInfo(QtPrivate::fromFilesystemPath(file)) {}
+    Q_WEAK_OVERLOAD
+    QFILEINFO_MAYBE_EXPLICIT QFileInfo(const QDir &dir, const std::filesystem::path &file)
+        : QFileInfo(dir, QtPrivate::fromFilesystemPath(file))
     {
     }
-#endif // QT_CONFIG(cxx17_filesystem)
-
 #undef QFILEINFO_MAYBE_EXPLICIT
 
     ~QFileInfo();
@@ -63,12 +58,9 @@ public:
     void setFile(const QString &file);
     void setFile(const QFileDevice &file);
     void setFile(const QDir &dir, const QString &file);
-#ifdef Q_QDOC
-    void setFile(const std::filesystem::path &file);
-#elif QT_CONFIG(cxx17_filesystem)
-    template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
-    void setFile(const T &file) { setFile(QtPrivate::fromFilesystemPath(file)); }
-#endif // QT_CONFIG(cxx17_filesystem)
+    Q_WEAK_OVERLOAD
+    void setFile(const std::filesystem::path &file)
+    { setFile(QtPrivate::fromFilesystemPath(file)); }
 
     bool exists() const;
     static bool exists(const QString &file);
@@ -77,14 +69,12 @@ public:
     QString filePath() const;
     QString absoluteFilePath() const;
     QString canonicalFilePath() const;
-#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
     std::filesystem::path filesystemFilePath() const
     { return QtPrivate::toFilesystemPath(filePath()); }
     std::filesystem::path filesystemAbsoluteFilePath() const
     { return QtPrivate::toFilesystemPath(absoluteFilePath()); }
     std::filesystem::path filesystemCanonicalFilePath() const
     { return QtPrivate::toFilesystemPath(canonicalFilePath()); }
-#endif // QT_CONFIG(cxx17_filesystem)
     QString fileName() const;
     QString baseName() const;
     QString completeBaseName() const;
@@ -95,13 +85,11 @@ public:
     QString path() const;
     QString absolutePath() const;
     QString canonicalPath() const;
-#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
     std::filesystem::path filesystemPath() const { return QtPrivate::toFilesystemPath(path()); }
     std::filesystem::path filesystemAbsolutePath() const
     { return QtPrivate::toFilesystemPath(absolutePath()); }
     std::filesystem::path filesystemCanonicalPath() const
     { return QtPrivate::toFilesystemPath(canonicalPath()); }
-#endif // QT_CONFIG(cxx17_filesystem)
     QDir dir() const;
     QDir absoluteDir() const;
 
@@ -128,13 +116,11 @@ public:
     QString symLinkTarget() const;
     QString junctionTarget() const;
 
-#if QT_CONFIG(cxx17_filesystem) || defined(Q_QDOC)
     std::filesystem::path filesystemSymLinkTarget() const
     { return QtPrivate::toFilesystemPath(symLinkTarget()); }
 
     std::filesystem::path filesystemJunctionTarget() const
     { return QtPrivate::toFilesystemPath(junctionTarget()); }
-#endif // QT_CONFIG(cxx17_filesystem)
 
     QString owner() const;
     uint ownerId() const;
diff --git a/src/tools/syncqt/CMakeLists.txt b/src/tools/syncqt/CMakeLists.txt
index 3740b1bd4d..c04235df96 100644
--- a/src/tools/syncqt/CMakeLists.txt
+++ b/src/tools/syncqt/CMakeLists.txt
@@ -24,14 +26,16 @@ qt_get_tool_target_name(target_name syncqt)
 # To re-build syncqt use 'syncqt_build' target.
 qt_internal_add_configure_time_tool(${target_name}
     DEFINES ${compile_definitions}
-    COMPILE_OPTIONS ${optimize_full_flags}
     TOOLS_TARGET Core
     INSTALL_DIRECTORY "${INSTALL_LIBEXECDIR}"
+    PACKAGES Filesystem
+    LIBRARIES std::filesystem
     CMAKE_FLAGS
         -DCMAKE_CXX_STANDARD_REQUIRED:BOOL=TRUE
         -DCMAKE_CXX_STANDARD:STRING=${CMAKE_CXX_STANDARD}
         # std::filesystem API is only available in macOS 10.15+
         -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=10.15
+        -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}
         "${osx_architectures}"
     SOURCES
         "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp"
diff --git a/tests/auto/corelib/io/qdir/tst_qdir.cpp b/tests/auto/corelib/io/qdir/tst_qdir.cpp
index 65a7daaf7d..3eb6e83ce3 100644
--- a/tests/auto/corelib/io/qdir/tst_qdir.cpp
+++ b/tests/auto/corelib/io/qdir/tst_qdir.cpp
@@ -2448,7 +2448,6 @@ void tst_QDir::nonEmptyDir()
 
 void tst_QDir::stdfilesystem()
 {
-#if QT_CONFIG(cxx17_filesystem)
     namespace fs = std::filesystem;
     fs::path path(".");
     QDir dir(path);
@@ -2480,9 +2479,6 @@ void tst_QDir::stdfilesystem()
         QCOMPARE(filteredDir.nameFilters().size(), 1);
         QCOMPARE(filteredDir.nameFilters().first(), "subdir*");
     }
-#else
-    QSKIP("Not supported");
-#endif
 }
 
 QTEST_MAIN(tst_QDir)
diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp
index a7fbef2485..c648299502 100644
--- a/tests/auto/corelib/io/qfile/tst_qfile.cpp
+++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp
@@ -3908,7 +3908,6 @@ void tst_QFile::moveToTrash()
 
 void tst_QFile::stdfilesystem()
 {
-#if QT_CONFIG(cxx17_filesystem)
     namespace fs = std::filesystem;
     auto toFSPath = [](const QFile &file) { return fs::path(file.fileName().toStdU16String()); };
     fs::path path { "./path" };
@@ -3968,9 +3967,6 @@ void tst_QFile::stdfilesystem()
     path = "test-exists";
     fs::create_directory(path);
     QVERIFY(QFile::exists(path) == fs::exists(path));
-#else
-    QSKIP("Not supported");
-#endif
 }
 
 QTEST_MAIN(tst_QFile)
diff --git a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp
index d5d584c8eb..d41a3e40d6 100644
--- a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp
+++ b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp
@@ -2301,8 +2301,6 @@ void tst_QFileInfo::nonExistingFile()
 
 void tst_QFileInfo::stdfilesystem()
 {
-#if QT_CONFIG(cxx17_filesystem)
-
     namespace fs = std::filesystem;
 
     // Verify constructing with fs::path leads to valid objects
@@ -2385,10 +2383,6 @@ void tst_QFileInfo::stdfilesystem()
         COMPARE_PATHS(info.filesystemSymLinkTarget(), info.symLinkTarget());
 #undef COMPARE_PATHS
     }
-
-#else
-    QSKIP("Not supported");
-#endif
 }
 
 QTEST_MAIN(tst_QFileInfo)
diff --git a/tests/benchmarks/corelib/io/qdiriterator/CMakeLists.txt b/tests/benchmarks/corelib/io/qdiriterator/CMakeLists.txt
index 6a4579d35d..c2b94e7833 100644
--- a/tests/benchmarks/corelib/io/qdiriterator/CMakeLists.txt
+++ b/tests/benchmarks/corelib/io/qdiriterator/CMakeLists.txt
@@ -5,18 +5,13 @@
 ## tst_bench_qdiriterator Binary:
 #####################################################################
 
+find_package(Filesystem REQUIRED)
+
 qt_internal_add_benchmark(tst_bench_qdiriterator
     SOURCES
         tst_bench_qdiriterator.cpp
         qfilesystemiterator.cpp qfilesystemiterator.h
     LIBRARIES
         Qt::Test
-)
-
-## Scopes:
-#####################################################################
-
-qt_internal_extend_target(tst_bench_qdiriterator CONDITION (QT_FEATURE_cxx17_filesystem) AND (GCC AND (QMAKE_GCC_MAJOR_VERSION LESS 9))
-    LINK_OPTIONS
-        "-lstdc++fs"
+        std::filesystem
 )
diff --git a/tests/benchmarks/corelib/io/qdiriterator/tst_bench_qdiriterator.cpp b/tests/benchmarks/corelib/io/qdiriterator/tst_bench_qdiriterator.cpp
index c1826e99e4..e791d956e2 100644
--- a/tests/benchmarks/corelib/io/qdiriterator/tst_bench_qdiriterator.cpp
+++ b/tests/benchmarks/corelib/io/qdiriterator/tst_bench_qdiriterator.cpp
@@ -19,9 +19,7 @@
 
 #include "qfilesystemiterator.h"
 
-#if QT_CONFIG(cxx17_filesystem)
 #include <filesystem>
-#endif
 
 class tst_QDirIterator : public QObject
 {
@@ -212,7 +210,6 @@ void tst_QDirIterator::fsiterator()
 
 void tst_QDirIterator::stdRecursiveDirectoryIterator()
 {
-#if QT_CONFIG(cxx17_filesystem)
     QFETCH(QByteArray, dirpath);
 
     int count = 0;
@@ -227,9 +224,6 @@ void tst_QDirIterator::stdRecursiveDirectoryIterator()
         count = c;
     }
     qDebug() << count;
-#else
-    QSKIP("Not supported.");
-#endif
 }
 
 QTEST_MAIN(tst_QDirIterator)
-- 
2.37.2.windows.2

