Makefile gnu make-如何根据某些条件在foreach中定义变量

Makefile gnu make-如何根据某些条件在foreach中定义变量,makefile,gnu-make,Makefile,Gnu Make,我想根据一些条件在foreach循环中分配变量 例如,在实践中,我想使用它检查所有必需的工具(gccg++作为ld),检查它们是否在系统路径上找到。如果是,则保留它,如果不是,则尝试添加用户提供的路径前缀,如果可以在那里找到,则修改变量以使用完整路径,否则将早期错误报告给用户 总的来说,我的答案是: TEST_ARRAY = AA BB K := $(foreach myTest,$(TEST_ARRAY),\ $(if $(filter AA,$(myTest)),\

我想根据一些条件在foreach循环中分配变量


例如,在实践中,我想使用它检查所有必需的工具(gccg++作为ld),检查它们是否在系统路径上找到。如果是,则保留它,如果不是,则尝试添加用户提供的路径前缀,如果可以在那里找到,则修改变量以使用完整路径,否则将早期错误报告给用户

总的来说,我的答案是:

TEST_ARRAY = AA BB
K := $(foreach myTest,$(TEST_ARRAY),\
        $(if $(filter AA,$(myTest)),\
            $(eval $(myTest) := match),\
            $(eval $(myTest) := mismatch)))

$(info "AA: ${AA}")
$(info "BB: ${BB}")
输出为:

"AA: match"
"BB: mismatch"
对于我更具体的问题,答案相当长-工作代码片段如下所示:

#on widows
DEVNUL := NUL
WHICH := where
#on linux
#DEVNUL := /dev/null
#WHICH := which

# set path to be searched when command is not found in system PATH
GNU_PATH := c:\NSS\GNU_Tools_ARM_Embedded\5.4 2016q3\bin2
# optionally set command prefix - for example all your tools are not called "gcc" but "arm-gcc" so you would fill here "arm-" 
GNU_PREFIX := arm-none-eabi-
# set command suffix - for example on windows all executable files have suffix ".exe" 
GNU_SUFFIX := .exe

# escape spaces in path because make $(wildcard ) can not handle them :(
empty :=
space := $(empty) $(empty)
GNU_PATH_ESCAPED := $(subst  $(space),\ ,$(GNU_PATH))

# define used tool-chain commands
CC              := gccx
AS              := as
AR              := ar
LD              := ld
NM              := nm
OBJDUMP         := objdump
OBJCOPY         := objcopy
SIZE            := size

# detect if tool-chain commands are installed and fill correct path (prefer system PATH, then try to find them in suggested GNU_PATH)
# if not found on neither system path nor on user provided GNU_PATH then early error is reported to user
EXECUTABLES = CC AS AR LD NM OBJDUMP OBJCOPY SIZE
$(foreach myTestCommand,$(EXECUTABLES),\
    $(if $(shell ${WHICH} ${GNU_PREFIX}$($(myTestCommand)) 2>${DEVNUL} ),\
        $(eval $(myTestCommand) := ${GNU_PREFIX}$($(myTestCommand))),\
        $(if $(wildcard $(GNU_PATH_ESCAPED)\${GNU_PREFIX}$($(myTestCommand))$(GNU_SUFFIX)),\
            $(eval $(myTestCommand) := '$(GNU_PATH)/${GNU_PREFIX}$($(myTestCommand))$(GNU_SUFFIX)'),\
            $(error "Can not find tool ${GNU_PREFIX}$($(myTestCommand))$(GNU_SUFFIX), either make in in your system PATH or provide correct path in variable GNU_PATH"))))

# just print what tools will be used
$(foreach myTestCommand,$(EXECUTABLES),\
    $(info found tool $($(myTestCommand))))

default:
    @$(CC) --version
在我的测试用例中,除了位于
GNU路径
中提供的用户文件夹中的
gccx
之外,我的路径上有所有工具

found tool 'c:\NSS\GNU_Tools_ARM_Embedded\5.4 2016q3\bin2/arm-none-eabi-gccx.exe'
found tool arm-none-eabi-as
found tool arm-none-eabi-ar
found tool arm-none-eabi-ld
found tool arm-none-eabi-nm
found tool arm-none-eabi-objdump
found tool arm-none-eabi-objcopy
found tool arm-none-eabi-size