gcc preprocessor website generation
Mon, 07 Mar 2011 08:23 categories: codeAt some point I was helping a friend with doing a website for his stepfather. Since it was only five pages I wanted to have something very simple to generate html pages from a header, a body and footer where header and footer would of course be the same for every page.
My "brilliant" idea was to combine the gcc preprocessor with make to achieve this and here is the Makefile i wrote:
CC=gcc
CFLAGS=-x c -E -P
SOURCES:=$(wildcard *.tmpl)
INCLUDES:=$(wildcard *.incl)
OBJS:=$(patsubst %.tmpl, %.html, $(SOURCES))
all: $(OBJS)
%.html: %.tmpl $(INCLUDES)
$(CC) $(CFLAGS) -o $@ $<
.PHONY: clean
clean:
rm -f *.html
The CFLAGS
are forcing gcc to treat the files as c code, to only run the
preprocessor and to not generate line markers in the output.
Files named *.incl are for example header.incl
and footer.incl
and are
included by *.tmpl files. Every *.tmpl file is turned into a *.html file and
the *.tmpl files can contain statements like:
#include "header.incl"
<p>some text</p>
#include "footer.incl"
And of course due to the nature of make a *.html file will only be regenerated if one of it's dependendcies (the corresponding *.tmpl or one of the *.incl) change. No speed improvement for my five html pages setup but it's the idea that counts here ;)