15

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 '17 at 9:28
  • @Kusalananda this should work in many servers, so it should be make 3.81 compliant too
    – smarber
    Sep 1 '17 at 9:37
  • 3
    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 '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? Sep 2 '17 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 '17 at 18:26
23

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
4
  • 2
    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 '17 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 '17 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 '17 at 11:11
  • 1
    @smarber Ok, then amisax's answer is what you need. Sep 2 '17 at 11:21
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.