diff --git a/BUILD b/BUILD index 14c4fbc..aa916ed 100644 --- a/BUILD +++ b/BUILD @@ -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"], @@ -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 = [ diff --git a/subst.bzl b/subst.bzl new file mode 100644 index 0000000..b112605 --- /dev/null +++ b/subst.bzl @@ -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", + ), + }, +)