Makefile Rules and If Statements — How?

こ雲淡風輕ζ 提交于 2019-12-23 10:17:23

问题


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!


回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/6348643/makefile-rules-and-if-statements-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!