Skip to content

Commit af8c49d

Browse files
committed
[libc++] Introduce a setting to remove fstream from the library
This allows porting the library to platforms that are able to support <iostream> but that do not have a notion of a filesystem, and where it hence doesn't make sense to support std::fstream (and never will). Also, remove reliance on <fstream> in various tests that didn't actually need it. Differential Revision: https://reviews.llvm.org/D138327
1 parent 242a9cf commit af8c49d

File tree

30 files changed

+149
-120
lines changed

30 files changed

+149
-120
lines changed

libcxx/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ option(LIBCXX_ENABLE_LOCALIZATION
8484
the C locale API (e.g. embedded). When localization is not supported,
8585
several parts of the library will be disabled: <iostream>, <regex>, <locale>
8686
will be completely unusable, and other parts may be only partly available." ON)
87+
option(LIBCXX_ENABLE_FSTREAM
88+
"Whether to include support for <fstream>." ON) # TODO: Consider rolling that into LIBCXX_ENABLE_FILESYSTEM
8789
option(LIBCXX_ENABLE_UNICODE
8890
"Whether to include support for Unicode in the library. Disabling Unicode can
8991
be useful when porting to platforms that don't support UTF-8 encoding (e.g.
@@ -860,6 +862,7 @@ config_define_if(LIBCXX_ENABLE_PARALLEL_ALGORITHMS _LIBCPP_HAS_PARALLEL_ALGORITH
860862
config_define_if_not(LIBCXX_ENABLE_FILESYSTEM _LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
861863
config_define_if_not(LIBCXX_ENABLE_RANDOM_DEVICE _LIBCPP_HAS_NO_RANDOM_DEVICE)
862864
config_define_if_not(LIBCXX_ENABLE_LOCALIZATION _LIBCPP_HAS_NO_LOCALIZATION)
865+
config_define_if_not(LIBCXX_ENABLE_FSTREAM _LIBCPP_HAS_NO_FSTREAM)
863866
config_define_if_not(LIBCXX_ENABLE_UNICODE _LIBCPP_HAS_NO_UNICODE)
864867
config_define_if_not(LIBCXX_ENABLE_WIDE_CHARACTERS _LIBCPP_HAS_NO_WIDE_CHARACTERS)
865868
config_define_if_not(LIBCXX_ENABLE_VENDOR_AVAILABILITY_ANNOTATIONS _LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
set(LIBCXX_ENABLE_FSTREAM OFF CACHE BOOL "")

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ set(files
838838
wctype.h
839839
)
840840

841-
foreach(feature LIBCXX_ENABLE_FILESYSTEM LIBCXX_ENABLE_LOCALIZATION LIBCXX_ENABLE_THREADS LIBCXX_ENABLE_WIDE_CHARACTERS)
841+
foreach(feature LIBCXX_ENABLE_FILESYSTEM LIBCXX_ENABLE_LOCALIZATION LIBCXX_ENABLE_FSTREAM LIBCXX_ENABLE_THREADS LIBCXX_ENABLE_WIDE_CHARACTERS)
842842
if (NOT ${${feature}})
843843
set(requires_${feature} "requires LIBCXX_CONFIGURED_WITHOUT_SUPPORT_FOR_THIS_HEADER")
844844
endif()

libcxx/include/__config_site.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#cmakedefine _LIBCPP_HAS_PARALLEL_ALGORITHMS
2929
#cmakedefine _LIBCPP_HAS_NO_RANDOM_DEVICE
3030
#cmakedefine _LIBCPP_HAS_NO_LOCALIZATION
31+
#cmakedefine _LIBCPP_HAS_NO_FSTREAM
3132
#cmakedefine _LIBCPP_HAS_NO_WIDE_CHARACTERS
3233
#cmakedefine01 _LIBCPP_ENABLE_ASSERTIONS_DEFAULT
3334
#cmakedefine _LIBCPP_ENABLE_DEBUG_MODE

libcxx/include/fstream

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ _LIBCPP_PUSH_MACROS
210210
# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
211211
#endif
212212

213+
#if !defined(_LIBCPP_HAS_NO_FSTREAM)
214+
213215
_LIBCPP_BEGIN_NAMESPACE_STD
214216

215217
template <class _CharT, class _Traits>
@@ -1742,6 +1744,8 @@ extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
17421744

17431745
_LIBCPP_END_NAMESPACE_STD
17441746

1747+
#endif // _LIBCPP_HAS_NO_FSTREAM
1748+
17451749
_LIBCPP_POP_MACROS
17461750

17471751
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20

libcxx/include/module.modulemap.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ module std [system] {
855855
}
856856
module fstream {
857857
@requires_LIBCXX_ENABLE_LOCALIZATION@
858+
@requires_LIBCXX_ENABLE_FSTREAM@
858859
header "fstream"
859860
export *
860861
}

libcxx/src/ios.instantiations.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_stringbuf<char>;
3636
template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_stringstream<char>;
3737
template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ostringstream<char>;
3838
template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_istringstream<char>;
39+
40+
#ifndef _LIBCPP_HAS_NO_FSTREAM
3941
template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ifstream<char>;
4042
template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_ofstream<char>;
4143
template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS basic_filebuf<char>;
44+
#endif
4245

4346
// Add more here if needed...
4447

libcxx/test/libcxx/assertions/headers_declare_verbose_abort.sh.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ int main(int, char**) { return 0; }
325325
#endif
326326

327327
// RUN: %{build} -DTEST_51
328-
#if defined(TEST_51) && !defined(_LIBCPP_HAS_NO_LOCALIZATION)
328+
#if defined(TEST_51) && !defined(_LIBCPP_HAS_NO_LOCALIZATION) && !defined(_LIBCPP_HAS_NO_FSTREAM)
329329
# include <fstream>
330330
using HandlerType = decltype(std::__libcpp_verbose_abort);
331331
#endif

libcxx/test/libcxx/clang_tidy.sh.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ END-SCRIPT
9898
#include <float.h>
9999
#include <format>
100100
#include <forward_list>
101-
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
101+
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION) && !defined(_LIBCPP_HAS_NO_FSTREAM)
102102
# include <fstream>
103103
#endif
104104
#include <functional>

libcxx/test/libcxx/double_include.sh.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ END-SCRIPT
100100
#include <float.h>
101101
#include <format>
102102
#include <forward_list>
103-
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
103+
#if !defined(_LIBCPP_HAS_NO_LOCALIZATION) && !defined(_LIBCPP_HAS_NO_FSTREAM)
104104
# include <fstream>
105105
#endif
106106
#include <functional>

0 commit comments

Comments
 (0)