函数中的if条件-Makefile

时间:2019-02-13 10:29:50

标签: makefile

我正在使用makefile,而if条件之类的非常简单的事情并非一帆风顺。它给了我一个不可读的错误。 知道我下面的小功能怎么了吗?

prepare-test-example:
    ifeq ($(ENGINE),'aurora-postgresql')
        @cat examples/example.yaml > /tmp/stack_test.yaml
    else
        @cat examples/example.yaml examples/example_test.yaml > /tmp/stack_test.yaml
    endif

通话:

make test ENGINE=aurora-postgresql
/Library/Developer/CommandLineTools/usr/bin/make prepare-test-example ENGINE=aurora-postgresql
ifeq (aurora-postgresql,'aurora-postgresql')
/bin/sh: -c: line 0: syntax error near unexpected token `aurora-postgresql,'aurora-postgresql''
/bin/sh: -c: line 0: `ifeq (aurora-postgresql,'aurora-postgresql')'
make[1]: *** [prepare-test-example] Error 2

1 个答案:

答案 0 :(得分:2)

您已经缩进ifeq,因此它看起来像make应该传递给外壳的东西。

尝试

ifeq ($(ENGINE),'aurora-postgresql')
     files := examples/example.yaml
else
     files := examples/example.yaml examples/example_test.yaml
endif

prepare-test-example:
    @cat $(files)  > /tmp/stack_test.yaml

prepare-test-example:
    @if [ "$(ENGINE)" = "'aurora-postgresql'" ]; then \
        cat examples/example.yaml \
    ; else \
        cat examples/example.yaml examples/example_test.yaml \
    ; fi > /tmp/stack_test.yaml

为了娱乐,我在后面的示例(纯shell脚本)中重构了重定向。

也许您的意思是ifeq('$(ENGINE)','aurora-postgresql'),它更有意义,并且可以使上述代码有所简化。