aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 6254b9ae355fa833af465fd5d6c3c8f81647bee2 (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
CC := gcc

ifeq ($(PROD),1)
CFLAGS := -std=c99 -O2 # production flags
else
CFLAGS := -std=c99 -Wall -Wextra -Wpedantic -g -DDEBUG # debug flags
endif

# compile as static for the virtual machine
ifeq (vm,$(filter vm,$(MAKECMDGOALS)))
SFLAGS += -static
endif

SRCD := src
OBJD := obj
BIND := bin

DEPD := include
TSTD := tests
CFLAGS += -I$(SRCD)/$(DEPD)

# all the subdirs of the source dir (except DEPD and TSTD)
DIRS := $(filter-out $(DEPD) $(TSTD),$(shell find $(SRCD) -mindepth 1 -maxdepth 1 -type d -exec basename {} \;))

FILES = $(shell find $(SRCD)/$(SUBD) -type f)
CSRCS = $(filter %.c, $(FILES))
COBJS = $(CSRCS:$(SRCD)/%.c=$(OBJD)/%.o)

# get deps
CDEPS = $(COBJS:%.o=%.d)
-include $(CDEPS)

MODULE_TARGET := keylogger.ko
KERNEL_NAME := kernel-$(shell uname -r)
INITFS_NAME := initramfs.img

export

.PHONY: all clean $(DIRS) $(TSTD) $(DEPD)

all: $(DIRS) $(DEPD) $(TSTD)

clean:
	rm -rf $(BIND)
	rm -rf $(OBJD)

$(DIRS):
	$(eval TARGET := $($(shell echo $@ | tr 'a-z' 'A-Z')_TARGET))
	$(MAKE) $(BIND)/$(if $(TARGET),$(TARGET),$@) SUBD=$@

$(DEPD) $(TSTD): $(DEPD)
	$(MAKE) $(BIND)/$@/ SUBD=$@ SINGLE_OBJ=1

ifeq ($(SINGLE_OBJ),1)
# generic single object build
$(BIND)/%: $(OBJD)/%.o
	mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(SFLAGS) $^ -o $@
else
# generic build
$(BIND)/%: $(COBJS)
	mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(SFLAGS) $^ -o $@
endif

# generic compile to obj
$(OBJD)/%.o: $(SRCD)/%.c
	mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(SFLAGS) -MMD -MF $(@:%.o=%.d) -c $< -o $@

# generic build and copy dir
$(BIND)/%/: $(FILES) $(CSRCS:$(SRCD)/%.c=$(BIND)/%)
	mkdir -p $(dir $@)
	find $(@:$(BIND)/%=$(SRCD)/%) -type f ! -name "*.c" ! -name "*.h" -exec cp {} $@ \;

# generic build a module
$(BIND)/%.ko: $(FILES)
	mkdir -p $(dir $@); mkdir -p $(OBJD)/$(SUBD)
	make -C $(SRCD)/$(SUBD) name=$(@:$(BIND)/%.ko=%)
	mv $(SRCD)/$(SUBD)/*.o $(OBJD)/$(SUBD)
	mv $(SRCD)/$(SUBD)/*.ko $@
	make -C $(SRCD)/$(SUBD) clean

# VM things
vm: vm.sh $(BIND)/$(INITFS_NAME) $(BIND)/$(KERNEL_NAME)
	./vm.sh run
$(BIND)/$(INITFS_NAME): $(SRCD)/initramfs-init.sh module $(TSTD)
	./vm.sh initramfs $@ $<
$(BIND)/$(KERNEL_NAME):
	./vm.sh kernel $@