Opencv has a lot of configs and optimizations to enable. Generating a native bazel build is quite tedious, but here are the steps I've followed.
These are defined mainly in four files: cv_cpu_config.h
, custom_hal.hpp
, opencv2/opencv_modules.hpp
, and cvconfig.h
.
CPU optimization flags (e.g., SIMD operations) are toggled on or off in cv_cpu_config.h
. These flags are configured based on guidance from the CPU optimizations wiki. Ensure that the appropriate flags are enabled for your target architecture to maximize performance.
The custom_hal.hpp
file allows you to define custom hardware acceleration layers. This is useful if you are targeting specialized hardware or need to implement custom optimizations. In an attempt to generalize, I've left this blank
The opencv2/opencv_modules.hpp
file specifies which OpenCV modules are included in the build. You can modify this file to include or exclude specific modules based on your requirements. These are modules you want (i.e core, calib3d, features2d, etc)
The cvconfig.h
file contains general build configuration options. This includes settings for third-party dependencies, build flags, and other global configurations.
The opencv_module
macro creates a cc_library
for each module in OpenCV. Note that for each new dispatched file in a module, you'll need to define a mapping of the dispatched file to the actual operation. For example:
dispatched_files = { "stat": [ "avx2", "sse4_2", ] }
These mappings ensure that the correct SIMD or hardware-specific implementation is used during runtime, based on the capabilities of the target architecture. Be sure to review the dispatched files for each module and update the mappings as necessary.
When adding a new module, follow these steps:
modules/opencv/4.11.0/overlay/opencv.bzl
to CONFIG
opencv_module
macro to define the module's build rules.dispatched_files
mapping if the module includes hardware-specific optimizations.If you encounter build issues:
BUILD
files.cvconfig.h
file for any missing or misconfigured options.dispatched_files
mappings are accurate and complete.