动态生成目标列表

时间:2016-09-23 13:10:52

标签: makefile gnu-make

如果存在文件,我想添加要构建的目标。如果文件不存在,我希望跳过目标。

一个例子:

FILENAME = f

TARGETS := normal
ifneq($(shell stat test_$(FILENAME).c), "")
  TARGETS += test
endif

all: $(TARGETS)

normal: 
    @echo normal

test: 
    @echo test

我不确定$(shell stat ...)部分是否有效,但更大的问题是make当前文件夹中的任何文件test_f.c都会给出:

Makefile:4: *** multiple target patterns.  Stop.

删除ifneq ... endif块会使目标normal成为目标test。如果test_f.c存在,我该如何才能运行目标 .bootstrap-select.btn-group .dropdown-menu li a:hover { color: whitesmoke !important; background: #bf5279 !important; }

2 个答案:

答案 0 :(得分:1)

你可以做的是生成一个字符串变量(让我们称之为OPTIONAL),这样当'test_f.c'存在时,OPTIONAL=test;否则,OPTIONAL=_nothing_。然后添加OPTIONAL作为all的先决条件。 e.g:

FILENAME = f

TARGETS = normal
OPTIONAL = $(if $(wildcard test_f.c), test, )
all: $(TARGETS) $(OPTIONAL)

normal: 
    @echo normal

test: 
    @echo test

答案 1 :(得分:1)

您还可以使用for循环

迭代目标
.PHONY: all

RECIPES = one

all:    RECIPES += $(if $(wildcard test_f.c), two, )
all:
        for RECIPE in ${RECIPES} ; do \
                $(MAKE) $${RECIPE} ; \
        done
one:
        $(warning "One")

two:
        $(warning "Two")
> make
for RECIPE in one   ; do \
        /Applications/Xcode.app/Contents/Developer/usr/bin/make ${RECIPE} ; \
    done
makefile:11: "One"
make[1]: `one' is up to date.

> touch test_f.c

> make
for RECIPE in one  two ; do \
        /Applications/Xcode.app/Contents/Developer/usr/bin/make ${RECIPE} ; \
    done
makefile:11: "One"
make[1]: `one' is up to date.
makefile:14: "Two"
make[1]: `two' is up to date.
相关问题