File
Blob: build/capnp_embed.bzl
| 1 | """capnp_embed definition""" |
| 2 | |
| 3 | load("@capnp-cpp//src/capnp:cc_capnp_library.bzl", "capnp_provider") |
| 4 | load("@rules_cc//cc:cc_library.bzl", "cc_library") |
| 5 | |
| 6 | def _capnp_embed_impl(ctx): |
| 7 | return [ |
| 8 | capnp_provider( |
| 9 | includes = [ctx.file.src.dirname], |
| 10 | inputs = [ctx.file.src], |
| 11 | src_prefix = "", |
| 12 | ), |
| 13 | ] |
| 14 | |
| 15 | _capnp_embed = rule( |
| 16 | attrs = { |
| 17 | "src": attr.label(allow_single_file = True), |
| 18 | "deps": attr.label_list(), |
| 19 | }, |
| 20 | implementation = _capnp_embed_impl, |
| 21 | ) |
| 22 | |
| 23 | def capnp_embed( |
| 24 | name, |
| 25 | src, |
| 26 | visibility = None, |
| 27 | target_compatible_with = None, |
| 28 | deps = []): |
| 29 | """ |
| 30 | Bazel rule to include `src` in a Cap'n Proto search path for embedding. |
| 31 | |
| 32 | This is useful for including embedding the output of a `genrule` in a Cap'n Proto schema. |
| 33 | The generated target should be included in `cc_capnp_library` `deps`. |
| 34 | """ |
| 35 | if target_compatible_with == None: |
| 36 | target_compatible_with = select({ |
| 37 | "@//build/config:no_build": ["@platforms//:incompatible"], |
| 38 | "//conditions:default": [], |
| 39 | }) |
| 40 | |
| 41 | _capnp_embed( |
| 42 | name = name + "_gen", |
| 43 | src = src, |
| 44 | visibility = visibility, |
| 45 | target_compatible_with = target_compatible_with, |
| 46 | deps = deps, |
| 47 | ) |
| 48 | cc_library( |
| 49 | name = name, |
| 50 | target_compatible_with = target_compatible_with, |
| 51 | ) |