diff options
| author | Blake Romero <blake@blkrom.com> | 2025-10-10 16:13:46 +0100 |
|---|---|---|
| committer | Blake Romero <blake@blkrom.com> | 2025-10-10 17:04:13 +0100 |
| commit | b188817ad2a0c26264b742fed55320260650ecb7 (patch) | |
| tree | 12471da28b99b67d441895b2626c37cdf5b0e897 | |
| parent | 05cb9297c60377ffe870b9c91af2cb2c93873da3 (diff) | |
Refactor Makefile
| -rw-r--r-- | Makefile | 152 |
1 files changed, 77 insertions, 75 deletions
@@ -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) |
