Bazel & automatically generated cpp / hpp files

Viewed 2738

I am starting to use Bazel as my C++ project build system.

However I am stuck with the following problem:

I am in a scenario where I automatically generate the file.hpp file.cpp (literate programming).

To reproduce my problem one can simply use this minimal generator:

-- file.sh --
#!/bin/sh
echo "int foo();" >> file.hpp
echo "#include \"myLib/file.hpp\"\n\nint foo() { return 2017; }" >> file.cpp

My project repo is: (WORKSPACE is an empty file)

├── myLib
│   ├── BUILD
│   └── file.sh
└── WORKSPACE

The BUILD file is

genrule(
  name = "tangle_file",
  srcs = ["file.sh"],
  outs = ["file.cpp","file.hpp"],
  cmd =  "./$(location file.sh);cp file.cpp $(@D);cp file.hpp $(@D);"
)

cc_library(
    name = "file",
    srcs = ["file.cpp"],
    hdrs = ["file.hpp"],
#    deps = [":tangle_file"],
    visibility = ["//bin:__pkg__"],
)

I have two problems:

Question (A), dealing with the genrule() part:

The fact that I must use

cmd =  "./$(location file.sh);cp file.cpp $(@D);cp file.hpp $(@D);"

is quite mysterious.

My first attempt was:

cmd =  "./$(location file.sh)"

However in that case I get the following error:

declared output 'myLib/file.cpp' was not created by genrule. This is probably because the genrule actually didn't create this output, or because the output was a directory and the genrule was run remotely (note that only the contents of declared file outputs are copied from genrules run remotely)

Question (B), dealing with the cc_library() part

I do not know how to make Bazel aware of that the :file target depends on the :tangle_file target.

If I uncomment:

deps = [":tangle_file"],

I get the following error:

in deps attribute of cc_library rule //myLib:file: genrule rule '//myLib:tangle_file' is misplaced here (expected cc_inc_library, cc_library, objc_library, experimental_objc_library or cc_proto_library).

1 Answers
Related