[build][option] Build option to disable filesystem support.
authorFrancesco Petrogalli <francesco.petrogalli@arm.com>
Thu, 29 Apr 2021 16:32:51 +0000 (16:32 +0000)
committerFrancesco Petrogalli <francesco.petrogalli@arm.com>
Tue, 11 May 2021 12:54:54 +0000 (12:54 +0000)
CMakeLists.txt
modules/core/src/glob.cpp
modules/core/src/system.cpp
modules/core/src/utils/datafile.cpp
modules/core/src/utils/samples.cpp
modules/core/test/test_utils.cpp
modules/videoio/src/backend_plugin.cpp

index 9b5bc9f8bf56178790f9926fb5eccc011eea84ea..acc5c683f723a72c87f065fc6aba349107300eb8 100644 (file)
@@ -513,11 +513,16 @@ OCV_OPTION(OPENCV_ENABLE_MEMALIGN     "Enable posix_memalign or memalign usage"
 
 OCV_OPTION(ENABLE_PYLINT              "Add target with Pylint checks"                            (BUILD_DOCS OR BUILD_EXAMPLES) IF (NOT CMAKE_CROSSCOMPILING AND NOT APPLE_FRAMEWORK) )
 OCV_OPTION(ENABLE_FLAKE8              "Add target with Python flake8 checker"                    (BUILD_DOCS OR BUILD_EXAMPLES) IF (NOT CMAKE_CROSSCOMPILING AND NOT APPLE_FRAMEWORK) )
+OCV_OPTION(OPENCV_DISABLE_FILESYSTEM_SUPPORT "Disable filesystem support" OFF)
 
 if(ENABLE_IMPL_COLLECTION)
   add_definitions(-DCV_COLLECT_IMPL_DATA)
 endif()
 
+if(OPENCV_DISABLE_FILESYSTEM_SUPPORT)
+  add_definitions(-DOPENCV_HAVE_FILESYSTEM_SUPPORT=0)
+endif()
+
 set(OPENCV_MATHJAX_RELPATH "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0" CACHE STRING "URI to a MathJax installation")
 
 # ----------------------------------------------------------------------------
@@ -1125,6 +1130,10 @@ endif()
 status("    ccache:"                  OPENCV_COMPILER_IS_CCACHE THEN YES ELSE NO)
 status("    Precompiled headers:"     PCHSupport_FOUND AND ENABLE_PRECOMPILED_HEADERS THEN YES ELSE NO)
 
+if(OPENCV_DISABLE_FILESYSTEM_SUPPORT)
+  status("    Filesystem support is disabled")
+endif()
+
 # ========================== Dependencies ============================
 ocv_get_all_libs(deps_modules deps_extra deps_3rdparty)
 status("    Extra dependencies:" ${deps_extra})
index fa8592caa5bd8f89651465fddf7ebdbe4a83ec85..b7cf1bf236c96ecc8cddeb71fc81d698b399315e 100644 (file)
@@ -43,7 +43,9 @@
 #include "precomp.hpp"
 
 #include "opencv2/core/utils/filesystem.hpp"
+#include "opencv2/core/utils/filesystem.private.hpp"
 
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
 #if defined _WIN32 || defined WINCE
 # include <windows.h>
 const char dir_separators[] = "/\\";
@@ -131,12 +133,15 @@ namespace
 
 
 }
-#else
+#else // defined _WIN32 || defined WINCE
 # include <dirent.h>
 # include <sys/stat.h>
 const char dir_separators[] = "/";
-#endif
+#endif // defined _WIN32 || defined WINCE
+#endif // OPENCV_HAVE_FILESYSTEM_SUPPORT
+
 
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
 static bool isDir(const cv::String& path, DIR* dir)
 {
 #if defined _WIN32 || defined _WIN32_WCE
@@ -168,13 +173,20 @@ static bool isDir(const cv::String& path, DIR* dir)
     return is_dir != 0;
 #endif
 }
