(Emacs/config)~Makefile auto insert now generates dependencies

Using -M* options in gcc and clang we can generate dependencies for C
files which can be used by make to rebuild files based on other files.
This commit is contained in:
2024-04-16 22:03:52 +06:30
parent 98626cf2a8
commit f735120b97

View File

@@ -1081,30 +1081,28 @@ change it for C++.
"CC=gcc "CC=gcc
CFLAGS=-Wall -Wextra -Werror -Wswitch-enum -ggdb -fsanitize=address -std=c11 CFLAGS=-Wall -Wextra -Werror -Wswitch-enum -ggdb -fsanitize=address -std=c11
LIBS= LIBS=
ARGS= ARGS=
OUT=main.out OUT=main.out
SRC=src SRC=src
DIST=build DIST=build
CODE=$(addprefix $(SRC)/, ) # add source files here
CODE=$(addprefix $(SRC)/, main.c)
OBJECTS=$(CODE:$(SRC)/%.c=$(DIST)/%.o) OBJECTS=$(CODE:$(SRC)/%.c=$(DIST)/%.o)
DEPS=$(OBJECTS:%.o=%.d) DEPDIR:=$(DIST)/dependencies
DEPFLAGS=-MT $@ -MMD -MP -MF
DEPS:=$(CODE:$(SRC)/%.c=$(DEPDIR):%.d) $(DEPDIR)/main.d
.PHONY: all .PHONY: all
all: $(OUT) all: $(OUT)
$(OUT): $(DIST)/$(OUT) $(OUT): $(DIST)/$(OUT)
$(DIST)/$(OUT): $(OBJECTS) $(DIST)/$(OUT): $(OBJECTS) $(SRC)/main.o | $(DIST)
mkdir -p $(DIST)
$(CC) $(CFLAGS) $^ -o $@ $(LIBS) $(CC) $(CFLAGS) $^ -o $@ $(LIBS)
-include $(DEPS) $(DIST)/%.o: $(SRC)/%.c | $(DIST) $(DEPDIR)
$(CC) $(CFLAGS) $(DEPFLAGS) $(DEPDIR)/$*.d -c $< -o $@ $(LIBS)
$(DIST)/%.o: $(SRC)/%.c
mkdir -p $(DIST)
$(CC) $(CFLAGS) -MMD -c $< -o $@ $(LIBS)
.PHONY: run .PHONY: run
run: $(DIST)/$(OUT) run: $(DIST)/$(OUT)
@@ -1113,6 +1111,14 @@ run: $(DIST)/$(OUT)
.PHONY: .PHONY:
clean: clean:
rm -rfv $(DIST)/* rm -rfv $(DIST)/*
$(DIST):
mkdir -p $(DIST)
$(DEPDIR):
mkdir -p $(DEPDIR)
-include $(DEPS)
" "
_)) _))
#+end_src #+end_src