4 ms·
Here is a GNU Makefile snippet I produced many years ago. It lets you have files preprocessed with the shell, C preprocessor (GNU cpp) or M4: %: %.ct cpp
by kazinator 8y ago
Here is a GNU Makefile snippet I produced many years ago. It lets you have files preprocessed with the shell, C preprocessor (GNU cpp) or M4:
%: %.ct
cpp -P $(shell env | sed -e "s/.*/-D_ENV_'&'/") $< > $@
%: %.st
( export __FILE__=$< ; echo "cat <<!" ; cat $< ; echo "!" ) | bash > $@
%: %.mt
rm -f $@.err
m4 -D__FILE__=$< $(shell env | sed -e "s/\([^=]*\)=\(.*\)/-D'_ENV_\\1=\\2'/") $< > $@
if [ -s $@.err ] ; then cat $@.err ; exit 1 ; fi
rm -f $@.err
.ct files are "C preprocessor Templates". Environment variables are available as preprocessor symbols prefixed with _ENV.
.st files are "Shell here doc Templates". The content of the file is treated as the body of a here doc. The name of the file is available as __FILE__; other environoment variables are present naturally under their ordinary names.
.mt files are "M4 Template". The __FILE__ macro is available with the name of the file. The environment is available as _ENV_* namespaced macros, like in the .ct case.
I used this in a from-scratch embedded distro for preprocessing files in /etc and such.