From 7a31a6edeea4d868087d200ed27181b5a9c37a59 Mon Sep 17 00:00:00 2001 From: Francesco Petrogalli Date: Thu, 29 Apr 2021 16:32:51 +0000 Subject: [PATCH] [build][option] Build option to disable filesystem support. --- CMakeLists.txt | 9 ++++++ modules/core/src/glob.cpp | 42 ++++++++++++++++++++++++-- modules/core/src/system.cpp | 7 +++++ modules/core/src/utils/datafile.cpp | 19 ++++++++++++ modules/core/src/utils/samples.cpp | 8 +++++ modules/core/test/test_utils.cpp | 4 ++- modules/videoio/src/backend_plugin.cpp | 2 ++ 7 files changed, 88 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b5bc9f8bf..acc5c683f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/modules/core/src/glob.cpp b/modules/core/src/glob.cpp index fa8592caa5..b7cf1bf236 100644 --- a/modules/core/src/glob.cpp +++ b/modules/core/src/glob.cpp @@ -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 const char dir_separators[] = "/\\"; @@ -131,12 +133,15 @@ namespace } -#else +#else // defined _WIN32 || defined WINCE # include # include 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 - jakkhandy@hotmail.com @@ -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& result, bool recursive) { +#if OPENCV_HAVE_FILESYSTEM_SUPPORT CV_INSTRUMENT_REGION(); result.clear(); @@ -303,20 +317,44 @@ void cv::glob(String pattern, std::vector& 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& 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& 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 } diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index 42524073a1..c68ca458b7 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -53,6 +53,8 @@ #include #include +#include + 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; diff --git a/modules/core/src/utils/datafile.cpp b/modules/core/src/utils/datafile.cpp index 6a53c73499..3af83a5d8f 100644 --- a/modules/core/src/utils/datafile.cpp +++ b/modules/core/src/utils/datafile.cpp @@ -16,6 +16,7 @@ #include "opencv2/core/utils/filesystem.hpp" #include +#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* search_paths, const std::vector* 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 diff --git a/modules/core/src/utils/samples.cpp b/modules/core/src/utils/samples.cpp index c1162f85fe..5d1ee5af8b 100644 --- a/modules/core/src/utils/samples.cpp +++ b/modules/core/src/utils/samples.cpp @@ -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 } diff --git a/modules/core/test/test_utils.cpp b/modules/core/test/test_utils.cpp index d8789ddfc2..ed5f34603d 100644 --- a/modules/core/test/test_utils.cpp +++ b/modules/core/test/test_utils.cpp @@ -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 inline bool buffers_overlap(T * first, size_t first_num, T * second, size_t second_num) diff --git a/modules/videoio/src/backend_plugin.cpp b/modules/videoio/src/backend_plugin.cpp index 103acebc6e..e42c98649c 100644 --- a/modules/videoio/src/backend_plugin.cpp +++ b/modules/videoio/src/backend_plugin.cpp @@ -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");