How to make make file dependent on build options

Once upon a time, I remember that I used some Solaris product, and they had an ingenious option that would automatically detect changes to the compiler options and properly rebuild all the rules. For example, suppose I switch from:

g ++ -O3

to

g ++ -g

Then all files must be recompiled. I am using gnu make and have not found such a function, and I was wondering if anyone has a way to make it work.

+3
source share
3 answers

gmake gcc . Makefile , gcc, ( ).

+4

makefile:

all: a.out

a.out: boo.c Makefile
        cc -O3 -g boo.c
+1

( , , . ):

-include CFLAGS.save

CFLAGS := -O2 -g

all: foo

CFLAGS.save:
    echo 'CFLAGS_SAVE := $(CFLAGS)' > $@
ifeq ($(CFLAGS),$(CFLAGS_SAVE))
%.o: %.c CFLAGS.save
    gcc $(CFLAGS_SAVE) -c -o $@ $<
else
.PHONY: CFLAGS.save
%.o: %.c CFLAGS.save
    $(MAKE) $@
endif

foo: foo.o
    gcc -o $@ $^

: CFLAGS CFLAGS.save. CFLAGS CFLAGS_SAVE, . , CFLAGS.save , make . , CFLAGS , , . make . .

, CFLAGS, . , $(origin) CFLAGS, , . .

makeshould be simple. Because of this, distributors have enough problems understanding abuses on the part of tool collectors (unfortunately, most of the fault is aligned on a level automake). Please just say no to Cthulhoid build systems.

It make clean all CFLAGS='-Whatever -foo'will also work just as well.

+1
source

Source: https://habr.com/ru/post/1770922/


All Articles