# # Makefile # # # EXAMPLE DIRECTORY STRUCTURE # -------------------------------------------------------------------- # . # ├── build # │ ├── bin # │ │ └── * # │ ├── dep # │ │ ├── *.d # │ └── obj # │ └── *.o # ├── src # │ ├── * # │ │ ├── *.c # │ │ └── *.h # │ ├── *.c # │ └── *.h # ├── test # │ └── *.c # ├── LICENSE # ├── Makefile # └── README # # VARIABLES # -------------------------------------------------------------------- proj=dslibc testframework:=criterion # Compiler options CC:=gcc CFLAGS:=-std=c17 -Wall -Werror -g # Build directories bindir:=build/bin depdir:=build/dep objdir:=build/obj # Files srcfiles:=$(shell find src -name '*.c') objects:=$(patsubst %.c, $(objdir)/%.o, $(notdir $(srcfiles))) dependencies:=$(patsubst %.c, $(depdir)/%.d, $(notdir $(srcfiles))) tests:=$(wildcard test/*.c) # Executable files bin:=$(bindir)/$(proj) testbins:=$(patsubst %.c,$(bindir)/%, $(notdir $(tests))) # TARGETS # -------------------------------------------------------------------- .PHONY: build build: $(bindir) $(depdir) $(objdir) $(objects) $(bin) .PHONY: clean clean: -rm -r build .PHONY: info info: @echo srcfiles: $(srcfiles) @echo objects: $(objects) @echo dependencies: $(dependencies) @echo tests: $(tests) @echo bin: $(bin) @echo testbins: $(testbins) .PHONY: test test: build $(testbins) for test in $(testbins); do ./$$test --verbose --jobs 1; done # AUTOMATIC TARGETS # -------------------------------------------------------------------- # Create directories $(bindir) $(depdir) $(objdir): mkdir -p $@ # Build MAIN objects & dependencies $(objdir)/%.o: src/%.c $(CC) $(CFLAGS) -o $@ -c $< -MMD -MF $(depdir)/$(@F:.o=.d) # Build SUB-MODULE objects & dependencies $(objdir)/%.o:: src/*/%.c $(CC) $(CFLAGS) -o $@ -c $< -MMD -MF $(depdir)/$(@F:.o=.d) # Build MAIN executable $(bin): $(objects) $(CC) $(CFLAGS) -o $@ $^ # Build TEST executable (note: must filter-out main function to work) $(bindir)/%: $(tests) $(filter-out $(objdir)/main.o,$(objects)) $(CC) $(CFLAGS) -o $@ -l $(testframework) $^ # INCLUDES # -------------------------------------------------------------------- -include $(dependencies)