views:

251

answers:

1

Hi,

I am kind of stuck here. We have two makefiles (A requirement that I can't change)

  • defs.mk : It contains the source file names & their extra compile flags (apart from the standard flags) e.g: C_FILES = c/src/main/rule_main.c rule_main_OPTIONAL_FLAG = +w127 rule_main_DEBUG = TRUE

  • Makefile : It contains all the rules.

Now I want to add a facility so that I can define file specific flags (and optional file specific debug flag) as in :

CUSTOM_DEBUG_FLAG = $($(basename $(notdir $@))_DEBUG) ## rule_main_DEBUG macro from defs.mk
ifeq ($(CUSTOM_DEBUG_FLAG),TRUE)
  do something
endif

But this is not working since expansion of automatic variables is not supported within conditionals. Is there any other way to do it ?

TIA, Saurabh

+3  A: 

I usually take advantage of conditional functions:

SPECIFIC_FLAGS=$(if $(findstring $(CUSTOM_FLAG),TRUE),$(IF_TRUE),$(IF_FALSE))

Or use call function to define my own function:

debug_defs=$(if $(findstring $(1),file1 file2),-DDEBUG,-DNDEBUG)

%.o: src/$$(notdir %).c
    @cc -c $(CFLAGS) $(call debug_defs,$(notdir $(basename $@))
Alex B
Thank you, conditional functions work as a charm.-Saurabh
Saurabh