diff --git a/.bazelrc b/.bazelrc index ffcd8bbce..495be37d3 100644 --- a/.bazelrc +++ b/.bazelrc @@ -26,6 +26,9 @@ common --incompatible_config_setting_private_default_visibility # Link Google Test against Abseil and RE2. build --define='absl=1' +# https://github.com/bazelbuild/bazel/pull/26294 +build --experimental_cc_static_library + # AllocationGuard.CooperativeDeathTest checks for "SIGABRT received" # message printed by this signal handler. test --test_env="GTEST_INSTALL_FAILURE_SIGNAL_HANDLER=1" diff --git a/docs/quickstart.md b/docs/quickstart.md index cdfc71842..b8afe5578 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -254,6 +254,26 @@ build --cxxopt='-std=c++17' Congratulations! You've created your first binary using TCMalloc. +### Using TCMalloc in non-Bazel Projects + +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 +standalone bazel target: + +``` +tcmalloc$ bazel build tcmalloc:tcmalloc_standalone +``` + +Once this builds you can copy the resulting static library from +`bazel-bin/tcmalloc/libtcmalloc_standalone.a` into your project! + +Note that if you have dependencies in your project that are also +included in TCMalloc, it is up to you to ensure the versions are +compatible in order to avoid symbol conflicts (which may or may not be +hard errors at link time). + ## What's Next * Read our [overview](overview.md), if you haven't already. The overview diff --git a/tcmalloc/BUILD b/tcmalloc/BUILD index 9aaed5ec4..1334e20a5 100644 --- a/tcmalloc/BUILD +++ b/tcmalloc/BUILD @@ -111,6 +111,13 @@ cc_library( alwayslink = 1, ) +cc_static_library( + name = "tcmalloc_standalone", + tags = ["manual"], + visibility = [":__subpackages__"], + deps = [":tcmalloc"], +) + cc_library( name = "tcmalloc_internal_methods_only", srcs = [ diff --git a/tcmalloc/testing/BUILD b/tcmalloc/testing/BUILD index a7bb9c2a6..b36ce8804 100644 --- a/tcmalloc/testing/BUILD +++ b/tcmalloc/testing/BUILD @@ -1028,3 +1028,22 @@ create_tcmalloc_testsuite( "@com_google_googletest//:gtest_main", ], ) + +cc_import( + name = "standalone_import", + static_library = "//tcmalloc:tcmalloc_standalone", + alwayslink = True, +) + +cc_test( + name = "standalone_test", + srcs = ["standalone_test.cc"], + tags = [ + "noasan", + "nomsan", + "notsan", + ], + deps = [ + ":standalone_import", + ], +) diff --git a/tcmalloc/testing/standalone_test.cc b/tcmalloc/testing/standalone_test.cc new file mode 100644 index 000000000..e3a358292 --- /dev/null +++ b/tcmalloc/testing/standalone_test.cc @@ -0,0 +1,17 @@ +#include +#include + +int main() { + std::cout << "Standard Alignment: " << alignof(std::max_align_t) << '\n'; + + double *ptr = (double*) malloc(sizeof(double)); + std::cout << "Double Alignment: " << alignof(*ptr) << '\n'; + + char *ptr2 = (char*) malloc(1); + std::cout << "Char Alignment: " << alignof(*ptr2) << '\n'; + + void *ptr3; + std::cout << "Sizeof void*: " << sizeof(ptr3) << '\n'; + + return 0; +}