From 31c3c8dcb5ea281201b9c01140bceeadc80f7686 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Fri, 20 Aug 2021 20:57:59 +0300 Subject: New build system --- Makefile | 51 +++++++++++++++++++------------------- README | 5 ++++ arch/riscv/conf/init-link.S | 14 +++++++++++ arch/riscv/conf/kernel-link.S | 14 +++++++++++ arch/riscv/conf/kernel.its | 34 ++++++++++++++++++++++++++ arch/riscv/init-link.S | 14 ----------- arch/riscv/kernel-link.S | 14 ----------- arch/riscv/kernel.its | 34 -------------------------- arch/riscv/riscv-link.S | 22 ----------------- arch/riscv/source.mk | 8 ++---- scripts/gen-deps | 43 ++++++++++++++++++++++++++++++++ scripts/mkobjs | 57 ------------------------------------------- scripts/prepend | 11 --------- 13 files changed, 137 insertions(+), 184 deletions(-) create mode 100644 README create mode 100644 arch/riscv/conf/init-link.S create mode 100644 arch/riscv/conf/kernel-link.S create mode 100644 arch/riscv/conf/kernel.its delete mode 100644 arch/riscv/init-link.S delete mode 100644 arch/riscv/kernel-link.S delete mode 100644 arch/riscv/kernel.its delete mode 100644 arch/riscv/riscv-link.S create mode 100755 scripts/gen-deps delete mode 100755 scripts/mkobjs delete mode 100755 scripts/prepend diff --git a/Makefile b/Makefile index ecfecec..dbf6456 100644 --- a/Makefile +++ b/Makefile @@ -1,40 +1,39 @@ -DO != echo > deps.mk -CFLAGS := -fno-pie -ggdb3 -ffreestanding -nostdlib -INCLUDE_DIRS := include +DO != echo > deps.mk + +CFLAGS = -fno-pie -ggdb3 -ffreestanding -nostdlib +DEPFLAGS = -MT $@ -MMD -MP -MF $@.d all: apos.bin -ARCH ?= riscv -CROSS_COMPILE ?= riscv64-unknown-elf- +ARCH ?= riscv +CROSS_COMPILE ?= riscv64-unknown-elf- # Common programs -CC := gcc -AR := ar -CPP := cpp -OBJCOPY ?= objcopy -OBJCOPY_FLAGS ?= -Obinary +CC := gcc +AR := ar +CPP := cpp +OBJCOPY ?= objcopy +OBJCOPY_FLAGS ?= -Obinary KERNEL_SOURCES := -KERNEL_LINK := INIT_SOURCES := -INIT_LINK := -INCLUDE_DIRS := include +CLEANUP := build deps.mk kernel.* init.* apos.bin include arch/$(ARCH)/source.mk -INCLUDE_FLAGS != ./scripts/prepend -I $(INCLUDE_DIRS) - -KERNEL_OBJECTS != ./scripts/mkobjs $(CROSS_COMPILE)$(CC) $(CFLAGS)\ - $(INCLUDE_FLAGS) --kern-files $(KERNEL_SOURCES) +INCLUDE_FLAGS := -Iinclude -Iarch/$(ARCH)/include -KERNEL_LD != ./scripts/mkobjs $(CROSS_COMPILE)$(CPP)\ - $(INCLUDE_FLAGS) --kern-lds $(KERNEL_LINK) +COMPILE = $(CROSS_COMPILE)$(CC) $(CFLAGS) $(DEPFLAGS) $(INCLUDE_FLAGS) +GENLINK = $(CROSS_COMPILE)$(CPP) $(DEPFLAGS) $(INCLUDE_FLAGS) +STRIPLINK = sed -n '/^[^\#]/p' -INIT_OBJECTS != ./scripts/mkobjs $(CROSS_COMPILE)$(CC) $(CFLAGS)\ - $(INCLUDE_FLAGS) --init-files $(INIT_SOURCES) +KERNEL_LINK := arch/$(ARCH)/conf/kernel-link +INIT_LINK := arch/$(ARCH)/conf/init-link -INIT_LD != ./scripts/mkobjs $(CROSS_COMPILE)$(CPP)\ - $(INCLUDE_FLAGS) --init-lds $(INIT_LINK) +KERNEL_OBJECTS != ./scripts/gen-deps --kern "$(KERNEL_SOURCES)" +INIT_OBJECTS != ./scripts/gen-deps --init "$(INIT_SOURCES)" +KERNEL_LD != ./scripts/gen-deps --link "$(KERNEL_LINK).S" +INIT_LD != ./scripts/gen-deps --link "$(INIT_LINK).S" include deps.mk @@ -44,10 +43,10 @@ apos.bin: kernel.elf init.elf cat init.bin kernel.bin > $@ kernel.elf: $(KERNEL_OBJECTS) $(KERNEL_LD) - $(CROSS_COMPILE)$(CC) -T$(KERNEL_LD) $(CFLAGS) $(INCLUDE_FLAGS) $(KERNEL_OBJECTS) -o $@ + $(COMPILE) -T$(KERNEL_LD) $(KERNEL_OBJECTS) -o $@ init.elf: $(INIT_OBJECTS) $(INIT_LD) - $(CROSS_COMPILE)$(CC) -T$(INIT_LD) $(CFLAGS) $(INCLUDE_FLAGS) $(INIT_OBJECTS) -o $@ + $(COMPILE) -T$(INIT_LD) $(INIT_OBJECTS) -o $@ clean: - rm $(KERNEL_OBJECTS) $(INIT_OBJECTS) $(INIT_LD) $(KERNEL_LD) kernel.bin kernel.elf init.bin init.elf apos.bin + $(RM) -r $(CLEANUP) diff --git a/README b/README new file mode 100644 index 0000000..f668f3d --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +Requirements for a new arch: ++ arch/$(ARCH)/conf/init-link.S ++ arch/$(ARCH)/conf/init-kernel.S ++ arch/$(ARCH)/source.mk + -> populate KERNEL_SOURCES, INIT_SOURCES and CLEANUP if required diff --git a/arch/riscv/conf/init-link.S b/arch/riscv/conf/init-link.S new file mode 100644 index 0000000..db18822 --- /dev/null +++ b/arch/riscv/conf/init-link.S @@ -0,0 +1,14 @@ +#include +#include + +OUTPUT_ARCH(riscv) +ENTRY(_start) + +SECTIONS { + . = ABSOLUTE(PM_KERN); + .text ALIGN(4K) : AT(0) { + *(.init.start) + *(.text); + . = ALIGN(4K); + } +} diff --git a/arch/riscv/conf/kernel-link.S b/arch/riscv/conf/kernel-link.S new file mode 100644 index 0000000..873c7de --- /dev/null +++ b/arch/riscv/conf/kernel-link.S @@ -0,0 +1,14 @@ +#include +#include + +OUTPUT_ARCH(riscv) +ENTRY(main) + +SECTIONS { + . = ABSOLUTE(VM_KERN); + .text ALIGN(4K) : AT(0) { + *(.text.start); + *(.text); + . = ALIGN(4K); + } +} diff --git a/arch/riscv/conf/kernel.its b/arch/riscv/conf/kernel.its new file mode 100644 index 0000000..f984d40 --- /dev/null +++ b/arch/riscv/conf/kernel.its @@ -0,0 +1,34 @@ +/dts-v1/; + +/ { + description = "FUCK"; +#address-cells = <1>; + + images { + kernel { + description = "My default kenel"; + data = /incbin/("kernel.bin"); + type = "kernel"; + arch = "riscv"; + os = "apos"; + compression = "none"; + load = <0x83800000>; + entry = <0x83800000>; + hash-1 { + algo = "md5"; + }; + }; + + }; + + configurations { + default = "config"; + + config { + description = "Default configuration"; + kernel = "kernel"; + }; + + + }; +}; diff --git a/arch/riscv/init-link.S b/arch/riscv/init-link.S deleted file mode 100644 index db18822..0000000 --- a/arch/riscv/init-link.S +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include - -OUTPUT_ARCH(riscv) -ENTRY(_start) - -SECTIONS { - . = ABSOLUTE(PM_KERN); - .text ALIGN(4K) : AT(0) { - *(.init.start) - *(.text); - . = ALIGN(4K); - } -} diff --git a/arch/riscv/kernel-link.S b/arch/riscv/kernel-link.S deleted file mode 100644 index 873c7de..0000000 --- a/arch/riscv/kernel-link.S +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include - -OUTPUT_ARCH(riscv) -ENTRY(main) - -SECTIONS { - . = ABSOLUTE(VM_KERN); - .text ALIGN(4K) : AT(0) { - *(.text.start); - *(.text); - . = ALIGN(4K); - } -} diff --git a/arch/riscv/kernel.its b/arch/riscv/kernel.its deleted file mode 100644 index f984d40..0000000 --- a/arch/riscv/kernel.its +++ /dev/null @@ -1,34 +0,0 @@ -/dts-v1/; - -/ { - description = "FUCK"; -#address-cells = <1>; - - images { - kernel { - description = "My default kenel"; - data = /incbin/("kernel.bin"); - type = "kernel"; - arch = "riscv"; - os = "apos"; - compression = "none"; - load = <0x83800000>; - entry = <0x83800000>; - hash-1 { - algo = "md5"; - }; - }; - - }; - - configurations { - default = "config"; - - config { - description = "Default configuration"; - kernel = "kernel"; - }; - - - }; -}; diff --git a/arch/riscv/riscv-link.S b/arch/riscv/riscv-link.S deleted file mode 100644 index 0950850..0000000 --- a/arch/riscv/riscv-link.S +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - -OUTPUT_ARCH(riscv) -ENTRY(_start) - -SECTIONS { - . = ABSOLUTE(PM_KERN); - .init ALIGN(4K) : AT(0) { - *(.init.start); - *(.init.text); - *(.init.data); - . = ALIGN(4K); - } - - . = ABSOLUTE(VM_KERN); - .text ALIGN(4K) : AT(SIZEOF(.init)) { - *(.text.start); - *(.text.text); - *(.text.data); - } -} diff --git a/arch/riscv/source.mk b/arch/riscv/source.mk index 8bd67ee..626ecd2 100644 --- a/arch/riscv/source.mk +++ b/arch/riscv/source.mk @@ -1,9 +1,5 @@ -KERNEL_LOCAL != ./scripts/prepend arch/riscv/ main.c +KERNEL_LOCAL != echo arch/riscv/*.[cS] KERNEL_SOURCES += $(KERNEL_LOCAL) -INIT_LOCAL != ./scripts/prepend arch/riscv/init/ init.c head.S +INIT_LOCAL != echo arch/riscv/init/*.[cS] INIT_SOURCES += $(INIT_LOCAL) - -INCLUDE_DIRS += arch/riscv/include -KERNEL_LINK := arch/riscv/kernel-link.S -INIT_LINK := arch/riscv/init-link.S diff --git a/scripts/gen-deps b/scripts/gen-deps new file mode 100755 index 0000000..b764deb --- /dev/null +++ b/scripts/gen-deps @@ -0,0 +1,43 @@ +#!/bin/sh + +gencommon () { + dep="build/${s%.*}${1}.d" + obj="build/${s%.*}${1}" + + mkdir -p $(dirname ${dep}) + + echo "${dep}:" >> deps.mk + echo "include ${dep}" >> deps.mk + echo "${obj}: ${s}" >> deps.mk + +} +genlink () { + gencommon ".ld" + echo " \$(GENLINK) $< | \$(STRIPLINK) > \$@" >> deps.mk +} + +genrule () { + gencommon "${1}" + echo " \$(COMPILE) -c \$< -o \$@" >> deps.mk +} + +case "${1}" in + --kern) + suffix=.k.o + func=genrule + ;; + --init) + suffix=.i.o + func=genrule + ;; + --link) + suffix=.ld + func=genlink + ;; +esac + +for s in ${2} +do + ${func} ${suffix} + echo ${obj} +done diff --git a/scripts/mkobjs b/scripts/mkobjs deleted file mode 100755 index 188465b..0000000 --- a/scripts/mkobjs +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/sh - -genobj () { - path=$(dirname "$1")/ - obj=$(echo "$1" | sed "s/\\.[cS]/.${command_suffix}.o/") - $cc_command -MM -MP $n | \ - sed "s|\\(.*\)\\.o|${path}\\1.${command_suffix}.o|" >> deps.mk - - echo "$obj: $n" >> deps.mk - echo " $cc_command -c $1 -o $obj" >> deps.mk - - cc_objs="$cc_objs $obj" -} - -genlink () { - link=$(echo "$1" | sed 's/\.S/.ld/') - echo "$link: $1" >> deps.mk - echo " $cc_command $1 | sed -n '/^[^#]/p' > $link" >> deps.mk - - cc_objs="$cc_objs $link" -} - -for n in "$@" -do - case "$n" in - --kern-files) - command_suffix=k - command_func=genobj - continue - ;; - - --kern-lds) - command_func=genlink - continue - ;; - - --init-files) - command_suffix=i - command_func=genobj - continue - ;; - - --init-lds) - command_func=genlink - continue - ;; - esac - - if [ "$command_func" = "" ] - then - cc_command="$cc_command $n" - else - $command_func "$n" - fi -done - -echo "$cc_objs" diff --git a/scripts/prepend b/scripts/prepend deleted file mode 100755 index ef10da1..0000000 --- a/scripts/prepend +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -prep="$1" -shift - -for n in "$@" -do - ret="$ret $prep$n" -done - -echo "$ret" -- cgit v1.3