How do I get my custom header template rule to pass it's output downstream cc_binary/cc_library dependency?Is there any way to include a file with a bang (!) in the path in a genrule?How to resolve bazel “undeclared inclusion(s)” error?Bazel & automatically generated cpp / hpp filesDispatching C++ generated files into srcs and hdrsDeclared include source C++ compile action invalidationbazel error while running build file, wrong protocol, is it?Bazel Maven migration Transitive Dependencies ScopeHow to deal with implicit dependency (e.g C++ include) for incremental build in custom Skylark ruleBazel: How do you get the path to a generated file?How to build static library from the Generated source files using Bazel Build

Is there evidence for Col. Vindman literally being a "Never Trump"?

How should I conceal gaps between laminate flooring and wall trim?

Can the Wish spell be used to allow someone to be able to cast all of their spells at will?

How do you link two checking accounts from two different banks in two different countries?

How to persuade players not to cheat?

If I am just replacing the car engine, do I need to replace the odometer as well?

Has an engineer called Trevor Jackson invented a revolutionary battery allowing for a car range of 1500 miles?

Does the original Game Boy game "Tetris" have a battery memory inside the cartridge?

Locked out of my own server

Employer reneged on negotiated clauses that weren't part of agreed contract, citing budget cuts - what can I do?

Is Basalt Monolith a 1-card infinite combo with no payoff?

Why did Google not use an NP problem for their quantum supremacy experiment?

What would an inclusive curriculum look like in a computer science course?

/etc/shadow permissions security best practice (000 vs. 600 vs. 640)

English equivalent of the Malayalam saying "don't stab/poke the dead body"?

Tapping 3 times on one leg and 4 times on another leg in 4 beats. What is it called and how to do it?

Is there a simple way to typeset playing cards?

How to deal with a 6 year old who was "caught" cheating?

There are polygons with only right angles which have an odd number of corners

How can I learn about unpublished VFR reporting points?

Would webs catch fire if Fire Bolt is used on a creature inside of them?

How exactly will DESI simultaneously capture individual spectra from 5,000 galaxies using optical fibers?

'Have dinner by candlelight' - preposition 'By' means 'using'?

Totally Blind Chess



How do I get my custom header template rule to pass it's output downstream cc_binary/cc_library dependency?


Is there any way to include a file with a bang (!) in the path in a genrule?How to resolve bazel “undeclared inclusion(s)” error?Bazel & automatically generated cpp / hpp filesDispatching C++ generated files into srcs and hdrsDeclared include source C++ compile action invalidationbazel error while running build file, wrong protocol, is it?Bazel Maven migration Transitive Dependencies ScopeHow to deal with implicit dependency (e.g C++ include) for incremental build in custom Skylark ruleBazel: How do you get the path to a generated file?How to build static library from the Generated source files using Bazel Build






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









0

















I'm trying to build a rule for bazel which emulates the CMake *.in template system.



This has two challenges, the first is generate the template output. The second is make the output available to both genrules, filegroups and cc_* rules. The third is to get that dependency to transitively be passed to further downstream rules.



I have it generating the output file version.hpp in genfiles (or bazel-bin), and I can get the initial library rule to include it, but I can't seem to figure out how to make my cc_binary rule, which depends on the cc_library and transitively the header_template rule to find the header file.



I have the following .bzl rule:



def _header_template_impl(ctx):
# this generates the output from the template
ctx.actions.expand_template(
template = ctx.file.template,
output = ctx.outputs.out,
substitutions = ctx.attr.vars,
)

return [
# create a provider which says that this
# out file should be made available as a header
CcInfo(compilation_context=cc_common.create_compilation_context(
headers=depset([ctx.outputs.out])
)),

# Also create a provider referencing this header ???
DefaultInfo(files=depset(
[ctx.outputs.out]
))
]

header_template = rule(
implementation = _header_template_impl,
attrs =
"vars": attr.string_dict(
mandatory = True
),
"extension": attr.string(default=".hpp"),
"template": attr.label(
mandatory = True,
allow_single_file = True,
),
,
outputs =
"out": "%name%extension",
,
output_to_genfiles = True,
)


elsewhere I have a cc_library rule:



load("//:tools/header_template.bzl", "header_template")

# version control
BONSAI_MAJOR_VERSION = '2'
BONSAI_MINOR_VERSION = '0'
BONSAI_PATCH_VERSION = '9'
BONSAI_VERSION =
BONSAI_MAJOR_VERSION + '.' +
BONSAI_MINOR_VERSION + '.' +
BONSAI_PATCH_VERSION

header_template(
name = "bonsai_version",
extension = ".hpp",
template = "version.hpp.in",
vars =
"@BONSAI_MAJOR_VERSION@": BONSAI_MAJOR_VERSION,
"@BONSAI_MINOR_VERSION@": BONSAI_MINOR_VERSION,
"@BONSAI_PATCH_VERSION@": BONSAI_PATCH_VERSION,
"@BONSAI_VERSION@": BONSAI_VERSION,
,
)

# ...

private = glob([
"src/**/*.hpp",
"src/**/*.cpp",
"proto/**/*.hpp",
])
public = glob([
"include/*.hpp",
":bonsai_version",
])

cc_library(
# target name matches directory name so you can call:
# bazel build .
name = "bonsai",
srcs = private,
hdrs = public,
# public headers
includes = [
"include",
],

# ...

deps = [
":bonsai_version",
# ...
],

# ...
)


When I build, my source files need to be able to:



#include "bonsai_version.hpp"


I think the answer involves CcInfo but I'm grasping in the dark as to how it should be constructed.



I've already tried add "-I$(GENDIR)/" + package_name() to the copts, to no avail. The generated header still isn't available.



My expectation is that I should be able to return some kind of Info object that would allow me to add the dependency in srcs. Maybe it should be a DefaultInfo.



I've dug through the bazel rules examples and the source, but I'm missing something fundamental, and I can't find documentation that discuss this particular.



I'd like to be able to do the following:



header_template(
name = "some_header",
extension = ".hpp",
template = "some_header.hpp.in",
vars =
"@SOMEVAR@": "value",
"ANOTHERVAR": "another_value",
,
)

cc_library(
name = "foo",
srcs = ["foo.src", ":some_header"],
...
)

cc_binary(
name = "bar",
srcs = ["bar.cpp"],
deps = [":foo"],
)




and include the generated header like so:



#include "some_header.hpp"

void bar()










