How to write multiple conditions in Makefile.am with "else if"

Automake

Automake Problem Overview


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?

Automake Solutions


Solution 1 - Automake

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.

Solution 2 - Automake

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.)

Solution 3 - Automake

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

Solution 4 - Automake

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

Solution 5 - Automake

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWindView Question on Stackoverflow
Solution 1 - AutomakeR.D.View Answer on Stackoverflow
Solution 2 - AutomakeptomatoView Answer on Stackoverflow
Solution 3 - AutomakedonView Answer on Stackoverflow
Solution 4 - Automakeldav1sView Answer on Stackoverflow
Solution 5 - AutomakedkjkjView Answer on Stackoverflow