From ac6f8506a65c5c6e083a144361008360dcf0b34a Mon Sep 17 00:00:00 2001 From: Cullen Walsh Date: Thu, 12 Sep 2024 10:15:49 -0700 Subject: [PATCH] Unbreak cpp_unittest rules for OSS buck2 builds Summary: - Make gtest main stub it's own library to link in. This resolves issues with multiple definitions of main(), which folly sometimes triggers. If the test includes its own definition, it will be preferred, otherwise the linked version will be used - Turn gmock from an alias of test to it's own third party library. For macos they are packaged together, but on other systems they are published separately, and have separate pkgconfigs - Add implicit deps to gtest/gmock for cpp_unittest rules. Internally these dependencies are implicitly added, so this matches that behavior. Reviewed By: bigfootjon Differential Revision: D62543160 fbshipit-source-id: 3ed1ba5ec3b22928b11fefa86133acab13c16382 --- shim/shims.bzl | 10 +++++++--- shim/third-party/googletest/BUCK | 14 ++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/shim/shims.bzl b/shim/shims.bzl index c99042b16c1..f48e8583cbd 100644 --- a/shim/shims.bzl +++ b/shim/shims.bzl @@ -49,6 +49,12 @@ _HEADER_SUFFIXES = ( "-defs.tcc", ) +CPP_UNITTEST_MAIN_DEP = "shim//third-party/googletest:cpp_unittest_main" +CPP_UNITTEST_LIB_DEPS = [ + "shim//third-party/googletest:gtest", + "shim//third-party/googletest:gmock", +] + def _get_headers_from_sources(srcs): """ Return the headers likely associated with the given sources @@ -198,14 +204,12 @@ def cpp_unittest( extract_helper_lib = None, compiler_specific_flags = None, default_strip_mode = None, - srcs = [], **kwargs): _unused = (supports_static_listing, allocator, owner, tags, emails, extract_helper_lib, compiler_specific_flags, default_strip_mode) # @unused - srcs = srcs + ["shim//third-party/googletest:gtest_main.cpp"] + deps = deps + [CPP_UNITTEST_MAIN_DEP] + CPP_UNITTEST_LIB_DEPS prelude.cxx_test( deps = _maybe_select_map(deps + external_deps_to_targets(external_deps), _fix_deps), visibility = visibility, - srcs = srcs, **kwargs ) diff --git a/shim/third-party/googletest/BUCK b/shim/third-party/googletest/BUCK index bacd7d776a6..a4dc8e951ed 100644 --- a/shim/third-party/googletest/BUCK +++ b/shim/third-party/googletest/BUCK @@ -5,7 +5,6 @@ # License, Version 2.0 found in the LICENSE-APACHE file in the root directory # of this source tree. -load("@shim//build_defs:export_files.bzl", "export_file") load("@shim//third-party:third_party.bzl", "third_party_library") oncall("open_source") @@ -16,12 +15,15 @@ third_party_library( pkgconfig_name = "gtest", ) -alias( +third_party_library( name = "gmock", - actual = ":gtest", - visibility = ["PUBLIC"], + homebrew_package_name = "googletest", + pkgconfig_name = "gmock", ) -export_file( - name = "gtest_main.cpp", +cxx_library( + name = "cpp_unittest_main", + srcs = ["gtest_main.cpp"], + visibility = ["PUBLIC"], + deps = [":gtest"], )