Search code examples
c++bazelrxcpp

Add RxCpp to C++ Bazel project


I'm currently trying to compile a trivial c++ example project before including rxcpp to my main project, however, I'm running into difficulties where rxcpp library is not found.

All files are within the same directory.

Workstation specs:

  • Bazel version: 0.11.1-homebrew
  • MacOs: 10.13.3
  • Clang: Apple LLVM version 9.0.0 (clang-900.0.39.2) ; Target: x86_64-apple-darwin17.4.0

main.cc

#include <iostream>

#include "rxcpp/rx.hpp"

int main()
{
    std::cout << "Hello RxCpp!";
}

Where the Bazel workspace file is written as

# WORKSPACE
new_http_archive(
    name="RxCpp",
    url="https://github.com/Reactive-Extensions/RxCpp/archive/v4.0.0.zip",
    build_file="BUILD.rxcpp",
    strip_prefix = "RxCpp-4.0.0"
)

Where BUILD.rxcpp is written as

# BUILD.rxcpp
cc_library(
    name="rxcpp",
    hdrs=glob(["Rx/v2/src/rxcpp/*.hpp",
               "Rx/v2/src/rxcpp/**/*.hpp"]),
    visibility=["//visibility:public"]
)

and the Bazel project BUILD file is written as

# BUILD
cc_binary(
    name="some_binary_name",
    srcs=["main.cc"],
    deps=["@RxCpp//:rxcpp"]
)

Running

bazel build -c dbg --cxxopt='-std=c++14' //:some_binary_name --verbose_failures --sandbox_debug 

returned:

