aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 8d4d05d9ad28409e8937f03588daa923ad5dfb43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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
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))

bin:=$(bindir)/$(proj)
testbins:=$(patsubst $(testdir)/%.c,$(bindir)/%, $(testfiles))

# TARGETS
.PHONY: build
build: $(objdir) $(bindir) $(depdir) $(bin)

.PHONY: clean
clean:
	-rm -r $(buildir)

.PHONY: test
test: $(objdir) $(bindir) $(depdir) $(objfiles) $(testbins)
	@for test in $(testbins); do ./$$test --verbose; done

.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)"
	@echo "objfiles:  $(objfiles)"
	@echo "testfiles: $(testfiles)"
	@echo "testbins: $(testbins)"

# Create object files
$(objdir)/%.o: $(srcdir)/%.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) $(filter-out $(objdir)/demo.o,$(objfiles))
	$(CC) $(CFLAGS) -o $@ -l $(testframework) $^

# Create Directories
$(objdir):
	mkdir -p $@

$(bindir):
	mkdir -p $@

$(depdir):
	mkdir -p $@