Bazel & automatically generated cpp / hpp files

后端 未结 1 547
挽巷
挽巷 2020-12-11 05:41

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 automat

相关标签:
1条回答
  • 2020-12-11 06:03

    Question (A)

    The error that you are seeing is because the genrule cmd is not run inside of its output directory. If you hardcoded bazel-out/local-fastbuild/genfiles/myLib/file.cpp instead of file.cpp in your file.sh script, it would work. However, the recommended approach would be for your script to takes its output directory as an argument.

    For example,

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

    and

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

    Question (B)

    The fact that you have

    srcs = ["file.cpp"],
    hdrs = ["file.hpp"],
    

    in your cc_library is what tells Bazel that it depends on the genrule, since the genrule creates those files. If you want to make it more explicit, you could use the label syntax, which does the same thing:

    srcs = ["//myLib:file.cpp"],
    hdrs = ["//myLib:file.hpp"],
    
    0 讨论(0)
提交回复
热议问题