INFO: Analysed target //:some_binary_name (1 packages loaded).
INFO: Found 1 target...
ERROR: /Users/ddowney/workspace/cpp/rxcpp_test_build/BUILD:2:1: C++ compilation of rule '//:some_binary_name' failed (Exit 1): sandbox-exec failed: error executing command
  (cd /private/var/tmp/_bazel_ddowney/234783b10f6b175e3bb988e899ac1d0e/execroot/__main__ && \
  exec env - \
    APPLE_SDK_PLATFORM='' \
    APPLE_SDK_VERSION_OVERRIDE='' \
    DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer \
    PATH=/Users/ddowney/anaconda3/bin:/Users/ddowney/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/go/bin:/usr/local/share/dotnet:/opt/X11/bin:/Users/ddowney/anaconda3/bin:/Users/ddowney/.cargo/bin:/Users/ddowney/Library/Android/sdk/platform-tools/:/Users/ddowney/Library/Android/sdk/platform-tools/ \
    SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk \
    TMPDIR=/var/folders/sj/dylcfjks0hbc43fk8ylf71fw0000gn/T/ \
    XCODE_VERSION_OVERRIDE=9.2.0 \
  /usr/bin/sandbox-exec -f /private/var/tmp/_bazel_ddowney/234783b10f6b175e3bb988e899ac1d0e/bazel-sandbox/6190111679734456077/sandbox.sb /private/var/tmp/_bazel_ddowney/234783b10f6b175e3bb988e899ac1d0e/execroot/__main__/_bin/process-wrapper '--timeout=0' '--kill_delay=15' external/local_config_cc/wrapped_clang '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer -g '-std=c++11' '-std=c++14' -iquote . -iquote bazel-out/darwin-dbg/genfiles -iquote external/RxCpp -iquote bazel-out/darwin-dbg/genfiles/external/RxCpp -iquote external/bazel_tools -iquote bazel-out/darwin-dbg/genfiles/external/bazel_tools -isystem external/bazel_tools/tools/cpp/gcc3 -MD -MF bazel-out/darwin-dbg/bin/_objs/some_binary_name/main.d '-frandom-seed=bazel-out/darwin-dbg/bin/_objs/some_binary_name/main.o' '-isysroot __BAZEL_XCODE_SDKROOT__' -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c main.cc -o bazel-out/darwin-dbg/bin/_objs/some_binary_name/main.o)
main.cc:1:10: fatal error: 'rx/rx.hpp' file not found
#include "rx/rx.hpp"
         ^~~~~~~~~~~
1 error generated.
Target //:some_binary_name failed to build
INFO: Elapsed time: 2.123s, Critical Path: 1.84s
FAILED: Build did NOT complete successfully

I would expect to find the rxcpp library under bazel-out/darwin-dbg/bin/external/RxCpp in the current directory, however, this is not the case.

[Edit]

The output of the build directory is as follows:

ls -la /private/var/tmp/_bazel_ddowney/234783b10f6b175e3bb988e899ac1d0e/execroot/__main__/external/RxCpp/Rx/v2/src/rxcpp/
total 616
drwxr-xr-x  26 ddowney  wheel    832 21 Mar 13:51 .
drwxr-xr-x   3 ddowney  wheel     96 21 Mar 13:51 ..
drwxr-xr-x  61 ddowney  wheel   1952 21 Mar 13:51 operators
-rwxr-xr-x   1 ddowney  wheel   6408 22 Mar  2017 rx-connectable_observable.hpp
-rwxr-xr-x   1 ddowney  wheel   9340 22 Mar  2017 rx-coordination.hpp
-rwxr-xr-x   1 ddowney  wheel   5675 22 Mar  2017 rx-coroutine.hpp
-rwxr-xr-x   1 ddowney  wheel   5538 22 Mar  2017 rx-grouped_observable.hpp
-rwxr-xr-x   1 ddowney  wheel   6171 22 Mar  2017 rx-includes.hpp
-rwxr-xr-x   1 ddowney  wheel    251 22 Mar  2017 rx-lite.hpp
-rwxr-xr-x   1 ddowney  wheel   8282 22 Mar  2017 rx-notification.hpp
-rwxr-xr-x   1 ddowney  wheel  63252 22 Mar  2017 rx-observable.hpp
-rwxr-xr-x   1 ddowney  wheel  21989 22 Mar  2017 rx-observer.hpp
-rwxr-xr-x   1 ddowney  wheel  14552 22 Mar  2017 rx-operators.hpp
-rwxr-xr-x   1 ddowney  wheel   9371 22 Mar  2017 rx-predef.hpp
-rwxr-xr-x   1 ddowney  wheel  28270 22 Mar  2017 rx-scheduler.hpp
-rwxr-xr-x   1 ddowney  wheel   1045 22 Mar  2017 rx-sources.hpp
-rwxr-xr-x   1 ddowney  wheel    495 22 Mar  2017 rx-subjects.hpp
-rwxr-xr-x   1 ddowney  wheel  41330 22 Mar  2017 rx-subscriber.hpp
-rwxr-xr-x   1 ddowney  wheel  14088 22 Mar  2017 rx-subscription.hpp
-rwxr-xr-x   1 ddowney  wheel   3551 22 Mar  2017 rx-test.hpp
-rwxr-xr-x   1 ddowney  wheel   3917 22 Mar  2017 rx-trace.hpp
-rwxr-xr-x   1 ddowney  wheel  23700 22 Mar  2017 rx-util.hpp
-rwxr-xr-x   1 ddowney  wheel    232 22 Mar  2017 rx.hpp
drwxr-xr-x  10 ddowney  wheel    320 21 Mar 13:51 schedulers
drwxr-xr-x  12 ddowney  wheel    384 21 Mar 13:51 sources
drwxr-xr-x   6 ddowney  wheel    192 21 Mar 13:51 subjects

Solution

  • Bazel by default adds -iquote only to the root of your external repository. Therefore the include path in main.cc should be:

    #include "Rx/v2/src/rxcpp/rx.hpp"
    

    Or, what you will probably prefer, add includes attribute to BUILD.rxcpp:

    cc_library(
        name="rxcpp",
        hdrs=glob(["Rx/v2/src/rxcpp/*.hpp",
                   "Rx/v2/src/rxcpp/**/*.hpp"]),
        visibility=["//visibility:public"],
        includes=["Rx/v2/src/"],
    )