compiledb icon indicating copy to clipboard operation
compiledb copied to clipboard

Fails/crashes for makefiles that use a recursive $(MAKE) pattern to rebuild when makefile changes

Open travisdowns opened this issue 5 years ago • 0 comments

I have a makefile that uses a specific matter to trigger a full rebuild when the Makefile itself changes, like so

include config.mk

# rebuild when makefile changes
-include dummy.rebuild

EXE := bench

.PHONY: all clean

CXX ?= g++
CC ?= gcc
ASM ?= nasm
ASM_FLAGS ?= -DNASM_ENABLE_DEBUG=$(NASM_DEBUG) -w+all

ARCH_FLAGS := -march=$(CPU_ARCH)


# make submakes use the specified compiler also
export CXX
export CC

INCLUDES += -Ifmt/include

COMMON_FLAGS := -MMD -Wall $(ARCH_FLAGS) -g $(O_LEVEL) $(INCLUDES) $(NDEBUG)

CPPFLAGS +=
CFLAGS += $(COMMON_FLAGS)
CXXFLAGS += $(COMMON_FLAGS) -Wno-unused-variable 

SRC_FILES := $(wildcard *.cpp) $(wildcard *.c) fmt/src/format.cc

# on most compilers we should use no-pie since the nasm stuff isn't position independent
# but since old compilers don't support it, you can override it with PIE= on the command line
PIE ?= -no-pie
LDFLAGS += $(PIE) -lpthread

EXTRA_DEPS :=

OBJECTS := $(SRC_FILES:.cpp=.o) $(ASM_FILES:.asm=.o)
OBJECTS := $(OBJECTS:.cc=.o)
OBJECTS := $(OBJECTS:.c=.o)
DEPFILES = $(OBJECTS:.o=.d)
# $(info OBJECTS=$(OBJECTS))

# VPATH = test:$(PSNIP_DIR)/cpu

###########
# Targets #
###########

all: bench

-include $(DEPFILES)

clean:
	rm -f *.d *.o $(EXE)

$(EXE): $(OBJECTS) $(EXTRA_DEPS)
	$(CXX) $(OBJECTS) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS)  -o $@

%.o : %.c
	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

%.o : %.cpp
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<


%.o: %.asm
	$(ASM) $(ASM_FLAGS) -f elf64 $<

# https://stackoverflow.com/a/3892826/149138
dummy.rebuild: Makefile
	touch $@
	$(MAKE) -s clean

Note the dummy.rebuild target. It causes $(MAKE) -s clean to be called up front, cleaning the project so the build rebuilds everything. When running compiledb make on this project it takes almost a minute and errors out like so:

$ compiledb make
## Building [make]...
make: Nothing to be done for 'all'.

make[3593]: Warning: File 'dummy.rebuild' has modification time 1.1 s in the future
make[9854]: fork: Resource temporarily unavailable
make[9854]: fork: Resource temporarily unavailable
/bin/sh: 1: Cannot fork
make[9853]: *** [Makefile:56: clean] Error 2

Looks like an infinite recursion.

travisdowns avatar Jun 19 '20 23:06 travisdowns