本文介绍了如何使$(error ...)在GNU Make中有条件地工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不满足某些先决条件,我想使用$(error ...)中止我的制作过程. test -d /foobar失败时,fails_to_work目标应中止.

I'd like to use $(error ...) to abort my make process if certain preconditions aren't met. The fails_to_work target should abort when failing test -d /foobar.

BAD.mk

all: this_works fails_to_work

this_works:
        @echo echo works...
        @test -d ~ || echo ~ is not a directory
        @test -d /foobar || echo /foobar is not a directory

fails_to_work:
        @echo error does not work...
        @test -d ~ || $(error ~ is not a directory)
        @test -d /foobar || $(error /foobar is not a directory)

$ make -f BAD.mk

$ make -f BAD.mk

echo works...
/foobar is not a directory
BAD.mk:9: *** ~ is not a directory.  Stop.

如您所见,即使在屏幕上也没有显示错误不起作用...". fails_to_work的配方在开始之前会失败.我该如何解决?我的用例之一是@test -d $(MY_ENV_VAR),但我认为这与示例中给出的硬编码路径没有什么不同.

As you can see, not even "error does not work..." is echoed to the screen. The recipe for fails_to_work fails before it gets started. How do I solve this? One of my use cases is@test -d $(MY_ENV_VAR), but I don't think that differs from the hard-coded paths given in the example.

更新(版本信息)

$ make --version

$ make --version

GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for x86_64-pc-linux-gnu

推荐答案

您正在尝试将食谱中的shell内容有条件地调用makefile内容,但您发现这是行不通的.

You're trying to get the shell stuff in a recipe to conditionally invoke makefile stuff, which doesn't work, as you've found.

我可以想到两种选择:

  1. 只需删除$(error)内容.如果test失败,则它将返回非零的退出状态,并且Make进程将在该点终止.

  1. Simply remove the $(error) stuff. If test fails, then it will return a non-zero exit status, and the Make process will terminate at that point.

将测试排除在规则之外,并使用Make Conditional(条件调用)(依次调用shell功能),例如:

Take the test out of the rule, and use a Make conditional (which in turn invokes shell functionality), e.g.:

ifeq ($(shell test -d /foobar; echo $$?),1)
$(error Not a directory)
endif

这篇关于如何使$(error ...)在GNU Make中有条件地工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:32