+#endif // OPENCV_HAVE_FILESYSTEM_SUPPORT
 
 bool cv::utils::fs::isDirectory(const cv::String& path)
 {
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
     CV_INSTRUMENT_REGION();
     return isDir(path, NULL);
+#else
+    CV_UNUSED(path);
+    CV_Error(Error::StsNotImplemented, "File system support is disabled in this OpenCV build!");
+#endif
 }
 
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
 static bool wildcmp(const char *string, const char *wild)
 {
     // Based on wildcmp written by Jack Handy - <A href="mailto:jakkhandy@hotmail.com">jakkhandy@hotmail.com</A>
@@ -267,9 +279,11 @@ static void glob_rec(const cv::String& directory, const cv::String& wildchart, s
         CV_Error_(CV_StsObjectNotFound, ("could not open directory: %s", directory.c_str()));
     }
 }
+#endif // OPENCV_HAVE_FILESYSTEM_SUPPORT
 
 void cv::glob(String pattern, std::vector<String>& result, bool recursive)
 {
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
     CV_INSTRUMENT_REGION();
 
     result.clear();
@@ -303,20 +317,44 @@ void cv::glob(String pattern, std::vector<String>& result, bool recursive)
 
     glob_rec(path, wildchart, result, recursive, false, path);
     std::sort(result.begin(), result.end());
+#else // OPENCV_HAVE_FILESYSTEM_SUPPORT
+    CV_UNUSED(pattern);
+    CV_UNUSED(result);
+    CV_UNUSED(recursive);
+    CV_Error(Error::StsNotImplemented, "File system support is disabled in this OpenCV build!");
+#endif // OPENCV_HAVE_FILESYSTEM_SUPPORT
 }
 
 void cv::utils::fs::glob(const cv::String& directory, const cv::String& pattern,
         std::vector<cv::String>& result,
         bool recursive, bool includeDirectories)
 {
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
     glob_rec(directory, pattern, result, recursive, includeDirectories, directory);
     std::sort(result.begin(), result.end());
+#else // OPENCV_HAVE_FILESYSTEM_SUPPORT
+    CV_UNUSED(directory);
+    CV_UNUSED(pattern);
+    CV_UNUSED(result);
+    CV_UNUSED(recursive);
+    CV_UNUSED(includeDirectories);
+    CV_Error(Error::StsNotImplemented, "File system support is disabled in this OpenCV build!");
+#endif // OPENCV_HAVE_FILESYSTEM_SUPPORT
 }
 
 void cv::utils::fs::glob_relative(const cv::String& directory, const cv::String& pattern,
         std::vector<cv::String>& result,
         bool recursive, bool includeDirectories)
 {
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
     glob_rec(directory, pattern, result, recursive, includeDirectories, cv::String());
     std::sort(result.begin(), result.end());
+#else // OPENCV_HAVE_FILESYSTEM_SUPPORT
+    CV_UNUSED(directory);
+    CV_UNUSED(pattern);
+    CV_UNUSED(result);
+    CV_UNUSED(recursive);
+    CV_UNUSED(includeDirectories);
+    CV_Error(Error::StsNotImplemented, "File system support is disabled in this OpenCV build!");
+#endif // OPENCV_HAVE_FILESYSTEM_SUPPORT
 }
index 42524073a123ad717a66fd3553c2cfaea4c770a4..c68ca458b7106fbe7772b5091b667dc3aff282e5 100644 (file)
@@ -53,6 +53,8 @@
 #include <opencv2/core/utils/tls.hpp>
 #include <opencv2/core/utils/instrumentation.hpp>
 
