aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 9b6375c132ed3546ad5f36d1d19da3422bb84673 (plain) (blame)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
DO		!= echo -n > deps.mk

# this could be done better
DEBUGFLAGS	!= [ $(RELEASE) ] \
			&& echo "-flto -O2 -g -DNDEBUG" \
			|| echo "-O0 -g -DDEBUG"

CFLAGS		= -ffreestanding -nostdlib -std=c17 -Wall -Wextra -Wvla
DEPFLAGS	= -MT $@ -MMD -MP -MF $@.d
LINTFLAGS	= -fsyntax-only
PREPROCESS	= -E
LDFLAGS		!= [ $(LLVM) ] \
			|| echo -static-libgcc -lgcc

BUILD		= build
ARCH_BUILD	= $(BUILD)/arch/$(ARCH)
ARCH_SOURCE	= arch/$(ARCH)

all: apos.bin
# bmake requires us to run make depend to genereate deps.mk beforehand,
# so create an empty rule that lets the rest of the script do its job
depend:

# default values, overwrite if/when needed
ARCH		?= riscv64
CROSS_COMPILE	?= $(ARCH)-unknown-elf

OBJCOPY		!= [ $(LLVM) ] \
			&& echo llvm-objcopy \
			|| echo $(CROSS_COMPILE)-objcopy

COMPILER	!= [ $(LLVM) ] \
			&& echo clang --target="$(CROSS_COMPILE)" \
			|| echo $(CROSS_COMPILE)-gcc


KERNEL_SOURCES	!= echo common/*.c common/uapi/*.c lib/*.c
CLEANUP		:= build deps.mk kernel.* init.* apos.bin docs/output
CLEANUP_CMD	:=
INIT_SOURCES	:=

include arch/$(ARCH)/source.mk

COMPILE_FLAGS	:= $(CFLAGS) $(ARCH_CFLAGS)
LINK_FLAGS	:= $(LDFLAGS) $(ARCH_LDFLAGS)

INCLUDE_FLAGS	:= -I include -I arch/$(ARCH)/include\
	-include config.h -include arch/$(ARCH)/config.h

# This makes sure .bss is loaded into the binary
OBJCOPY_FLAGS	?= -Obinary --set-section-flags .bss=alloc,load,contents

COMPILE		= $(COMPILER) $(DEBUGFLAGS)\
		  $(COMPILE_FLAGS) $(DEPFLAGS) $(INCLUDE_FLAGS)

LINT		= $(COMPILER) $(DEBUGFLAGS)\
		  $(COMPILE_FLAGS) $(LINTFLAGS) $(INCLUDE_FLAGS)

GENELF		= $(COMPILER) $(DEBUGFLAGS)\
		  $(COMPILE_FLAGS) $(INCLUDE_FLAGS)

GENLINK		= $(COMPILER) $(COMPILE_FLAGS) $(PREPROCESS) $(DEPFLAGS) $(INCLUDE_FLAGS)
STRIPLINK	= sed -n '/^[^\#]/p'
KERN_SIZE	= wc -c kernel.bin | awk '{print $$1}'
KERN_INFO	= sed "s/<KERNEL_SIZE>/$$($(KERN_SIZE))/"

KERNEL_LINK	:= arch/$(ARCH)/conf/kernel-link
INIT_LINK	:= arch/$(ARCH)/conf/init-link

KERN_FLAGS	!= [ $(UBSAN) ] && echo -fsanitize=undefined || echo
INIT_FLAGS	:=

KERNEL_OBJECTS	!= ./scripts/gen-deps --kernel --compile "$(KERNEL_SOURCES)"
INIT_OBJECTS	!= ./scripts/gen-deps --init --compile "$(INIT_SOURCES)"
KERNEL_LD	!= ./scripts/gen-deps --kernel --link "$(KERNEL_LINK).S"
INIT_LD		!= ./scripts/gen-deps --init --link "$(INIT_LINK).S"

-include deps.mk

$(INIT_LD): kernel.bin

lint: $(INIT_OBJECTS:.o=.o.l) $(KERNEL_OBJECTS:.o=.o.l)

init.elf: $(INIT_OBJECTS) $(INIT_LD)
	$(GENELF) -T $(INIT_LD) $(INIT_OBJECTS) -o init.elf $(LINK_FLAGS)

kernel.elf: $(KERNEL_OBJECTS) $(KERNEL_LD)
	$(GENELF) -T $(KERNEL_LD) $(KERNEL_OBJECTS) -o kernel.elf $(LINK_FLAGS)

init.bin: init.elf
	$(OBJCOPY) $(OBJCOPY_FLAGS) init.elf init.bin

kernel.bin: kernel.elf
	$(OBJCOPY) $(OBJCOPY_FLAGS) kernel.elf kernel.bin

apos.bin: init.bin kernel.bin
	cat init.bin kernel.bin > apos.bin

format:
	find arch lib common include -iname '*.[ch]' |\
		uncrustify -c uncrustify.conf --no-backup -F -

.PHONY: docs
docs:
	./scripts/warn-undocumented
	doxygen docs/doxygen.conf

RM	?= rm -f

.PHONY: clean
clean:
	$(RM) -r $(CLEANUP)

clean_run: clean
	$(CLEANUP_CMD)