-
Notifications
You must be signed in to change notification settings - Fork 29
/
Makefile
294 lines (239 loc) · 7.57 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# This file is part of BoneOS.
# BoneOS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# BoneOS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with BoneOS. If not, see <http://www.gnu.org/licenses/>.
# Subdirectory makes need to know this directory
BUILDROOT := $(realpath .)
export BUILDROOT
# Remember this the first time it is used
# so you don't have to keep specifying it
ifdef CROSSROOT
$(shell echo $(CROSSROOT) > Makefile.cached.CROSSROOT)
else
ifneq (,$(wildcard Makefile.cached.CROSSROOT))
CROSSROOT := $(shell cat Makefile.cached.CROSSROOT)
endif
endif
GRUB_MKRESCUE ?= grub-mkrescue
MEMORY_USE_RAM ?= 1G
ifdef CROSSROOT
HOST_ENV = $(shell uname -p)
# Allow user to override cross-compiler directory
CROSSROOT ?= $(BUILDROOT)/cross
CC := $(CROSSROOT)/cross/$(HOST_ENV)/bin/i686-elf-gcc
LD := $(CROSSROOT)/cross/$(HOST_ENV)/bin/i686-elf-ld
AR := $(CROSSROOT)/cross/$(HOST_ENV)/bin/i686-elf-ar
endif
GENERATED_CONFIG := config/GENERATED-CONFIG.mk
ifndef GENCONFIG
-include $(GENERATED_CONFIG)
include config/ARCH.mk
include config/PLAT.mk
# Detect path to libgcc
LIBGCC := $(shell $(CC) $(ARCH_MACHINE) -print-libgcc-file-name)
LIBGCCDIR := $(dir $(LIBGCC))
LIBGCCFILENAME := $(notdir $(LIBGCC))
LIBGCCNAME := $(patsubst lib%.a,%,$(LIBGCCFILENAME))
else
include config/ARCH.mk
include config/PLAT.mk
endif
export CC
export LD
export AR
export AS
export GRUB_MKRESCUE
export MEMORY_USE_RAM
VIDEO_DRIVER := VGA
VIDEO_DRIVER_LOW := vga
VIDEO_DRIVER_MODE := graphics_mode
VIDEO_DRIVER_RES_W := 320
VIDEO_DRIVER_RES_H := 200
VIDEO_DRIVER_RES := $(VIDEO_DRIVER_RES_W)x$(VIDEO_DRIVER_RES_H)
export VIDEO_DRIVER
export VIDEO_DRIVER_LOW
export VIDEO_DRIVER_RES_W
export VIDEO_DRIVER_RES_H
export VIDEO_DRIVER_RES
export VIDEO_DRIVER_MODE
# Programs
BOCHS := bochs
QEMU := qemu-system-$(ARCH_QEMU)
PYTHON := python
SH := sh
VB := virtualbox
VBM := VBoxManage
GDB := gdb_q
INCDIRS := $(BUILDROOT)/include \
$(BUILDROOT)/arch/$(ARCH) \
$(BUILDROOT)/include/arch/$(ARCH) \
$(BUILDROOT)/include/apps \
$(BUILDROOT)/include/bin \
$(BUILDROOT)/include/sbin \
$(BUILDROOT)/include/libc \
$(BUILDROOT)/include/platform/$(PLAT) \
$(BUILDROOT)/include/libc/string \
$(BUILDROOT)/include/arch/shared/x86 \
$(BUILDROOT)/include/gui
# Parameters
LDPARAMS := -melf_$(ARCH_LINKER) --build-id=none
CFLAGS := \
$(ARCH_MACHINE) -std=c11 \
-O0 -g -Wall -Wextra -Wpedantic -Werror -g \
-Wno-error=missing-field-initializers \
-nostdlib -ffreestanding $(patsubst %,-I%,$(INCDIRS)) \
-DDEBUG \
$(ARCH_CFLAGS)
ASFLAGS := $(ARCH_AS_FLAGS)
NASM ?= nasm
NASMFLAGS := -f elf$(ARCH_NN)
export CFLAGS
export LDFLAGS
export CFLAGS
export NASM
export NASMFLAGS
# Paths
BONEOS_ISO := BoneOS.iso
BONEOS_BIN := BoneOS.bin
BONEOS_BOOT_DIR := boot/boot
BONEOS_BOOT_BIN := $(BONEOS_BOOT_DIR)/$(BONEOS_BIN)
BONEOS_GRUB_CFG := $(BONEOS_BOOT_DIR)/grub/grub.cfg
LINKER_SCRIPT := arch/$(ARCH)/link/linker.ld
SCRIPT_CC := utils/cross_compiler/toolchain.py
libraries = \
libc/libc.a \
arch/$(ARCH)/libarch.a \
apps/libapps.a \
bin/libbin.a \
var/libvar.a \
sbin/libsbin.a \
arch/shared/$(ARCH_FAMILY)/libshared_arch.a \
platform/$(PLAT)/libplatform.a \
gui/libgui.a
export libraries
# -----------------------------------------------
# Targets
# -----------------------------------------------
#
# Standard targets
all: iso
clean: clean-subdirs
rm -f $(BONEOS_ISO)
rm -rf iso
.PHONY: all clean
#
# Build and clean subdirectories
$(libraries): subdirs
subdirs:
(cd libc && $(MAKE))
(cd arch && $(MAKE))
(cd apps && $(MAKE))
(cd bin && $(MAKE))
(cd sbin && $(MAKE))
(cd var && $(MAKE))
(cd gui && $(MAKE))
(cd platform && $(MAKE))
clean-subdirs:
(cd libc && $(MAKE) clean)
(cd arch && $(MAKE) clean)
(cd apps && $(MAKE) clean)
(cd bin && $(MAKE) clean)
(cd sbin && $(MAKE) clean)
(cd var && $(MAKE) clean)
(cd gui && $(MAKE) clean)
(cd libc && $(MAKE) clean)
(cd platform && $(MAKE) clean)
.PHONY: subdirs clean-subdirs
#
# Link
$(BONEOS_BIN): $(libraries) $(LINKER_SCRIPT)
$(LD) $(LDPARAMS) \
-T $(LINKER_SCRIPT) \
-o $@ \
--start-group $(libraries) --end-group \
-Map=BoneOS.map \
-L$(LIBGCCDIR) -l$(LIBGCCNAME)
#
# Build ISO
$(BONEOS_BOOT_BIN): $(BONEOS_BIN)
mkdir -p $(dir $(BONEOS_BOOT_BIN))
cp $(BONEOS_BIN) $(BONEOS_BOOT_BIN)
$(BONEOS_ISO): $(BONEOS_BOOT_BIN)
cp BoneOS.bin boot/boot/BoneOS.bin
$(GRUB_MKRESCUE) --output=BoneOS.iso boot
iso: $(BONEOS_ISO)
#
# Get toolchain
$(COMPILER): get-toolchain
get-toolchain:
$(PYTHON) utils/cross_compiler/toolchain.py
.PHONY: iso
no_vnc:
$(SH) utils/novnc/install.sh
$(SH) utils/novnc/run.sh
.PHONY: no_vnc
#
# Launch and debug
gdb_q: $(BONEOS_BIN)
$(QEMU) -kernel $(BONEOS_BIN) -display sdl -s -S \
-no-shutdown -no-reboot -d cpu_reset \
-m $(MEMORY_USE_RAM)
debug_q:
$(GDB) --symbols=$(BONEOS_BIN) --tui \
-iex 'set architecture i386:x86-64' \
-ex 'target remote localhost:1234' \
-ex 'layout regs' \
-ex 'set radix 16' \
qemu_compile: $(BONEOS_BIN)
$(QEMU) -kernel $(BONEOS_BIN) -display sdl -k en-us -s -m $(MEMORY_USE_RAM)
qemu_iso: $(BONEOS_BIN) $(BONEOS_ISO)
$(QEMU) -cdrom $(BONEOS_ISO) -k en-us -s -m $(MEMORY_USE_RAM)
bochs: $(BONEOS_ISO)
$(BOCHS) -f bochsrc.bxrc -q
new_line:
find . \( -name "*.asm" -o -name "*.h" -o -name "*.c" \) -print0 | \
xargs -0 -iFILE sh -c 'echo >> FILE'
custom:
xterm -fg white -bg black
unset TMUX
distclean: clean
rm -f $(GENERATED_CONFIG)
configure-help:
@echo ''
@echo 'Usage: configure [options]'
@echo 'Options: [defaults in brackets after descriptions]'
@echo ''
@echo '--cc=CC use C compiler [$(CC)]'
@echo '--ld=LD use LD linker [$(LD)]'
@echo '--ar=AR use AR archiver [$(AR)]'
@echo '--as=AS use AS assembler [$(AS)]'
@echo '--grub-mkconfig=MC use grub-mkconfig compiler [$(GRUB_MKCONFIG)]'
@echo ''
@echo '--arch=ARCH compile kernel that runs on ARCH'
@echo ' Available ARCH: x86, x86-64'
@echo '--plat=PLAT compile kernel that is of platform PLAT'
@echo ' Available PLAT: pc'
@echo ''
@echo '--m=MEMORY_USE_RAM compile kernel that has'
@echo ' MEMORY_USE_RAM memory.'
@echo ''
configure-set:
@echo '# This file is automatically generated' > $(GENERATED_CONFIG)
@echo '# Invoke the configure script to make changes' >> $(GENERATED_CONFIG)
@echo "CC := $(CC)" >> $(GENERATED_CONFIG)
@echo "LD := $(LD)" >> $(GENERATED_CONFIG)
@echo "AR := $(AR)" >> $(GENERATED_CONFIG)
@echo "AS := $(AS)" >> $(GENERATED_CONFIG)
@echo "GRUB_MKRESCUE := $(GRUB_MKRESCUE)" >> $(GENERATED_CONFIG)
@echo "MEMORY_USE_RAM := $(MEMORY_USE_RAM)" >> $(GENERATED_CONFIG)
@echo "ARCH := $(ARCH)" >> $(GENERATED_CONFIG)
@echo "PLAT := $(PLAT)" >> $(GENERATED_CONFIG)
@echo '' >> $(GENERATED_CONFIG)
.PHONY: gdb_q qemu_compile bochs qemu_iso custom new_line distclean configure-help configure-set