简体   繁体   English

在目标依赖项之前设置Makefile变量

[英]Set Makefile variable before target dependency

I'm working on a Makefile for building one or all targets from a list TARGETS (in this example, RPMS for one or many architectures). 我正在制作一个Makefile,用于从TARGETS列表中构建一个或所有TARGETS (在此示例中,是针对一种或多种体系结构的RPMS)。

If the variable TARGET is given, that particular RPM is built. 如果给定了变量TARGET ,则将构建该特定RPM。 If the variable TARGET is not set, the rule for each TARGET in TARGETS would be invoked, ie: 如果未设置变量TARGET ,则将调用TARGETS中每个TARGET的规则,即:

TARGETS = x86_64 i686
ifdef TARGET
all: $(TARGET)
else
all: $(TARGETS)
endif

<magical part:>
$(TARGETS): TARGET = $@: rpm

rpm:
  rpmbuild --target=$(TARGET) SPECS/foo.spec

I also tried: 我也尝试过:

TARGETS = x86_64 i686
ifdef TARGET
all: $(TARGET)
else
all: $(TARGETS)
endif

<magical part:>
$(TARGETS): TARGET = $@
$(TARGETS): rpm

rpm:
  rpmbuild --target=$(TARGET) SPECS/foo.spec

Can this be done, or would I be better off calling make anew for each target, eg: 可以做到这一点,还是最好对每个目标调用make new,例如:

TARGETS = x86_64 i686
ifdef TARGET
all: rpm
else
all: $(TARGETS)
endif

$(TARGETS):
  make TARGET=$@

rpm:
  rpmbuild --target=$(TARGET) SPECS/foo.spec

Ideal invocation would be TARGET=x86_64 make equivalent to make x86_64 . 理想的调用是使TARGET=x86_64 makemake x86_64等效。

I'm not entirely sure why you want to do this, but here's one way to do it: 我不确定您为什么要这样做,但是这是一种方法:

TARGETS = a b c
ifdef TARGET
all: $(TARGET)
else
all: $(TARGETS)
endif

$(sort $(TARGETS) $(TARGET)):
    @echo $@

The sort function removes duplicates. sort功能删除重复项。 If your TARGET is one of the elements in TARGETS , Make will complain about a duplication in the target list, unless you remove it. 如果TARGETTARGETS中的元素之一,则Make会抱怨目标列表中的重复项,除非将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM