GNU Make:如何从 space 分隔的字符串设置数组?
GNU Make: How to set an array from a space-separated string?
I'm writing a Terminal Match-Anything Pattern Rule, i.e. %::
, that, as expected, will run only if no other target is matched. In its recipe I want to iterate over makefile's explicit targets and check if the found pattern ($*
) is the beginning of any other target
到目前为止,我已经成功地在 space 分隔的字符串 中获取了所有需要的目标,并将其存储在变量 TARGETS
中,但是我做不到' 将其转换为数组,以便能够遍历字符串中的每个单词。
For instance
%::
$(eval TARGETS ::= $(shell grep -Ph "^[^\t].*::.*##" ./Makefile | cut -d : -f 1 | sort))
echo $(TARGETS)
gives me just what I was expecting:
build clean compile deploy execute init run serve
问题
如何在 GNU Make 4.2.1
循环中遍历每个 $(TARGET)
字符串单词?
我找到了一堆 BASH 解决方案,但其中 none 在我的测试中有效:
- Reading a delimited string into an array in Bash
- How to split one string into multiple strings separated by at least >one space in bash shell?
在食谱中使用 eval
和 shell
通常是一个非常糟糕的主意。食谱是已经一个shell脚本,所以你应该只使用shell脚本。
不太清楚你到底想做什么。如果你想在食谱中这样做,你可以使用 shell 循环:
%::
TARGETS=$$(grep -Ph "^[^\t].*::.*##" ./Makefile | cut -d : -f 1 | sort); \
for t in $$TARGETS; do \
echo $$t; \
done
如果你想在食谱之外 执行它,你可以使用 GNU make foreach
函数。
I'm writing a Terminal Match-Anything Pattern Rule, i.e.
%::
, that, as expected, will run only if no other target is matched. In its recipe I want to iterate over makefile's explicit targets and check if the found pattern ($*
) is the beginning of any other target
到目前为止,我已经成功地在 space 分隔的字符串 中获取了所有需要的目标,并将其存储在变量 TARGETS
中,但是我做不到' 将其转换为数组,以便能够遍历字符串中的每个单词。
For instance
%:: $(eval TARGETS ::= $(shell grep -Ph "^[^\t].*::.*##" ./Makefile | cut -d : -f 1 | sort)) echo $(TARGETS)
gives me just what I was expecting:
build clean compile deploy execute init run serve
问题
如何在 GNU Make 4.2.1
循环中遍历每个 $(TARGET)
字符串单词?
我找到了一堆 BASH 解决方案,但其中 none 在我的测试中有效:
- Reading a delimited string into an array in Bash
- How to split one string into multiple strings separated by at least >one space in bash shell?
在食谱中使用 eval
和 shell
通常是一个非常糟糕的主意。食谱是已经一个shell脚本,所以你应该只使用shell脚本。
不太清楚你到底想做什么。如果你想在食谱中这样做,你可以使用 shell 循环:
%::
TARGETS=$$(grep -Ph "^[^\t].*::.*##" ./Makefile | cut -d : -f 1 | sort); \
for t in $$TARGETS; do \
echo $$t; \
done
如果你想在食谱之外 执行它,你可以使用 GNU make foreach
函数。