1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 00:03:03 +04:00

build: made environment access a separate feature

This commit is contained in:
Maksim Shabunin
2024-10-17 23:35:38 +03:00
parent 2756c20e3e
commit 04818d6dd5
44 changed files with 176 additions and 275 deletions
+18 -25
View File
@@ -1170,10 +1170,10 @@ bool haveOpenCL()
if (!g_isOpenCLInitialized)
{
CV_TRACE_REGION("Init_OpenCL_Runtime");
const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
if (envPath)
std::string envPath = utils::getConfigurationParameterString("OPENCV_OPENCL_RUNTIME");
if (!envPath.empty())
{
if (cv::String(envPath) == "disabled")
if (envPath == "disabled")
{
g_isOpenCLAvailable = false;
g_isOpenCLInitialized = true;
@@ -2119,24 +2119,18 @@ static bool parseOpenCLDeviceConfiguration(const std::string& configurationStr,
return true;
}
#if defined WINRT || defined _WIN32_WCE
static cl_device_id selectOpenCLDevice(const char* configuration = NULL)
{
CV_UNUSED(configuration)
return NULL;
}
#else
static cl_device_id selectOpenCLDevice(const char* configuration = NULL)
static cl_device_id selectOpenCLDevice(const std::string & configuration_ = std::string())
{
std::string platform, deviceName;
std::vector<std::string> deviceTypes;
if (!configuration)
configuration = getenv("OPENCV_OPENCL_DEVICE");
std::string configuration(configuration_);
if (configuration.empty())
configuration = utils::getConfigurationParameterString("OPENCV_OPENCL_DEVICE");
if (configuration &&
(strcmp(configuration, "disabled") == 0 ||
!parseOpenCLDeviceConfiguration(std::string(configuration), platform, deviceTypes, deviceName)
if (!configuration.empty() &&
(configuration == "disabled" ||
!parseOpenCLDeviceConfiguration(configuration, platform, deviceTypes, deviceName)
))
return NULL;
@@ -2204,7 +2198,7 @@ static cl_device_id selectOpenCLDevice(const char* configuration = NULL)
if (!isID)
{
deviceTypes.push_back("GPU");
if (configuration)
if (!configuration.empty())
deviceTypes.push_back("CPU");
}
else
@@ -2272,7 +2266,7 @@ static cl_device_id selectOpenCLDevice(const char* configuration = NULL)
}
not_found:
if (!configuration)
if (configuration.empty())
return NULL; // suppress messages on stderr
std::ostringstream msg;
@@ -2287,7 +2281,6 @@ not_found:
CV_LOG_ERROR(NULL, msg.str());
return NULL;
}
#endif
#ifdef HAVE_OPENCL_SVM
namespace svm {
@@ -2340,12 +2333,12 @@ static unsigned int getSVMCapabilitiesMask()
static unsigned int mask = 0;
if (!initialized)
{
const char* envValue = getenv("OPENCV_OPENCL_SVM_CAPABILITIES_MASK");
if (envValue == NULL)
const std::string envValue = utils::getConfigurationParameterString("OPENCV_OPENCL_SVM_CAPABILITIES_MASK");
if (envValue.empty())
{
return ~0U; // all bits 1
}
mask = atoi(envValue);
mask = atoi(envValue.c_str());
initialized = true;
}
return mask;
@@ -2482,8 +2475,8 @@ public:
std::string configuration = configuration_;
if (configuration_.empty())
{
const char* c = getenv("OPENCV_OPENCL_DEVICE");
if (c)
const std::string c = utils::getConfigurationParameterString("OPENCV_OPENCL_DEVICE");
if (!c.empty())
configuration = c;
}
Impl* impl = findContext(configuration);
@@ -2494,7 +2487,7 @@ public:
return impl;
}
cl_device_id d = selectOpenCLDevice(configuration.empty() ? NULL : configuration.c_str());
cl_device_id d = selectOpenCLDevice(configuration);
if (d == NULL)
return NULL;
+21 -24
View File
@@ -44,6 +44,7 @@
#if defined(HAVE_OPENCL)
#include "opencv2/core.hpp" // CV_Error
#include "opencv2/core/utils/configuration.private.hpp"
#if defined(HAVE_OPENCL_STATIC)
#if defined __APPLE__
@@ -64,18 +65,14 @@ CV_SUPPRESS_DEPRECATED_END
#define ERROR_MSG_CANT_LOAD "Failed to load OpenCL runtime\n"
#define ERROR_MSG_INVALID_VERSION "Failed to load OpenCL runtime (expected version 1.1+)\n"
static const char* getRuntimePath(const char* defaultPath)
static std::string getRuntimePath(const std::string & defaultPath)
{
const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
if (envPath)
{
static const char disabled_str[] = "disabled";
if ((strlen(envPath) == sizeof(disabled_str) - 1) &&
(memcmp(envPath, disabled_str, sizeof(disabled_str) - 1) == 0))
return NULL;
return envPath;
}
return defaultPath;
const std::string res = cv::utils::getConfigurationParameterString(
"OPENCV_OPENCL_RUNTIME", defaultPath);
if (res == "disabled")
return std::string();
else
return res;
}
#if defined(__APPLE__)
@@ -91,9 +88,9 @@ static void* AppleCLGetProcAddress(const char* name)
if (!initialized)
{
const char* defaultPath = "/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL";
const char* path = getRuntimePath(defaultPath);
if (path)
handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
std::string path = getRuntimePath(defaultPath);
if (!path.empty())
handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
if (handle == NULL)
{
if (path != NULL && path != defaultPath)
@@ -129,13 +126,13 @@ static void* WinGetProcAddress(const char* name)
handle = GetModuleHandleA("OpenCL.dll");
if (!handle)
{
const char* defaultPath = "OpenCL.dll";
const char* path = getRuntimePath(defaultPath);
if (path)
handle = LoadLibraryA(path);
const std::string defaultPath = "OpenCL.dll";
const std::string path = getRuntimePath(defaultPath);
if (!path.empty())
handle = LoadLibraryA(path.c_str());
if (!handle)
{
if (path != NULL && path != defaultPath)
if (!path.empty() && path != defaultPath)
fprintf(stderr, ERROR_MSG_CANT_LOAD);
}
else if (GetProcAddress(handle, OPENCL_FUNC_TO_CHECK_1_1) == NULL)
@@ -205,8 +202,8 @@ static void* GetProcAddress(const char* name)
bool foundOpenCL = false;
for (unsigned int i = 0; i < (sizeof(defaultAndroidPaths)/sizeof(char*)); i++)
{
const char* path = (i==0) ? getRuntimePath(defaultAndroidPaths[i]) : defaultAndroidPaths[i];
if (path) {
const std::string path = (i==0) ? getRuntimePath(defaultAndroidPaths[i]) : defaultAndroidPaths[i];
if (!path.empty()) {
handle = GetHandle(path);
if (handle) {
foundOpenCL = true;
@@ -236,10 +233,10 @@ static void* GetProcAddress(const char* name)
if (!initialized)
{
const char* defaultPath = "libOpenCL.so";
const char* path = getRuntimePath(defaultPath);
if (path)
const std::string path = getRuntimePath(defaultPath);
if (!path.empty())
{
handle = GetHandle(path);
handle = GetHandle(path.c_str());
if (!handle)
{
if (path == defaultPath)
@@ -111,7 +111,7 @@ protected:
bool readPrioritySettings()
{
bool hasChanges = false;
cv::String prioritized_backends = utils::getConfigurationParameterString("OPENCV_PARALLEL_PRIORITY_LIST", NULL);
cv::String prioritized_backends = utils::getConfigurationParameterString("OPENCV_PARALLEL_PRIORITY_LIST");
if (prioritized_backends.empty())
return hasChanges;
CV_LOG_INFO(NULL, "core(parallel): Configured priority list (OPENCV_PARALLEL_PRIORITY_LIST): " << prioritized_backends);
+20 -33
View File
@@ -450,13 +450,11 @@ struct HWFeatures
void initialize(void)
{
#ifndef NO_GETENV
if (getenv("OPENCV_DUMP_CONFIG"))
if (utils::getConfigurationParameterBool("OPENCV_DUMP_CONFIG"))
{
fprintf(stderr, "\nOpenCV build configuration is:\n%s\n",
cv::getBuildInformation().c_str());
}
#endif
initializeNames();
@@ -731,12 +729,10 @@ struct HWFeatures
#endif
bool skip_baseline_check = false;
#ifndef NO_GETENV
if (getenv("OPENCV_SKIP_CPU_BASELINE_CHECK"))
if (utils::getConfigurationParameterBool("OPENCV_SKIP_CPU_BASELINE_CHECK"))
{
skip_baseline_check = true;
}
#endif
int baseline_features[] = { CV_CPU_BASELINE_FEATURES };
if (!checkFeatures(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0]))
&& !skip_baseline_check)
@@ -786,15 +782,10 @@ struct HWFeatures
void readSettings(const int* baseline_features, int baseline_count)
{
bool dump = true;
const char* disabled_features =
#ifdef NO_GETENV
NULL;
#else
getenv("OPENCV_CPU_DISABLE");
#endif
if (disabled_features && disabled_features[0] != 0)
std::string disabled_features = utils::getConfigurationParameterString("OPENCV_CPU_DISABLE");
if (!disabled_features.empty())
{
const char* start = disabled_features;
const char* start = disabled_features.c_str();
for (;;)
{
while (start[0] != 0 && isSymbolSeparator(start[0]))
@@ -1080,20 +1071,19 @@ String tempfile( const char* suffix )
{
#if OPENCV_HAVE_FILESYSTEM_SUPPORT
String fname;
#ifndef NO_GETENV
const char *temp_dir = getenv("OPENCV_TEMP_PATH");
#endif
std::string temp_dir = utils::getConfigurationParameterString("OPENCV_TEMP_PATH");
#if defined _WIN32
#ifdef WINRT
RoInitialize(RO_INIT_MULTITHREADED);
std::wstring temp_dir = GetTempPathWinRT();
std::wstring temp_dir_rt = GetTempPathWinRT();
std::wstring temp_file = GetTempFileNameWinRT(L"ocv");
if (temp_file.empty())
return String();
temp_file = temp_dir.append(std::wstring(L"\\")).append(temp_file);
temp_file = temp_dir_rt.append(std::wstring(L"\\")).append(temp_file);
DeleteFileW(temp_file.c_str());
char aname[MAX_PATH];
@@ -1103,12 +1093,12 @@ String tempfile( const char* suffix )
RoUninitialize();
#elif defined(_WIN32_WCE)
const auto kMaxPathSize = MAX_PATH+1;
wchar_t temp_dir[kMaxPathSize] = {0};
wchar_t temp_dir_ce[kMaxPathSize] = {0};
wchar_t temp_file[kMaxPathSize] = {0};
::GetTempPathW(kMaxPathSize, temp_dir);
::GetTempPathW(kMaxPathSize, temp_dir_ce);
if(0 != ::GetTempFileNameW(temp_dir, L"ocv", 0, temp_file)) {
if(0 != ::GetTempFileNameW(temp_dir_ce, L"ocv", 0, temp_file)) {
DeleteFileW(temp_file);
char aname[MAX_PATH];
size_t copied = wcstombs(aname, temp_file, MAX_PATH);
@@ -1119,12 +1109,12 @@ String tempfile( const char* suffix )
char temp_dir2[MAX_PATH] = { 0 };
char temp_file[MAX_PATH] = { 0 };
if (temp_dir == 0 || temp_dir[0] == 0)
if (temp_dir.empty())
{
::GetTempPathA(sizeof(temp_dir2), temp_dir2);
temp_dir = temp_dir2;
temp_dir = std::string(temp_dir2);
}
if(0 == ::GetTempFileNameA(temp_dir, "ocv", 0, temp_file))
if(0 == ::GetTempFileNameA(temp_dir.c_str(), "ocv", 0, temp_file))
return String();
DeleteFileA(temp_file);
@@ -1139,7 +1129,7 @@ String tempfile( const char* suffix )
char defaultTemplate[] = "/tmp/__opencv_temp.XXXXXX";
# endif
if (temp_dir == 0 || temp_dir[0] == 0)
if (temp_dir.empty())
fname = defaultTemplate;
else
{
@@ -2289,9 +2279,9 @@ size_t utils::getConfigurationParameterSizeT(const char* name, size_t defaultVal
return read<size_t>(name, defaultValue);
}
cv::String utils::getConfigurationParameterString(const char* name, const char* defaultValue)
std::string utils::getConfigurationParameterString(const char* name, const std::string & defaultValue)
{
return read<cv::String>(name, defaultValue ? cv::String(defaultValue) : cv::String());
return read<cv::String>(name, defaultValue);
}
utils::Paths utils::getConfigurationParameterPaths(const char* name, const utils::Paths &defaultValue)
@@ -2588,11 +2578,8 @@ public:
}
ippFeatures = cpuFeatures;
const char* pIppEnv = getenv("OPENCV_IPP");
cv::String env;
if(pIppEnv != NULL)
env = pIppEnv;
if(env.size())
std::string env = utils::getConfigurationParameterString("OPENCV_IPP");
if(!env.empty())
{
#if IPP_VERSION_X100 >= 201900
const Ipp64u minorFeatures = ippCPUID_MOVBE|ippCPUID_AES|ippCPUID_CLMUL|ippCPUID_ABR|ippCPUID_RDRAND|ippCPUID_F16C|
+1 -1
View File
@@ -60,7 +60,7 @@ static std::vector<cv::String>& _getDataSearchSubDirectory()
CV_EXPORTS void addDataSearchPath(const cv::String& path)
{
if (utils::fs::isDirectory(path))
if (!path.empty() && utils::fs::isDirectory(path))
_getDataSearchPath().push_back(path);
}
CV_EXPORTS void addDataSearchSubDirectory(const cv::String& subdir)
+6 -6
View File
@@ -447,8 +447,8 @@ cv::String getCacheDirectory(const char* sub_directory_name, const char* configu
#elif defined __ANDROID__
// no defaults
#elif defined __APPLE__
const char* tmpdir_env = getenv("TMPDIR");
if (tmpdir_env && utils::fs::isDirectory(tmpdir_env))
const std::string tmpdir_env = utils::getConfigurationParameterString("TMPDIR");
if (!tmpdir_env.empty() && utils::fs::isDirectory(tmpdir_env))
{
default_cache_path = tmpdir_env;
}
@@ -461,16 +461,16 @@ cv::String getCacheDirectory(const char* sub_directory_name, const char* configu
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
if (default_cache_path.empty())
{
const char* xdg_cache_env = getenv("XDG_CACHE_HOME");
if (xdg_cache_env && xdg_cache_env[0] && utils::fs::isDirectory(xdg_cache_env))
const std::string xdg_cache_env = utils::getConfigurationParameterString("XDG_CACHE_HOME");
if (!xdg_cache_env.empty() && utils::fs::isDirectory(xdg_cache_env))
{
default_cache_path = xdg_cache_env;
}
}
if (default_cache_path.empty())
{
const char* home_env = getenv("HOME");
if (home_env && home_env[0] && utils::fs::isDirectory(home_env))
const std::string home_env = utils::getConfigurationParameterString("HOME");
if (!home_env.empty() && utils::fs::isDirectory(home_env))
{
cv::String home_path = home_env;
cv::String home_cache_path = utils::fs::join(home_path, ".cache/");
+3 -2
View File
@@ -7,6 +7,7 @@
//
#include "opencv2/core/utils/plugin_loader.private.hpp" // DynamicLib
#include "opencv2/core/utils/configuration.private.hpp"
namespace cv { namespace detail {
@@ -47,8 +48,8 @@ static FN_vaGetImage fn_vaGetImage = NULL;
static std::shared_ptr<cv::plugin::impl::DynamicLib> loadLibVA()
{
std::shared_ptr<cv::plugin::impl::DynamicLib> lib;
const char* envPath = getenv("OPENCV_LIBVA_RUNTIME");
if (envPath)
const std::string envPath = utils::getConfigurationParameterString("OPENCV_LIBVA_RUNTIME");
if (!envPath.empty())
{
lib = std::make_shared<cv::plugin::impl::DynamicLib>(envPath);
return lib;