[libcxx-commits] [libcxx] [libcxx] Fix freestanding build with filesystem disabled on Windows (PR #164602)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 22 04:00:35 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: miyav (moleium)

<details>
<summary>Changes</summary>

Building libc++ for freestanding targets on Windows with LIBCXX_ENABLE_FILESYSTEM=OFF fails due to compilation errors.

The root cause is twofold:
1. path.cpp and filesystem_error.cpp are compiled even when filesystem is disabled. These files depend on wide character and localization support, which freestanding builds typically don't provide.
2. print.cpp transitively depends on filesystem headers for Windows error reporting, pulling in <filesystem> even when the feature is disabled.

This patch fixes both problems by:
- Guarding filesystem sources in src/CMakeLists.txt with LIBCXX_ENABLE_FILESYSTEM
- Making print.cpp call GetLastError() directly when filesystem support is unavailable

Fixes #<!-- -->164074

---
Full diff: https://github.com/llvm/llvm-project/pull/164602.diff


2 Files Affected:

- (modified) libcxx/src/CMakeLists.txt (+3-3) 
- (modified) libcxx/src/print.cpp (+4-2) 


``````````diff
diff --git a/libcxx/src/CMakeLists.txt b/libcxx/src/CMakeLists.txt
index f59fe0e08fccb..78b88a9e8c912 100644
--- a/libcxx/src/CMakeLists.txt
+++ b/libcxx/src/CMakeLists.txt
@@ -9,10 +9,7 @@ set(LIBCXX_SOURCES
   error_category.cpp
   exception.cpp
   expected.cpp
-  filesystem/filesystem_clock.cpp
-  filesystem/filesystem_error.cpp
   filesystem/path_parser.h
-  filesystem/path.cpp
   functional.cpp
   hash.cpp
   include/apple_availability.h
@@ -117,6 +114,9 @@ endif()
 
 if (LIBCXX_ENABLE_FILESYSTEM)
   list(APPEND LIBCXX_SOURCES
+    filesystem/filesystem_clock.cpp
+    filesystem/filesystem_error.cpp
+    filesystem/path.cpp
     filesystem/directory_entry.cpp
     filesystem/directory_iterator.cpp
     filesystem/file_descriptor.h
diff --git a/libcxx/src/print.cpp b/libcxx/src/print.cpp
index 3f2baa6dcc60b..1b6a800e1a003 100644
--- a/libcxx/src/print.cpp
+++ b/libcxx/src/print.cpp
@@ -13,8 +13,6 @@
 
 #include <__system_error/system_error.h>
 
-#include "filesystem/error.h"
-
 #if defined(_LIBCPP_WIN32API)
 #  define WIN32_LEAN_AND_MEAN
 #  define NOMINMAX
@@ -51,7 +49,11 @@ __write_to_windows_console([[maybe_unused]] FILE* __stream, [[maybe_unused]] wst
                     __view.size(),
                     nullptr,
                     nullptr) == 0) {
+#    if _LIBCPP_HAS_FILESYSTEM
     std::__throw_system_error(filesystem::detail::get_last_error(), "failed to write formatted output");
+#    else
+    std::__throw_system_error(error_code(GetLastError(), system_category()), "failed to write formatted output");
+#    endif
   }
 }
 #  endif // _LIBCPP_HAS_WIDE_CHARACTERS

``````````

</details>


https://github.com/llvm/llvm-project/pull/164602


More information about the libcxx-commits mailing list