aboutsummaryrefslogtreecommitdiff
path: root/Makefile
diff options
context:
space:
mode:
Diffstat (limited to 'Makefile')
-rw-r--r--Makefile61
1 files changed, 41 insertions, 20 deletions
diff --git a/Makefile b/Makefile
index 544ff31..64ec306 100644
--- a/Makefile
+++ b/Makefile
@@ -1,62 +1,83 @@
CC := gcc
-CFLAGS = -std=c99 -Wall -Wextra -Wpedantic -g
+
+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
-FILES = $(shell find $(SRCD)/$(SUBD) -type f)
+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))
-HSRCS = $(filter %.h, $(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
-all: daemon client module tests vm
+.PHONY: all clean $(DIRS) $(TSTD) $(DEPD)
+
+all: $(DIRS) $(DEPD) $(TSTD)
clean:
rm -rf $(BIND)
rm -rf $(OBJD)
-daemon client module tests libs:
+$(DIRS):
$(eval TARGET := $($(shell echo $@ | tr 'a-z' 'A-Z')_TARGET))
$(MAKE) $(BIND)/$(if $(TARGET),$(TARGET),$@) SUBD=$@
-# generic link
-$(BIND)/%: $(COBJS) $(HSRCS)
+$(DEPD) $(TSTD): $(DEPD)
+ $(MAKE) $(BIND)/$@/ SUBD=$@
+
+# generic build
+$(BIND)/%: $(COBJS)
mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(SFLAGS) $^ -o $@
-# generic bulid
+# generic compile to obj
$(OBJD)/%.o: $(SRCD)/%.c
mkdir -p $(dir $@)
- $(CC) $(CFLAGS) $(SFLAGS) -c $^ -o $@
+ $(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 {} $@ \;
-# build a module
-$(BIND)/%.ko: $(CSRCS) $(HSRCS)
+# 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
-# copy and build the testing files and other shared files (no .c files)
-$(BIND)/tests: libs
-$(BIND)/tests $(BIND)/libs: $(FILES) $(CSRCS:$(SRCD)/%.c=$(BIND)/%)
- mkdir -p $(BIND)/$(SUBD)
- find $(SRCD)/$(SUBD) -type f ! -name '*.c' -exec cp {} $(BIND)/$(SUBD) \;
-
-
# VM things
vm: vm.sh $(BIND)/$(INITFS_NAME) $(BIND)/$(KERNEL_NAME)
./vm.sh run
-$(BIND)/$(INITFS_NAME): $(SRCD)/initramfs-init.sh module
- ./vm.sh initramfs $@ $^
+$(BIND)/$(INITFS_NAME): $(SRCD)/initramfs-init.sh module tests
+ ./vm.sh initramfs $@ $<
$(BIND)/$(KERNEL_NAME):
./vm.sh kernel $@