title: Bazel & automatically generated cpp / hpp files
body: <p>I am starting to use Bazel as my C++ project build system.</p>
  
  <p><strong>However I am stuck with the following problem:</strong></p>
  
  <p>I am in a scenario where I automatically generate the file.hpp file.cpp (literate programming).</p>
  
  <p>To reproduce my problem one can simply use this minimal generator:</p>
  
  <pre><code>-- file.sh --
  #!/bin/sh
  echo "int foo();" &gt;&gt; file.hpp
  echo "#include \"myLib/file.hpp\"\n\nint foo() { return 2017; }" &gt;&gt; file.cpp
  </code></pre>
  
  <p>My project repo is: (WORKSPACE is an empty file)</p>
  
  <pre><code>├── myLib
  │   ├── BUILD
  │   └── file.sh
  └── WORKSPACE
  </code></pre>
  
  <p>The BUILD file is</p>
  
  <pre><code>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__"],
  )
  </code></pre>
  
  <p>I have two problems:</p>
  
  <p><strong>Question (A), dealing with the genrule() part:</strong></p>
  
  <p>The fact that I must use </p>
  
  <pre><code>cmd =  "./$(location file.sh);cp file.cpp $(@D);cp file.hpp $(@D);"
  </code></pre>
  
  <p>is quite mysterious.</p>
  
  <p>My first attempt was:</p>
  
  <pre><code>cmd =  "./$(location file.sh)"
  </code></pre>
  
  <p>However in that case I get the following error:</p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <p><strong>Question (B), dealing with the cc_library() part</strong></p>
  
  <p>I do not know how to make Bazel aware of that the <strong>:file</strong> target depends on the <strong>:tangle_file</strong> target. </p>
  
  <p>If I uncomment:</p>
  
  <pre><code>deps = [":tangle_file"],
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>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).</p>
  </blockquote>