85

I want to compile my project with autoconf/automake. There are 2 conditions defined in my configure.ac

AM_CONDITIONAL(HAVE_CLIENT, test $enable-client -eq 1)
AM_CONDITIONAL(HAVE_SERVER, test $enable-server -eq 1)

I want to separate _LIBS from these 2 conditions in Makefile.am

if HAVE_CLIENT

libtest_LIBS = \

    $(top_builddir)/libclient.la

else if HAVE_SERVER

libtest_LIBS = \

    $(top_builddir)/libserver.la

else

libtest_LIBS = 

endif

but else if HAVE_SERVER does NOT work.

How to write 'else if' in makefile.am?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Wind
  • 851
  • 1
  • 6
  • 3

5 Answers5

168

ptomato's code can also be written in a cleaner manner like:

ifeq ($(TARGET_CPU),x86)
  TARGET_CPU_IS_X86 := 1
else ifeq ($(TARGET_CPU),x86_64)
  TARGET_CPU_IS_X86 := 1
else
  TARGET_CPU_IS_X86 := 0
endif

This doesn't answer OP's question but as it's the top result on google, I'm adding it here in case it's useful to anyone else.

blueyed
  • 27,102
  • 4
  • 75
  • 71
R.D.
  • 2,471
  • 2
  • 15
  • 21
  • 7
    but here you need to put two `endif` together at the same level of indentation, which altogether wouldn't look good either –  Dec 27 '12 at 08:34
  • 1
    @amc in the example above a single `endif` is enough, which I've just added. The problem with the indentation is given in all cases (when using a bigger/nested `else` block). – blueyed Mar 16 '15 at 01:34
  • 2
    Continuing in the spirit of “as it’s the top result on google” :) if you’re trying to replicate this, make sure you don’t forget the space before the parenthesis in `else ifeq (…)`. `make` gets confused if it’s missing and will end the conditional early. – Lucas Werkmeister Jun 21 '18 at 10:42
  • 2
    You cannot use this syntax in Makefile.am – Łukasz Daniluk Jul 30 '18 at 07:33
  • 2
    This fails with GNU Make 3.80. Error messages are, *`Extraneous text after 'else' directive.`*. – jww Jul 17 '19 at 06:18
  • It is useful, even ten years on. Thank you. – Bathsheba Mar 29 '23 at 09:27
17

I would accept ldav1s' answer if I were you, but I just want to point out that 'else if' can be written in terms of 'else's and 'if's in any language:

if HAVE_CLIENT
  libtest_LIBS = $(top_builddir)/libclient.la
else
  if HAVE_SERVER
    libtest_LIBS = $(top_builddir)/libserver.la
  else
    libtest_LIBS = 
  endif
endif

(The indentation is for clarity. Don't indent the lines, they won't work.)

ptomato
  • 56,175
  • 13
  • 112
  • 165
  • 1
    I have tried this one, but failed. Is it possible Makefile.am doesn't support recursive if conditions. – Wind Nov 13 '11 at 13:59
  • 4
    Did you remove the indentation? All those statements are supposed to start in column 1. – ptomato Nov 13 '11 at 14:05
  • Sorry, I didn't remove the indentation, I'll try it. – Wind Nov 14 '11 at 02:17
  • 1
    I have just tried that, and it works great. Thanks a lot for your help. – Wind Nov 14 '11 at 02:28
  • 2
    You can use indentation in line 2, 5 and 7. (only with tabs not with spaces) Only the if, else and endif statements do not allow indentation. – ndreisg Dec 19 '17 at 13:21
16
ifeq ($(CHIPSET),8960)
   BLD_ENV_BUILD_ID="8960"
else ifeq ($(CHIPSET),8930)
   BLD_ENV_BUILD_ID="8930"
else ifeq ($(CHIPSET),8064)
   BLD_ENV_BUILD_ID="8064"
else ifeq ($(CHIPSET), 9x15)
   BLD_ENV_BUILD_ID="9615"
else
   BLD_ENV_BUILD_ID=
endif
j0k
  • 22,600
  • 28
  • 79
  • 90
don
  • 185
  • 1
  • 2
  • 5
    This fails with GNU Make 3.80. Error messages are, *`Extraneous text after 'else' directive`*. – jww Jul 17 '19 at 06:19
  • I like this style and this syntax works as written in the version of gnu make I am using. Question: Can this syntax work if the conditional test is for something different on each 'else ifeq' ??? For example could I do this: ifeq ($chipset),1860) BLD_ENV_BUILD_ID="8960" else ifeq ($hostname, "JohnsLaptop") BLD_ENV_BUILD_ID="9615" else BILD_ENV_BUILD_ID= endif – LivingDust Apr 20 '21 at 17:21
7
ifdef $(HAVE_CLIENT)
libtest_LIBS = \
    $(top_builddir)/libclient.la
else
ifdef $(HAVE_SERVER)
libtest_LIBS = \
    $(top_builddir)/libserver.la
else
libtest_LIBS = 
endif
endif

NOTE: DO NOT indent the if then it don't work!

dkjkj
  • 91
  • 1
  • 4
6

As you've discovered, you can't do that. You can do:

libtest_LIBS = 

...

if HAVE_CLIENT
libtest_LIBS += libclient.la
endif

if HAVE_SERVER
libtest_LIBS += libserver.la
endif
ldav1s
  • 15,885
  • 2
  • 53
  • 56
  • Yes, this is good. Thanks for your reply, but actually I have 3 conditions. CLIENT, SERVER, non-CLIENT-non-SERVER. I don't know how to add the 3rd condition in my code. – Wind Nov 13 '11 at 14:03
  • There's actually 4 conditions wrt AM_CONDITIONAL: !CLIENT && !SERVER, !CLIENT && SERVER, CLIENT && SERVER, CLIENT && !SERVER. This answer will work with all these conditions. For the "3rd condition" (!CLIENT && !SERVER) libtest_LIBS is set to the value you wanted in the 'else if'. – ldav1s Nov 14 '11 at 18:05