Makefile 规则和 If 语句——如何实现?

发布于 2024-11-15 07:37:07 字数 592 浏览 6 评论 0原文

我是 Makefiles 新手,所以请耐心等待。

我需要修改 Makefile,以便某些规则根据变量调用不同的实用程序。

现在,规则如下:

ci:
    [shell syntax for tool (A)]

但现在我需要 ci 根据变量具有不同的语法。所以我在文件顶部定义了一个全局变量:

TOOL = toolA

或者

TOOL = toolB

理想情况下我想要的是这样的东西,但是显然它不起作用

ifeq ($TOOL, toolA)
    ci:
        [syntax for tool (A)]
else
    ci:
        [syntax for tool (B)
endif

有谁知道实现类似的东西的最佳方法这对吗?

谢谢!!

编辑:工具语法比一行更复杂。有时它是多行,而不仅仅是“toolA args等”。抱歉造成混乱!

I'm new to Makefiles so please bear with me.

I need to modify a Makefile so some rules call different utilities depending on a variable.

Right now, a rule looks like:

ci:
    [shell syntax for tool (A)]

But now I need ci to have a different syntax depending on the variable. So I define a global variable at the top of the file:

TOOL = toolA

or

TOOL = toolB

Ideally what I'd like is something like this, but obviously it doesn't work:

ifeq ($TOOL, toolA)
    ci:
        [syntax for tool (A)]
else
    ci:
        [syntax for tool (B)
endif

Does anyone know the best way to implement something like this properly?

Thanks!!

EDIT: The tool syntax is more complicated than one line. Sometimes its multiple lines and not just "toolA args etc etc". Sorry for the confusion!

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

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

发布评论

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

评论(3

戒ㄋ 2024-11-22 07:37:07

您只是缺少一些括号:

ifeq ($(TOOL), toolA)
...

PS 您可以使条件更严格一些(并删除一些冗余):

ci:
ifeq ($(TOOL), toolA)
    [syntax for tool (A)]
else
    [syntax for tool (B)
endif

You're just missing some parentheses:

ifeq ($(TOOL), toolA)
...

P.S. You can make the conditional a little tighter (and remove a little redundancy):

ci:
ifeq ($(TOOL), toolA)
    [syntax for tool (A)]
else
    [syntax for tool (B)
endif
乖乖 2024-11-22 07:37:07

通常,您可以使用宏来完成此操作:

  # put tool A or tool B command line here
  TOOL=...

  ci:
       $(TOOL) args

可以使用诸如 TOOLARGS 宏之类的东西来扩展,例如

  ci:
       $(TOOL) $(TOOLARGS)

然后您可以修改 makefile,或者将宏放在命令行上

  $ make TOOL=... TOOLARGS=...

如果您想封装它,您可以使用 if 来设置参数。

Usually, you do that with a macro:

  # put tool A or tool B command line here
  TOOL=...

  ci:
       $(TOOL) args

That can be extended with something like a TOOLARGS macro, so something like

  ci:
       $(TOOL) $(TOOLARGS)

Then you can modify the makefile, or put the macros on the command line

  $ make TOOL=... TOOLARGS=...

If you want to encapsulate it, you could use an if to set the arguments.

云归处 2024-11-22 07:37:07

试试这个:

TOOLARGS_toolA = -a1 -a2
TOOLARGS_toolB = -b1 -b2

ci:
        $(TOOL) $(TOOLARGS_$(TOOL))

现在如果 TOOLtoolA 它将使用参数 -a1 -a2,如果 TOOLtoolB 然后它将使用参数-b1 -b2

Try this:

TOOLARGS_toolA = -a1 -a2
TOOLARGS_toolB = -b1 -b2

ci:
        $(TOOL) $(TOOLARGS_$(TOOL))

Now if TOOL is toolA it will use the args -a1 -a2, and if TOOL is toolB then it will use the args -b1 -b2.

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