Skip to content

Commit

Permalink
build: Generate files from subst templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaqx0r committed Apr 8, 2024
1 parent 10faa58 commit afb9c6a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
34 changes: 34 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
load("@buildifier_prebuilt//:rules.bzl", "buildifier")
load("//:subst.bzl", "subst_template")

package(default_visibility = [":__subpackages__"])

VERSION = "0.13"

cc_library(
name = "filter",
srcs = ["filter.c"],
Expand Down Expand Up @@ -63,6 +66,37 @@ cc_binary(
],
)

SUBSTITUTIONS = {
"@sysconfdir@": "/etc",
"@pkgexdir@": "/usr/share/doc/filergen/examples",
"@sbindir@": "/usr/sbin",
"@VERSION@": VERSION,
}

subst_template(
name = "fgadm",
src = "fgadm.in",
substitutions = SUBSTITUTIONS,
)

subst_template(
name = "fgadm.conf",
src = "fgadm.conf.in",
substitutions = SUBSTITUTIONS,
)

subst_template(
name = "rules.filter",
src = "rules.filter.in",
substitutions = SUBSTITUTIONS,
)

subst_template(
name = "filtergen.spec",
src = "filtergen.spec.in",
substitutions = SUBSTITUTIONS,
)

buildifier(
name = "buildifier",
exclude_patterns = [
Expand Down
27 changes: 27 additions & 0 deletions subst.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Generate files from templates with substitutions."""

def _subst_template_impl(ctx):
"""Implementation of substitution from template."""
output = ctx.actions.declare_file(ctx.label.name)
ctx.actions.expand_template(
template = ctx.file.src,
output = output,
substitutions = ctx.attr.substitutions,
)
return [
DefaultInfo(files = depset([output])),
]

subst_template = rule(
implementation = _subst_template_impl,
attrs = {
"src": attr.label(
doc = "Source template to generate from.",
mandatory = True,
allow_single_file = True,
),
"substitutions": attr.string_dict(
doc = "Substitutions to apply to the template",
),
},
)

0 comments on commit afb9c6a

Please sign in to comment.