GNU Make:如何用空格分隔的字符串设置数组? [英] GNU Make: How to set an array from a space-separated string?

查看:49
本文介绍了GNU Make:如何用空格分隔的字符串设置数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写终端 任何匹配模式规则,即%:: ,正如预期的那样,仅在没有其他 target 匹配时才运行.在它的 recipe 中,我想遍历makefile的显式目标,并检查找到的模式( $ * )是否是任何其他目标

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

现在,我已经成功地将所有所需的目标保存在以空格分隔的字符串中,并将其存储在变量 TARGETS 中,但是我无法将其转换为数组,以便能够遍历字符串中的每个单词.

By now I'm successfully getting all desired targets in a space-separated string and storing it in a variable TARGETS, however I couldn't turn it in an array to be able to iterate over each word in the string.

例如

%::
   $(eval TARGETS ::= $(shell grep -Ph "^[^\t].*::.*##" ./Makefile | cut -d : -f 1 | sort))
   echo $(TARGETS)

给了我我所期望的:

build clean compile deploy execute init run serve

问题

我该如何遍历 GNU Make 4.2.1 循环中的每个 $(TARGET)字符串词?

The Question

How could I iterate over each of $(TARGET) string words inside a GNU Make 4.2.1 loop?

我找到了一堆BASH解决方案,但是在我的测试中没有一个起作用:

推荐答案

在食谱中使用 eval shell 通常是一个非常糟糕的主意.食谱已经是 一个Shell脚本,因此您应该只使用Shell脚本即可.

It's generally a really bad idea to use eval and shell inside a recipe. A recipe is already a shell script so you should just use shell scripting.

目前尚不清楚您要做什么.如果要在配方中执行此操作,则可以使用Shell循环:

It's not really clear exactly what you want to do. If you want to do this in a recipe, you can use a shell loop:

%::
        TARGETS=$$(grep -Ph "^[^\t].*::.*##" ./Makefile | cut -d : -f 1 | sort); \
        for t in $$TARGETS; do \
             echo $$t; \
        done

如果要在食谱的外部中执行此操作,则可以使用GNU make的 foreach 函数.

If you want to do it outside of a recipe you can use the GNU make foreach function.

这篇关于GNU Make:如何用空格分隔的字符串设置数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