Skip to content

Always pass -D__BAZEL__ to C compiler #2160

Closed as not planned
Closed as not planned

Description

@jart

As we know, C++ headers have quadratic stat() multiplied by all the -isystem flags Bazel passes to gcc. But if all include paths are vendored to the Bazel execroot, it's only quadratic, and compiling goes way faster. But rewriting include lines breaks people who want to compile the sources for a particular project outside of Bazel. It also creates headaches for people packaging libraries for Linux distros.

Maybe the solution is to write code like this:

#ifdef __BAZEL__
#  include "external/zlib/zlib.h"
#else
#  include <zlib.h>
#endif

If that is the optimal approach, then maybe it would make sense to make that a standard define, so -D__BAZEL__ doesn't need to be manually passed copts.

Activity

  1. added
    P4
    on Dec 1, 2016
  2. abergmeier commented on Dec 2, 2016

    @abergmeier
    Contributor

    Why then not take the more general approach and have a VEND_HDR_PREFIX defined, which can be used like so:

    #ifdef VEND_HDR_PREFIX
    #  include " VEND_HDR_PREFIX /zlib.h"
    #else
    #  include <zlib.h>
    #endif
    

    This way you have at least a slim chance to upstream that.

    IIRC, defines with two underscores and then uppercase are reserved for the standard (implementor).

  3. jart commented on Dec 2, 2016

    @jart
    ContributorAuthor

    Well there would be a different prefix for every third party library, because they're all in their own bazel repositories.

  4. lberki commented on Dec 2, 2016

    @lberki
    Contributor

    Does the performance matter in practice? I was under the impression that the look-for-every-include-under-every-include-path-entry algorithm isn't a concern for Bazel, because there aren't that many include path entries.

  5. abergmeier-dsfishlabs commented on Dec 2, 2016

    @abergmeier-dsfishlabs
    Contributor

    Should this not perhaps be rather solved at Compiler invocation level? I would rather have a map which you feed the compiler so it no longer searches for <zlib.h> but is taught where it is. Especially since a lot of that information is already available in Bazel.

  6. lberki commented on Dec 2, 2016

    @lberki
    Contributor

    Does any compiler at the moment provide that functionality?

  7. abergmeier-dsfishlabs commented on Dec 2, 2016

    @abergmeier-dsfishlabs
    Contributor

    Not that I know of. Would be awesome, though 🤓

  8. jart commented on Dec 3, 2016

    @jart
    ContributorAuthor

    Does the performance matter in practice?

    I ran strace on GCC invocations for TensorFlow and determined those stat() system calls were >50% of wall time. So yes, but with a grain of salt.

    Part of the problem is that TensorFlow's build configuration is not optimal. It basically globs everything into one big cc_library. This means every single one of our dependencies (nearly all of which specify the includes attribute) are part of the compilation for nearly every .cc file. If TensorFlow broke up that cc_library into more finely grained targets, it would make TensorFlow builds faster. But it would not make builds faster for downstream targets referencing TensorFlow as a single label.

    Should this not perhaps be rather solved at Compiler invocation level? I would rather have a map which you feed the compiler [...]

    That would be the optimal solution. I spent an hour digging through the GCC docs looking for exactly that a month ago. Couldn't find anything that would let me do that. If we know any GCC experts it would be a good idea to consult them though.

  9. abergmeier-dsfishlabs commented on Dec 5, 2016

    @abergmeier-dsfishlabs
    Contributor

    @jart For Tensorflow then, I would expect a persistent compiler (e.g. GCC) process to be another (better) option. This one could cache include file locations on previous stat calls.
    That being said - do not know whether there is a variant like that.
    A quick Google found this however (at least interesting): http://clang.llvm.org/doxygen/classclang_1_1FileSystemStatCache.html

  10. abergmeier-dsfishlabs commented on Dec 5, 2016

    @abergmeier-dsfishlabs
    Contributor

    Perhaps cc @DanAlbert?

  11. DanAlbert commented on Dec 6, 2016

    @DanAlbert

    Not sure what I can offer here.

  12. 1 remaining item

  13. ulfjack commented on Dec 8, 2016

    @ulfjack
    Contributor

    I think Bazel should not put the remote packages under external, but just under the name of the remote repository. So for zlib, the files would be under zlib/. That way, you can make it
    #include <zlib/zlib.h>
    or
    #include "zlib/zlib.h"

    Ideally, upstream would install the files under /usr/lib/include/zlib/ instead of mashing everything into a single directory, which seems very non-ideal if you ask me. If they don't want to do that (backwards compat?), maybe they can at least provide an optional mechanism?

  14. DanAlbert commented on Dec 13, 2016

    @DanAlbert

    I certainly haven't heard of anything like it. Aren't modules supposed to be the solution for this?

  15. jart commented on Dec 13, 2016

    @jart
    ContributorAuthor

    Yes but when are modules actually going to happen? And will they require existing codebases to be rewritten in order to reap the benifits? https://youtu.be/ND-TuW0KIgg

  16. abergmeier-dsfishlabs commented on Dec 14, 2016

    @abergmeier-dsfishlabs
    Contributor

    Yes but when are modules actually going to happen?

    Depending on when the different directions of Microsoft and Clang are resolved. Soonest seems C++20. And when I look at the pace that XCode ships Clang variants it way well take until 2022 on Mac.
    That is actually the reason why I thought a persistent gcc compiler may exist. Because modules will not be a solution for cross platform for a LONG time.

    And will they require existing codebases to be rewritten in order to reap the benifits?

    To a certain extend yes. Seems more rewrite for Microsoft (<- am prefering) than Clang approach.

  17. hlopko commented on Oct 25, 2017

    @hlopko
    Member

    Aren't header maps solving this problem? We have an external contribution at #3712. Could you take a look at it and comment if it will help your use case?

  18. added
    team-Rules-CPP
    and removed on Oct 11, 2018
  19. removed their assignment
    on Dec 6, 2019
  20. fmeum commented on Dec 12, 2022

    @fmeum
    Collaborator

    With Bazel 6, all Bazel cc_* rules define BAZEL_CURRENT_REPOSITORY to the name of the repository containing the target, which can be used for #2160 (comment). Relying on it for #2160 (comment) would seem a bit hacky though.

  21. github-actions commented on Feb 16, 2024

    @github-actions

    Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale.

  22. added
    stale
    on Feb 16, 2024
  23. github-actions commented on May 16, 2024

    @github-actions

    This issue has been automatically closed due to inactivity. If you're still interested in pursuing this, please post @bazelbuild/triage in a comment here and we'll take a look. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P4staleteam-Rules-CPPtype: feature request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions