Gil Colgate
Gil Colgate

Reputation: 119

In Bazel, how can I make a C++ library depend on a general rule?

I have a library that depends on graphics files that are generated by a shell script.

I would like the library, when it is compiled, to use the shell script to generate the graphics files, which should be copied as if it were a 'data' statement, but whenever I try to make the library depend on the genrule, I get

in deps attribute of cc_library rule //graphics_assets genrule rule '//graphics_assets:assets_gen_rule' is misplaced here (expected cc_inc_library, cc_library, objc_library or cc_proto_library)

Upvotes: 3

Views: 2493

Answers (1)

Gil Colgate
Gil Colgate

Reputation: 119


        # This is the correct format.
        # Here we want to run all the shader .glsl files through the program     
        # file_utils:archive_tool (which we also build elsewhere) and copy the 
        # output .arc file to the data.

        # 1. List the source files

        filegroup(
            name = "shader_source",
            srcs = glob([
                "shaders/*.glsl",
            ]),
        )

        # 2. invoke file_utils::archive_tool on the shaders

        genrule(
            name = "shaders_gen_rule",
            srcs = [":shader_source"],
            outs = ["shaders.arc"],
            cmd = "echo $(locations shader_source) > temp.txt ;  \
              $(location //common/file_utils:archive_tool)  \
               --create_from_list=temp.txt \
               --archive $(OUTS)  ;   \
              $(location //common/file_utils:archive_tool)   \
              --ls --archive $(OUTS) ",
            tools = ["//common/file_utils:archive_tool"],
        )

        # 3. when a a binary depends on this tool the arc file will be copied.
        # This is the thing I had trouble with

        cc_library(
            name = "shaders",
            srcs = [], # Something
            data = [":shaders_gen_rule"],
            linkstatic = 1,
        )

Upvotes: 1

Related Questions