Skip to content

Commit

Permalink
Makefile: add auto dependency generation (-MMD -MP)
Browse files Browse the repository at this point in the history
Add auto-dependency generation based on the following guide by GNU make
maintainer Paul Smith:
http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/

Skeleton outline:
  DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td
  %.o : %.c
  %.o : %.c $(DEPDIR)/%.d
          $(QUIET_CC)$(CC) $(DEPFLAGS) $(CFLAGS) -c $<
          @mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d && touch $@
  $(DEPDIR)/%.d: ;
  .PRECIOUS: $(DEPDIR)/%.d
  include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))

Tested with gcc and clang
  • Loading branch information
johanmalm committed Jan 17, 2018
1 parent e7e5874 commit f342e97
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ jgmenu-socket
README.html
config.mk
valgrind.log
binsiz.log
.d/
29 changes: 21 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ VER = $(shell ./scripts/version-gen.sh)

include ./Makefile.inc

DEPDIR := .d
$(shell mkdir -p $(DEPDIR) >/dev/null)
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td

SCRIPTS_SHELL = jgmenu_run jgmenu-init.sh

SCRIPTS_PYTHON = jgmenu-pmenu.py jgmenu-unity-hack.py
Expand All @@ -26,20 +30,26 @@ OBJS = x11-ui.o config.o util.o geometry.o isprog.o sbuf.o icon-find.o \
unix_sockets.o bl.o cache.o back.o terminal.o restart.o theme.o \
gtkconf.o font.o args.o widgets.o pm.o socket.o

LIB_H = $(shell find . -name '*.h' -print)

SRCS = $(patsubst %.o,%.c,$(OBJS))
JGMENU_LIB = libjgmenu.a

all: $(PROGS)
@echo ""
@echo "Warning: The CLI has changed. Please read release notes."

$(PROGS): % : $(OBJS) %.o
jgmenu: jgmenu.o $(OBJS)
jgmenu-xdg: jgmenu-xdg.o util.o sbuf.o xdgdirs.o xdgapps.o argv-buf.o
jgmenu-ob: jgmenu-ob.o util.o sbuf.o
jgmenu-socket: jgmenu-socket.o util.o sbuf.o unix_sockets.o socket.o
jgmenu-lx: jgmenu-lx.o util.o sbuf.o xdgdirs.o argv-buf.o back.o
$(PROGS):
$(QUIET_LINK)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

%.o: %.c $(LIB_H)
$(QUIET_CC)$(CC) $(CFLAGS) -c $*.c
%.o : %.c
%.o : %.c $(DEPDIR)/%.d
$(QUIET_CC)$(CC) $(DEPFLAGS) $(CFLAGS) -c $<
@mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d && touch $@

$(DEPDIR)/%.d: ;
.PRECIOUS: $(DEPDIR)/%.d

install: $(PROGS)
@install -d $(DESTDIR)$(bindir)
Expand All @@ -60,7 +70,8 @@ endif
@./scripts/create_desktop_file.sh $(DESTDIR)$(prefix)

clean:
@$(RM) $(PROGS) *.o *.a
@$(RM) $(PROGS) *.o *.a $(DEPDIR)/*.d
@$(RM) -r .d/
@$(MAKE) --no-print-directory -C tests/ clean
@$(MAKE) --no-print-directory -C tests/helper/ clean

Expand All @@ -77,3 +88,5 @@ ex:
check:
@./scripts/checkpatch-wrapper.sh *.c
@./scripts/checkpatch-wrapper.sh *.h

include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))

0 comments on commit f342e97

Please sign in to comment.