share|improve this question


































    0

















    I'm trying to build a rule for bazel which emulates the CMake *.in template system.



    This has two challenges, the first is generate the template output. The second is make the output available to both genrules, filegroups and cc_* rules. The third is to get that dependency to transitively be passed to further downstream rules.



    I have it generating the output file version.hpp in genfiles (or bazel-bin), and I can get the initial library rule to include it, but I can't seem to figure out how to make my cc_binary rule, which depends on the cc_library and transitively the header_template rule to find the header file.



    I have the following .bzl rule:



    def _header_template_impl(ctx):
    # this generates the output from the template
    ctx.actions.expand_template(
    template = ctx.file.template,
    output = ctx.outputs.out,
    substitutions = ctx.attr.vars,
    )

    return [
    # create a provider which says that this
    # out file should be made available as a header
    CcInfo(compilation_context=cc_common.create_compilation_context(
    headers=depset([ctx.outputs.out])
    )),

    # Also create a provider referencing this header ???
    DefaultInfo(files=depset(
    [ctx.outputs.out]
    ))
    ]

    header_template = rule(
    implementation = _header_template_impl,
    attrs =
    "vars": attr.string_dict(
    mandatory = True
    ),
    "extension": attr.string(default=".hpp"),
    "template": attr.label(
    mandatory = True,
    allow_single_file = True,
    ),
    ,
    outputs =
    "out": "%name%extension",
    ,
    output_to_genfiles = True,
    )


    elsewhere I have a cc_library rule:



    load("//:tools/header_template.bzl", "header_template")

    # version control
    BONSAI_MAJOR_VERSION = '2'
    BONSAI_MINOR_VERSION = '0'
    BONSAI_PATCH_VERSION = '9'
    BONSAI_VERSION =
    BONSAI_MAJOR_VERSION + '.' +
    BONSAI_MINOR_VERSION + '.' +
    BONSAI_PATCH_VERSION

    header_template(
    name = "bonsai_version",
    extension = ".hpp",
    template = "version.hpp.in",
    vars =
    "@BONSAI_MAJOR_VERSION@": BONSAI_MAJOR_VERSION,
    "@BONSAI_MINOR_VERSION@": BONSAI_MINOR_VERSION,
    "@BONSAI_PATCH_VERSION@": BONSAI_PATCH_VERSION,
    "@BONSAI_VERSION@": BONSAI_VERSION,
    ,
    )

    # ...

    private = glob([
    "src/**/*.hpp",
    "src/**/*.cpp",
    "proto/**/*.hpp",
    ])
    public = glob([
    "include/*.hpp",
    ":bonsai_version",
    ])

    cc_library(
    # target name matches directory name so you can call:
    # bazel build .
    name = "bonsai",
    srcs = private,
    hdrs = public,
    # public headers
    includes = [
    "include",
    ],

    # ...

    deps = [
    ":bonsai_version",
    # ...
    ],

    # ...
    )


    When I build, my source files need to be able to:



    #include "bonsai_version.hpp"


    I think the answer involves CcInfo but I'm grasping in the dark as to how it should be constructed.



    I've already tried add "-I$(GENDIR)/" + package_name() to the copts, to no avail. The generated header still isn't available.



    My expectation is that I should be able to return some kind of Info object that would allow me to add the dependency in srcs. Maybe it should be a DefaultInfo.



    I've dug through the bazel rules examples and the source, but I'm missing something fundamental, and I can't find documentation that discuss this particular.



    I'd like to be able to do the following:



    header_template(
    name = "some_header",
    extension = ".hpp",
    template = "some_header.hpp.in",
    vars =
    "@SOMEVAR@": "value",
    "ANOTHERVAR": "another_value",
    ,
    )

    cc_library(
    name = "foo",
    srcs = ["foo.src", ":some_header"],
    ...
    )

    cc_binary(
    name = "bar",
    srcs = ["bar.cpp"],
    deps = [":foo"],
    )




    and include the generated header like so:



    #include "some_header.hpp"

    void bar()










    share|improve this question






























      0












      0








      0








      I'm trying to build a rule for bazel which emulates the CMake *.in template system.



      This has two challenges, the first is generate the template output. The second is make the output available to both genrules, filegroups and cc_* rules. The third is to get that dependency to transitively be passed to further downstream rules.



      I have it generating the output file version.hpp in genfiles (or bazel-bin), and I can get the initial library rule to include it, but I can't seem to figure out how to make my cc_binary rule, which depends on the cc_library and transitively the header_template rule to find the header file.



      I have the following .bzl rule:



      def _header_template_impl(ctx):
      # this generates the output from the template
      ctx.actions.expand_template(
      template = ctx.file.template,
      output = ctx.outputs.out,
      substitutions = ctx.attr.vars,
      )

      return [
      # create a provider which says that this
      # out file should be made available as a header
      CcInfo(compilation_context=cc_common.create_compilation_context(
      headers=depset([ctx.outputs.out])
      )),

      # Also create a provider referencing this header ???
      DefaultInfo(files=depset(
      [ctx.outputs.out]
      ))
      ]

      header_template = rule(
      implementation = _header_template_impl,
      attrs =
      "vars": attr.string_dict(
      mandatory = True
      ),
      "extension": attr.string(default=".hpp"),
      "template": attr.label(
      mandatory = True,
      allow_single_file = True,
      ),
      ,
      outputs =
      "out": "%name%extension",
      ,
      output_to_genfiles = True,
      )


      elsewhere I have a cc_library rule:



      load("//:tools/header_template.bzl", "header_template")

      # version control
      BONSAI_MAJOR_VERSION = '2'
      BONSAI_MINOR_VERSION = '0'
      BONSAI_PATCH_VERSION = '9'
      BONSAI_VERSION =
      BONSAI_MAJOR_VERSION + '.' +
      BONSAI_MINOR_VERSION + '.' +
      BONSAI_PATCH_VERSION

      header_template(
      name = "bonsai_version",
      extension = ".hpp",
      template = "version.hpp.in",
      vars =
      "@BONSAI_MAJOR_VERSION@": BONSAI_MAJOR_VERSION,
      "@BONSAI_MINOR_VERSION@": BONSAI_MINOR_VERSION,
      "@BONSAI_PATCH_VERSION@": BONSAI_PATCH_VERSION,
      "@BONSAI_VERSION@": BONSAI_VERSION,
      ,
      )

      # ...

      private = glob([
      "src/**/*.hpp",
      "src/**/*.cpp",
      "proto/**/*.hpp",
      ])
      public = glob([
      "include/*.hpp",
      ":bonsai_version",
      ])

      cc_library(
      # target name matches directory name so you can call:
      # bazel build .
      name = "bonsai",
      srcs = private,
      hdrs = public,
      # public headers
      includes = [
      "include",
      ],

      # ...

      deps = [
      ":bonsai_version",
      # ...
      ],

      # ...
      )


      When I build, my source files need to be able to:



      #include "bonsai_version.hpp"


      I think the answer involves CcInfo but I'm grasping in the dark as to how it should be constructed.



      I've already tried add "-I$(GENDIR)/" + package_name() to the copts, to no avail. The generated header still isn't available.



      My expectation is that I should be able to return some kind of Info object that would allow me to add the dependency in srcs. Maybe it should be a DefaultInfo.



      I've dug through the bazel rules examples and the source, but I'm missing something fundamental, and I can't find documentation that discuss this particular.



      I'd like to be able to do the following:



      header_template(
      name = "some_header",
      extension = ".hpp",
      template = "some_header.hpp.in",
      vars =
      "@SOMEVAR@": "value",
      "ANOTHERVAR": "another_value",
      ,
      )

      cc_library(
      name = "foo",
      srcs = ["foo.src", ":some_header"],
      ...
      )

      cc_binary(
      name = "bar",
      srcs = ["bar.cpp"],
      deps = [":foo"],
      )




      and include the generated header like so:



      #include "some_header.hpp"

      void bar()










      share|improve this question

















      I'm trying to build a rule for bazel which emulates the CMake *.in template system.



      This has two challenges, the first is generate the template output. The second is make the output available to both genrules, filegroups and cc_* rules. The third is to get that dependency to transitively be passed to further downstream rules.



      I have it generating the output file version.hpp in genfiles (or bazel-bin), and I can get the initial library rule to include it, but I can't seem to figure out how to make my cc_binary rule, which depends on the cc_library and transitively the header_template rule to find the header file.



      I have the following .bzl rule:



      def _header_template_impl(ctx):
      # this generates the output from the template
      ctx.actions.expand_template(
      template = ctx.file.template,
      output = ctx.outputs.out,
      substitutions = ctx.attr.vars,
      )

      return [
      # create a provider which says that this
      # out file should be made available as a header
      CcInfo(compilation_context=cc_common.create_compilation_context(
      headers=depset([ctx.outputs.out])
      )),

      # Also create a provider referencing this header ???
      DefaultInfo(files=depset(
      [ctx.outputs.out]
      ))
      ]

      header_template = rule(
      implementation = _header_template_impl,
      attrs =
      "vars": attr.string_dict(
      mandatory = True
      ),
      "extension": attr.string(default=".hpp"),
      "template": attr.label(
      mandatory = True,
      allow_single_file = True,
      ),
      ,
      outputs =
      "out": "%name%extension",
      ,
      output_to_genfiles = True,
      )


      elsewhere I have a cc_library rule:



      load("//:tools/header_template.bzl", "header_template")

      # version control
      BONSAI_MAJOR_VERSION = '2'
      BONSAI_MINOR_VERSION = '0'
      BONSAI_PATCH_VERSION = '9'
      BONSAI_VERSION =
      BONSAI_MAJOR_VERSION + '.' +
      BONSAI_MINOR_VERSION + '.' +
      BONSAI_PATCH_VERSION

      header_template(
      name = "bonsai_version",
      extension = ".hpp",
      template = "version.hpp.in",
      vars =
      "@BONSAI_MAJOR_VERSION@": BONSAI_MAJOR_VERSION,
      "@BONSAI_MINOR_VERSION@": BONSAI_MINOR_VERSION,
      "@BONSAI_PATCH_VERSION@": BONSAI_PATCH_VERSION,
      "@BONSAI_VERSION@": BONSAI_VERSION,
      ,
      )

      # ...

      private = glob([
      "src/**/*.hpp",
      "src/**/*.cpp",
      "proto/**/*.hpp",
      ])
      public = glob([
      "include/*.hpp",
      ":bonsai_version",
      ])

      cc_library(
      # target name matches directory name so you can call:
      # bazel build .
      name = "bonsai",
      srcs = private,
      hdrs = public,
      # public headers
      includes = [
      "include",
      ],

      # ...

      deps = [
      ":bonsai_version",
      # ...
      ],

      # ...
      )


      When I build, my source files need to be able to:



      #include "bonsai_version.hpp"


      I think the answer involves CcInfo but I'm grasping in the dark as to how it should be constructed.



      I've already tried add "-I$(GENDIR)/" + package_name() to the copts, to no avail. The generated header still isn't available.



      My expectation is that I should be able to return some kind of Info object that would allow me to add the dependency in srcs. Maybe it should be a DefaultInfo.



      I've dug through the bazel rules examples and the source, but I'm missing something fundamental, and I can't find documentation that discuss this particular.



      I'd like to be able to do the following:



      header_template(
      name = "some_header",
      extension = ".hpp",
      template = "some_header.hpp.in",
      vars =
      "@SOMEVAR@": "value",
      "ANOTHERVAR": "another_value",
      ,
      )

      cc_library(
      name = "foo",
      srcs = ["foo.src", ":some_header"],
      ...
      )

      cc_binary(
      name = "bar",
      srcs = ["bar.cpp"],
      deps = [":foo"],
      )




      and include the generated header like so:



      #include "some_header.hpp"

      void bar()







      bazel






      share|improve this question
















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 28 at 21:30







      mikeestee

















      asked Mar 28 at 21:01









      mikeesteemikeestee

      314 bronze badges




      314 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          1


















          The answer looks like it is:



          def _header_template_impl(ctx):
          # this generates the output from the template
          ctx.actions.expand_template(
          template = ctx.file.template,
          output = ctx.outputs.out,
          substitutions = ctx.attr.vars,
          )

          return [
          # create a provider which says that this
          # out file should be made available as a header
          CcInfo(compilation_context=cc_common.create_compilation_context(

          # pass out the include path for finding this header
          includes=depset([ctx.outputs.out.dirname]),

          # and the actual header here.
          headers=depset([ctx.outputs.out])
          ))
          ]


          elsewhere:



          header_template(
          name = "some_header",
          extension = ".hpp",
          template = "some_header.hpp.in",
          vars =
          "@SOMEVAR@": "value",
          "ANOTHERVAR": "another_value",
          ,
          )

          cc_library(
          name = "foo",
          srcs = ["foo.cpp"],
          deps = [":some_header"],
          ...
          )

          cc_binary(
          name = "bar",
          srcs = ["bar.cpp"],
          deps = [":foo"],
          )





          share|improve this answer

































            0


















            If your header has a generic name (eg config.h) and you want it to be private (ie srcs instead of hdrs), you might need a different approach. I've seen this problem for gflags, which "leaked" config.h and affected libraries that depended on it (issue).



            Of course, in both cases, the easiest solution is to generate and commit header files for the platforms you target.



            Alternatively, you can set copts for the cc_library rule that uses the generated private header:



            cc_library(
            name = "foo",
            srcs = ["foo.cpp", "some_header.hpp"],
            copts = ["-I$(GENDIR)/my/package/name"],
            ...
            )


            If you want this to work when your repository is included as an external repository, you have a bit more work cut out for you due to bazel issue #4463.



            PS. You might want to see if cc_fix_config from https://github.com/antonovvk/bazel_rules works for you. It's just a wrapper around perl but I found it useful.






            share|improve this answer


























            • it's a public header, so that behavior was desired, but I suppose I could add an option that would move the inclusion in the rule for public/private. for whatever reason, the GENDIR trick wasn't working for me. routing around genfile directories is fraught because I don't think the header gets generated when the template changes.

              – mikeestee
              Mar 29 at 23:00











            • If your library has a dependency on some_header.hpp and the file doesn't exist in the source tree, Bazel should ensure it's up-to-date before building the library, but maybe there's some weirdness or bug in play. However, if using CcInfo works for your case then it's certainly less error-prone than messing around with copts and GENDIR.

              – Rodrigo Queiro
              Apr 1 at 9:11












            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );














            draft saved

            draft discarded
















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406794%2fhow-do-i-get-my-custom-header-template-rule-to-pass-its-output-downstream-cc-bi%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown


























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1


















            The answer looks like it is:



            def _header_template_impl(ctx):
            # this generates the output from the template
            ctx.actions.expand_template(
            template = ctx.file.template,
            output = ctx.outputs.out,
            substitutions = ctx.attr.vars,
            )

            return [
            # create a provider which says that this
            # out file should be made available as a header
            CcInfo(compilation_context=cc_common.create_compilation_context(

            # pass out the include path for finding this header
            includes=depset([ctx.outputs.out.dirname]),

            # and the actual header here.
            headers=depset([ctx.outputs.out])
            ))
            ]


            elsewhere:



            header_template(
            name = "some_header",
            extension = ".hpp",
            template = "some_header.hpp.in",
            vars =
            "@SOMEVAR@": "value",
            "ANOTHERVAR": "another_value",
            ,
            )

            cc_library(
            name = "foo",
            srcs = ["foo.cpp"],
            deps = [":some_header"],
            ...
            )

            cc_binary(
            name = "bar",
            srcs = ["bar.cpp"],
            deps = [":foo"],
            )





            share|improve this answer






























              1


















              The answer looks like it is:



              def _header_template_impl(ctx):
              # this generates the output from the template
              ctx.actions.expand_template(
              template = ctx.file.template,
              output = ctx.outputs.out,
              substitutions = ctx.attr.vars,
              )

              return [
              # create a provider which says that this
              # out file should be made available as a header
              CcInfo(compilation_context=cc_common.create_compilation_context(

              # pass out the include path for finding this header
              includes=depset([ctx.outputs.out.dirname]),

              # and the actual header here.
              headers=depset([ctx.outputs.out])
              ))
              ]


              elsewhere:



              header_template(
              name = "some_header",
              extension = ".hpp",
              template = "some_header.hpp.in",
              vars =
              "@SOMEVAR@": "value",
              "ANOTHERVAR": "another_value",
              ,
              )

              cc_library(
              name = "foo",
              srcs = ["foo.cpp"],
              deps = [":some_header"],
              ...
              )

              cc_binary(
              name = "bar",
              srcs = ["bar.cpp"],
              deps = [":foo"],
              )





              share|improve this answer




























                1














                1










                1









                The answer looks like it is:



                def _header_template_impl(ctx):
                # this generates the output from the template
                ctx.actions.expand_template(
                template = ctx.file.template,
                output = ctx.outputs.out,
                substitutions = ctx.attr.vars,
                )

                return [
                # create a provider which says that this
                # out file should be made available as a header
                CcInfo(compilation_context=cc_common.create_compilation_context(

                # pass out the include path for finding this header
                includes=depset([ctx.outputs.out.dirname]),

                # and the actual header here.
                headers=depset([ctx.outputs.out])
                ))
                ]


                elsewhere:



                header_template(
                name = "some_header",
                extension = ".hpp",
                template = "some_header.hpp.in",
                vars =
                "@SOMEVAR@": "value",
                "ANOTHERVAR": "another_value",
                ,
                )

                cc_library(
                name = "foo",
                srcs = ["foo.cpp"],
                deps = [":some_header"],
                ...
                )

                cc_binary(
                name = "bar",
                srcs = ["bar.cpp"],
                deps = [":foo"],
                )





                share|improve this answer














                The answer looks like it is:



                def _header_template_impl(ctx):
                # this generates the output from the template
                ctx.actions.expand_template(
                template = ctx.file.template,
                output = ctx.outputs.out,
                substitutions = ctx.attr.vars,
                )

                return [
                # create a provider which says that this
                # out file should be made available as a header
                CcInfo(compilation_context=cc_common.create_compilation_context(

                # pass out the include path for finding this header
                includes=depset([ctx.outputs.out.dirname]),

                # and the actual header here.
                headers=depset([ctx.outputs.out])
                ))
                ]


                elsewhere:



                header_template(
                name = "some_header",
                extension = ".hpp",
                template = "some_header.hpp.in",
                vars =
                "@SOMEVAR@": "value",
                "ANOTHERVAR": "another_value",
                ,
                )

                cc_library(
                name = "foo",
                srcs = ["foo.cpp"],
                deps = [":some_header"],
                ...
                )

                cc_binary(
                name = "bar",
                srcs = ["bar.cpp"],
                deps = [":foo"],
                )






                share|improve this answer













                share|improve this answer




                share|improve this answer










                answered Mar 28 at 21:51









                mikeesteemikeestee

                314 bronze badges




                314 bronze badges


























                    0


















                    If your header has a generic name (eg config.h) and you want it to be private (ie srcs instead of hdrs), you might need a different approach. I've seen this problem for gflags, which "leaked" config.h and affected libraries that depended on it (issue).



                    Of course, in both cases, the easiest solution is to generate and commit header files for the platforms you target.



                    Alternatively, you can set copts for the cc_library rule that uses the generated private header:



                    cc_library(
                    name = "foo",
                    srcs = ["foo.cpp", "some_header.hpp"],
                    copts = ["-I$(GENDIR)/my/package/name"],
                    ...
                    )


                    If you want this to work when your repository is included as an external repository, you have a bit more work cut out for you due to bazel issue #4463.



                    PS. You might want to see if cc_fix_config from https://github.com/antonovvk/bazel_rules works for you. It's just a wrapper around perl but I found it useful.






                    share|improve this answer


























                    • it's a public header, so that behavior was desired, but I suppose I could add an option that would move the inclusion in the rule for public/private. for whatever reason, the GENDIR trick wasn't working for me. routing around genfile directories is fraught because I don't think the header gets generated when the template changes.

                      – mikeestee
                      Mar 29 at 23:00











                    • If your library has a dependency on some_header.hpp and the file doesn't exist in the source tree, Bazel should ensure it's up-to-date before building the library, but maybe there's some weirdness or bug in play. However, if using CcInfo works for your case then it's certainly less error-prone than messing around with copts and GENDIR.

                      – Rodrigo Queiro
                      Apr 1 at 9:11















                    0


















                    If your header has a generic name (eg config.h) and you want it to be private (ie srcs instead of hdrs), you might need a different approach. I've seen this problem for gflags, which "leaked" config.h and affected libraries that depended on it (issue).



                    Of course, in both cases, the easiest solution is to generate and commit header files for the platforms you target.



                    Alternatively, you can set copts for the cc_library rule that uses the generated private header:



                    cc_library(
                    name = "foo",
                    srcs = ["foo.cpp", "some_header.hpp"],
                    copts = ["-I$(GENDIR)/my/package/name"],
                    ...
                    )


                    If you want this to work when your repository is included as an external repository, you have a bit more work cut out for you due to bazel issue #4463.



                    PS. You might want to see if cc_fix_config from https://github.com/antonovvk/bazel_rules works for you. It's just a wrapper around perl but I found it useful.






                    share|improve this answer


























                    • it's a public header, so that behavior was desired, but I suppose I could add an option that would move the inclusion in the rule for public/private. for whatever reason, the GENDIR trick wasn't working for me. routing around genfile directories is fraught because I don't think the header gets generated when the template changes.

                      – mikeestee
                      Mar 29 at 23:00











                    • If your library has a dependency on some_header.hpp and the file doesn't exist in the source tree, Bazel should ensure it's up-to-date before building the library, but maybe there's some weirdness or bug in play. However, if using CcInfo works for your case then it's certainly less error-prone than messing around with copts and GENDIR.

                      – Rodrigo Queiro
                      Apr 1 at 9:11













                    0














                    0










                    0









                    If your header has a generic name (eg config.h) and you want it to be private (ie srcs instead of hdrs), you might need a different approach. I've seen this problem for gflags, which "leaked" config.h and affected libraries that depended on it (issue).



                    Of course, in both cases, the easiest solution is to generate and commit header files for the platforms you target.



                    Alternatively, you can set copts for the cc_library rule that uses the generated private header:



                    cc_library(
                    name = "foo",
                    srcs = ["foo.cpp", "some_header.hpp"],
                    copts = ["-I$(GENDIR)/my/package/name"],
                    ...
                    )


                    If you want this to work when your repository is included as an external repository, you have a bit more work cut out for you due to bazel issue #4463.



                    PS. You might want to see if cc_fix_config from https://github.com/antonovvk/bazel_rules works for you. It's just a wrapper around perl but I found it useful.






                    share|improve this answer














                    If your header has a generic name (eg config.h) and you want it to be private (ie srcs instead of hdrs), you might need a different approach. I've seen this problem for gflags, which "leaked" config.h and affected libraries that depended on it (issue).



                    Of course, in both cases, the easiest solution is to generate and commit header files for the platforms you target.



                    Alternatively, you can set copts for the cc_library rule that uses the generated private header:



                    cc_library(
                    name = "foo",
                    srcs = ["foo.cpp", "some_header.hpp"],
                    copts = ["-I$(GENDIR)/my/package/name"],
                    ...
                    )


                    If you want this to work when your repository is included as an external repository, you have a bit more work cut out for you due to bazel issue #4463.



                    PS. You might want to see if cc_fix_config from https://github.com/antonovvk/bazel_rules works for you. It's just a wrapper around perl but I found it useful.







                    share|improve this answer













                    share|improve this answer




                    share|improve this answer










                    answered Mar 29 at 6:53









                    Rodrigo QueiroRodrigo Queiro

                    1,1246 silver badges14 bronze badges




                    1,1246 silver badges14 bronze badges















                    • it's a public header, so that behavior was desired, but I suppose I could add an option that would move the inclusion in the rule for public/private. for whatever reason, the GENDIR trick wasn't working for me. routing around genfile directories is fraught because I don't think the header gets generated when the template changes.

                      – mikeestee
                      Mar 29 at 23:00











                    • If your library has a dependency on some_header.hpp and the file doesn't exist in the source tree, Bazel should ensure it's up-to-date before building the library, but maybe there's some weirdness or bug in play. However, if using CcInfo works for your case then it's certainly less error-prone than messing around with copts and GENDIR.

                      – Rodrigo Queiro
                      Apr 1 at 9:11

















                    • it's a public header, so that behavior was desired, but I suppose I could add an option that would move the inclusion in the rule for public/private. for whatever reason, the GENDIR trick wasn't working for me. routing around genfile directories is fraught because I don't think the header gets generated when the template changes.

                      – mikeestee
                      Mar 29 at 23:00











                    • If your library has a dependency on some_header.hpp and the file doesn't exist in the source tree, Bazel should ensure it's up-to-date before building the library, but maybe there's some weirdness or bug in play. However, if using CcInfo works for your case then it's certainly less error-prone than messing around with copts and GENDIR.

                      – Rodrigo Queiro
                      Apr 1 at 9:11
















                    it's a public header, so that behavior was desired, but I suppose I could add an option that would move the inclusion in the rule for public/private. for whatever reason, the GENDIR trick wasn't working for me. routing around genfile directories is fraught because I don't think the header gets generated when the template changes.

                    – mikeestee
                    Mar 29 at 23:00





                    it's a public header, so that behavior was desired, but I suppose I could add an option that would move the inclusion in the rule for public/private. for whatever reason, the GENDIR trick wasn't working for me. routing around genfile directories is fraught because I don't think the header gets generated when the template changes.

                    – mikeestee
                    Mar 29 at 23:00













                    If your library has a dependency on some_header.hpp and the file doesn't exist in the source tree, Bazel should ensure it's up-to-date before building the library, but maybe there's some weirdness or bug in play. However, if using CcInfo works for your case then it's certainly less error-prone than messing around with copts and GENDIR.

                    – Rodrigo Queiro
                    Apr 1 at 9:11





                    If your library has a dependency on some_header.hpp and the file doesn't exist in the source tree, Bazel should ensure it's up-to-date before building the library, but maybe there's some weirdness or bug in play. However, if using CcInfo works for your case then it's certainly less error-prone than messing around with copts and GENDIR.

                    – Rodrigo Queiro
                    Apr 1 at 9:11


















                    draft saved

                    draft discarded















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406794%2fhow-do-i-get-my-custom-header-template-rule-to-pass-its-output-downstream-cc-bi%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown









                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript