Search code examples
c++buck

In Buck, how do I expand a genrule output directory?


I have a genrule that outputs a directory containing C++ headers. I also have a cxx_library which uses the output of the genrule. The headers field of the cxx_library looks like this:

  ...
  headers = [
    ':my-headers',
  ],
  ...

The problem is that my C++ source files include the headers like this:

#include "my_header.h"

But Buck prepares the staging area like this:

my-cxx-library#default,private-headers/out

Where out is an alias to the folder containing my generated headers.

So in order to use the headers, I would have to include them like this:

#include "out/my_header.h"

The library is not my own, so I would not like to change the source-code. Instead, I would like to use something like a subdir_glob to include everything inside :my-headers/out.

I tried this:

  ...
  headers = subdir_glob([
    (':my-headers/out', '**/*.h'),
  ]),
  ...

However, it seems that when done this way, the string :my-headers does not get resolved to the output path of :my-headers.

Is there a function in that can expand a rule to its output path?


Solution

  • This isn't trivially possible today, but there is a workaround you can use:

    genrule(
      name = 'headers',
      cmd = 'generate_headers.py --outdir=$OUT'
      out = 'headers-dir',
    )
    genrule(
      name = 'generated_header_A.h',
      cmd = 'cp $(location :headers)/genereated_header_A.h $OUT',
      out = 'generated_header_A.h',
    )
    

    Then, in your cxx_library, you just need to declare your headers by referencing the location:

      headers = {
        'some_namespace/header_A.h': ':generated_header_A.h',
      },
    

    The above code assumes all of this is in the same build file, but if it isn't, you just need to use a fully-qualified build target instead.