Bazel -> CMake conversion ideas

Skip to first unread message

Lukács T. Berki

Nov 18, 2022, 6:33:39 PM11/18/22
to Laramie Leavitt, bazel-discuss, John Field, Josh Haberman, Yun Peng, Pedro Liberal Fernandez
Hey Laramie,

+bazel-discuss (our public mailing list)
+Josh (who wrote a rudimentary Bazel->CMake converter when the mountains were young)
+Yun (who knows about remote repositories)
+Pedro (who knows about C++)

As a followup to our somewhat hasty 1:1 this afternoon, I don't know of anyone who offers a shrink-wrapped way to convert BUILD files to CMake and I myself know next to nothing about CMake, but there are a couple of avenues you could try that are a little more promising than executing BUILD files with Python. In increasing order of complexity:

  1. You could do "bazel query --output=xml //...". This will dump all the targets in your repository, essentially recovering the same information as the Python interpreter would get in the arguments of cc_*() functions, except in a way that's more stable. We also have proto and BUILD output formats (no JSON, though)
  2. You could do "bazel aquery //..." to make Bazel dump the actions that are generated in your repository. This could in theory be used to tell CMake to run those exact shell commands. You get argv, input and output files and shell environment in a format that's easy to parse. You can get the output in user-friendly text format, proto or JSON.
  3. (this is getting scary) You could write your own aspect that propagates through the dependencies of the targets you are interested in and looks in their CcInfo to extract C++-level information ("this is a library and it has the following source files, header files and the linker should be invoked with those extra options" and things like that). It'd be quite an adventure, but it looks possible.

re: re-using the files Bazel downloaded, you have the following options:
  1. The directory returned by "bazel info repository_cache" contains a sha256 content-addressable storage with all the files Bazel downloaded. You can also tell Bazel to put this at a specific location using "--repository_cache=<path>"
  2. You can call "bazel sync --experimental_repository_resolved_file=<path>" to make Bazel dump a Starlark file with a repository -> checksum mapping that can be used to index into the repository cache. It's experimental so it's a bit rough around the edges, though, and we might break it (I don't think we will, but we won't guarantee that)

Lukács T. Berki | Software Engineer | | 

Google Germany GmbH | Erika-Mann-Str. 33  | 80636 München | Germany | Geschäftsführer: Paul Manicle, Halimah DeLaine Prado | Registergericht und -nummer: Hamburg, HRB 86891
Reply all
Reply to author
0 new messages