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

8
  • 1
    Seems to work without error messages when using GNU make 4.2.1.
    – Kusalananda
    Sep 1, 2017 at 9:28
  • @Kusalananda this should work in many servers, so it should be make 3.81 compliant too
    – smarber
    Sep 1, 2017 at 9: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? Sep 1, 2017 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? Sep 2, 2017 at 5:44
  • 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 2, 2017 at 18:26

3 Answers 3

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
5
  • 3
    Actually I wanted to use ifdef because the value of toto is not interesting in my case, I only need to know if it's here or not
    – smarber
    Sep 1, 2017 at 9:52
  • 2
    @smarber So is toto supposed to be a target or not? Please edit your question to clarify what you want to do. Sep 1, 2017 at 22:00
  • 1
    @Gilles toto is juste an option that I want to pass to make my_test, if toto is given I do some extra actions
    – smarber
    Sep 2, 2017 at 11:11
  • 1
    @smarber Ok, then amisax's answer is what you need. Sep 2, 2017 at 11:21
  • 1
    Is there any way to not specify that toto=1? Just make my_test toto? Because ifdef checks that toto is defined, not whether it equals 1, 2, or 48 Sep 24, 2022 at 21:27
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

1
  • 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, 2022 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .