aboutsummaryrefslogtreecommitdiff
path: root/Makefile
diff options
context:
space:
mode:
authorBlake Romero <blake@blkrom.com>2025-10-09 13:28:32 +0100
committerBlake Romero <blake@blkrom.com>2025-10-09 13:28:32 +0100
commit05cb9297c60377ffe870b9c91af2cb2c93873da3 (patch)
tree0119f2c241587aa66f639859a2dac5f64094cb68 /Makefile
parent4960244e28e1f0f52136fdc19918f4bc6e0ce71c (diff)
Add dependencies to Makefile & restructure project
Diffstat (limited to 'Makefile')
-rw-r--r--Makefile75
1 files changed, 56 insertions, 19 deletions
diff --git a/Makefile b/Makefile
index 0f9ec16..8d4d05d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,41 +1,74 @@
+# 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
CC:=gcc
CFLAGS:=-std=c17 -Wall -Werror -g
+# LIBRARIES
+testframework:=criterion
+
# DIRECTORIES
-objdir:=obj
+buildir:=build
+objdir:=$(buildir)/obj
+bindir:=$(buildir)/bin
+depdir:=$(buildir)/dep
srcdir:=src
-bindir:=bin
testdir:=test
# FILES
srcfiles:=$(wildcard $(srcdir)/*.c)
objfiles=$(patsubst $(srcdir)/%.c,$(objdir)/%.o, $(srcfiles))
-objfiles:=$(filter-out $(objdir)/main.o, $(objfiles))
-testfiles:=$(wildcard $(testdir)/*tests.c)
+testfiles:=$(wildcard $(testdir)/test_*.c)
+dependencies:=$(patsubst $(srcdir)/%.c,$(depdir)/%.d, $(srcfiles))
bin:=$(bindir)/$(proj)
testbins:=$(patsubst $(testdir)/%.c,$(bindir)/%, $(testfiles))
# TARGETS
-
.PHONY: build
-build: $(objdir) $(bindir) $(bin)
+build: $(objdir) $(bindir) $(depdir) $(bin)
.PHONY: clean
clean:
- -rm -r $(bindir) $(objdir)
+ -rm -r $(buildir)
.PHONY: test
-test: build $(testbins)
- @for test in $(testbins); do ./$$test; done
+test: $(objdir) $(bindir) $(depdir) $(objfiles) $(testbins)
+ @for test in $(testbins); do ./$$test --verbose; done
-.phony: run
+.PHONY: run
run: build
@$(bin)
+# .PHONY: demo
+# demo: $(objfiles) $(testdir)/demo.c
+# $(CC) $(CFLAGS) -o $(bindir)/demo -c $^
+
+# .PHONY: shared
+# shared: $(objfiles)
+# $(CC) -fPIC -shared -o $(proj).so $^
+
.PHONY: info
info:
@echo "srcfiles: $(srcfiles)"
@@ -43,21 +76,25 @@ info:
@echo "testfiles: $(testfiles)"
@echo "testbins: $(testbins)"
-# Create binary
-$(bin): $(objfiles) $(srcdir)/main.c
- $(CC) $(CFLAGS) $^ -o $@
-
# Create object files
$(objdir)/%.o: $(srcdir)/%.c
- $(CC) $(CFLAGS) -o $@ -c $<
+ $(CC) $(CFLAGS) -o $@ -c $< -MMD -MF $(depdir)/$(@F:.o=.d)
+-include $(dependencies)
+
+# Create binary
+$(bin): $(objfiles)
+ $(CC) $(CFLAGS) -o $@ $^
# Make test binaries
-$(bindir)/%: $(testfiles) $(objfiles)
- $(CC) $(CFLAGS) -o $@ -l criterion $^
+$(bindir)/%: $(testfiles) $(filter-out $(objdir)/demo.o,$(objfiles))
+ $(CC) $(CFLAGS) -o $@ -l $(testframework) $^
# Create Directories
$(objdir):
- mkdir $@
+ mkdir -p $@
$(bindir):
- mkdir $@
+ mkdir -p $@
+
+$(depdir):
+ mkdir -p $@