1

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",
    ],
)
Colin
  • 3,670
  • 1
  • 25
  • 36

2 Answers2

1

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

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • "You have shot yourself in the foot, here." Yeah, sigh. Maybe starting with the choice of bazel at all. Second trying to use cmake with bazel. – Colin Feb 10 '22 at 18:29
  • Maybe more color: the reason I did cc_library for ccd is because that library's existing cmake system writes the headers and libraries into /usr/wherever. Which is exactly what I don't want - I want to write the .so or .a and *.h into the bazel sandbox (or whatever it's called). I didn't want to do the same thing for fcl, because it's so complex. So I thought I'd try to find a way to feed the ccd build artifacts into cmake for fcl. But it's obv a big foot-gun... – Colin Feb 10 '22 at 18:44
  • @Colin - you can install `ccd` wherever you want by setting `CMAKE_INSTALL_PREFIX` to any directory. That directory can then be the value of `ccd_ROOT` in `fcl`'s build. You don't have to install them on top of each other. – Alex Reinking Feb 10 '22 at 18:48
  • Ah, any ideas for how to point that into bazel's sandbox? Something like `cache_entries = {"CMAKE_INSTALL_PREFIX"="@libccd"}` (if I use bazel's cmake to build ccd as well)? I couldn't get that to work. – Colin Feb 10 '22 at 18:58
  • @Colin - I don't really use Bazel, but it _would_ be a cache entry in CMake. – Alex Reinking Feb 10 '22 at 18:59
0

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)

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90