aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: e4162231a0131c559c93125bea905bef4912a4ea (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
CC=gcc
CFLAGS=-std=c17 -Wall -Werror -g
test_framework=criterion

# Directories
bin_dir=bin
obj_dir=obj
src_dir=src
test_dir=tests

# Files
src_files=$(wildcard $(src_dir)/*.c)
obj_files=$(patsubst $(src_dir)/%.c, $(obj_dir)/%.o, $(src_files))
test_files=$(wildcard $(test_dir)/*.c)
test_bins=$(patsubst $(test_dir)/%.c, $(bin_dir)/%, $(test_files))

# Actions
.PHONY: all
all: test

.PHONY: test
test: $(test_bins)
	for test in $(test_bins); do ./$$test ; done

.PHONY: clean
clean:
	rm -r $(bin_dir) $(obj_dir)

.PHONY: demo
demo: $(bin_dir)/demo

.PHONY: info
info:
	@printf "src_files: %s\n" "$(src_files)"
	@printf "obj_files: %s\n" "$(obj_files)"
	@printf "test_files: %s\n" "$(test_files)"
	@printf "test_bins: %s\n" "$(test_bins)"

# Make Directories
$(obj_dir):
	mkdir $@

$(bin_dir):
	mkdir $@

# Make object files
$(obj_dir)/%.o: $(src_files) $(obj_dir)
	$(CC) $(CFLAGS) -o $@ -c $<

# Make test binaries
$(bin_dir)/%: $(bin_dir) $(test_files) $(obj_files)
	$(CC) $(CFLAGS) -o $@ -l $(test_framework) $(filter-out $<,$^)

# Make demo executable
$(bin_dir)/demo: $(bin_dir) $(obj_files) demo/main.c
		$(CC) $(CFLAGS) -o $@ $(filter-out $<,$^)