Allow disabling of filesystem library.

Summary: Filesystem doesn't work on Windows, so we need a mechanism to turn it off for the time being.

Reviewers: ldionne, serge-sans-paille, EricWF

Reviewed By: EricWF

Subscribers: mstorsjo, mgorny, christof, jdoerfert, libcxx-commits

Differential Revision: https://reviews.llvm.org/D59619

llvm-svn: 356633
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: f1d87f8b4c430d72e7ab07e13508336a7841c3e6
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d9ef20a..389d36f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -73,6 +73,12 @@
 option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON)
 option(LIBCXX_ENABLE_STATIC "Build libc++ as a static library." ON)
 option(LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY "Build libc++experimental.a" ON)
+set(ENABLE_FILESYSTEM_DEFAULT ON)
+if (WIN32)
+  set(ENABLE_FILESYSTEM_DEFAULT OFF)
+endif()
+option(LIBCXX_ENABLE_FILESYSTEM "Build filesystem as part of the main libc++ library"
+    ${ENABLE_FILESYSTEM_DEFAULT})
 option(LIBCXX_INCLUDE_TESTS "Build the libc++ tests." ${LLVM_INCLUDE_TESTS})
 
 # Benchmark options -----------------------------------------------------------
diff --git a/docs/BuildingLibcxx.rst b/docs/BuildingLibcxx.rst
index cb00b89..fb22a1d 100644
--- a/docs/BuildingLibcxx.rst
+++ b/docs/BuildingLibcxx.rst
@@ -206,6 +206,13 @@
   libraries that may be used in with other shared libraries that use different
   C++ library. We want to avoid avoid exporting any libc++ symbols in that case.
 
+.. option:: LIBCXX_ENABLE_FILESYSTEM:BOOL
+
+   **Default**: ``ON`` except on Windows.
+
+   This option can be used to enable or disable the filesystem components on
+   platforms that may not support them. For example on Windows.
+
 .. _libc++experimental options:
 
 libc++experimental Specific Options
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index e73e459..701f66e 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -3,8 +3,6 @@
 # Get sources
 # FIXME: Don't use glob here
 file(GLOB LIBCXX_SOURCES ../src/*.cpp)
-list(APPEND LIBCXX_SOURCES ../src/filesystem/operations.cpp
-                           ../src/filesystem/directory_iterator.cpp)
 if(WIN32)
   file(GLOB LIBCXX_WIN32_SOURCES ../src/support/win32/*.cpp)
   list(APPEND LIBCXX_SOURCES ${LIBCXX_WIN32_SOURCES})
@@ -13,12 +11,16 @@
   list(APPEND LIBCXX_SOURCES ${LIBCXX_SOLARIS_SOURCES})
 endif()
 
-# Filesystem uses __int128_t, which requires a definition of __muloi4 when
-# compiled with UBSAN. This definition is not provided by libgcc_s, but is
-# provided by compiler-rt. So we need to disable it to avoid having multiple
-# definitions. See filesystem/int128_builtins.cpp.
-if (NOT LIBCXX_USE_COMPILER_RT)
-  list(APPEND LIBCXX_SOURCES ../src/filesystem/int128_builtins.cpp)
+if (LIBCXX_ENABLE_FILESYSTEM)
+  list(APPEND LIBCXX_SOURCES ../src/filesystem/operations.cpp
+                             ../src/filesystem/directory_iterator.cpp)
+  # Filesystem uses __int128_t, which requires a definition of __muloi4 when
+  # compiled with UBSAN. This definition is not provided by libgcc_s, but is
+  # provided by compiler-rt. So we need to disable it to avoid having multiple
+  # definitions. See filesystem/int128_builtins.cpp.
+  if (NOT LIBCXX_USE_COMPILER_RT)
+    list(APPEND LIBCXX_SOURCES ../src/filesystem/int128_builtins.cpp)
+  endif()
 endif()
 
 # Add all the headers to the project for IDEs.
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 6ea7135..408ab62 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -30,6 +30,7 @@
 pythonize_bool(LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY)
 pythonize_bool(LIBCXX_ENABLE_RTTI)
 pythonize_bool(LIBCXX_ENABLE_SHARED)
+pythonize_bool(LIBCXX_ENABLE_FILESYSTEM)
 pythonize_bool(LIBCXX_BUILD_32_BITS)
 pythonize_bool(LIBCXX_GENERATE_COVERAGE)
 pythonize_bool(LIBCXXABI_ENABLE_SHARED)
diff --git a/test/libcxx/input.output/filesystems/lit.local.cfg b/test/libcxx/input.output/filesystems/lit.local.cfg
index 4a285ee..132b73c 100644
--- a/test/libcxx/input.output/filesystems/lit.local.cfg
+++ b/test/libcxx/input.output/filesystems/lit.local.cfg
@@ -1,3 +1,5 @@
 # Disable all of the filesystem tests if the dylib under test doesn't support them.
 if 'dylib-has-no-filesystem' in config.available_features:
   config.unsupported = True
+if 'c++filesystem-disabled' in config.available_features:
+  config.unsupported = True
diff --git a/test/lit.site.cfg.in b/test/lit.site.cfg.in
index de7bffa..ed9a711 100644
--- a/test/lit.site.cfg.in
+++ b/test/lit.site.cfg.in
@@ -6,6 +6,7 @@
 config.cxx_library_root         = "@LIBCXX_LIBRARY_DIR@"
 config.enable_exceptions        = @LIBCXX_ENABLE_EXCEPTIONS@
 config.enable_experimental      = @LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY@
+config.enable_filesystem        = @LIBCXX_ENABLE_FILESYSTEM@
 config.enable_rtti              = @LIBCXX_ENABLE_RTTI@
 config.enable_shared            = @LIBCXX_ENABLE_SHARED@
 config.enable_32bit             = @LIBCXX_BUILD_32_BITS@
diff --git a/test/std/input.output/filesystems/lit.local.cfg b/test/std/input.output/filesystems/lit.local.cfg
index 4a285ee..a594334 100644
--- a/test/std/input.output/filesystems/lit.local.cfg
+++ b/test/std/input.output/filesystems/lit.local.cfg
@@ -1,3 +1,6 @@
 # Disable all of the filesystem tests if the dylib under test doesn't support them.
 if 'dylib-has-no-filesystem' in config.available_features:
   config.unsupported = True
+if 'c++filesystem-disabled' in config.available_features:
+  config.unsupported = True
+
diff --git a/utils/libcxx/test/config.py b/utils/libcxx/test/config.py
index c78e189..cac4d7c 100644
--- a/utils/libcxx/test/config.py
+++ b/utils/libcxx/test/config.py
@@ -435,6 +435,9 @@
         if self.long_tests:
             self.config.available_features.add('long_tests')
 
+        if not self.get_lit_bool('enable_filesystem', default=True):
+            self.config.available_features.add('c++filesystem-disabled')
+
         # Run a compile test for the -fsized-deallocation flag. This is needed
         # in test/std/language.support/support.dynamic/new.delete
         if self.cxx.hasCompileFlag('-fsized-deallocation'):