TMPL: Line-based templator

Last modification on

Motivation

While working on a simpler alternative to make, I found the need to template build files to select between debug and release builds. Instead of integrating templating into the build system, I figured creating a generalized templator could prevent feature bloat and perform better, since build files would only need to be "compiled" once during initial configuration. The result is tmpl.

Program

The program fits inside a single, small C file (~240 LOC).

The syntax is intentionally similar to that of the C preprocessor, since it is easy to parse, familiar and intuitive.

Key-value pairs for templating are specified via commandline arguments using -D KEY=VALUE, through a config file using -c CONFIG, or the environment.

The source code is available at: https://git.sinitax.com/sinitax/tmpl

Examples

A Makefile to demonstrate templating:

#default PREFIX /usr/local
#default BINDIR /bin
#default CC gcc

#ifdef DEBUG
#define OPT_CFLAGS -Og -g
#else
#define OPT_CFLAGS -O2
#endif

all: test

test: test.c
    #{CC} -o $@ $< #{OPT_CFLAGS} #{EXTRA_CFLAGS}

clean:
    rm -f test

install:
    install -m755 -t "#{DESTDIR}#{PREFIX}#{BINDIR}"

uninstall:
    rm -f "#{DESTDIR}#{PREFIX}#{BINDIR}/test"

.PHONY: all clean install uninstall