os-k/Makefile

259 lines
9.9 KiB
Makefile
Raw Normal View History

2019-02-06 15:41:24 +01:00
#=----------------------------------------------------------------------------=#
# GNU GPL OS/K #
# #
# Desc: Project Makefile #
# #
# #
# Copyright © 2018-2019 The OS/K Team #
# #
# This file is part of OS/K. #
# #
# OS/K 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 #
# any later version. #
# #
# OS/K 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 OS/K. If not, see <https://www.gnu.org/licenses/>. #
#=----------------------------------------------------------------------------=#
2019-04-10 10:55:22 +02:00
.PHONY: all test test32 debug gdb install_mbr dust clean OS/K
2019-04-09 17:16:13 +02:00
2019-03-17 22:32:52 +01:00
## VARIABLES ----------------------------------------------------------------- #
2019-04-09 23:35:31 +02:00
# Debug
mode ?= debug
# Programs
2019-01-14 23:16:26 +01:00
ASM=nasm
2019-04-10 10:55:22 +02:00
LD=x86_64-elf-ld
OBJCOPY=x86_64-elf-objcopy
2019-03-17 22:32:52 +01:00
CCNAME=x86_64-elf-gcc
ASMFLAGS=-f elf64
LDFLAGS=-melf_x86_64
2019-03-17 22:32:52 +01:00
COPTIM=-O2
2019-03-18 17:25:44 +01:00
CWARNS=-Wall -Wextra -Werror=implicit-function-declaration
2019-04-06 07:53:58 +02:00
CINCLUDES=-Iinclude
2019-03-18 17:25:44 +01:00
CFLAGS1=-nostdlib -ffreestanding -mcmodel=large -std=gnu11
2019-04-09 23:35:31 +02:00
CFLAGS2= -c -mno-red-zone -mno-mmx -mno-sse -mno-sse2
2019-03-18 17:25:44 +01:00
CFLAGS=$(CFLAGS1) $(CFLAGS2)
2019-04-09 23:35:31 +02:00
ifeq ($(mode), release)
CFLAGS += -D_NO_DEBUG
dep += dust
endif
ifeq ($(mode), debug)
CFLAGS += -g
endif
KCC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CINCLUDES) -D_OSK_SOURCE -D_KALEID_KERNEL -fstack-protector-all
2019-01-14 23:16:26 +01:00
2019-04-09 23:35:31 +02:00
# Folders
MBRDIR=boot/grub
2019-01-16 11:01:18 +01:00
LOADERDIR=boot/loader
2019-04-06 07:53:58 +02:00
KALEIDDIR=kaleid
INCLUDEDIR=include
2019-01-14 23:16:26 +01:00
OBJDIR=build/obj
2019-03-18 13:45:26 +01:00
KOBJDIR=build/obj/kaleid
LOBJDIR=build/obj/boot
2019-01-14 23:16:26 +01:00
BINDIR=build/bin
BUILDDIR=build
2019-04-09 17:16:13 +02:00
vpath %.c $(KALEIDDIR)
2019-04-09 23:35:31 +02:00
# Color codes
CL='\033[0;32m'
CL2='\033[1;36m'
CL3='\033[0m'
NC='\033[1;37m'
2019-04-09 23:35:31 +02:00
## SOURCES INSCRIPTION-------------------------------------------------------- #
# Lib C sources
LibCSources = libc/atoi.c libc/itoa.c \
2019-04-09 23:35:31 +02:00
libc/mem.c libc/ctype.c \
libc/rand.c libc/sprintf.c \
libc/status.c libc/string.c \
libc/strtol.c extras/argv.c \
extras/prog.c
2019-03-18 13:45:26 +01:00
2019-04-09 23:36:30 +02:00
KernObj=$(patsubst %.c,$(KOBJDIR)/%.o,$(KernSources))
2019-04-09 17:16:13 +02:00
# Kernel sources
2019-04-23 11:32:45 +02:00
KernSources = libbuf/buf.c libbuf/bputc.c libbuf/bscroll.c \
2019-04-09 23:35:31 +02:00
libbuf/bprint.c kernel/cpu/cpuid.c \
kernel/cpu/idt.c kernel/init/init.c \
kernel/init/table.c kernel/io/cursor.c \
kernel/ke/log.c kernel/io/vga.c \
kernel/ke/panic.c kernel/mm/map.c \
kernel/mm/heap.c kernel/mm/malloc.c \
kernel/mm/gdt.c kernel/ps/sched.c \
kernel/init/info.c kernel/init/ssp.c \
2019-04-24 18:42:38 +02:00
kernel/io/rtc.c kernel/io/keyb.c
2019-04-09 17:16:13 +02:00
2019-04-09 23:35:31 +02:00
LibCObj=$(patsubst %.c,$(KOBJDIR)/%.o,$(LibCSources))
## KALEID MAKEFILE ----------------------------------------------------------- #
all : OS/K $(dep)
2019-04-09 17:16:13 +02:00
## MISC MAKEFILE ------------------------------------------------------------- #
./ProjectTree: ./.stylehlp_sh
@cat ./.stylehlp_sh > ./ProjectTree
@echo "\n" >> ./ProjectTree
@tree --dirsfirst >> ./ProjectTree
@echo ${CL2}[$@] ${CL}Generated.${CL3}
$(KOBJDIR):
@mkdir -p $(KOBJDIR)
2019-04-09 23:35:31 +02:00
## LIB C MAKEFILE ------------------------------------------------------------ #
$(KOBJDIR)/libc/atoi.o: $(KALEIDDIR)/libc/atoi.c $(INCLUDEDIR)/*/*.h | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(KCC) -D_NEED_ATOI $< -o $@.1
@$(KCC) -D_NEED_ATOL $< -o $@.2
@$(KCC) -D_NEED_ATOU $< -o $@.3
@$(KCC) -D_NEED_ATOUL $< -o $@.4
2019-04-22 20:15:32 +02:00
@$(LD) $(LDFLAGS) -r $@.1 $@.2 $@.3 $@.4 -o $@
2019-04-10 10:59:13 +02:00
@rm -f $@.1 $@.2 $@.3 $@.4
2019-03-18 13:09:34 +01:00
@echo ${CL2}[$@] ${CL}Compiled.${CL3}
2019-04-09 23:35:31 +02:00
$(KOBJDIR)/libc/itoa.o: $(KALEIDDIR)/libc/itoa.c $(INCLUDEDIR)/*/*.h | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(KCC) -D_NEED_ITOA $< -o $@.1
@$(KCC) -D_NEED_LTOA $< -o $@.2
@$(KCC) -D_NEED_UTOA $< -o $@.3
@$(KCC) -D_NEED_ULTOA $< -o $@.4
2019-04-22 20:15:32 +02:00
@$(LD) $(LDFLAGS) -r $@.1 $@.2 $@.3 $@.4 -o $@
2019-04-10 10:59:13 +02:00
@rm -f $@.1 $@.2 $@.3 $@.4
2019-03-18 13:09:34 +01:00
@echo ${CL2}[$@] ${CL}Compiled.${CL3}
2019-04-09 23:35:31 +02:00
$(KOBJDIR)/libc/mem.o: $(KALEIDDIR)/libc/mem.c $(INCLUDEDIR)/*/*.h | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
2019-03-17 22:47:21 +01:00
@$(KCC) -fno-strict-aliasing $< -o $@
2019-03-18 13:09:34 +01:00
@echo ${CL2}[$@] ${CL}Compiled.${CL3}
2019-03-17 22:47:21 +01:00
2019-04-22 20:15:32 +02:00
$(KOBJDIR)/kernel/cpu/idt.o: $(KALEIDDIR)/kernel/cpu/idt.c \
$(KALEIDDIR)/kernel/cpu/isr.asm $(INCLUDEDIR)/*/*.h | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/cpu/isr.asm -o $@.1
@$(KCC) $< -o $@.2
@$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@
@rm -f $@.1 $@.2
@echo ${CL2}[$@] ${CL}Compiled.${CL3}
2019-04-24 18:42:38 +02:00
$(KOBJDIR)/kernel/io/keyb.o: $(KALEIDDIR)/kernel/io/keyb.c \
$(KALEIDDIR)/kernel/io/keyb.asm $(INCLUDEDIR)/*/*.h | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/io/keyb.asm -o $@.1
@$(KCC) $< -o $@.2
@$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@
@rm -f $@.1 $@.2
@echo ${CL2}[$@] ${CL}Compiled.${CL3}
$(KOBJDIR)/kernel/io/rtc.o: $(KALEIDDIR)/kernel/io/rtc.c \
$(KALEIDDIR)/kernel/io/rtc.asm $(INCLUDEDIR)/*/*.h | $(KOBJDIR)
@mkdir -p $(shell dirname $@)
@$(ASM) $(ASMFLAGS) $(KALEIDDIR)/kernel/io/rtc.asm -o $@.1
@$(KCC) $< -o $@.2
@$(LD) $(LDFLAGS) -r $@.1 $@.2 -o $@
@rm -f $@.1 $@.2
@echo ${CL2}[$@] ${CL}Compiled.${CL3}
2019-04-09 23:35:31 +02:00
## MAIN MAKEFILE ------------------------------------------------------------- #
$(KOBJDIR)/%.o: %.c $(INCLUDEDIR)/*/*.h | $(KOBJDIR)
2019-04-09 17:16:13 +02:00
@mkdir -p $(shell dirname $@)
2019-03-17 22:47:21 +01:00
@$(KCC) $< -o $@
2019-03-18 13:09:34 +01:00
@echo ${CL2}[$@] ${CL}Compiled.${CL3}
2019-04-06 07:53:58 +02:00
2019-03-17 22:32:52 +01:00
test: all
2019-04-25 16:31:06 +02:00
@qemu-system-x86_64 -rtc base=localtime -m 4G -hda $(BUILDDIR)/bin/disk.img \
-d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log &
2019-04-09 17:16:13 +02:00
2019-03-17 22:32:52 +01:00
test32: all
@qemu-system-i386 -hda $(BUILDDIR)/bin/disk.img -d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log &
2019-03-17 22:32:52 +01:00
debug: all
@qemu-system-x86_64 -m 64M -hda $(BUILDDIR)/bin/disk.img -no-reboot \
-no-shutdown -d cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log &
@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > $(BUILDDIR)/kaleid64_disasm.asm
@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > $(BUILDDIR)/kaleid32_disasm.asm
2019-03-29 10:29:05 +01:00
gdb: all
@qemu-system-x86_64 -m 64M -hda $(BUILDDIR)/bin/disk.img -no-reboot \
2019-04-22 22:32:21 +02:00
-no-shutdown -d cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log &
2019-03-29 10:29:05 +01:00
@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > kaleid64_disasm.asm
@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > kaleid32_disasm.asm
2019-03-29 13:45:24 +01:00
@gdb \
-ex "set arch i386:x86-64:intel" \
-ex "target remote localhost:1234" \
-ex "symbol-file $(BINDIR)/kaleid" \
-ex "break BtStartKern" \
2019-04-24 21:08:44 +02:00
ddd: all
@qemu-system-x86_64 -m 64M -hda $(BUILDDIR)/bin/disk.img -no-reboot \
-no-shutdown -d cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log &
@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > kaleid64_disasm.asm
@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > kaleid32_disasm.asm
@ddd
2019-03-17 22:32:52 +01:00
install_mbr: $(BINDIR)/disk.img $(MBRDIR)/grub.cfg
@mkdir -p $(BINDIR)/disk
@echo ${CL2}[$@] ${NC}Installing MBR on image...${CL3}
2019-03-18 22:46:58 +01:00
-@$(MBRDIR)/umount.sh $(BINDIR)/disk || /bin/true
2019-03-17 22:32:52 +01:00
@$(MBRDIR)/grub-install.sh $(BINDIR)/disk.img $(BINDIR)/disk $(MBRDIR)/grub.cfg
@rmdir $(BINDIR)/disk
@echo ${CL2}[$@] ${CL}Success.${CL3}
2019-04-09 23:35:31 +02:00
dust:
-@$(MBRDIR)/umount.sh $(BINDIR)/disk || /bin/true
@rm -Rf $(OBJDIR)/*
@echo ${CL2}[$@] ${CL}Cleaned.${CL3}
2019-03-17 22:32:52 +01:00
clean:
2019-03-18 22:46:58 +01:00
-@$(MBRDIR)/umount.sh $(BINDIR)/disk || /bin/true
2019-04-09 17:16:13 +02:00
@rm -Rvf ./ProjectTree $(BUILDDIR)/*.log
2019-04-09 23:35:31 +02:00
@rm -Rvf $(BINDIR)/* $(OBJDIR)/*
@echo ${CL2}[$@] ${CL}Cleaned.${CL3}
2019-03-17 22:32:52 +01:00
2019-03-18 13:45:26 +01:00
$(BINDIR)/kaleid: $(LOBJDIR)/kaleid.x86_64
2019-03-17 22:32:52 +01:00
@echo ${CL2}[$@] ${NC}Objcopy...${CL3}
2019-04-10 10:55:22 +02:00
@$(OBJCOPY) -I elf64-x86-64 -O elf32-i386 $(LOBJDIR)/kaleid.x86_64 $(BINDIR)/kaleid
2019-03-17 22:32:52 +01:00
@echo ${CL2}[$@] ${CL}Success.${CL3}
2019-04-09 17:16:13 +02:00
$(LOBJDIR)/kaleid.x86_64: $(LibCObj) $(KernObj) $(LOBJDIR)/loader.o
2019-03-17 22:32:52 +01:00
@echo ${CL2}[$@] ${NC}Linking kernel objects...${CL3}
2019-04-09 17:16:13 +02:00
@$(LD) $(LDFLAGS) -T $(BUILDDIR)/kernel.ld \
$(LOBJDIR)/loader.o $(KernObj) $(LibCObj) \
2019-03-18 13:45:26 +01:00
-o $(LOBJDIR)/kaleid.x86_64
2019-03-17 22:32:52 +01:00
@echo ${CL2}[$@] ${CL}Success.${CL3}
2019-03-19 12:24:27 +01:00
$(LOBJDIR)/loader.o: $(LOADERDIR)/loader.asm $(LOADERDIR)/*/*.inc
2019-03-17 22:32:52 +01:00
@echo ${CL2}[$@] ${NC}Making loader...${CL3}
2019-04-09 17:16:13 +02:00
@mkdir -p $(shell dirname $@)
2019-03-18 13:45:26 +01:00
@$(ASM) $(ASMFLAGS) $(LOADERDIR)/loader.asm -o $(LOBJDIR)/loader.o > /dev/null
2019-03-17 22:32:52 +01:00
@echo ${CL2}[$@] ${CL}Success.${CL3}
2019-03-17 22:32:52 +01:00
$(BINDIR)/disk.img: $(MBRDIR)/create_disk.sh
@echo ${CL2}[$@]${NC} Constructing disk image...${CL3}
2019-03-18 22:46:58 +01:00
-@$(MBRDIR)/umount.sh $(BINDIR)/disk || /bin/true
2019-03-17 22:32:52 +01:00
@$(MBRDIR)/create_disk.sh $(BINDIR)/disk.img
@make install_mbr
@echo ${CL2}[$@]${NC} Constructing disk image...${CL3}
2019-03-18 13:45:26 +01:00
OS/K: $(BINDIR)/kaleid $(BINDIR)/disk.img ./ProjectTree
@mkdir -p $(BINDIR)/disk
2019-04-09 23:35:31 +02:00
@echo ${CL2}[[$@]] ${NC}Integrating kernel, $(mode) mode.${CL3}
2019-03-18 22:46:58 +01:00
-@$(MBRDIR)/umount.sh $(BINDIR)/disk || /bin/true
2019-03-18 13:45:26 +01:00
@$(MBRDIR)/mount.sh $(BINDIR)/disk.img $(BINDIR)/disk
@cp $(BINDIR)/kaleid $(BINDIR)/disk/boot/kaleid
@$(MBRDIR)/umount.sh $(BINDIR)/disk
@echo ${CL2}[[$@]] ${CL}Success.${CL3}
@rmdir $(BINDIR)/disk