-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
134 lines (100 loc) · 3.75 KB
/
Makefile
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
SHELL = /bin/bash
ifeq ($(BUILD),release)
NAME = kfs
ARGS_CARGO = --no-default-features --release
endif
VERSION = 5
HOST = $(shell uname)
TOOLCHAIN_ARCH = i386
QEMU = qemu-system-$(TOOLCHAIN_ARCH)
GRUB_CFG = grub.cfg
DOCKER_DIR = docker
DOCKER_TAG = kfs/toolchain
DIR_CONFIG = config
DIR_SRCS = srcs
MAKEFILE_PATH = $(dir $(abspath Makefile))
DIR_ISO = iso
DIR_GRUB = $(DIR_ISO)/boot/grub
DIR_LOGS = logs
BUILD ?= debug
NAME ?= kfs_$(VERSION)
################################################################################
# GDB SETUP
################################################################################
GDB ?= 0
# Check if custom gdb startup script exist and use it if present
ifneq (, $(wildcard ./start.gdb))
GDB_STARTUP=--command=./start.gdb
else
# Default gdb startup command
GDB_STARTUP=-ex "target remote localhost:1234" -ex "break kinit" -ex "c";
endif
################################################################################
# QEMU additional arguments
################################################################################
QEMU_ARGS :=
# Add qemu gdb option if gdb env variable is set to 1
ifeq ($(GDB), 1)
QEMU_ARGS += -s -S
endif
################################################################################
# Prepare Docker toolchain if there is no local toolchain
################################################################################
ifeq ($(and $(shell which grub-mkrescue), $(shell which xorriso), $(shell which mformat), $(shell which cargo), $(shell which i386-elf-ld)),)
ifeq ($(shell docker images -q ${DOCKER_TAG} 2> /dev/null),)
BUILD_DOCKER := $(shell docker build $(DOCKER_DIR) -t $(DOCKER_TAG) >&2)
endif
BUILD_PREFIX = docker run --rm -v $(MAKEFILE_PATH):/root:Z $(DOCKER_TAG) '
BUILD_SUFFIX = '
endif
################################################################################
# Prepare Docker env if there is no qemu
################################################################################
ifeq ($(and $(shell which $(QEMU))),)
ifeq ($(shell docker images -q ${DOCKER_TAG} 2> /dev/null),)
BUILD_DOCKER := $(shell docker build $(DOCKER_DIR) -t $(DOCKER_TAG) >&2)
endif
RUN_PREFIX = docker run --rm -it -v $(MAKEFILE_PATH):/root:Z $(DOCKER_TAG) '
RUN_SUFFIX = '
endif
################################################################################
all: $(NAME)
doc:
cargo doc $(ARGS_CARGO) --open
boot: $(NAME) $(DIR_LOGS)
$(BUILD_PREFIX) cargo run $(ARGS_CARGO) -- $(NAME) $@ "$(QEMU_ARGS)" $(BUILD_SUFFIX)
test: $(NAME) $(DIR_LOGS)
$(BUILD_PREFIX) cargo test $(ARGS_CARGO) -- $(NAME) $@ "$(QEMU_ARGS)" $(BUILD_SUFFIX)
# This rule will run qemu with flags to wait gdb to connect to it
debug: $(NAME)
gdb $(DIR_ISO)/boot/$(NAME) $(GDB_STARTUP)
pkill qemu $(RUN_SUFFIX) # When exiting gdb kill qemu
# Rule to create iso file which can be run with qemu
$(NAME): build $(DIR_GRUB)/$(GRUB_CFG) Makefile;
# Build kernel using cargo
build:
$(BUILD_PREFIX) cargo build $(ARGS_CARGO) $(BUILD_SUFFIX)
# Check if the rust can compile without actually compiling it
check:
$(BUILD_PREFIX) cargo check $(ARGS_CARGO) $(BUILD_SUFFIX)
$(DIR_GRUB)/$(GRUB_CFG): $(DIR_CONFIG)/$(GRUB_CFG) | $(DIR_GRUB)
cp -f $(DIR_CONFIG)/$(GRUB_CFG) $(DIR_GRUB)
ifeq ($(strip $(HOST)),Darwin) # sed on macOS doesn't work like GNU sed
sed -i '' "s/__kfs__/$(NAME)/" $(DIR_GRUB)/$(GRUB_CFG)
else
sed -i "s/__kfs__/$(NAME)/" $(DIR_GRUB)/$(GRUB_CFG)
endif
$(DIR_GRUB):
mkdir -p $(DIR_GRUB)
$(DIR_LOGS):
mkdir -p $(DIR_LOGS)
clean:
rm -rf $(DIR_LOGS)
rm -rf target
rm -rf Cargo.lock
rm -rf $(DIR_ISO)
fclean: clean
rm -rf kfs*
re: fclean
@$(MAKE) --no-print-directory
.PHONY: all doc boot debug test build check clean fclean re