Recursion 在GNU makefile中扩展变量时如何限制递归深度?

Recursion 在GNU makefile中扩展变量时如何限制递归深度?,recursion,makefile,build,gnu-make,Recursion,Makefile,Build,Gnu Make,我需要GNU makefile的宏(变量),它通过给定掩码在一些顶级目录中搜索文件/目录。例如,当前工作目录是/home/sysop/powerup/native/apps/toopl。还存在目录/home/sysop/powerup/native/SDK/build。我想找到SDK/build目录相对于当前目录的位置。因此,我为此编写了递归宏: upfind = $(if $(wildcard $(1)),$(1),$(if $(filter $(abspath $(1)),$(abspath

我需要GNU makefile的宏(变量),它通过给定掩码在一些顶级目录中搜索文件/目录。例如,当前工作目录是/home/sysop/powerup/native/apps/toopl。还存在目录/home/sysop/powerup/native/SDK/build。我想找到SDK/build目录相对于当前目录的位置。因此,我为此编写了递归宏:

upfind = $(if $(wildcard $(1)),$(1),$(if $(filter $(abspath $(1)),$(abspath ../$(1))),$(error "can't find $(1)"),$(call upfind,../$(1))))
我现在可以通过以下方式使用它:

relpath = $(call upfind, ../SDK/build)
这将为relpath变量赋值“../../SDK/build”


一切正常,但我需要将这样的宏传播到多个makefile,所以我正在寻找最小化它的方法(upfind macro)。我希望,有人能建议我如何以更简洁的方式重写这个宏。例如,在某种程度上限制递归是不够的,不需要使用$(abspath)宏。但是如何确定递归级别或度量参数($(1))长度呢?

不确定您在这里要问什么,所以这可能不是答案

递归限制非常简单。 首先,整理一下:

assert-root = $(if $(filter $(abspath $1),$(abspath ../$1)),$(error Can't find $1))
upfind = $(if $(wildcard $1),$1,${assert-root}$(call upfind,../$1))
(注意如何不调用
$assert root
,它只是继承现有的
$1
) 更清楚地说明了如何限制递归深度:只需传递一个不断加长的
$2

maxup := 3
assert-depth = $(if $(filter ${maxup},$(words $2)),$(error Can't find [$1] within ${maxup} parents))
upfind = $(if $(wildcard $1),$1,${assert-depth}$(call upfind,../$1,_ $2))
如果你愿意,可以同时做这两件事

upfind = $(if $(wildcard $1),$1,${assert-depth}${assert-root}$(call upfind,../$1,_ $2))

不确定你在问什么,所以这可能不是答案

递归限制非常简单。 首先,整理一下:

assert-root = $(if $(filter $(abspath $1),$(abspath ../$1)),$(error Can't find $1))
upfind = $(if $(wildcard $1),$1,${assert-root}$(call upfind,../$1))
(注意如何不调用
$assert root
,它只是继承现有的
$1
) 更清楚地说明了如何限制递归深度:只需传递一个不断加长的
$2

maxup := 3
assert-depth = $(if $(filter ${maxup},$(words $2)),$(error Can't find [$1] within ${maxup} parents))
upfind = $(if $(wildcard $1),$1,${assert-depth}$(call upfind,../$1,_ $2))
如果你愿意,可以同时做这两件事

upfind = $(if $(wildcard $1),$1,${assert-depth}${assert-root}$(call upfind,../$1,_ $2))