Subject: How to properly use shell commands that are defined in make(1)
To: None <tech-pkg@netbsd.org>
From: Roland Illig <rillig@NetBSD.org>
List: tech-pkg
Date: 11/24/2005 14:04:04
Quite a few shell programs in pkgsrc use commands similar to how they 
are used in the pkgsrc Makefiles. As it were much better if the tools 
were used not only similar but _equal_ to inside Makefiles, I've written 
a quiet little, but useful make(1) target:

sh-define-cmds:
.for _c_ in ${CMDS}
         @${ECHO} ${_c_:Q}"() { "${${_c_}:Q}" \"\$$@\"; }"
.endfor

It looks cryptic, and indeed it is. But its usage is very simple:

$ make sh-define-cmds CMDS=MAKE
MAKE() { /usr/bin/make "$@"; }

This output can then be evaluated by a shell script using the "." command.

This also works in really weird environments, like the following:

$ make sh-define-cmds \
     CMDS=PKG_DELETE \
     PKGTOOLS_ENV=FOO=bar \
     PKG_DBDIR="fo            bar"
PKG_DELETE() { /usr/bin/env FOO=bar /home/roland/pkg/sbin/pkg_delete -K 
fo\ \ \ \ \ \ \ \ \ \ \ \ bar "$@"; }

(Well, if you try it out right now pkgsrc will print many warnings, but 
that's because there are some bugs left in bsd.prefs.mk.)

I'd like to commit that code. After that, we should change the complete 
pkgsrc infrastructure to use this code. Or something equivalent.

Roland