Automatic Make recipes generation

I can report that mk-fdeps can bootstrap it’s own Makefile.

To test this I created a second makefile named Makefile2, containing the following notable changes:

# Built with manually specified dependencies
MK_FDEPS=./mk-fdeps

# ...

.PHONY: depend
depend deps.mk:
	$(MK_FDEPS) $(srcs) > deps.mk

# ...

-include deps.mk

The full makefile can be found in the following hidden box:

Makefile2 (click here)
BUILD_DIR ?= build
PREFIX ?= /usr/local/bin

FC=gfortran
FC_FLAGS=-O0 -g -fbounds-check -J$(BUILD_DIR)

# Built with manually specified dependencies
MK_FDEPS=./mk-fdeps

.PHONY: all clean
all: $(BUILD_DIR)/mk-fdeps

$(BUILD_DIR):
	mkdir -p $@

$(BUILD_DIR)/%.o: src/%.f90 | $(BUILD_DIR)
	$(FC) $(FC_FLAGS) -c $< -o $@

srcs = $(wildcard src/*.f90)
target_deps = $(srcs:src/%.f90=$(BUILD_DIR)/%.o)

$(BUILD_DIR)/mk-fdeps: $(target_deps) | $(BUILD_DIR)
	$(FC) $(FC_FLAGS) $^ -o $@

.PHONY: depend
depend deps.mk:
	$(MK_FDEPS) $(srcs) > deps.mk

RM = rm -rf
clean:
	$(RM) $(BUILD_DIR) deps.mk

-include deps.mk

The sequence of commands to test this from the root project directory is:

make all                 # original makefile
cp build/mk-fdeps .      # copy executable into current folder
make -f Makefile2 clean  # get rid of build folder
make -f Makefile2

To ease the bootstrapping process one idea would be to create an ordered build list (see Module dependencies ). This is supported by the nagfor =depend -otype=blist command or the compile-order.txt file generated by FF08Depends.

Perhaps the build list could be checked into the repository under compile-order.txt:

src/string_builder.f90
src/lexer.f90
src/parser.f90
src/string_arena.f90
src/graph.f90
src/hash_table.f90
src/int_darray.f90
src/makefile_deps.f90
src/main.f90

allowing to bootstrap the tool with a one-liner:

gfortran -o mk-fdeps $(cat compile-order.txt) 

Afterward you can keep using the Makefile with auto-generated dependencies. You must however make sure the compile-order.txt is valid when you check-in any changes.

Edit: I just realized the generated dep.mk fulfills the same role as compile-order.txt. So you could just check that file in and make sure it remains valid when files are added/removed.