From 3d36fc437b3f488a0db830fa63bbaf368dda9bb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20L=C3=B3pez?= <63005462+diegolopezrm@users.noreply.github.com> Date: Thu, 3 Sep 2026 11:51:05 -0500 Subject: [PATCH 1/3] feat(hooks): let projects choose which OpenCV to build against MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dartcv builds the OpenCV version this package pins. That is the right default, but it leaves no way to build against a specific one, and some projects need that: a Flutter client whose image processing has to agree with a backend service pinned to a given OpenCV cannot accept whichever version the package happens to track, because results move between releases. The CMake side already supports all of it — DARTCV_OPENCV_VERSION, DARTCV_BUILD_OPENCV_FROM_SOURCE with OPENCV_VERSION, and DARTCV_DISABLE_DOWNLOAD_OPENCV with OpenCV_DIR — but the hook never passes any of them, so none is reachable from a pubspec.yaml. Setting them by hand means environment variables threaded through Gradle, CocoaPods and the hook, which is not something a project can rely on. This exposes two user defines: opencv_version: build that upstream tag from source opencv_dir: use an OpenCV that is already built, optionally per platform, since a cross-compiled one lives in a different place for each target Projects that set neither build exactly as before. --- packages/dartcv/README.md | 32 +++++++++++++++++++ .../lib/src/hook_helpers/run_build.dart | 23 +++++++++++++ 2 files changed, 55 insertions(+) diff --git a/packages/dartcv/README.md b/packages/dartcv/README.md index 2b1ad4d5..1757f486 100644 --- a/packages/dartcv/README.md +++ b/packages/dartcv/README.md @@ -149,8 +149,40 @@ hooks: use_opencl: false linux: use_opencl: true + # which OpenCV to build against, see below + # opencv_version: "4.12.0" + # opencv_dir: /path/to/opencv/lib/cmake/opencv4 ``` +### Choosing which OpenCV to build against + +By default dartcv builds the OpenCV version this package pins, which is the one +its bindings are developed and tested against. Two options change that, for +projects that need a specific OpenCV — usually because their results have to +match another environment, such as a Python service processing the same images. + +- `opencv_version`: build this upstream tag from source instead of the pinned + one, for example `"4.12.0"`. Must be 4.12 or newer; dartcv calls APIs that do + not exist before then, and the build stops with a message saying so. +- `opencv_dir`: use an OpenCV that is already built, given as the directory + holding `OpenCVConfig.cmake`. Takes precedence over `opencv_version`, and can + also be set per platform, since a cross-compiled OpenCV lives somewhere + different for each target: + +```yaml +hooks: + user_defines: + dartcv4: + include_modules: + - ximgproc + android: + opencv_dir: /path/to/OpenCV-android-sdk/sdk/native/jni + ios: + opencv_dir: /path/to/opencv-ios/device-arm64/lib/cmake/opencv4 +``` + +Neither option changes anything for projects that do not set them. + - `debug`: enable debug mode, default is `false`, if enabled, all messages will be printed to stderr. - `treeshake`: enable linker dead-code elimination, default is `false`. When enabled, the native library is compiled with function-level sections and the linker garbage-collects code that is not reachable from the exported symbols. - `use_opencl`: whether to build OpenCV with OpenCL support, default is `false` for all platforms. It can be configured per platform under a platform key (`windows`, `linux`, `macos`, `android`, `ios`). OpenCL is **always disabled on `ios`** since iOS does not support it. Enabling OpenCL may accelerate some operations (e.g. `dnn`), but it is disabled by default because the OpenCV OpenCL runtime can cause a racy hang-at-exit during process teardown; enable it only if you need the acceleration. diff --git a/packages/dartcv/lib/src/hook_helpers/run_build.dart b/packages/dartcv/lib/src/hook_helpers/run_build.dart index daf3f7e6..802399cb 100644 --- a/packages/dartcv/lib/src/hook_helpers/run_build.dart +++ b/packages/dartcv/lib/src/hook_helpers/run_build.dart @@ -64,6 +64,15 @@ Future runBuild(BuildInput input, BuildOutputBuilder output, {Set? final parallelJobs = userDefines["parallel_jobs"] as int? ?? Platform.numberOfProcessors; final includeModules = userDefines["include_modules"] as List?; final excludeModules = userDefines["exclude_modules"] as List?; + // Which OpenCV to build against. + // + // `opencv_version` builds that upstream tag from source instead of the + // version this package pins, for consumers who need their results to match + // another environment running a specific OpenCV. `opencv_dir` points at an + // OpenCV that is already built — it wins over `opencv_version`, and may also + // be given per platform, since a cross-compiled OpenCV lives in a different + // place for each target. + final opencvVersion = userDefines["opencv_version"] as String?; final platformDefines = { OS.windows: userDefines["windows"] as Map?, OS.linux: userDefines["linux"] as Map?, @@ -77,6 +86,7 @@ Future runBuild(BuildInput input, BuildOutputBuilder output, {Set? // Reads `use_opencl` from the platform-specific user defines // (e.g. `hooks.user_defines.dartcv4.windows.use_opencl` in pubspec.yaml). // Defaults to false when unspecified; always false on iOS (not supported). + final opencvDir = (platformDefines[targetOS]?['opencv_dir'] ?? userDefines['opencv_dir']) as String?; final useOpenCL = targetOS != OS.iOS && (platformDefines[targetOS]?['use_opencl'] as bool? ?? false); // Whether to enable linker dead-code elimination (`DARTCV_TREESHAKE`). @@ -114,6 +124,11 @@ Future runBuild(BuildInput input, BuildOutputBuilder output, {Set? logger.info("[dartcv4] exclude modules: $excludeModulesFiltered\n"); logger.info("[dartcv4] merged modules: $modules\n"); logger.info("[dartcv4] platform defines: $platformDefines\n"); + if (opencvDir != null) { + logger.info("[dartcv4] using the OpenCV in: $opencvDir\n"); + } else if (opencvVersion != null) { + logger.info("[dartcv4] building OpenCV $opencvVersion from source\n"); + } final moduleDefines = { 'DARTCV_WITH_CALIB3D': modules.contains('calib3d') ? 'ON' : 'OFF', @@ -164,6 +179,14 @@ Future runBuild(BuildInput input, BuildOutputBuilder output, {Set? if (!useOpenCL) 'WITH_OPENCLAMDFFT': 'OFF', if (targetOS == OS.iOS || targetOS == OS.macOS) 'WITH_OPENCL_SVM': 'OFF', // 'FFMPEG_USE_STATIC_LIBS': 'OFF', + if (opencvDir != null) ...{ + 'DARTCV_BUILD_OPENCV_FROM_SOURCE': 'OFF', + 'DARTCV_DISABLE_DOWNLOAD_OPENCV': 'ON', + 'OpenCV_DIR': opencvDir, + } else if (opencvVersion != null) ...{ + 'DARTCV_BUILD_OPENCV_FROM_SOURCE': 'ON', + 'OPENCV_VERSION': opencvVersion, + }, 'DARTCV_ENABLE_INSTALL': 'ON', 'DARTCV_TREESHAKE': treeshake ? 'ON' : 'OFF', if (keepFile != null) 'DARTCV_KEEP_FILE': keepFile, From 036e2c91a18d3a0568f64a847de74e7e53c381c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20L=C3=B3pez?= <63005462+diegolopezrm@users.noreply.github.com> Date: Thu, 3 Sep 2026 12:05:02 -0500 Subject: [PATCH 2/3] build: stop the pubspec from overwriting a requested OpenCV version _find_version sets OPENCV_VERSION unconditionally, so a value passed in with -DOPENCV_VERSION was replaced by the pubspec's two lines later. The build then used the pinned version while reporting the requested one, which is the worst of both. The pubspec is now read only when nothing has asked for a version, which is what makes the opencv_version hook option in the previous commit take effect. --- packages/dartcv/src/CMakeLists.txt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/dartcv/src/CMakeLists.txt b/packages/dartcv/src/CMakeLists.txt index 029a49ed..9711300e 100644 --- a/packages/dartcv/src/CMakeLists.txt +++ b/packages/dartcv/src/CMakeLists.txt @@ -67,7 +67,14 @@ macro(_find_version _version_var _version_file _regex_find _regex_replace) endmacro() _find_version(PROJECT_VERSION "${PROJ_DIR}/pubspec.yaml" "^version: (.+)*$" "version: (.+)*$") -_find_version(OPENCV_VERSION "${PROJ_DIR}/pubspec.yaml" "^opencv_version: (.+)*$" "opencv_version: (.+)*$") + +# The pubspec records the OpenCV this package is developed against, and is the +# default. A caller that asked for a different one — -DOPENCV_VERSION, or +# `opencv_version` in a project's hooks — has already set it, so reading the +# pubspec here would silently discard that choice. +if(NOT OPENCV_VERSION) + _find_version(OPENCV_VERSION "${PROJ_DIR}/pubspec.yaml" "^opencv_version: (.+)*$" "opencv_version: (.+)*$") +endif() # Set OpenCV version if(NOT OPENCV_VERSION) From 3c556522520fe72b9355ecff92e9b11767611390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20L=C3=B3pez?= <63005462+diegolopezrm@users.noreply.github.com> Date: Fri, 4 Sep 2026 01:16:16 -0500 Subject: [PATCH 3/3] docs(dartcv): warn that a non-default OpenCV is unsupported Requested in review. Choosing a different OpenCV moves the risk onto the project: OpenCV changes its API between releases, so a non-default version may fail to compile, fail to link, or build cleanly and behave differently at runtime, and none of that is opencv_dart's to carry. Says so where someone is deciding whether to set the option, and asks that a bug be reproduced against the pinned version before it is reported. --- packages/dartcv/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/dartcv/README.md b/packages/dartcv/README.md index 1757f486..1217c3d6 100644 --- a/packages/dartcv/README.md +++ b/packages/dartcv/README.md @@ -161,6 +161,18 @@ its bindings are developed and tested against. Two options change that, for projects that need a specific OpenCV — usually because their results have to match another environment, such as a Python service processing the same images. +> [!WARNING] +> The pinned version is the only one dartcv is developed and tested against. +> OpenCV changes its API between releases, so a different version may fail to +> compile, fail to link, or build cleanly and then behave differently at +> runtime. **Problems that come from a non-default OpenCV are not supported by +> opencv_dart**: before reporting one, reproduce it with the pinned version, +> and if it only happens with yours, it is yours to carry. +> +> Use these options when you have a reason to accept that — matching another +> environment's results, or a platform SDK you do not control — and pin the +> version you tested, rather than tracking whatever is newest. + - `opencv_version`: build this upstream tag from source instead of the pinned one, for example `"4.12.0"`. Must be 4.12 or newer; dartcv calls APIs that do not exist before then, and the build stops with a message saying so.