22

Makefile

my_test:
ifdef $(toto)
        @echo 'toto is defined'
else
        @echo 'no toto around'
endif

Expected behavior

$ make my_test
no toto around

$ make my_test toto
toto is defined

Current behavior

$ make my_test
no toto around

$ make my_test toto
no toto around
make: *** No rule to make target `toto'.  Stop.

When I run make my_test I get the else text as expected no toto around. However

make my_test toto
no toto around
make: *** No rule to make target `toto'.  Stop.

Makefile version

 $ make -v
   GNU Make 3.81

SLE version

$ cat /etc/*release
  VERSION_ID="11.4"
  PRETTY_NAME="SUSE Linux Enterprise Server 11 SP4"

PS

The point is to make make my_test verbose if toto, if toto not given then the command will run silently

xhienne
  • 17,075
  • 2
  • 52
  • 68
smarber
  • 1,181
  • 2
  • 12
  • 25
  • 1
    Seems to work without error messages when using GNU `make` 4.2.1. – Kusalananda Sep 01 '17 at 09:28
  • @Kusalananda this should work in many servers, so it should be make 3.81 compliant too – smarber Sep 01 '17 at 09:37
  • 4
    In what way does make “behave weirdly”? You ask to build `toto`, there's no rule to build `toto`, make tells you that there's no rule to build `toto`. What else did you expect? – Gilles 'SO- stop being evil' Sep 01 '17 at 21:59
  • If you want Make to check if `toto` is defined then you'll need to define it - `make my_test toto=1` might be what you're after? – garethTheRed Sep 02 '17 at 05:44
  • @garethTheRed yes that works for me but the value of toto isn't interesting for me, all I need is "is toto passed to my command or not" – smarber Sep 02 '17 at 11:13
  • @smarber It matters to `make` whether `toto` was passed as a target or as a variable assignment on the command line. – Kusalananda Sep 02 '17 at 12:04
  • @Kusalananda I see. In this case isn't it possible to pass an option/flag like `-t` ? – smarber Sep 02 '17 at 18:20
  • 2
    @smarber You can't pass arguments to `make` without `make` interpreting them as its own command line flags, targets or variable assignments. – Kusalananda Sep 02 '17 at 18:26

3 Answers3

32

You need to remove the dollar around toto, and also pass toto from the command line differently

Command line

make toto=1  my_test

Makefile

my_test:
ifdef toto
        @echo 'toto is defined'
else
        @echo 'no toto around'
endif
amisax
  • 2,957
  • 17
  • 23
0

You can use these Makefile contents, trick is the filter function:

my_test:
ifeq (toto, $(filter toto,$(MAKECMDGOALS)))
        @echo 'toto is defined'
else
        @echo 'no toto around'
endif
        @echo run command $(if $(filter toto,$(MAKECMDGOALS)),--verbose,--normally)

%:
        @:

Results:

$ make my_test
no toto around
run command --normally

$ make my_test toto
toto is defined
run command --verbose

$ make toto my_test
toto is defined
run command --verbose

$ make my_test totofofo
no toto around
run command --normally

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
Alex Stragies
  • 5,857
  • 2
  • 32
  • 56
  • Interesting, but the problem is that it silenced any errors when misspelling a command: Now `make my_test toto` works but `make mytest toto` returns nothing... – x0s Nov 29 '22 at 16:29
0

Makefile can be trick, normaly i do some thing like

# turn them into do-nothing targets
$(eval toto:;@:)

my_test:
ifeq (toto, $(filter toto,$(MAKECMDGOALS)))
        @echo 'toto is defined'
else
        @echo 'no toto around'
endif
dnetto
  • 1
  • 2