Unverified Commit d0528578 authored by PhoeniX's avatar PhoeniX

Migrate to cmake build system

parent a4e95bba
......@@ -6,13 +6,17 @@ build:
stage: build
cache:
paths:
- deps
- build/*-prefix
script:
- make images
- mkdir build
- cd build
- cmake ..
- make all iso VERBOSE=1
artifacts:
name: "$CI_BUILD_REF_NAME"
paths:
- bin
- build/pxkrnl
- build/phoenixos.iso
pages:
stage: docs
......
cmake_minimum_required(VERSION 3.11.4)
include(ExternalProject)
include(cmake/toolchain.cmake)
project(pxos ASM C CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdlib -ffreestanding")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlib -ffreestanding")
set(SRCDIR "${CMAKE_SOURCE_DIR}/src")
include(cmake/modules.cmake)
include(cmake/kernel.cmake)
include(cmake/deps.cmake)
include(cmake/iso.cmake)
include(cmake/launch.cmake)
include(cmake/cpplint.cmake)
include(cmake/pvs.cmake)
add_lint_target("${LINT_SOURCES}")
# PhoeniX OS Makefile
# Copyright © 2017-2019 Yury Popov a.k.a. PhoeniX
.PHONY: all
all: kernel
MAKEROOT := $(PWD)/makefiles
sinclude $(MAKEROOT)/root.mk
function(add_lint_target SOURCES_LIST)
find_program(LINT cpplint)
list(REMOVE_DUPLICATES SOURCES_LIST)
list(SORT SOURCES_LIST)
list(FILTER SOURCES_LIST INCLUDE REGEX "\\.(c(c|uh?|pp|xx|\\+\\+|)?|h(pp|\\+\\+|xx)?)$")
add_custom_target(lint
COMMAND "${CMAKE_COMMAND}" -E chdir
"${CMAKE_CURRENT_SOURCE_DIR}"
"${LINT}" ${SOURCES_LIST}
DEPENDS ${SOURCES_LIST}
VERBATIM
)
endfunction()
ExternalProject_Add(
ovmf.fd
URL https://mirrors.edge.kernel.org/ubuntu/pool/universe/e/edk2/ovmf_0~20180205.c0d9813c-2_all.deb
URL_HASH SHA256=6c73c1b99fe745d95dd5fc3e1d09924018628d40d8cbad556696f2906d335636
EXCLUDE_FROM_ALL TRUE
CONFIGURE_COMMAND ""
BUILD_COMMAND ${TAR} xf <SOURCE_DIR>/data.tar.xz -C <BINARY_DIR> --strip-components 4 ./usr/share/ovmf/OVMF.fd
INSTALL_COMMAND ""
)
ExternalProject_Get_Property(ovmf.fd BINARY_DIR)
set(OVMF_PATH ${BINARY_DIR}/OVMF.fd)
ExternalProject_Add(
syslinux
URL https://mirrors.edge.kernel.org/pub/linux/utils/boot/syslinux/6.xx/syslinux-6.03.tar.xz
URL_HASH SHA256=26d3986d2bea109d5dc0e4f8c4822a459276cf021125e8c9f23c3cca5d8c850e
EXCLUDE_FROM_ALL TRUE
DOWNLOAD_NO_EXTRACT ON
CONFIGURE_COMMAND ""
BUILD_COMMAND ${TAR} xf <DOWNLOADED_FILE> -C <SOURCE_DIR> --strip-components 3 syslinux-6.03/bios/core/isolinux.bin syslinux-6.03/bios/com32/elflink/ldlinux/ldlinux.c32 syslinux-6.03/bios/com32/lib/libcom32.c32 syslinux-6.03/bios/com32/mboot/mboot.c32
COMMAND cp <SOURCE_DIR>/isolinux.bin <SOURCE_DIR>/elflink/ldlinux/ldlinux.c32 <SOURCE_DIR>/lib/libcom32.c32 <SOURCE_DIR>/mboot/mboot.c32 <BINARY_DIR>/
INSTALL_COMMAND ""
)
ExternalProject_Get_Property(syslinux BINARY_DIR)
set(SYSLINUX_PATH ${BINARY_DIR})
set(ISOROOT isodir)
set(ISONAME "PhoeniX-OS")
set(ISOOPTS -quiet -U -A ${ISONAME} -V ${ISONAME} -volset ${ISONAME} -J -joliet-long -r)
set(ISOOPTS ${ISOOPTS} -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table)
set(ISOOPTS ${ISOOPTS} -eltorito-alt-boot -e efiboot.img -no-emul-boot)
add_custom_target(phoenixos.iso
mkdir -p ${ISOROOT}
COMMAND truncate -s 4m ${ISOROOT}/efiboot.img
COMMAND ${MKFAT} -t 1 -h 1 -s 8192 -v EFI -L 32 -i ${ISOROOT}/efiboot.img
COMMAND ${MMD} -i ${ISOROOT}/efiboot.img ::efi
COMMAND ${MMD} -i ${ISOROOT}/efiboot.img ::efi/boot
COMMAND ${MCOPY} -i ${ISOROOT}/efiboot.img pxkrnl ::efi/boot/bootx64.efi
COMMAND cp pxkrnl ${SYSLINUX_PATH}/isolinux.bin ${SYSLINUX_PATH}/ldlinux.c32 ${SYSLINUX_PATH}/libcom32.c32 ${SYSLINUX_PATH}/mboot.c32 ${ISOROOT}/
COMMAND echo 'default /mboot.c32 /pxkrnl' > ${ISOROOT}/isolinux.cfg
COMMAND ${MKISO} ${ISOOPTS} -o phoenixos.iso ${ISOROOT}
DEPENDS pxkrnl syslinux
)
add_custom_target(iso DEPENDS phoenixos.iso)
set(LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/ld/system.ld")
set(KERNDIR_TAG kernel.cmake)
file(GLOB_RECURSE KERNDIRS RELATIVE ${SRCDIR} ${SRCDIR}/**/${KERNDIR_TAG})
list(TRANSFORM KERNDIRS REPLACE "/${KERNDIR_TAG}$" "")
list(TRANSFORM KERNDIRS REPLACE "/" ".")
foreach(kerndir ${KERNDIRS})
list (APPEND INCDIRS "${SRCDIR}/${kerndir}/include")
endforeach(kerndir)
list(REMOVE_ITEM KERNDIRS "boot")
foreach(kerndir ${KERNDIRS})
file(GLOB SRCS "${SRCDIR}/${kerndir}/*.s" "${SRCDIR}/${kerndir}/*.c" "${SRCDIR}/${kerndir}/*.cpp")
add_library(${kerndir} STATIC ${SRCS})
file(GLOB_RECURSE INCS ${SRCDIR}/${kerndir}/include/*.h ${SRCDIR}/${kerndir}/include/*.hpp)
list(APPEND LINT_SOURCES ${INCS})
list(APPEND LINT_SOURCES ${SRCS})
target_include_directories(${kerndir} PRIVATE ${INCDIRS})
endforeach(kerndir)
file(GLOB kern_SRCS "${SRCDIR}/boot/*.s" "${SRCDIR}/boot/*.c" "${SRCDIR}/boot/*.cpp")
add_executable(pxkrnl.elf ${kern_SRCS})
file(GLOB_RECURSE kern_INCS ${SRCDIR}/boot/include/*.h ${SRCDIR}/boot/include/*.hpp)
list(APPEND LINT_SOURCES ${kern_INCS})
list(APPEND LINT_SOURCES ${kern_SRCS})
target_include_directories(pxkrnl.elf PRIVATE ${INCDIRS})
set_target_properties(pxkrnl.elf PROPERTIES LINK_DEPENDS ${LINKER_SCRIPT})
set_target_properties(pxkrnl.elf PROPERTIES LINK_FLAGS "-T ${LINKER_SCRIPT}")
target_link_libraries(pxkrnl.elf ${KERNDIRS} modules-linked)
add_custom_target(pxkrnl ALL
${CMAKE_LLVM_OBJCOPY} --strip-all pxkrnl.elf pxkrnl.elf.strip
COMMAND ${CMAKE_OBJCOPY} -Opei-x86-64 --subsystem efi-app --file-alignment 1 --section-alignment 1 pxkrnl.elf.strip pxkrnl
DEPENDS pxkrnl.elf
)
set(EFIROOT efi)
add_custom_target(${EFIROOT}
mkdir -p ${EFIROOT}/efi/boot/
COMMAND cp pxkrnl ${EFIROOT}/efi/boot/bootx64.efi
DEPENDS pxkrnl
)
set(QEMU_FLAGS -smp cores=2,threads=2 -cpu Broadwell -serial stdio)
add_custom_target(launch
${QEMU} ${QEMU_FLAGS} -kernel pxkrnl
DEPENDS pxkrnl
)
add_custom_target(launch-efi
${QEMU} ${QEMU_FLAGS} -drive file=fat:rw:${EFIROOT},format=raw,media=disk -bios ${OVMF_PATH}
DEPENDS ${EFIROOT} ovmf.fd
)
add_custom_target(launch-iso
${QEMU} ${QEMU_FLAGS} -cdrom phoenixos.iso
DEPENDS iso
)
add_custom_target(launch-iso-efi
${QEMU} ${QEMU_FLAGS} -cdrom phoenixos.iso -bios ${OVMF_PATH}
DEPENDS iso ovmf.fd
)
set(MODDIR_TAG module.cmake)
file(GLOB_RECURSE MODDIRS RELATIVE ${SRCDIR} ${SRCDIR}/**/${MODDIR_TAG})
list(TRANSFORM MODDIRS REPLACE "/${MODDIR_TAG}$" "")
file(WRITE ${CMAKE_BINARY_DIR}/modules.s ".section .modules, \"a\"\n")
file(APPEND ${CMAKE_BINARY_DIR}/modules.s ".global __modules_start__\n__modules_start__:\n")
add_library(modules-linked STATIC ${CMAKE_BINARY_DIR}/modules.s)
foreach(moddir ${MODDIRS})
string(REPLACE "/" "." mod ${moddir})
set(ROOT_${mod} ${SRCDIR}/${moddir})
file(GLOB SRCS_${mod} ${ROOT_${mod}}/*.s ${ROOT_${mod}}/*.c ${ROOT_${mod}}/*.cpp)
add_library(mod_${mod} SHARED ${SRCS_${mod}})
add_custom_command(
TARGET mod_${mod} POST_BUILD
COMMAND ${CMAKE_LLVM_OBJCOPY} --strip-non-alloc --strip-debug --strip-unneeded libmod_${mod}.so libmod_${mod}.strip.so
COMMAND touch ${CMAKE_BINARY_DIR}/modules.s
)
list(APPEND LINT_SOURCES ${SRCS_${mod}})
file(GLOB_RECURSE INCS_${mod} ${ROOT_${mod}}/*.h ${ROOT_${mod}}/*.hpp)
list(APPEND LINT_SOURCES ${INCS_${mod}})
add_dependencies(modules-linked mod_${mod})
file(APPEND ${CMAKE_BINARY_DIR}/modules.s ".global __module_${mod}\n__module_${mod}:\n")
file(APPEND ${CMAKE_BINARY_DIR}/modules.s ".incbin \"libmod_${mod}.strip.so\"\n")
endforeach(moddir)
file(APPEND ${CMAKE_BINARY_DIR}/modules.s ".global __modules_end__\n__modules_end__:\n")
find_program(PVS_ANALYZER pvs-studio-analyzer)
find_program(PLOG_CONVERT plog-converter)
set(PVS_LOG ${CMAKE_BINARY_DIR}/pvs.log)
set(PVS_TASKS ${CMAKE_BINARY_DIR}/pvs.tasks)
set(PVS_HTML ${CMAKE_BINARY_DIR}/pvs.html)
set(PVS_FILTER "GA:1,2;OP:1,2,3;64:1;CS:1,2,3")
add_custom_target(check-pvs
COMMAND "${PVS_ANALYZER}" analyze -i -j4 -a13 -o "${PVS_LOG}"
COMMAND "${PLOG_CONVERT}" -m cwe -a "${PVS_FILTER}" -t tasklist -o "${PVS_TASKS}" "${PVS_LOG}"
COMMAND "${PLOG_CONVERT}" -m cwe -a "${PVS_FILTER}" -t fullhtml -o "${PVS_HTML}" "${PVS_LOG}"
DEPENDS ${LINT_SOURCES}
VERBATIM
)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR x86_64)
set(CMAKE_CROSSCOMPILING ON)
set(CROSS_LLVM_PATH /usr/local/opt/llvm/bin)
set(CROSS_BINUTILS_PATH /usr/local/opt/binutils/bin)
find_program(CMAKE_C_COMPILER clang HINTS ${CROSS_LLVM_PATH})
find_program(CMAKE_CXX_COMPILER clang++ HINTS ${CROSS_LLVM_PATH})
find_program(CMAKE_LLVM_OBJCOPY llvm-objcopy HINTS ${CROSS_LLVM_PATH})
find_program(CMAKE_OBJCOPY objcopy HINTS ${CROSS_BINUTILS_PATH})
find_program(CMAKE_AR llvm-ar HINTS ${CROSS_LLVM_PATH})
find_program(CMAKE_NM llvm-nm HINTS ${CROSS_LLVM_PATH})
find_program(CMAKE_RANLIB llvm-ranlib HINTS ${CROSS_LLVM_PATH})
find_program(CMAKE_LINKER ld.lld HINTS ${CROSS_LLVM_PATH})
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
set(CROSS_LINKER_FLAGS "")
set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_LINKER> -o <TARGET> <LINK_FLAGS> <OBJECTS> <LINK_LIBRARIES>")
set(CMAKE_CXX_CREATE_SHARED_LIBRARY "<CMAKE_LINKER> --shared -o <TARGET> <LINK_FLAGS> <OBJECTS> <LINK_LIBRARIES>")
set(CMAKE_EXE_LINKER_FLAGS "${CROSS_LINKER_FLAGS} -g -nostdlib -O2 --hash-style=sysv" CACHE STRING "" FORCE)
set(CMAKE_MODULE_LINKER_FLAGS "${CROSS_LINKER_FLAGS} -g -nostdlib -O2 --hash-style=sysv" CACHE STRING "" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS "${CROSS_LINKER_FLAGS} -g -nostdlib -O2 --hash-style=sysv" CACHE STRING "" FORCE)
set(CMAKE_STATIC_LINKER_FLAGS ${CROSS_LINKER_FLAGS} CACHE STRING "" FORCE)
set(CROSS_TARGET x86_64-none-elf)
set(CROSS_COMPILER_FLAGS "")
set(CMAKE_ASM_COMPILER_TARGET ${CROSS_TARGET})
set(CMAKE_C_COMPILER_TARGET ${CROSS_TARGET})
set(CMAKE_CXX_COMPILER_TARGET ${CROSS_TARGET})
set(CMAKE_C_FLAGS "${CROSS_COMPILER_FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64 -mno-sse")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-exceptions -fno-rtti -fshort-wchar")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O2 -Os")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -fpic -fpie")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wproperty-attribute-mismatch")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wcast-qual -Wcast-align -Winline")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11")
set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <OBJECTS>")
set(CMAKE_CXX_ARCHIVE_CREATE "${CMAKE_C_ARCHIVE_CREATE}")
find_program(TAR tar)
find_program(MKISO xorriso)
if(MKISO)
set(MKISO ${MKISO} -as mkisofs)
else()
find_program(MKISO mkisofs)
endif()
find_program(MKFAT mformat)
find_program(MCOPY mcopy)
find_program(MMD mmd)
find_program(QEMU qemu-system-x86_64)
project(stub)
SET_PROPERTY(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
......@@ -32,10 +32,7 @@ SECTIONS
} :text
.rodata : ALIGN(1) {
. = ALIGN(8);
__modules_start__ = . ;
*(.modules)
. = ALIGN(64);
__modules_end__ = . ;
. = ALIGN(8);
__data_start__ = . ;
......
CFLAGS := -g -m64 -mno-sse
CFLAGS += -nostdlib -std=c++11
CFLAGS += -O2 -Os -Wall -Wextra -Werror
CFLAGS += -Wproperty-attribute-mismatch
CFLAGS += -Wcast-qual -Wcast-align -Winline
CFLAGS += -ffreestanding -fno-exceptions -fno-rtti -fshort-wchar
CFLAGS += -ffunction-sections -fdata-sections -fpic -fpie
LDFLAGS := -g -nostdlib -O2
ODIR=.output
OOBJDIR=$(ODIR)/obj
OMODDIR=$(ODIR)/mod
OBINDIR=$(ODIR)/bin
OLIBDIR=$(ODIR)/lib
OIMGDIR=$(ODIR)/img
ISOROOT=$(ODIR)/iso
SRCDIR=src
BIN=$(OIMGDIR)/pxkrnl
define SCANDIRS
$(foreach f,$(wildcard $(1)/$(2)) $(foreach s,$(wildcard $(1)/*),$(call SCANDIRS,$(s),$(2))),$(dir $(f)))
endef
define UNIQ
$(if $1,$(firstword $1) $(call UNIQ,$(filter-out $(firstword $1),$1)))
endef
define SRCOBJ
$(patsubst $(SRCDIR)/%,$(OOBJDIR)/%,$(patsubst %.cpp,%.o,$(patsubst %.s,%.o,$(1))))
endef
define DEPSRC
$(patsubst %.o,%.d,$(call SRCOBJ,$(1)))
endef
define RULESCAN
LIBNAME_$(1) := $$(subst /,_,$$(patsubst %/,%,$(1)))
CSRCS_$(1) := $$(wildcard $(SRCDIR)/$(1)*.cpp)
ASRCS_$(1) := $$(wildcard $(SRCDIR)/$(1)*.s)
SRCS_$(1) := $$(CSRCS_$(1)) $$(ASRCS_$(1))
OBJS_$(1) := $$(call SRCOBJ,$$(SRCS_$(1)))
INCLUDES_$(1) := $$(wildcard $(SRCDIR)/$(1)include)
INCLUDES += $$(INCLUDES_$(1))
SOURCES += $$(SRCS_$(1))
endef
define KERNRULES
$(call RULESCAN,$(1))
KERNOBJS += $(OLIBDIR)/$$(LIBNAME_$(1)).a
$(OLIBDIR)/$$(LIBNAME_$(1)).a: $$(OBJS_$(1))
@ mkdir -p $$(dir $$@)
$(QECHO) AR $$(subst $(OLIBDIR)/,,$$@)
$(Q) $(AR) cru $$@ $$?
endef
define LIBRULES
LIBOBJS += $(OLIBDIR)/$$(patsubst %/,%,$(1)).a
endef
define MODRULES
$(call RULESCAN,$(1))
MODOBJS += $(OOBJDIR)/mod_$$(LIBNAME_$(1)).o
$(OOBJDIR)/mod_$$(LIBNAME_$(1)).o: $$(OBJS_$(1))
@ mkdir -p $$(dir $$@)
$(QECHO) MODLD $$(patsubst %/,%,$(1))
$(Q) $(LD) $(LDFLAGS) --shared -s -o $$@ $$?
endef
define DOWN_RULE
deps/$(DEP_$(1)_FILE):
@ mkdir -p $$(dir $$@)
$(QECHO) DL $$@
$(Q) wget -q '$(DEP_$(1)_URL)' -O $$@
@ touch $$@
endef
DEP_LIST := SYSLINUX OVMF
DEP_SYSLINUX_VER = 6.03
DEP_SYSLINUX_URL = https://www.kernel.org/pub/linux/utils/boot/syslinux/6.xx/syslinux-6.03.zip
DEP_SYSLINUX_FILE = syslinux-$(DEP_SYSLINUX_VER).zip
DEP_OVMF_VER = 20180205
DEP_OVMF_URL = https://mirrors.edge.kernel.org/ubuntu/pool/universe/e/edk2/ovmf_0~20180205.c0d9813c-2_all.deb
DEP_OVMF_FILE = ovmf-$(DEP_OVMF_VER).deb
$(foreach d,$(DEP_LIST),$(eval $(call DOWN_RULE,$(d))))
bin/phoenixos: $(BIN)
@ mkdir -p $(dir $@)
$(QECHO) CP $@
$(Q) cp $^ $@
SYSLINUX_DEP_FILELIST := \
bios/core/isolinux.bin \
bios/com32/elflink/ldlinux/ldlinux.c32 \
bios/com32/lib/libcom32.c32 \
bios/com32/mboot/mboot.c32
SYSLINUX_FILELIST := $(notdir $(SYSLINUX_DEP_FILELIST))
ISOFILES := $(foreach f, $(SYSLINUX_FILELIST) phoenixos isolinux.cfg efiboot.img, $(ISOROOT)/$(f))
define SYSLINUX_EXTRACT
$(ISOROOT)/$(1): deps/$(DEP_SYSLINUX_FILE)
@ mkdir -p $$(dir $$@)
$(Q) unzip -q -u -j -o deps/$(DEP_SYSLINUX_FILE) -d $(ISOROOT) $(SYSLINUX_DEP_FILELIST)
@ touch $(foreach f, $(SYSLINUX_FILELIST), $(ISOROOT)/$(f))
endef
$(foreach f,$(SYSLINUX_FILELIST),$(eval $(call SYSLINUX_EXTRACT,$(f))))
$(ISOROOT)/phoenixos: $(BIN)
@ mkdir -p $(dir $@)
$(Q) cp $< $@
$(ISOROOT)/isolinux.cfg:
@ mkdir -p $(dir $@)
$(Q) echo 'default /mboot.c32 /phoenixos' > $@
$(ISOROOT)/efiboot.img: $(BIN)
@ mkdir -p $(dir $@)
$(Q) truncate -s 4m $@
$(Q) $(MKFAT) -t 1 -h 1 -s 8192 -v EFI -L 32 -i $@
$(Q) $(MMD) -i $@ ::efi
$(Q) $(MMD) -i $@ ::efi/boot
$(Q) $(MCOPY) -i $@ $(BIN) ::efi/boot/bootx64.efi
ISOOPTS := -quiet -U -A $(ISONAME) -V $(ISONAME) -volset $(ISONAME) -J -joliet-long -r
ISOOPTS += -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table
ISOOPTS += -eltorito-alt-boot -e efiboot.img -no-emul-boot
bin/phoenixos.iso: $(ISOFILES)
@ mkdir -p $(dir $@)
$(QECHO) ISO $@
@ rm -f $@
$(Q) $(MKISO) $(ISOOPTS) -o $@ $(ISOROOT)
.PHONY: images kernel iso
kernel: bin/phoenixos
iso: bin/phoenixos.iso
images: kernel iso
sinclude $(MAKEROOT)/toolchain.mk
sinclude $(MAKEROOT)/defs.mk
sinclude $(MAKEROOT)/common.mk
sinclude $(MAKEROOT)/deps.mk
sinclude $(MAKEROOT)/rules.mk
sinclude $(MAKEROOT)/images.mk
sinclude $(MAKEROOT)/utils.mk
KERNEL_DIRS := $(subst $(SRCDIR)/,,$(call SCANDIRS,$(SRCDIR),kernel.mk))
LIBRARY_DIRS := $(subst $(SRCDIR)/,,$(call SCANDIRS,$(SRCDIR),library.mk))
MODULE_DIRS := $(subst $(SRCDIR)/,,$(call SCANDIRS,$(SRCDIR),module.mk))
$(foreach dir,$(KERNEL_DIRS),$(eval $(call KERNRULES,$(dir))))
$(foreach dir,$(LIBRARY_DIRS),$(eval $(call LIBRULES,$(dir))))
$(foreach dir,$(MODULE_DIRS),$(eval $(call MODRULES,$(dir))))
CFLAGS += $(patsubst %,-I%,$(INCLUDES))
DEPS := $(foreach src,$(SOURCES),$(call DEPSRC,$(src)))
ifeq ($(filter clean,$(MAKECMDGOALS)),)
sinclude $(DEPS)
endif
$(OOBJDIR)/%.d: $(SRCDIR)/%.cpp
@ mkdir -p $(dir $@)
$(Q) $(CC) $(CFLAGS) -MM -MT $(call SRCOBJ,$^) -c $^ -o $@
@ touch $@
$(OOBJDIR)/%.d: $(SRCDIR)/%.s
@ mkdir -p $(dir $@)
@ touch $@
$(OOBJDIR)/%.o: $(SRCDIR)/%.cpp
@ mkdir -p $(dir $@)
$(QECHO) CC $(subst $(SRCDIR)/,,$<)
$(Q) $(CC) $(CFLAGS) -c $< -o $@
$(OOBJDIR)/%.o: $(SRCDIR)/%.s
@ mkdir -p $(dir $@)
$(QECHO) AS $(subst $(SRCDIR)/,,$<)
$(Q) $(CC) -c $< -o $@
$(BIN).elf: $(KERNOBJS) $(OOBJDIR)/modules-linked.o
@ mkdir -p $(dir $@)
$(QECHO) LD $(subst $(OIMGDIR)/,,$@)
$(Q) $(LD) $(LDFLAGS) -Tld/system.ld -o $@ $^
$(BIN).elf.strip: $(BIN).elf
$(Q) $(STRIP) -o $@ $^
$(BIN): $(BIN).elf.strip
@ mkdir -p $(dir $@)
$(QECHO) OC $(subst $(OIMGDIR)/,,$@)
$(Q) $(OBJCOPY) -Opei-x86-64 --subsystem efi-app --file-alignment 1 --section-alignment 1 $^ $@
$(OOBJDIR)/modules-linked.o: $(MODOBJS)
@ mkdir -p $(dir $@)
$(Q) cat $^ > $(@:.o=.b); echo -n ' ' >> $(@:.o=.b)
$(Q) $(OBJCOPY) -Oelf64-x86-64 -Bi386 -Ibinary --rename-section .data=.modules --set-section-flags .data=alloc,load,data,readonly $(@:.o=.b) $@
PREFIX=x86_64-linux-gnu-
NPROC=1
CLANG_BIN?=$(dir $(shell which clang))
CLANG_BIN?=/usr/bin
ifeq ($(OS),Windows_NT)
PREFIX=x86_64-w64-mingw32-
else
UNAME_S=$(shell uname -s)
ifeq ($(UNAME_S),Darwin)
PREFIX=x86_64-elf-
CLANG_BIN=/usr/local/opt/llvm/bin
NPROC=$(shell sysctl -n hw.ncpu)
else
NPROC=$(shell grep -c ^processor /proc/cpuinfo)
endif
endif
ifneq ($(PARALLEL),0)
MAKEFLAGS += "-j $(NPROC)"
endif
ifeq ($(VERBOSE),1)
Q=
QECHO=@ true
else
Q=@
QECHO=@ echo
endif
ifeq ($(wildcard $(CLANG_BIN)/clang),)
$(error CLANG not found in $(CLANG_BIN), make sure that it is installed and set CLANG_BIN environment variable)
endif
CC=$(CLANG_BIN)/clang --target=x86_64-none-elf
LD=$(CLANG_BIN)/ld.lld
AR=$(CLANG_BIN)/llvm-ar
OBJCOPY=$(PREFIX)objcopy
STRIP=$(PREFIX)strip
CLANG_VER=$(shell $(CLANG_BIN)/clang -v 2>&1 | head -n1 | perl -p -e 's/.*clang version (\d+)(\.\d+)*.*/$$1/g')
CLANG_5_GT=$(shell [ $(CLANG_VER) -gt 4 ] && echo true)
ifneq ($(CLANG_5_GT),true)
$(error CLANG v$(CLANG_VER) is not supported, use v5 or later)
endif
ifeq ($(UNAME_S),Darwin)
OBJCOPY=gobjcopy
endif
QEMU=qemu-system-x86_64
ifneq ($(shell which cpplint),)
LINT=$(shell which cpplint) --quiet
else
LINT=@ sh -c "echo No lint found"; true
endif
ifneq ($(shell which xorriso),)
MKISO := $(shell which xorriso) -as mkisofs
else
ifneq ($(shell which mkisofs),)
MKISO := $(shell which mkisofs)
else
ifneq ($(shell which genisoimage),)
MKISO := $(shell which genisoimage)
else
MKISO=@ sh -c "echo Neither xorriso nor mkisofs nor genisoimage was found"; false
endif
endif
endif
ifneq ($(shell which mformat),)
MKFAT := $(shell which mformat)
else
MKFAT := @ sh -c "echo mformat not found"; false
endif
ifneq ($(shell which mcopy),)
MCOPY := $(shell which mcopy)
else
MCOPY := @ sh -c "echo mcopy not found"; false
endif
ifneq ($(shell which mmd),)
MMD := $(shell which mmd)
else
MMD := @ sh -c "echo mmd not found"; false
endif
EFIBIOS=$(ODIR)/bios/OVMF.fd
EFIROOT=$(ODIR)/efi
EFIKERN=$(EFIROOT)/efi/boot/bootx64.efi
.PHONY: clean check launch launch-efi
clean:
$(QECHO) RM $(ODIR) bin
$(Q) rm -rf $(ODIR) bin
CHECKSRCS := $(filter %.cpp,$(SOURCES))
CHECKHDRS := $(foreach i,$(INCLUDES),$(wildcard $(i)/*.hpp))
$(ODIR)/check-report.txt: $(CHECKSRCS) $(CHECKHDRS)
$(Q) $(LINT) $^ > $@ || rm $@
check: $(ODIR)/check-report.txt
@ [ -f $< ] && cat $<
all: check
$(EFIBIOS): deps/$(DEP_OVMF_FILE)
@ mkdir -p $(dir $@)
$(Q) tar xOf deps/$(DEP_OVMF_FILE) data.tar.xz | tar xO './usr/share/ovmf/OVMF.fd' > $@
$(EFIKERN): $(BIN)
@ mkdir -p $(dir $@)
$(Q) cp $< $@
launch: $(BIN)
$(QEMU) -kernel $< -smp cores=2,threads=2 -cpu Broadwell -serial stdio
launch-efi: $(EFIKERN) $(EFIBIOS)
$(QEMU) -drive file=fat:rw:$(EFIROOT),format=raw,media=disk -smp cores=2,threads=2 -cpu Broadwell -bios $(EFIBIOS) -serial stdio
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment