1

I have a makefile

PATH = "MyFolder_"{ver}

compile:
    [list_of_commands] $PATH

And run it like this

make ver="1.1" compile

How do I stop compiling in case if no ver specified?
I want something like this

#input
make compile

And then output

No version specified. Compilation terminated.
GriMel
  • 2,272
  • 5
  • 22
  • 40

1 Answers1

2

There are many ways to do it. Partly it depends on which version of make you're using, and which operating system you're running it on (which shell make invokes).

Note you should NOT use a variable PATH in your makefile; that's the system's PATH variable and resetting it will break all your recipes.

Also it's usually a bad idea to include quotes in the variable like that. If you want it quoted then add the quotes in the recipe.

If you have GNU make you can do this:

ifeq ($(ver),)
$(error No version specified.)
endif

If you don't have GNU make but you're using a UNIX system or Windows with a UNIX shell, you can do this:

MYPATH = MyFolder_$(ver)
compile:
        [ -n "$(ver)" ] || { echo "No version specified."; exit 1; }
        [list_of_commands] "$(MYPATH)"

If you're using Windows with Windows command.com then you can do something similar but I'm not sure of the details.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • I have GNU make. I tried the first variant and it throws error "No version specified" even if i specified it `make ver="1.1" compile` throws error =( – GriMel Jan 15 '16 at 13:34
  • @GriMel Make sure you have that *exact* if statement because it should work just fine. – Etan Reisner Jan 15 '16 at 13:41
  • It is probably worth pointing out that the latter solution doesn't prevent *other* targets from being run when `ver` is not set the way the `$(error)` solution does. – Etan Reisner Jan 15 '16 at 13:42
  • @EtanReisner wait, can I put ifeq-endif inside compile or I can only use them outside? – GriMel Jan 15 '16 at 13:55
  • @GriMel Both. But for this snippet I don't think it would make a difference (though outside is likely what I'd do). – Etan Reisner Jan 15 '16 at 13:58
  • @EtanReisner found the solution [here](http://stackoverflow.com/a/4483467/3434076). ifeq-endif should be on the same level as **compile** Could you, please, correct your answer to look like the acccepted in the link? So I'll make your answer accepted. – GriMel Jan 15 '16 at 14:46
  • This isn't my answer. It is MadScientist's. And the answer here doesn't put it inside the recipe. The version in this answer that does go in a recipe that uses a **shell** condition and indented it appropriately. – Etan Reisner Jan 15 '16 at 14:59
  • Just as I showed in my example the ifeq/endif statement _cannot be indented with TAB_. If you indent it with a TAB then it's part of the recipe and will not do what you want. You can put it anywhere you want, as long as you don't indent it with a TAB. It's most instructuve (IMO) if you put it just before your set the `MYPATH` variable. But not required. – MadScientist Jan 15 '16 at 17:06