0

For many reasons, I prefer Boost.UTF to gtest (or other alternatives). I recently decided to use Bazel as my build system, and since I'm essentially at tutorial level, I looked online for a way to use Boost in Bazel, but none of them seems to handle for Boost.UTF, and since this library is not header only (like the ones handled in https://github.com/nelhage/rules_boost), I am not sure how to proceed.

How can I add Boost.UTF to Bazel, so I can use it for my test modules?

Any hint is welcome, thanks.

P.S. The only way to work around the issue I see is to try to install boost on the machine I build with and try to have Bazel use that. I guess that is how it deals with the standard libs anyway.

EDIT: This is the code of my unit test.

btest.cpp

#define BOOST_TEST_MODULE CompactStateTest
#include <boost/test/included/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(Suite1)
    BOOST_AUTO_TEST_CASE(Test1)
    {
        int x(0);
        BOOST_CHECK_EQUAL(x, 0);
    }
BOOST_AUTO_TEST_SUITE_END()

BUILD (the "Makefile" for bazel)

cc_test(
    name = "btest",
    srcs = ["btest.cpp",],
    deps = ["@boost//:test",],
)
Dirich
  • 412
  • 3
  • 13
  • This a bit unclear but seems too broad. What exactly is the problem you're having? – Captain Obvlious Jun 13 '17 at 23:19
  • I reworder my question to make it clearer. In short, I want to add Boost.UTF to Bazel but I don't know how and internet search failed me as the only examples are for header-only Boost libraries, which UTF is not. – Dirich Jun 14 '17 at 07:19
  • There is a way to use Boost.Test as header only: see [here](http://www.boost.org/doc/libs/1_64_0/libs/test/doc/html/boost_test/usage_variants.html#boost_test.usage_variants.single_header). You need to add the `boost` path that contains the root of the headers to Bazel and that should be it. – Raffi Jun 25 '17 at 19:12
  • Thanks @Raffi. The Test I tried to compile, as proof of concept, was actually using the approach you mention, and I got the double main error. But now that I think more about it, it is probably my fault for including in the bazel module for boost.UTF all the files in the library: maybe it is not about macro configuration, but about excluding some files from the module. – Dirich Jun 26 '17 at 23:16
  • I am not sure to understand :) A boost.test "module" has necessarily a `main` because a test module is a fully executable program. This program can link to the static or shared library of boost.test (compiled independently), or use the header variant, but in all cases boost.test defines a `main` for you. If you have already a main in your static or shared library that you want to test, then this cannot work. The good way would be to have a static/shared library that contains all the implementation you want to test without a `main`. – Raffi Jun 28 '17 at 18:24
  • @Raffi: I've added the files I use to my original post. I use the link from said post to set up bazel to use boost. The result of compilation is failure because of: /usr/bin/ld.gold: error: bazel-out/local-fastbuild/bin/external/boost/_objs/test/external/boost/libs/test/src/unit_test_main.pic.o: multiple definition of 'main' /usr/bin/ld.gold: bazel-out/local-fastbuild/bin/external/boost/_objs/test/external/boost/libs/test/src/cpp_main.pic.o: previous definition here – Dirich Jul 03 '17 at 20:24
  • @Dirich That is the problem: the link includes all the files of the said library (through [this function](https://github.com/nelhage/rules_boost/blob/master/boost/boost.bzl#L30)) which are sourced to your project with `["@boost//:test",]`. For the include only, we do not need the srcs of Boost.Test, but for the other versions (static/dynamic), some files of Boost.Test should be discarded. The sources of BT are given by [this rule](https://github.com/boostorg/test/blob/develop/build/Jamfile.v2#L63). – Raffi Jul 04 '17 at 00:25

1 Answers1

1

From bazel's (and cc_test's) point of view, a test is a binary that returns non-zero exit code when it fails, possibly (not obligatory) writing an xml file specified by XML_OUTPUT_FILE env var set at the test execution time with the xml test results.

So your goal is to write cc_test rule with all the deps set, so bazel can compile and run it. For that you will need to add a dependency to cc_library for Boost.UTF. This will be a standard bazel cc_library with hdrs and srcs (and/or deps).

I'm anticipating your next question on how to depend files installed on your local system, for that you will find local_repository (and it's new_ variant) useful.

hlopko
  • 3,100
  • 22
  • 27
  • Thanks for the try, turns out it won't work because, I think, boost.UTF needs some macro switch to be set, as compilation of the boost.UTF package fails due to double main. – Dirich Jun 20 '17 at 18:38
  • I was hinting at local_repository in my P.S., but if I realized that if I am using the system version then I need no BUILD file for boost, since the headers are already in the include path for the compilers (set in CROSSTOOL) and I just need to specify the necessary linkopts for Boost.UTF in the modules using it. – Dirich Jun 20 '17 at 18:41
  • You can set [defines](https://bazel.build/versions/master/docs/be/c-cpp.html#cc_library.defines) if you need to talk to the preprocessor. – hlopko Jun 21 '17 at 07:08