Skip to content

Commit c287423

Browse files
authored
Merge pull request opencv#26331 from mshabunin:fix-unified-getenv
build: made environment access a separate feature
2 parents c3747a6 + 04818d6 commit c287423

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+176
-275
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ OCV_OPTION(ENABLE_CONFIG_VERIFICATION "Fail build if actual configuration doesn'
536536
OCV_OPTION(OPENCV_ENABLE_MEMALIGN "Enable posix_memalign or memalign usage" ON)
537537
OCV_OPTION(OPENCV_DISABLE_FILESYSTEM_SUPPORT "Disable filesystem support" OFF)
538538
OCV_OPTION(OPENCV_DISABLE_THREAD_SUPPORT "Build the library without multi-threaded code." OFF)
539+
OCV_OPTION(OPENCV_DISABLE_ENV_SUPPORT "Disable environment variables access (getenv)" (CMAKE_SYSTEM_NAME MATCHES "Windows(CE|Phone|Store)"))
539540
OCV_OPTION(OPENCV_SEMIHOSTING "Build the library for semihosting target (Arm). See https://developer.arm.com/documentation/100863/latest." OFF)
540541
OCV_OPTION(ENABLE_CUDA_FIRST_CLASS_LANGUAGE "Enable CUDA as a first class language, if enabled dependant projects will need to use CMake >= 3.18" OFF
541542
VISIBLE_IF (WITH_CUDA AND NOT CMAKE_VERSION VERSION_LESS 3.18)

cmake/platforms/OpenCV-WinRT.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
set(WINRT TRUE)
22

3-
add_definitions(-DWINRT -DNO_GETENV)
3+
add_definitions(-DWINRT)
44

55
# Making definitions available to other configurations and
66
# to filter dependency restrictions at compile time.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
add_definitions(-DNO_GETENV)
1+
# empty

doc/tutorials/introduction/env_reference/env_reference.markdown

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ import cv2 # variables set after this may not have effect
5959

6060
## Types
6161

62-
- _non-null_ - set to anything to enable feature, in some cases can be interpreted as other types (e.g. path)
6362
- _bool_ - `1`, `True`, `true`, `TRUE` / `0`, `False`, `false`, `FALSE`
6463
- _number_/_size_ - unsigned number, suffixes `MB`, `Mb`, `mb`, `KB`, `Kb`, `kb`
6564
- _string_ - plain string or can have a structure
@@ -70,17 +69,17 @@ import cv2 # variables set after this may not have effect
7069
## General, core
7170
| name | type | default | description |
7271
|------|------|---------|-------------|
73-
| OPENCV_SKIP_CPU_BASELINE_CHECK | non-null | | do not check that current CPU supports all features used by the build (baseline) |
72+
| OPENCV_SKIP_CPU_BASELINE_CHECK | bool | false | do not check that current CPU supports all features used by the build (baseline) |
7473
| OPENCV_CPU_DISABLE | `,` or `;`-separated | | disable code branches which use CPU features (dispatched code) |
7574
| OPENCV_SETUP_TERMINATE_HANDLER | bool | true (Windows) | use std::set_terminate to install own termination handler |
7675
| OPENCV_LIBVA_RUNTIME | file path | | libva for VA interoperability utils |
7776
| OPENCV_ENABLE_MEMALIGN | bool | true (except static analysis, memory sanitizer, fuzzying, _WIN32?) | enable aligned memory allocations |
7877
| OPENCV_BUFFER_AREA_ALWAYS_SAFE | bool | false | enable safe mode for multi-buffer allocations (each buffer separately) |
7978
| OPENCV_KMEANS_PARALLEL_GRANULARITY | num | 1000 | tune algorithm parallel work distribution parameter `parallel_for_(..., ..., ..., granularity)` |
8079
| OPENCV_DUMP_ERRORS | bool | true (Debug or Android), false (others) | print extra information on exception (log to Android) |
81-
| OPENCV_DUMP_CONFIG | non-null | | print build configuration to stderr (`getBuildInformation`) |
80+
| OPENCV_DUMP_CONFIG | bool | false | print build configuration to stderr (`getBuildInformation`) |
8281
| OPENCV_PYTHON_DEBUG | bool | false | enable extra warnings in Python bindings |
83-
| OPENCV_TEMP_PATH | non-null / path | `/tmp/` (Linux), `/data/local/tmp/` (Android), `GetTempPathA` (Windows) | directory for temporary files |
82+
| OPENCV_TEMP_PATH | path | `/tmp/` (Linux), `/data/local/tmp/` (Android), `GetTempPathA` (Windows) | directory for temporary files |
8483
| OPENCV_DATA_PATH_HINT | paths | | paths for findDataFile |
8584
| OPENCV_DATA_PATH | paths | | paths for findDataFile |
8685
| OPENCV_SAMPLES_DATA_PATH_HINT | paths | | paths for findDataFile |
@@ -272,7 +271,7 @@ Some external dependencies can be detached into a dynamic library, which will be
272271
| ⭐ OPENCV_FFMPEG_CAPTURE_OPTIONS | string (see note) | | extra options for VideoCapture FFmpeg backend |
273272
| ⭐ OPENCV_FFMPEG_WRITER_OPTIONS | string (see note) | | extra options for VideoWriter FFmpeg backend |
274273
| OPENCV_FFMPEG_THREADS | num | | set FFmpeg thread count |
275-
| OPENCV_FFMPEG_DEBUG | non-null | | enable logging messages from FFmpeg |
274+
| OPENCV_FFMPEG_DEBUG | bool | false | enable logging messages from FFmpeg |
276275
| OPENCV_FFMPEG_LOGLEVEL | num | | set FFmpeg logging level |
277276
| OPENCV_FFMPEG_DLL_DIR | dir path | | directory with FFmpeg plugin (legacy) |
278277
| OPENCV_FFMPEG_IS_THREAD_SAFE | bool | false | enabling this option will turn off thread safety locks in the FFmpeg backend (use only if you are sure FFmpeg is built with threading support, tested on Linux) |
@@ -286,7 +285,7 @@ Some external dependencies can be detached into a dynamic library, which will be
286285
| OPENCV_VIDEOIO_MFX_BITRATE_DIVISOR | num | 300 | this option allows to tune encoding bitrate (video quality/size) |
287286
| OPENCV_VIDEOIO_MFX_WRITER_TIMEOUT | num | 1 | timeout for encoding operation (in seconds) |
288287
| OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS | bool | true | allow HW-accelerated transformations (DXVA) in MediaFoundation processing graph (may slow down camera probing process) |
289-
| OPENCV_DSHOW_DEBUG | non-null | | enable verbose logging in the DShow backend |
288+
| OPENCV_DSHOW_DEBUG | bool | false | enable verbose logging in the DShow backend |
290289
| OPENCV_DSHOW_SAVEGRAPH_FILENAME | file path | | enable processing graph tump in the DShow backend |
291290
| OPENCV_VIDEOIO_V4L_RANGE_NORMALIZED | bool | false | use (0, 1) range for properties (V4L) |
292291
| OPENCV_VIDEOIO_V4L_SELECT_TIMEOUT | num | 10 | timeout for select call (in seconds) (V4L) |
@@ -297,7 +296,7 @@ Some external dependencies can be detached into a dynamic library, which will be
297296
### videoio tests
298297
| name | type | default | description |
299298
|------|------|---------|-------------|
300-
| OPENCV_TEST_VIDEOIO_BACKEND_REQUIRE_FFMPEG | | | test app will exit if no FFmpeg backend is available |
299+
| OPENCV_TEST_VIDEOIO_BACKEND_REQUIRE_FFMPEG | bool | false | test app will exit if no FFmpeg backend is available |
301300
| OPENCV_TEST_V4L2_VIVID_DEVICE | file path | | path to VIVID virtual camera device for V4L2 test (e.g. `/dev/video5`) |
302301
| OPENCV_TEST_PERF_CAMERA_LIST | paths | | cameras to use in performance test (waitAny_V4L test) |
303302
| OPENCV_TEST_CAMERA_%d_FPS | num | | fps to set for N-th camera (0-based index) (waitAny_V4L test) |
@@ -327,7 +326,7 @@ Some external dependencies can be detached into a dynamic library, which will be
327326

328327
| name | type | default | description |
329328
|------|------|---------|-------------|
330-
| OPENCV_LEGACY_WAITKEY | non-null | | switch `waitKey` return result (default behavior: `return code & 0xff` (or -1), legacy behavior: `return code`) |
329+
| OPENCV_LEGACY_WAITKEY | bool | false | switch `waitKey` return result (default behavior: `return code & 0xff` (or -1), legacy behavior: `return code`) |
331330
| $XDG_RUNTIME_DIR | | | Wayland backend specific - create shared memory-mapped file for interprocess communication (named `opencv-shared-??????`) |
332331
| OPENCV_HIGHGUI_FB_MODE | string | `FB` | Selects output mode for the framebuffer backend (`FB` - regular frambuffer, `EMU` - emulation, perform internal checks but does nothing, `XVFB` - compatible with _xvfb_ virtual frambuffer) |
333332
| OPENCV_HIGHGUI_FB_DEVICE | file path | | Path to frambuffer device to use (will be checked first) |

modules/core/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ if(OPENCV_DISABLE_THREAD_SUPPORT)
182182
ocv_target_compile_definitions(${the_module} PUBLIC "OPENCV_DISABLE_THREAD_SUPPORT=1")
183183
endif()
184184

185+
if(OPENCV_DISABLE_ENV_SUPPORT)
186+
ocv_append_source_file_compile_definitions(${CMAKE_CURRENT_SOURCE_DIR}/src/system.cpp "NO_GETENV")
187+
endif()
188+
185189
if(OPENCV_SEMIHOSTING)
186190
ocv_target_compile_definitions(${the_module} PRIVATE "-DOPENCV_SEMIHOSTING")
187191
endif(OPENCV_SEMIHOSTING)

modules/core/include/opencv2/core/utils/configuration.private.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
namespace cv { namespace utils {
1313

1414
typedef std::vector<std::string> Paths;
15-
CV_EXPORTS bool getConfigurationParameterBool(const char* name, bool defaultValue);
16-
CV_EXPORTS size_t getConfigurationParameterSizeT(const char* name, size_t defaultValue);
17-
CV_EXPORTS cv::String getConfigurationParameterString(const char* name, const char* defaultValue);
15+
CV_EXPORTS bool getConfigurationParameterBool(const char* name, bool defaultValue = false);
16+
CV_EXPORTS size_t getConfigurationParameterSizeT(const char* name, size_t defaultValue = 0);
17+
CV_EXPORTS std::string getConfigurationParameterString(const char* name, const std::string & defaultValue = std::string());
1818
CV_EXPORTS Paths getConfigurationParameterPaths(const char* name, const Paths &defaultValue = Paths());
1919

2020
}} // namespace

modules/core/src/ocl.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,10 +1170,10 @@ bool haveOpenCL()
11701170
if (!g_isOpenCLInitialized)
11711171
{
11721172
CV_TRACE_REGION("Init_OpenCL_Runtime");
1173-
const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
1174-
if (envPath)
1173+
std::string envPath = utils::getConfigurationParameterString("OPENCV_OPENCL_RUNTIME");
1174+
if (!envPath.empty())
11751175
{
1176-
if (cv::String(envPath) == "disabled")
1176+
if (envPath == "disabled")
11771177
{
11781178
g_isOpenCLAvailable = false;
11791179
g_isOpenCLInitialized = true;
@@ -2119,24 +2119,18 @@ static bool parseOpenCLDeviceConfiguration(const std::string& configurationStr,
21192119
return true;
21202120
}
21212121

2122-
#if defined WINRT || defined _WIN32_WCE
2123-
static cl_device_id selectOpenCLDevice(const char* configuration = NULL)
2124-
{
2125-
CV_UNUSED(configuration)
2126-
return NULL;
2127-
}
2128-
#else
2129-
static cl_device_id selectOpenCLDevice(const char* configuration = NULL)
2122+
static cl_device_id selectOpenCLDevice(const std::string & configuration_ = std::string())
21302123
{
21312124
std::string platform, deviceName;
21322125
std::vector<std::string> deviceTypes;
21332126

2134-
if (!configuration)
2135-
configuration = getenv("OPENCV_OPENCL_DEVICE");
2127+
std::string configuration(configuration_);
2128+
if (configuration.empty())
2129+
configuration = utils::getConfigurationParameterString("OPENCV_OPENCL_DEVICE");
21362130

2137-
if (configuration &&
2138-
(strcmp(configuration, "disabled") == 0 ||
2139-
!parseOpenCLDeviceConfiguration(std::string(configuration), platform, deviceTypes, deviceName)
2131+
if (!configuration.empty() &&
2132+
(configuration == "disabled" ||
2133+
!parseOpenCLDeviceConfiguration(configuration, platform, deviceTypes, deviceName)
21402134
))
21412135
return NULL;
21422136

@@ -2204,7 +2198,7 @@ static cl_device_id selectOpenCLDevice(const char* configuration = NULL)
22042198
if (!isID)
22052199
{
22062200
deviceTypes.push_back("GPU");
2207-
if (configuration)
2201+
if (!configuration.empty())
22082202
deviceTypes.push_back("CPU");
22092203
}
22102204
else
@@ -2272,7 +2266,7 @@ static cl_device_id selectOpenCLDevice(const char* configuration = NULL)
22722266
}
22732267

22742268
not_found:
2275-
if (!configuration)
2269+
if (configuration.empty())
22762270
return NULL; // suppress messages on stderr
22772271

22782272
std::ostringstream msg;
@@ -2287,7 +2281,6 @@ static cl_device_id selectOpenCLDevice(const char* configuration = NULL)
22872281
CV_LOG_ERROR(NULL, msg.str());
22882282
return NULL;
22892283
}
2290-
#endif
22912284

22922285
#ifdef HAVE_OPENCL_SVM
22932286
namespace svm {
@@ -2340,12 +2333,12 @@ static unsigned int getSVMCapabilitiesMask()
23402333
static unsigned int mask = 0;
23412334
if (!initialized)
23422335
{
2343-
const char* envValue = getenv("OPENCV_OPENCL_SVM_CAPABILITIES_MASK");
2344-
if (envValue == NULL)
2336+
const std::string envValue = utils::getConfigurationParameterString("OPENCV_OPENCL_SVM_CAPABILITIES_MASK");
2337+
if (envValue.empty())
23452338
{
23462339
return ~0U; // all bits 1
23472340
}
2348-
mask = atoi(envValue);
2341+
mask = atoi(envValue.c_str());
23492342
initialized = true;
23502343
}
23512344
return mask;
@@ -2482,8 +2475,8 @@ struct Context::Impl
24822475
std::string configuration = configuration_;
24832476
if (configuration_.empty())
24842477
{
2485-
const char* c = getenv("OPENCV_OPENCL_DEVICE");
2486-
if (c)
2478+
const std::string c = utils::getConfigurationParameterString("OPENCV_OPENCL_DEVICE");
2479+
if (!c.empty())
24872480
configuration = c;
24882481
}
24892482
Impl* impl = findContext(configuration);
@@ -2494,7 +2487,7 @@ struct Context::Impl
24942487
return impl;
24952488
}
24962489

2497-
cl_device_id d = selectOpenCLDevice(configuration.empty() ? NULL : configuration.c_str());
2490+
cl_device_id d = selectOpenCLDevice(configuration);
24982491
if (d == NULL)
24992492
return NULL;
25002493

modules/core/src/opencl/runtime/opencl_core.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#if defined(HAVE_OPENCL)
4545

4646
#include "opencv2/core.hpp" // CV_Error
47+
#include "opencv2/core/utils/configuration.private.hpp"
4748

4849
#if defined(HAVE_OPENCL_STATIC)
4950
#if defined __APPLE__
@@ -64,18 +65,14 @@ CV_SUPPRESS_DEPRECATED_END
6465
#define ERROR_MSG_CANT_LOAD "Failed to load OpenCL runtime\n"
6566
#define ERROR_MSG_INVALID_VERSION "Failed to load OpenCL runtime (expected version 1.1+)\n"
6667

67-
static const char* getRuntimePath(const char* defaultPath)
68+
static std::string getRuntimePath(const std::string & defaultPath)
6869
{
69-
const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
70-
if (envPath)
71-
{
72-
static const char disabled_str[] = "disabled";
73-
if ((strlen(envPath) == sizeof(disabled_str) - 1) &&
74-
(memcmp(envPath, disabled_str, sizeof(disabled_str) - 1) == 0))
75-
return NULL;
76-
return envPath;
77-
}
78-
return defaultPath;
70+
const std::string res = cv::utils::getConfigurationParameterString(
71+
"OPENCV_OPENCL_RUNTIME", defaultPath);
72+
if (res == "disabled")
73+
return std::string();
74+
else
75+
return res;
7976
}
8077

8178
#if defined(__APPLE__)
@@ -91,9 +88,9 @@ static void* AppleCLGetProcAddress(const char* name)
9188
if (!initialized)
9289
{
9390
const char* defaultPath = "/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL";
94-
const char* path = getRuntimePath(defaultPath);
95-
if (path)
96-
handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
91+
std::string path = getRuntimePath(defaultPath);
92+
if (!path.empty())
93+
handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
9794
if (handle == NULL)
9895
{
9996
if (path != NULL && path != defaultPath)
@@ -129,13 +126,13 @@ static void* WinGetProcAddress(const char* name)
129126
handle = GetModuleHandleA("OpenCL.dll");
130127
if (!handle)
131128
{
132-
const char* defaultPath = "OpenCL.dll";
133-
const char* path = getRuntimePath(defaultPath);
134-
if (path)
135-
handle = LoadLibraryA(path);
129+
const std::string defaultPath = "OpenCL.dll";
130+
const std::string path = getRuntimePath(defaultPath);
131+
if (!path.empty())
132+
handle = LoadLibraryA(path.c_str());
136133
if (!handle)
137134
{
138-
if (path != NULL && path != defaultPath)
135+
if (!path.empty() && path != defaultPath)
139136
fprintf(stderr, ERROR_MSG_CANT_LOAD);
140137
}
141138
else if (GetProcAddress(handle, OPENCL_FUNC_TO_CHECK_1_1) == NULL)
@@ -205,8 +202,8 @@ static void* GetProcAddress(const char* name)
205202
bool foundOpenCL = false;
206203
for (unsigned int i = 0; i < (sizeof(defaultAndroidPaths)/sizeof(char*)); i++)
207204
{
208-
const char* path = (i==0) ? getRuntimePath(defaultAndroidPaths[i]) : defaultAndroidPaths[i];
209-
if (path) {
205+
const std::string path = (i==0) ? getRuntimePath(defaultAndroidPaths[i]) : defaultAndroidPaths[i];
206+
if (!path.empty()) {
210207
handle = GetHandle(path);
211208
if (handle) {
212209
foundOpenCL = true;
@@ -236,10 +233,10 @@ static void* GetProcAddress(const char* name)
236233
if (!initialized)
237234
{
238235
const char* defaultPath = "libOpenCL.so";
239-
const char* path = getRuntimePath(defaultPath);
240-
if (path)
236+
const std::string path = getRuntimePath(defaultPath);
237+
if (!path.empty())
241238
{
242-
handle = GetHandle(path);
239+
handle = GetHandle(path.c_str());
243240
if (!handle)
244241
{
245242
if (path == defaultPath)

modules/core/src/parallel/registry_parallel.impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class ParallelBackendRegistry
111111
bool readPrioritySettings()
112112
{
113113
bool hasChanges = false;
114-
cv::String prioritized_backends = utils::getConfigurationParameterString("OPENCV_PARALLEL_PRIORITY_LIST", NULL);
114+
cv::String prioritized_backends = utils::getConfigurationParameterString("OPENCV_PARALLEL_PRIORITY_LIST");
115115
if (prioritized_backends.empty())
116116
return hasChanges;
117117
CV_LOG_INFO(NULL, "core(parallel): Configured priority list (OPENCV_PARALLEL_PRIORITY_LIST): " << prioritized_backends);

0 commit comments

Comments
 (0)