An example to illustrate my question:
Top level makefile
rootdir = $(realpath .)
export includedir = $(rootdir)/include
default:
@$(MAKE) --directory
Didier's answer is the best one, but the following might give you some ideas:
includedir=/a/b/c/d
currentdir=/a/b/e/f/g
up=; while ! expr $includedir : $currentdir >/dev/null; do up=../$up; currentdir=`dirname $currentdir`; done; relative=$up`expr $includedir : $currentdir'/*\(.*\)'`
echo "up=$up currentdir=$currentdir, relative=$relative"
Sorted!
(no-one said it had to be pretty...)