有如下makefile脚本:
# Rules
.PHONY : test
# ==============================================================================
ifeq ($(COCO_TESTCASE), ust_discovery_test)
UST_SYS_XML ?= system-ss_picocom.xml
else
export UST_SYS_XML ?= system-discovered.xml
endif
#.EXPORT_ALL_VARIABLES:
test:
bash ./test.sh
其中 test.sh 内容如下:
#!/bin/bash -fe
echo "debug point " $UST_SYS_XML
如果在Makefile所在的目录下执行make test COCO_TESTCASE=ust_discovery_test命令,会打印期待的结果,不管变量UST_SYS_XML有没有用export标记,
debug point system-ss_picocom.xml
如果在Makefile所在目录的上层目录执行make -f xx/Makefile test COCO_TESTCASE=ust_discovery_test, 则test.sh中不能正确拿到UST_SYS_XML变量,
UST_SYS_XML: Undefined variable.
有谁知道为啥吗?
找到了一种解决办法,是添加.EXPORT_ALL_VARIABLES:
# Rules
.PHONY : test
# ==============================================================================
ifeq ($(COCO_TESTCASE), ust_discovery_test)
UST_SYS_XML ?= system-ss_picocom.xml
else
export UST_SYS_XML ?= system-discovered.xml
endif
.EXPORT_ALL_VARIABLES:
test:
bash ./test.sh
'.EXPORT_ALL_VARIABLES'
Simply by being mentioned as a target, this tells 'make' to export all variables to child processes by default. *Note Communicating Variables to a Sub-'make': Variables/Recursion.