Using Cygwin v3.5.3, Makefile (GNU Make v4.4.1) on Win11, I have a common canned recipe that is being called from various other targets with different variables.
MYDate = date -u +'$1: %d-%m-%Y %H:%M:%S'
define cand-recipe =
$(call MYDate, $(var1))
$ echo -e "nIn the canned recipe"
$ rsync -aqh $(sdir) $(ddir) 2> $(elog)
endef
I call this canned recipe with targets like:
mytarget: var1 := some value
mytarget: sdir := $(SRCDir)
mytarget: ddir := $(DSTDir)
mytarget: elog := $(ERRlogfile)
mytarget:
$(cand-recipe)
All this is working fine but if I try to add more info with if-else bash statements or process error log then make reports syntax errors or reports error:
*** prerequisites cannot be defined in recipes. Stop.
MYDate = date -u +'$1: %d-%m-%Y %H:%M:%S'
define cand-recipe =
$(call MYDate, $(var1))
$ echo -e "nIn the canned recipe"
$ rsync -aqh $(sdir) $(ddir) 2> $(elog)
$ if [ -s $(elog) ]; then
$ $(call MYDate, ERROR:);
$ $(eval elogvar := $(file < $(elog)));
$ echo -e "n$(elogvar)n";
$ $(file >> $(elog),$(call MYDate, ERROR:));
fi
endef
I’m just trying to read the elog
file. If this error log file is NOT empty then I print date on terminal, put the contents of the error log file into a variable, print the contents of error log file on terminal and append the date to the error log file.