aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 394c568193a8047126dfba81e40028461d4c9e2a (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
101
102
#
# 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; 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)