TARGET_EXEC := test
BUILD_DIR := ./build
SRC_DIRS := ./src

# False targets
# (these are the only ones you manually run)
all: $(TARGET_EXEC)

run: all
	./$(TARGET_EXEC)

clean:
	-rm -r $(BUILD_DIR)
	@echo ""

.PHONY: clean all run

#################################################
# Flags and autodetection

# -MMD and -MP generate makefiles with extension .d.
CPPFLAGS := -Wall -MMD -MP -I src
LDFLAGS := -l fftw3

# Find all cpp files in source dirs
SRCS := $(shell find $(SRC_DIRS) -name '*.cpp')
# Turns ./build/a.cpp into ./build/a.cpp.o
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
# Turns ./build/a.cpp.o into ./build/a.cpp.d
DEPS := $(OBJS:.o=.d)

#################################################
# Build targets

# C++ build step
$(BUILD_DIR)/%.cpp.o: %.cpp
	mkdir -p $(dir $@)
	g++ $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@


# Final build step
$(TARGET_EXEC) : $(OBJS)
	g++ $(OBJS) -o $@ $(LDFLAGS)


# Include generated makefiles
-include $(DEPS)