制造:取决于目标的可变需求 [英] Make: variable requirements that depend on the target
问题描述
我有一个Makefile
,看起来像这样:
I have a Makefile
that looks roughly like this:
default: en
.PHONY: default en es
en: NUMBER=number
es: NUMBER=numero
TARGET=$(NUMBER)-word
INTERMEDIATE=$(NUMBER)-tmp
en: number
es: number
number: $(INTERMEDIATE)
touch $(TARGET)
$(INTERMEDIATE):
@echo Please provide file $(INTERMEDIATE)
@exit 1
clean:
rm *-word *-tmp
要使用它,请致电make en
(也是默认值)或make es
.如我所见,此Makefile具有两个主要功能:
To use it, you'd call make en
(also a default) or make es
. As I see it, this Makefile has two main features:
- 用户必须提前提供中间要求(
$(INTERMEDIATE)
). - 此要求是一个变量,其值取决于目标.
运行此命令时,我得到以下信息:
When I run this, I get the following:
$ make
Please provide file number-tmp
make: *** [-tmp] Error 1
这个错误告诉我,对于我想要的变量来说,解决变量的时间太晚了.失败的规则为-tmp
,表示在确定要运行的规则时无法告诉$(NUMBER)
的值.但是,它会在运行规则时得到解决.
This error suggests to me that variables are resolved one stage too late for what I intend. The failing rule is -tmp
, implying it can't tell the value of $(NUMBER)
when deciding what rules to run. However it's resolved by the time it's running the rule.
那我怎么做呢?
推荐答案
文档明确指出,您不能在目标或先决条件内使用特定于目标的变量值:
The documentation is clear that you cannot use target-specific variable values inside targets or prerequisites:
另一个例外是特定于目标的变量值.此功能使您可以根据make当前正在构建的目标为同一变量定义不同的值. 与自动变量一样,这些值仅在目标配方的上下文中(以及在其他特定于目标的分配中)可用.
您可以通过多种方式处理此问题.最简单的方法是将选择更改为使用变量而不是目标,然后使用构造的宏名称.假设您将值存储在LV
变量中(您不应该使用LANG
等,因为这对shell来说是特殊的),如下所示:
There are multiple ways you could handle this. The simplest would be to change the selection to using a variable instead of a target, and use constructed macro names. Suppose you stored the value in the LV
variable (you should not use LANG
etc. because this is special to the shell), like this:
# Must be set on the command line
LV =
en_NUMBER := number
es_NUMBER := numero
TARGET := $($(LV)_NUMBER)-word
INTERMEDIATE := $($(LV)_NUMBER)-tmp
ifeq ($(TARGET),-word)
$(error Missing/invalid LV value: '$(LV)')
endif
number: $(INTERMEDIATE)
touch $(TARGET)
clean:
rm *-word *-tmp
如果运行make LV=en
,将得到number-word
.如果运行make LV=es
,将得到numero-word
.如果运行make
(无设置)或make LV=foo
(无效设置),则会出现错误.
If you run make LV=en
you'll get number-word
. If you run make LV=es
you'll get numero-word
. If you run make
(no setting) or make LV=foo
(invalid setting) you'll get an error.
还有其他方法可以做到这一点,但是如果您对您想要的内容没有完全的了解,我们就无法确定.
There are other ways to do this but without a complete understanding of what you want we can't say for sure.
这篇关于制造:取决于目标的可变需求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!