From b188817ad2a0c26264b742fed55320260650ecb7 Mon Sep 17 00:00:00 2001 From: Blake Romero Date: Fri, 10 Oct 2025 16:13:46 +0100 Subject: Refactor Makefile --- Makefile | 152 ++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 77 insertions(+), 75 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 8d4d05d..394c568 100644 --- a/Makefile +++ b/Makefile @@ -1,100 +1,102 @@ -# TODO: make shared library - -# Directory structure -# =================== -# . -# ├── build -# │ ├── bin -# │ │ └── * -# │ ├── dep -# │ │ ├── *.d -# │ └── obj -# │ └── *.o -# ├── src -# │ ├── *.c -# │ └── *.h -# ├── test -# │ └── test_*.c -# ├── LICENSE -# ├── Makefile -# └── README.md - -proj:=dslibc - -# COMPILER OPTIONS +# +# 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 -# LIBRARIES -testframework:=criterion +# Build directories +bindir:=build/bin +depdir:=build/dep +objdir:=build/obj -# DIRECTORIES -buildir:=build -objdir:=$(buildir)/obj -bindir:=$(buildir)/bin -depdir:=$(buildir)/dep -srcdir:=src -testdir:=test - -# FILES -srcfiles:=$(wildcard $(srcdir)/*.c) -objfiles=$(patsubst $(srcdir)/%.c,$(objdir)/%.o, $(srcfiles)) -testfiles:=$(wildcard $(testdir)/test_*.c) -dependencies:=$(patsubst $(srcdir)/%.c,$(depdir)/%.d, $(srcfiles)) +# 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 $(testdir)/%.c,$(bindir)/%, $(testfiles)) +testbins:=$(patsubst %.c,$(bindir)/%, $(notdir $(tests))) # TARGETS +# -------------------------------------------------------------------- .PHONY: build -build: $(objdir) $(bindir) $(depdir) $(bin) +build: $(bindir) $(depdir) $(objdir) $(objects) $(bin) .PHONY: clean clean: - -rm -r $(buildir) + -rm -r build -.PHONY: test -test: $(objdir) $(bindir) $(depdir) $(objfiles) $(testbins) - @for test in $(testbins); do ./$$test --verbose; done +.PHONY: info +info: + @echo srcfiles: $(srcfiles) + @echo objects: $(objects) + @echo dependencies: $(dependencies) + @echo tests: $(tests) + @echo bin: $(bin) + @echo testbins: $(testbins) -.PHONY: run -run: build - @$(bin) +.PHONY: test +test: build $(testbins) + for test in $(testbins); do ./$$test --verbose; done -# .PHONY: demo -# demo: $(objfiles) $(testdir)/demo.c -# $(CC) $(CFLAGS) -o $(bindir)/demo -c $^ -# .PHONY: shared -# shared: $(objfiles) -# $(CC) -fPIC -shared -o $(proj).so $^ +# AUTOMATIC TARGETS +# -------------------------------------------------------------------- +# Create directories +$(bindir) $(depdir) $(objdir): + mkdir -p $@ -.PHONY: info -info: - @echo "srcfiles: $(srcfiles)" - @echo "objfiles: $(objfiles)" - @echo "testfiles: $(testfiles)" - @echo "testbins: $(testbins)" +# Build MAIN objects & dependencies +$(objdir)/%.o: src/%.c + $(CC) $(CFLAGS) -o $@ -c $< -MMD -MF $(depdir)/$(@F:.o=.d) -# Create object files -$(objdir)/%.o: $(srcdir)/%.c +# Build SUB-MODULE objects & dependencies +$(objdir)/%.o:: src/*/%.c $(CC) $(CFLAGS) -o $@ -c $< -MMD -MF $(depdir)/$(@F:.o=.d) --include $(dependencies) -# Create binary -$(bin): $(objfiles) +# Build MAIN executable +$(bin): $(objects) $(CC) $(CFLAGS) -o $@ $^ -# Make test binaries -$(bindir)/%: $(testfiles) $(filter-out $(objdir)/demo.o,$(objfiles)) +# Build TEST executable (note: must filter-out main function to work) +$(bindir)/%: $(tests) $(filter-out $(objdir)/main.o,$(objects)) $(CC) $(CFLAGS) -o $@ -l $(testframework) $^ -# Create Directories -$(objdir): - mkdir -p $@ -$(bindir): - mkdir -p $@ - -$(depdir): - mkdir -p $@ +# INCLUDES +# -------------------------------------------------------------------- +-include $(dependencies) -- cgit