Skip to content

Add cc_static_library for non-bazel projects #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

keith
Copy link
Contributor

@keith keith commented Jun 13, 2025

Bazel 7.4.x+ have a new cc_static_library rule that creates a
redistributable static library from a set of deps. This allows us to
create a standalone version of tcmalloc that users can easily pull into
non-bazel projects.

This should lower the friction for cmake users to use tcmalloc.

If you are not using Bazel, you can instead build a standalone version
of TCMalloc that you can vendor in your project. Before doing so you
should still run the tests, as described above, to make sure the
vendored library will work in your environment. Then build the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppose I also link against Abseil, but I build it with CMake, in addition to the TCMalloc library provided by this target.

  1. I assume I absolutely, positively have to use the same version of Abseil?
  2. What symbols end up exported from the cc_static_library?
  3. Anything else someone should be aware of?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great callouts 🙏🏻

  1. added a note about this
  2. a whole lot more than is considered public API but I believe to fix that we'd have to flip the fvisibility default, or potentially produce a relocatable object via an allowlist of symbols. I don't think there's a difference between the public symbols included in this archive vs the combined public symbols in all the static libs that would make up tcmalloc in a normal bazel build
  3. i didn't do anything with headers in this case, so i guess if users actually wanted to access some public API, we'd have to handle that separately, but I figured that shouldn't be an issue for most cases

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a convenient way of hermetically testing this? I poked Bazel briefly by adding this to variants.bzl's list, but it didn't like being specified as the malloc attribute, nor does it really know what to do with it as a dep.

I'm trying to figure out which is the lesser evil of symbol visibility versus potential ODR issues.

  • Symbol visibility probably means making MallocExtension & friends not work, because quite a few of those contain Abseil types. In practice, those ABIs don't change much; but this isn't guaranteed by Abseil either.
  • No symbol visibility means exporting a lot of other stuff, and potentially running afoul of ODR issues in subtle ways if/when it does happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a test skeleton pulling the example from the quickstart, they have to be manual unless you're ok with us flipping --experimental_cc_static_library in the .bazelrc.

What do you think we should throw in there instead of those default contents?

Bazel 7.4.x+ have a new `cc_static_library` rule that creates a
redistributable static library from a set of deps. This allows us to
create a standalone version of tcmalloc that users can easily pull into
non-bazel projects.

This should lower the friction for cmake users to use tcmalloc.
@keith keith force-pushed the ks/add-cc_static_library-for-non-bazel-projects branch from 24b1691 to 3fc9406 Compare June 16, 2025 23:31
@keith
Copy link
Contributor Author

keith commented Jun 18, 2025

I flipped the default of the experimental feature since it seems like I have traction flipping that upstream too, this way we can run those tests always too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants