eddy_em: (Default)
eddy_em ([personal profile] eddy_em) wrote2022-06-14 08:34 pm
Entry tags:

Полезный Makefile

Переделал свой шаблон Makefile, чтобы если цель — debug, не нужно было каждый раз писать make debug: текущая цель сохраняется в файле. Чтобы сменить цель, сначала делаем make clean, затем уже make goal.
# run `make DEF=...` to add extra defines
PROGRAM := runscope
LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all
LDFLAGS += -lusefull_macros
SRCS := $(wildcard *.c)
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
OBJDIR := mk
CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu99
OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o))
DEPS := $(OBJS:.o=.d)
TARGFILE := $(OBJDIR)/TARGET
CC = gcc
#TARGET := RELEASE

ifeq ($(shell test -e $(TARGFILE) && echo -n yes),yes)
        TARGET := $(file < $(TARGFILE))
else
        TARGET := release
endif

all: 
        @make $(TARGET)

release: $(PROGRAM)

debug: CFLAGS += -DEBUG -Werror
debug: TARGET := debug
debug: $(PROGRAM)

$(TARGFILE): $(OBJDIR)
        @echo -e "\t\tTARGET: $(TARGET)"
        @echo "$(TARGET)" > $(TARGFILE)

$(PROGRAM) : $(TARGFILE) $(OBJS)
        @echo -e "\t\tLD $(PROGRAM)"
        $(CC) $(LDFLAGS) $(OBJS) -o $(PROGRAM)

$(OBJDIR):
        @mkdir $(OBJDIR)

ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif

$(OBJDIR)/%.o: %.c
        @echo -e "\t\tCC $<"
        $(CC) -MD -c $(LDFLAGS) $(CFLAGS) $(DEFINES) -o $@ $<

clean:
        @echo -e "\t\tCLEAN"
        @rm -rf $(OBJDIR) 2>/dev/null || true

xclean: clean
        @rm -f $(PROGRAM)

.PHONY: clean xclean

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org