3

In my hunt for a build system that can handle multiple languages and build code in incremental manner, I found Bazel to be a very good solution. However, there is one issue I do not have a solution for.

I have C++ code with circular dependencies. Until it is resolved, I need a build system that can build such code. My question is, can Bazel build code with circular dependencies, especially C++ code? If yes, can someone please share the guidelines or some useful links for the same?

anatolyg
  • 26,506
  • 9
  • 60
  • 134
Amit Jain
  • 231
  • 5
  • 14
  • 2
    Is there something preventing you from fixing the circular dependency in the first place? – byxor Jul 24 '17 at 16:10
  • Please share your code. – Neo Jul 24 '17 at 16:18
  • Circular dependency is often the result of poor design. – Roy Jul 24 '17 at 16:20
  • Hi All,Thanks for your response. We have identified the dependencies and working on to solve it. But the effort will take time. We want to introduce Bazel as our build system. So we have 2 choices, either we wait until the dependecies are resolved, or we find a way Bazel can handle circular dependency. So I want to know if Bazel is capable of building codes that has circualr dependencies. – Amit Jain Jul 24 '17 at 17:06

1 Answers1

5

It depends on how exactly your circular dependency looks like.

  1. Two .cc files depend on each other's .h files

    Either put both .cc and .h files into the same cc_library rule, or use header-only rules for the .h files and have the cc_library's for the .cc files depend on the corresponding other cc_library.

  2. Two .h files that #include each other

    These would both need to be in the same cc_library for Bazel to be able to handle it.

    Alternatively, if you have include guards, then you could refactor like this: Let's say the files are a.h and b.h. You'd rename b.h to b-impl.h, remove the #include for a.h from b-impl.h, and add a new file b.h that #includes a.h. That'll make the inclusion order always consistent, and remove the cycle while being mostly backwards compatible (as long as they don't both try to declare the same symbols and the code including them was dependent on the order).

  3. Two .cc files that #include each other

    Err... I hope that's not your case.

  4. Two .a files with mutual symbol references

    This is usually handled with --start-group a.a b.a --end-group, but there is no mechanism in Bazel for that right now. See https://github.com/bazelbuild/bazel/issues/818.

Ulf Adams
  • 1,319
  • 10
  • 12
  • Hi Ulf, Thanks for your reponse ! It helps a lot. We have circular dependencies in between archives.So, say there are 2 libs, libA.a and libB.a. Each of them have multiple objects and each object has multiple functions. Now the functions of the 2 libraries depend on each other. So far we are using the --start-group and --end-group options with gcc to resolve the link issues as described in the following link : [link](https://stackoverflow.com/questions/5651869/gcc-what-are-the-start-group-and-end-group-command-line-options) .Is there any similar functionality available in Bazel ? – Amit Jain Jul 25 '17 at 18:46
  • Alternatively, if such functionality is not available in Bazel, is there a way we can use gcc link feature in Bazel to resolve this issue ? – Amit Jain Jul 25 '17 at 18:47
  • I'm afraid I don't have a good answer for that, but there's an existing entry in our issue tracker. – Ulf Adams Jul 26 '17 at 20:02
  • Thanks so much Ulf ! – Amit Jain Jul 27 '17 at 14:06