Skip to content

Commit

Permalink
Unbreak cpp_unittest rules for OSS buck2 builds
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ckwalsh authored and facebook-github-bot committed Sep 12, 2024
1 parent b58bf17 commit bce99ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
10 changes: 7 additions & 3 deletions shim/shims.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)

Expand Down
14 changes: 8 additions & 6 deletions shim/third-party/googletest/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"],
)

0 comments on commit bce99ee

Please sign in to comment.