Building a CMake library within a Bazel project
Asked Answered
B

2

8

I've written a module on top of a private fork off of TensorFlow that uses nanomsg.

For my local development server, I used cmake install to install nanomsg (to /usr/local) and accessed the header files from their installed location. The project runs fine locally.

However, I now need to package nanomsg within my TensorFlow workspace. I've tried the following two approaches, and find neither satisfactory:

  1. Similar to this answer for OpenCV, I precompiled nanomsg into a private repository, loaded it within my workspace (within tensorflow/workspace.bzl) using an http_archive directive then included the headers and libraries in the relevant build script. This runs fine, but is not a portable solution.

  2. A more portable solution, I created a genrule to run a specific sequence of cmake commands that can be used to build nanomsg. This approach is neater, but the genrule cannot be reused to cmake other projects. (I referred to this discussion).

Clearly cmake is not supported as a first-class citizen in Bazel builds. Is there anyone who has faced this problem in your own projects created a generic, portable way to include libraries within Bazel projects that are built using cmake? If so, how did you approach it?

Bissonnette answered 10/9, 2017 at 23:25 Comment(1)
How portable do you want it to be? Linux, MacOS, Windows? For 2, why is it a problem that you can't use the same genrule for other projects? I work on Bazel, and so far I haven't heard of anyone who's come up with a generic solution.Intertidal
E
6

As Ulf wrote, I think your suggested option 2 should work fine.

Regarding "can I identify if the cmake fails", yes: cmake should return with an error exit code (!= 0) when it fails. This in turn will cause Bazel to automatically recognize the genrule action as failed and thus fail the build. Because Bazel sets "set -e -o pipefail" before running your command (cf. https://docs.bazel.build/versions/master/be/general.html#genrule-environment), it should also work if you chain multiple cmake commands in your genrule "cmd".

If you call out to a shell script in your "cmd" attribute that then actually runs the cmake commands, make sure to put "set -e -o pipefail" in the first line of your script yourself. Otherwise the script will not fail when cmake fails.

If I misunderstood your question "Can I identify if the cmake fails", please let me know. :)

Enfleurage answered 12/9, 2017 at 14:45 Comment(0)
T
6

This new project: https://github.com/bazelbuild/rules_foreign_cc seems like a solution(it build rules for cmake to build your project inside bazel).

Tague answered 26/4, 2019 at 3:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.