+#include <opencv2/core/utils/filesystem.private.hpp>
+
 namespace cv {
 
 static void _initSystem()
@@ -947,6 +949,7 @@ String format( const char* fmt, ... )
 
 String tempfile( const char* suffix )
 {
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
     String fname;
 #ifndef NO_GETENV
     const char *temp_dir = getenv("OPENCV_TEMP_PATH");
@@ -1033,6 +1036,10 @@ String tempfile( const char* suffix )
             return fname + suffix;
     }
     return fname;
+#else // OPENCV_HAVE_FILESYSTEM_SUPPORT
+    CV_UNUSED(suffix);
+    CV_Error(Error::StsNotImplemented, "File system support is disabled in this OpenCV build!");
+#endif // OPENCV_HAVE_FILESYSTEM_SUPPORT
 }
 
 static ErrorCallback customErrorCallback = 0;
index 6a53c734997164471c8e353ac5aee3ee60166c64..3af83a5d8f20ee230696e6fa377046abbb20bebd 100644 (file)
@@ -16,6 +16,7 @@
 #include "opencv2/core/utils/filesystem.hpp"
 
 #include <opencv2/core/utils/configuration.private.hpp>
+#include "opencv2/core/utils/filesystem.private.hpp"
 
 #ifdef _WIN32
 #define WIN32_LEAN_AND_MEAN
@@ -67,6 +68,7 @@ CV_EXPORTS void addDataSearchSubDirectory(const cv::String& subdir)
     _getDataSearchSubDirectory().push_back(subdir);
 }
 
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
 static bool isPathSep(char c)
 {
     return c == '/' || c == '\\';
@@ -96,12 +98,14 @@ static bool isSubDirectory_(const cv::String& base_path, const cv::String& path)
     }
     return true;
 }
+
 static bool isSubDirectory(const cv::String& base_path, const cv::String& path)
 {
     bool res = isSubDirectory_(base_path, path);
     CV_LOG_VERBOSE(NULL, 0, "isSubDirectory(): base: " << base_path << "  path: " << path << "  => result: " << (res ? "TRUE" : "FALSE"));
     return res;
 }
