bazel connect find_package from cmake

Viewed 217

I'm trying to use bazel's cmake from rules_foreign_cc to build a library called fcl. That package has, in it's CMakeLists.txt, a find_package(ccd QUIET) statement. It wants to find the ccd (or libccd) package. I build that package as well, but just using cc_library. How can I get the find_package statement to find the package that I build?

So

cc_library(
    name = "libccd",
    visibility = ["//visibility:public"],
    include_prefix = "libccd/src",
    hdrs = [stuff],
    srcs = [stuff],
}

cmake(
    name = "fcl",
    cache_entries = {
        ?maybe this?
    },
    env_vars = {
        ?maybe this?
    },
    lib_source = "@fcl//:all",
    deps = [
        ":libccd",
    ],
)
2 Answers

I build that package as well, but just using cc_library. How can I get the find_package statement to find the package that I build?

You have shot yourself in the foot, here.

fcl's find_package statement is going to search for a file called ccd-config.cmake, using the search procedure documented here: https://cmake.org/cmake/help/latest/command/find_package.html#config-mode-search-procedure

If you had that file, I would suggest setting the cache variable ccd_ROOT to whichever local prefix into which you have installed ccd. But since you went through Bazel, that file will not exist.

You therefore have three options:

  1. Use CMake to build ccd to get it to generate the correct package configuration files.
  2. Write your own Findccd.cmake find module, setting the cache variable CMAKE_MODULE_PATH in fcl's build to whichever directory in your source tree contains it. This find module will have to locate the Bazel-produced artifacts and replicate upstream ccd's interface, including variable and target naming.
  3. Write a Bazel build for fcl.

Writing find modules is very annoying, especially when the upstream has put in the effort to create CMake package configuration files. There is documentation for doing so here, though: https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html#a-sample-find-module

I highly recommend you to bazelize fcl. Not sure, but maybe this has been done already here. I bazelized a few CMake projects in the past and it always worked well (e.g. openexr, oneTBB, fmt)

Related