GNU Makefiles中是否存在类似python装饰器之类的机制? [英] Is there a mechanism for something like python decorators in GNU Makefiles?

查看:87
本文介绍了GNU Makefiles中是否存在类似python装饰器之类的机制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己在Makefiles中声明GNU make traphons phony的两种可能性之间有些挣扎.

I find myself a little torn between two possibilities to declare GNU make tragets phony in Makefiles.

一个人一口气宣布所有假人:

One is declaring all phonies in one go:

.PHONY: targ1 targ2 targ3

targ1:
     ...

targ2:
     ...

targ3:
     ...

(对我而言)具有更易读和更整洁的优点.但是,人们看不到哪个目标是假的.

Which has the advantage of being more readable (to me) and more tidy. But one can't see quickly which targets are phony.

另一种可能性是声明音律以及规则(直接在前面或后面):

The other possibility is declaring the phoniness along with the rule (directly in front or behind):

.PHONY: targ1
targ1:
     ...

.PHONY: targ2
targ2:
     ...

targ3:
     ...
.PHONY: targ3

(对我来说)哪个更难读.我也不喜欢规则名称的重复.是否有类似python中的函数装饰器的解决方案?像这样:

Which (to me) is harder to read. Also I don't like the duplication of the rule name. Is there a solution like function decorators in python? Something like this:

@pny
targ1:
    ...

@pny
targ2:
    ...

@pny
targ3:
    ...

我怀疑类似这样的事情可能对应用程序有用,而不仅仅是发出规则的假冒,这只是我个人喜好的问题.因此,我的问题的标题更广泛.

I suspect something like this might be more useful for applications other than just making a rule phony, seeing it's just a matter of my personal tastes. Hence the broader title of my question.

推荐答案

我不知道该内置的Make.但是对于(不太便携式,不建议使用)解决方案,您可以执行以下操作:

I'm not aware of a Make built-in for that. But for a (not-too-portable, not advisable) solution you could do something like this:

MAKEFILE = $(lastword $(MAKEFILE_LIST))
.PHONY: $(shell grep -E -A1 "^\s*\#\s*phy" $(MAKEFILE) | \
                     grep -Pio "^[a-z][-_.a-z0-9]+\s*(?=:)")

#phy
targ1:
    ...

#phy
targ2:
    ...

#phy
targ3:
    ...

此解决方案在每个规则上方的行中在Makefile中搜索字符串#phy.它提取规则名称.两者都使用unix程序grep和shell调用.然后将规则名称用作.PHONY的源.

This solution searches the Makefile for the string #phy in the line above every rule. It extracts the rule-name. Both using the unix program grep and a shell call. The rule names are then taken as sources for the .PHONY.

要创建更通用的装饰器",可以将其与以下答案中的技术结合: https://stackoverflow.com/a/36941727/8655091 ,即根据输入参数使用define来构建生成规则文本的部分.然后,在foreach循环中call -ed定义该定义,以自动创建规则.

To make a more general "decorator", you could combine it with techniques from this answer: https://stackoverflow.com/a/36941727/8655091, i.e. the part where define is used to build the text of a make rules, dependent on an input parameter. That define is then later call-ed in a foreach loop to acutally create the rule.

但是,grep(尤其是带有-P选项的)可能并非在每个系统上都存在.另外,包含子makefile可能会引起问题.另外,大多数遇到此 DIRTY HACK 的人都会想伤害您.

However, grep especially with the -P option might not be present on every system. Also, including sub-makefiles might make problems. Also, most people encountering this DIRTY HACK, will want to harm you.

这篇关于GNU Makefiles中是否存在类似python装饰器之类的机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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