user3053087
user3053087

Reputation: 87

conditionally define an variable in a Makefile

I have a tag in my Makefile as follows:

%.o: %.c
    CFLAGS="$(CFLAGS)"
    $(CC) -c $(CFLAGS) $(STACK_OPTS) $(SIO_FLAGS) $(IO_FLAGS) $(LOCAL_INCLUDES) $(OAK_FLAGS) -DSYNC_WRITE $(OAK_LIBS) $<
    $(CC) -MM $(CFLAGS) $(OAK_FLAGS) $(LOCAL_INCLUDES) $^ > $*.d
    @cp -f $*.d $*.d.tmp
    @sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | \
      sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
    @rm -f $*.d.tmp

I need to CONDITIONALLY add another define variable for a group of .c source file and have it set ONLY for those ones. Can that be done? I presume it would be inserted after the CFLAGS= line.

Any ideas? The var I want to define is FOO_OTO. Just needs to be defined not set to any value.

Upvotes: 0

Views: 751

Answers (2)

MadScientist
MadScientist

Reputation: 100836

You can set variables on a per-target basis (assuming GNU make) using target-specific variables. So if you have a list of source files in FOO_SRC and you want to add some extra flags to them, you can do something like this:

EXTRA_CFLAGS =

FOO_SRC = foo.c bar.c baz.c

$(FOO_SRC:.c=.o): EXTRA_CFLAGS = -DEXTRA

Then add $(EXTRA_CFLAGS) to your compile rule. By the way, it's useless to have CFLAGS="$(CFLAGS)" in your rule. This does nothing (except cost you some performance).

Upvotes: 2

Ola
Ola

Reputation: 827

If you have a variable holding the names of the files you need like so:

FILES_WITH_FOO_OTO = foo.c bar.c

You can use the following construct:

$(CC) -c $(CFLAGS) $(if $(filter $<,$(FILES_WITH_FOO_OTO)), -DFOO_OTO)

This is a complete rewrite of my answer as I apparently misunderstood the qusetion.

Upvotes: 0

Related Questions