I have a C codebase I'm trying to build with Bazel. This codebase is covered with unit tests that use the fff library for generating function mocks in C. The actual library is not important though, I have a problem with the whole concept of function mocks.
Right now I have a makefile where I link and run my tests. When I build a test, I compile and link the library under test and the test source itself. This test also defines mocks for library's dependencies. When linked, mocked symbols get resolved to mock implementations and everything's working as expected. But the reason it does so is that I don't link the actual dependency library, I only link the mock symbols defined in the test source.
The main question is: how do I do this with Bazel? When linking the binary for a cc_test
target, Bazel compilis and links all transitive dependencies. Since the library under test depends (via deps
) on the real implementation of a symbol, this real definition is linked alongside the mock one and naturally I get this error: multiple definition of XXX
.
Example:
cc_library(
name = "a",
# a.cc has the real version of "void some_function()".
srcs = ["a.cc"],
hdrs = ["a.h"],
)
# This test is working just fine.
cc_test(
name = "a_test",
srcs = ["a_test.cpp"],
deps = [":a"],
)
cc_library(
name = "b",
# b.cc includes a.h and uses "void some_function()".
srcs = ["b.cc"],
hdrs = ["b.h"],
deps = [":a"],
)
# This test has two definitions for "void some_function()":
# the real one and the mock one.
cc_test(
name = "b_test",
# b_test.cpp has the mock version of "void some_function()".
srcs = ["b_test.cpp"],
deps = [":b"],
)
I'm not exactly new to Bazel, but I'm not an expert either and after spending many hours trying things I have failed. Any advice on how do I make it work?