Git Product home page Git Product logo

bazel-to-cmake's Introduction

Bazel to CMake

This project converts Bazel BUILD and WORKSPACE files to CMakeLists.txt. The Bazel BUILD file serves as your source of truth, and this tool generates an idiomatic CMakeLists.txt so users can do a CMake-based build.

This is not an official Google product.

Project Status: Experimental

This project is experimental, and is only being used by a few projects currently. It is not part of the official Bazel ecosystem or roadmap.

The tool is quite incomplete at the moment, but I am working on adding more features so that it is effective for more and more projects. The goal is to support a wide range of Bazel rules and features, including advanced features like aspects, but how to make this work well is an area of open experimentation and research.

Usage

From the root directory of a Bazel project (where your BUILD and WORKSPACE files live) run the following command.

$ path/to/bazel_to_cmake.py CMakeLists.txt

You can check the generated CMakeLists.txt file into your repository if you wish. If you take this approach you will probably want a test that ensures it stays up-to-date with your Bazel BUILD file. You may even want to create a cc_test() in Bazel that runs the CMake build to continuously verify that it works.

(TODO: create a Bazel rule that does all of the above conveniently without the user having to fuss).

Why convert Bazel to CMake?

Bazel BUILD files are a nice way to write build systems. BUILD files are high level, mostly declarative, and generally easy on the eyes. WORKSPACE files are likewise a nice declarative way of describing your dependencies. When you need imperative logic, you can write supporting functions in separate .bzl files. All of these files are written in a subset of Python, a nice readable language that many people know.

The Bazel tool itself has nice features too. It builds your code in a sandbox, which ensures that each build step has properly declared all of its dependencies. It also enforces access control, ensuring that your private rules and headers stay private. Of major build systems, Bazel gives you the most tools for imposing structure on your build and enforcing this structure. When I port build systems to Bazel, I often catch things that weren't quite right before, but happened to work in a more permissive build system. A correct build is easier to reproduce, parallelize, distribute across a cluster, and is just generally easier to reason about.

These attributes make Bazel a great "source of truth" build system, in my opinion. During development you can build with the most rigorous checking, and your build system can be written in a nice, readable language.

On the other hand, Bazel is inconvenient for some uses:

  • when you are developing a C++ library, oftentimes the expectation from your clients is to be able to build it using one of the more established C++ build systems, one of which is CMake. Automatically providing a CMake build for the convenience of these users is preferable to manually maintaining support for another build system.
  • there are some platforms that Bazel does not support, but you still want to enable your users to build on and for those platforms without setting up cross-compilation. Automatically generated CMake build can be a great escape hatch for those users.

This tool is designed to let you write your build system in Bazel, then generate first-class CMakeLists.txt output so that CMake users can conveniently build your code in idiomatic CMake ways.

How the tool works

This tool does not use Bazel implementation at all. Instead it takes advantage of the fact that Bazel input files are a subset of Python by using Python to evaluate them. This means that the translation is fast and has no dependencies except Python. However it also means that every kind of Bazel rule needs specific support in this tool.

We aim to generate CMake output that is as idiomatic as possible. The tool tries to generate the CMake that a CMake expert would have written manually, based on best practices for CMake.

To translate idiomatically, we translate at a high level. For example cc_library() translates directly to add_library() in CMake. But we try to capture subtleties; for example a header-only library needs to have the "INTERFACE" attribute in CMake.

bazel-to-cmake's People

Contributors

haberman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bazel-to-cmake's Issues

Only files in base dir are parsed

Hi, i do have a project layout like this:
~/prj/WORKSPACE
~/prj/a/b/BUILD
~/prj/a/b/main.cpp
cmake_to_bazel is not able to provide a valid CMakeLists.txt because it assumes a BUILD file in ~/prj/
And even if there was a BUILD file in ~/prj/, the other files deep down the directory are not accessed at all, i.e. only one single CMakeLists.txt is created

Use a Starlark interpreter

This tool does not use Bazel implementation at all. Instead it takes advantage of the fact that Bazel input files are a subset of Python by using Python to evaluate them.

Have you considered using a real Starlark interpreter? For example, https://github.com/google/starlark-go is easy to embed.

Alternative approach

Hi, whoever is working on this, did you consider using bazel query results to build up the cmake files? Similar to this approach: https://github.com/lizan/bazel-cmakelists/
I think the advantage would be, that you'd have a real representation of how the project is supposed to look like, instead of parsing files directly?

unable to convert bazel to cmake

unable to convert bazel to cmake in apollo source code:https://github.com/ApolloAuto/apollo
it has both files BUILD and Workspace.
user@B45:~/apollo$ python /home/user/bazel-to-cmake/bazel_to_cmake.py CMakeLists.txt
Traceback (most recent call last):
File "/home/user/bazel-to-cmake/bazel_to_cmake.py", line 266, in
execfile("WORKSPACE", GetDict(WorkspaceFileFunctions(converter)))
NameError: name 'execfile' is not defined

not support new_local_repository

hi, I run the command and got the errors blew.

execfile("WORKSPACE", GetDict(WorkspaceFileFunctions(converter)))

File "WORKSPACE", line 81, in
new_local_repository(
NameError: name 'new_local_repository' is not defined

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.