规则的命令部分中使用的定义中使用的条件的 Makefile 问题

发布于 2024-07-07 08:41:16 字数 754 浏览 5 评论 0原文

我的 makefile 中有一个具有这种结构的部分:


bob:
ifdef DEBUG
    @echo running
endif
    @echo chug chug chug
ifdef DEBUG
    @echo done
endif    

bobit:
    @echo "before"
    @make bob
    @echo "after"

我在这里大大简化了,所有的 echo 实际上都是不平凡的命令块,并且还有更多条件性的东西,但这抓住了我的问题的本质。

出于技术原因,我现在不想进入,我需要摆脱那个子制作,但是因为 echo 代表了不平凡的代码量,我不想只是复制并过去 bob 的主体来代替子品牌。

理想情况下,我想做的是这样的


define BOB_BODY 
ifdef DEBUG
    @echo running
endif
    @echo chug chug chug
ifdef DEBUG
    @echo done
endif
endef

bob:
    $(BOB_BODY)

bobit:
    @echo "before"
    $(BOB_BODY)
    @echo "after"

不幸的是,条件似乎在欺骗我,它们产生“ifdef:命令未找到”错误,我尝试使用 eval 和 call 的各种组合来解决这个问题,但似乎无法找出让它发挥作用的方法。

我该如何进行这项工作? 这是否是解决问题的正确方法?

I have a section of makefile that has this sort of structure:


bob:
ifdef DEBUG
    @echo running
endif
    @echo chug chug chug
ifdef DEBUG
    @echo done
endif    

bobit:
    @echo "before"
    @make bob
    @echo "after"

I'm simplifying greatly here, all the echo's are actually non trivial blocks of commands and there is more conditional stuff, but this captures the essence of my problem.

For technical reasons I don't want to get into right now, I need to get rid of that submake, but because the echo's represent nontrivial amounts of code I don't want to just copy and past the body of bob in place of the submake.

Ideally what I'd like to do is something like this


define BOB_BODY 
ifdef DEBUG
    @echo running
endif
    @echo chug chug chug
ifdef DEBUG
    @echo done
endif
endef

bob:
    $(BOB_BODY)

bobit:
    @echo "before"
    $(BOB_BODY)
    @echo "after"

Unfortunately the conditionals seem to be shafting me, they produce "ifdef: Command not found" errors, I tried getting around this with various combinations of eval and call, but can't seem to figure out a way to get it to work.

How do I make this work? and is it even the right way to approach the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蝶…霜飞 2024-07-14 08:41:16

我解决这个问题的方法是使用 bash 条件语句,这实际上具有一定的意义,因为我们正在使用命令而不是制定规则。

所以我上面的理想解决方案变成了这样


define BOB_BODY
    @if [[ -n "$(DEBUG)" ]]; then \
        echo running; \
    fi;
    @echo chug chug chug
    @if [[ -n "$(DEBUG)" ]]; then \
        echo done; \
    fi
endef

鲍勃: $(BOB_BODY)

博比特: @echo“之前” $(BOB_BODY) @echo“之后”

The way I have fixed this is to use bash conditionals instead, which actually makes a certain amount of sense since we are playing with commands and not make rules.

So my ideal solution from above becomes something like


define BOB_BODY
    @if [[ -n "$(DEBUG)" ]]; then \
        echo running; \
    fi;
    @echo chug chug chug
    @if [[ -n "$(DEBUG)" ]]; then \
        echo done; \
    fi
endef

bob: $(BOB_BODY)

bobit: @echo "before" $(BOB_BODY) @echo "after"

記憶穿過時間隧道 2024-07-14 08:41:16

您只需更改 ifdef/define 的顺序即可:

ifdef DEBUG
  define BOB_BODY 
    @echo running
    @echo chug chug chug
    @echo done
  endef
else 
  define BOB_BODY 
    @echo chug chug chug
  endef
endif

更新

define CHUG
  @echo chug chug chug
endef

ifdef DEBUG
  define BOB_BODY 
    @echo running
    $(CHUG)
    @echo done
  endef
else 
  define BOB_BODY 
    $(CHUG)
  endef
endif

You can simply change the order of ifdef/define:

ifdef DEBUG
  define BOB_BODY 
    @echo running
    @echo chug chug chug
    @echo done
  endef
else 
  define BOB_BODY 
    @echo chug chug chug
  endef
endif

UPDATE

define CHUG
  @echo chug chug chug
endef

ifdef DEBUG
  define BOB_BODY 
    @echo running
    $(CHUG)
    @echo done
  endef
else 
  define BOB_BODY 
    $(CHUG)
  endef
endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文