0

I have a system that is built using Bazel and is using OpenCV as a 3rd party package.

When building on Linux I want to use a prebuilt package using conan, and when using Windows I want to use a locally compiled version, and not use conan.

What I have at the moment is in my WORKSPACE file:

load("@//conan:dependencies.bzl", "load_conan_dependencies")
load_conan_dependencies()

new_local_repository(
    name = "windows_opencv",
    build_file = "@//third_party:opencv_windows.BUILD",
    path = "C:\\opencv\\build",
)

and the conanfile.txt looks like this:

[requires]
OpenCV/3.4.13@company/dev

[generators]
BazelDeps
BazelToolchain

This works well for the Linux part, but because I don't have the OpenCV/3.4.13@company/dev package for Windows, I can't use conan install and not rules are generated, so trying to build on Windows causes this error:

ERROR: error loading package '': Label '//conan:dependencies.bzl' is invalid because 'conan' is not a package; perhaps you meant to put the colon here: '//:conan/dependencies.bzl'?

The way I currently see this is I have 2 options:

  • Have 2 conanfile.txt, 1 which contains the OpenCV package and another empty only with the generators, to create the necessary conan files so it would pass the load call
  • Create an empty OpenCV conan package and again just insall it so the load call would pass

Both options seems to me like bad hacks and workarounds, but I'm not familiar enough with Bazel to know if there's a better option.

What I would like to be able to do is only load the conan dependencies rule for Linux and ignore it on Windows.

Currently the selection between Linux & Windows OpenCV is done using alias:

alias(
    name = "opencv_binary",
    actual = select({
        "//mediapipe:windows": "@windows_opencv//:opencv",
        "//conditions:default": "@opencv//:opencv",
    }),
)

where @opencv//:opencv is the conan pacakge

La bla bla
  • 8,558
  • 13
  • 60
  • 109
  • Why not using a ``conanfile.py`` with a ``requirements()`` method, that you can put any ``if ...`` (like conditioning on Windows) to do a ``self.requires("opencv...")``? You can also use the ``generate()`` method for adding extra logic, as automating what files are prepared before the build. A ``conanfile.py`` can be used for ``conan install .`` in the same way, but it is more powerful, and it doesn't mean that you are creating a package, it is just for consuming. – drodri Jul 11 '22 at 11:01
  • I wasn't familiar with the `conanfile.py` until this morning, and I actually implemented something similar to what you just said in our `cmake` project a few hours ago. so yeah, it sounds like a great idea that would probably work. Thanks! – La bla bla Jul 11 '22 at 12:10

0 Answers0