5

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!

RaytheonLiszt
  • 225
  • 1
  • 6
  • 15
  • 1
    Consider implementing a tool-neutral (shell?) script that gets told (by argument or other means) what to do, and use that unconditionally. – Jonathan Leffler Jun 14 '11 at 19:05
  • That's what I thought of at first, but I wanted to do this without bringing in outside scripts. Might have to resort to that if I can't get this to work. Thanks though! – RaytheonLiszt Jun 14 '11 at 21:48

3 Answers3

5

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
Beta
  • 96,650
  • 16
  • 149
  • 150
  • Wow thanks! I wasn't that far off! But now I'm having problems with the if statement--but I'll create a new question for that. Thanks! – RaytheonLiszt Jun 14 '11 at 21:53
  • @Yasky: this *is* comparing strings. What are you trying to do? – Beta Jan 15 '13 at 22:28
  • I was trying to compare a string which is an output from `uname`. I resorted to writing a mini shell script in the Makefile though – Igbanam Jan 20 '13 at 23:59
  • @Yasky: I'm not sure what you mean, but if you post it as a question I'm sure someone can help. – Beta Jan 21 '13 at 04:48
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.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • That would work! But like with @mark4o's answer, the "tool syntax" above is more complicated than just one line...it needs several, and sometimes it's not just "toolA". I should have been more clear in my original post. Sorry about that. Thanks though! – RaytheonLiszt Jun 14 '11 at 21:51
  • 1
    @RaytheonLiszt: the more complex the commands, the more merit there is to using a script to cover up the complexity. – Jonathan Leffler Jun 14 '11 at 22:02
  • @Jonathan Leffler: very true. I think if I hit one more roadblock, I'm just going to go your way and use a script. =( – RaytheonLiszt Jun 14 '11 at 22:45
1

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.

mark4o
  • 58,919
  • 18
  • 87
  • 102
  • That is nice! But unfortunately the "tool syntax" above is more complicated than just one line...it needs several, and sometimes it's not just "toolA". I should ahve been more clear in my original post, sorry. Thanks though! – RaytheonLiszt Jun 14 '11 at 21:50