+#endif //OPENCV_HAVE_FILESYSTEM_SUPPORT
 
 static cv::String getModuleLocation(const void* addr)
 {
@@ -188,6 +192,7 @@ cv::String findDataFile(const cv::String& relative_path,
                         const std::vector<String>* search_paths,
                         const std::vector<String>* subdir_paths)
 {
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
     configuration_parameter = configuration_parameter ? configuration_parameter : "OPENCV_DATA_PATH";
     CV_LOG_DEBUG(NULL, cv::format("utils::findDataFile('%s', %s)", relative_path.c_str(), configuration_parameter));
 
@@ -410,10 +415,18 @@ cv::String findDataFile(const cv::String& relative_path,
 #endif
 
     return cv::String();  // not found
+#else // OPENCV_HAVE_FILESYSTEM_SUPPORT
+    CV_UNUSED(relative_path);
+    CV_UNUSED(configuration_parameter);
+    CV_UNUSED(search_paths);
+    CV_UNUSED(subdir_paths);
+    CV_Error(Error::StsNotImplemented, "File system support is disabled in this OpenCV build!");
+#endif // OPENCV_HAVE_FILESYSTEM_SUPPORT
 }
 
 cv::String findDataFile(const cv::String& relative_path, bool required, const char* configuration_parameter)
 {
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
     CV_LOG_DEBUG(NULL, cv::format("cv::utils::findDataFile('%s', %s, %s)",
                                   relative_path.c_str(), required ? "true" : "false",
                                   configuration_parameter ? configuration_parameter : "NULL"));
@@ -424,6 +437,12 @@ cv::String findDataFile(const cv::String& relative_path, bool required, const ch
     if (result.empty() && required)
         CV_Error(cv::Error::StsError, cv::format("OpenCV: Can't find required data file: %s", relative_path.c_str()));
     return result;
+#else // OPENCV_HAVE_FILESYSTEM_SUPPORT
+    CV_UNUSED(relative_path);
+    CV_UNUSED(required);
+    CV_UNUSED(configuration_parameter);
+    CV_Error(Error::StsNotImplemented, "File system support is disabled in this OpenCV build!");
+#endif // OPENCV_HAVE_FILESYSTEM_SUPPORT
 }
 
 }} // namespace
index c1162f85fee324d8cb1e3f62a681c4c1920784ac..5d1ee5af8b03bff10485c871d95f82f2e2c51252 100644 (file)
@@ -11,6 +11,7 @@
 #define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_VERBOSE + 1
 #include "opencv2/core/utils/logger.hpp"
 #include "opencv2/core/utils/filesystem.hpp"
+#include "opencv2/core/utils/filesystem.private.hpp"
 
 namespace cv { namespace samples {
 
@@ -49,6 +50,7 @@ CV_EXPORTS void addSamplesDataSearchSubDirectory(const cv::String& subdir)
 
 cv::String findFile(const cv::String& relative_path, bool required, bool silentMode)
 {
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
     CV_LOG_DEBUG(NULL, cv::format("cv::samples::findFile('%s', %s)", relative_path.c_str(), required ? "true" : "false"));
     cv::String result = cv::utils::findDataFile(relative_path,
                                                 "OPENCV_SAMPLES_DATA_PATH",
@@ -61,6 +63,12 @@ cv::String findFile(const cv::String& relative_path, bool required, bool silentM
     if (result.empty() && required)
         CV_Error(cv::Error::StsError, cv::format("OpenCV samples: Can't find required data file: %s", relative_path.c_str()));
     return result;
+#else
+    CV_UNUSED(relative_path);
+    CV_UNUSED(required);
+    CV_UNUSED(silentMode);
+    CV_Error(Error::StsNotImplemented, "File system support is disabled in this OpenCV build!");
+#endif
 }
 
 
index d8789ddfc2b0f3b79bbe8320e087a495bacb6250..ed5f34603de593d94f10fa112fbe52be71f49759 100644 (file)
@@ -9,6 +9,7 @@
 #include "opencv2/core/utils/buffer_area.private.hpp"
 
 #include "test_utils_tls.impl.hpp"
+#include "opencv2/core/utils/filesystem.private.hpp"
 
 namespace opencv_test { namespace {
 
@@ -336,7 +337,7 @@ TEST(Logger, DISABLED_message_if)
     }
 }
 
-
+#if OPENCV_HAVE_FILESYSTEM_SUPPORT
 TEST(Samples, findFile)
 {
     cv::utils::logging::LogLevel prev = cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_VERBOSE);
@@ -353,6 +354,7 @@ TEST(Samples, findFile_missing)
     ASSERT_ANY_THROW(path = samples::findFile("non-existed.file", true));
     cv::utils::logging::setLogLevel(prev);
 }
+#endif // OPENCV_HAVE_FILESYSTEM_SUPPORT
 
 template <typename T>
 inline bool buffers_overlap(T * first, size_t first_num, T * second, size_t second_num)
index 103acebc6e5e6ec91e7b40dbc2d76b7d2b92c8ac..e42c98649cfcab657d923dad24ebe7a0015420d5 100644 (file)
@@ -749,6 +749,7 @@ std::string getCapturePluginVersion(
     CV_Assert(plugin_backend_factory);
     return plugin_backend_factory->getCapturePluginVersion(version_ABI, version_API);
 #else
+    CV_UNUSED(backend_factory);
     CV_UNUSED(version_ABI);
     CV_UNUSED(version_API);
     CV_Error(Error::StsBadFunc, "Plugins are not available in this build");
@@ -768,6 +769,7 @@ std::string getWriterPluginVersion(
     CV_Assert(plugin_backend_factory);
     return plugin_backend_factory->getWriterPluginVersion(version_ABI, version_API);
 #else
+    CV_UNUSED(backend_factory);
     CV_UNUSED(version_ABI);
     CV_UNUSED(version_API);
     CV_Error(Error::StsBadFunc, "Plugins are not available in this build");