Generate platform code from configuration

This commit is contained in:
Pierre Pronchery 2018-09-16 16:36:25 +02:00
parent f897b4d457
commit c9e099f0c2
3 changed files with 197 additions and 3 deletions

7
src/kernel/platform.conf Normal file
View File

@ -0,0 +1,7 @@
#i386
ioport bus
vga bus at ioport bus
cmos bus at ioport bus
i8259a pic at ioport bus
cmos clock at cmos bus

182
src/kernel/platform.sh Executable file
View File

@ -0,0 +1,182 @@
#!/bin/sh
#$Id$
#Copyright (c) 2018 Pierre Pronchery <khorben@defora.org>
#This file is part of DeforaOS uKernel
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
#FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#variables
PLATFORMCONF="platform.conf"
PROGNAME="platform.sh"
TARGET="platform.c"
#functions
#error
_error()
{
echo "$PROGNAME: $@" 1>&2
return 2
}
#platform
_platform()
{
ret=0
line=0
buses=
devices=
#output the header
echo "/* \$Id\$ */
/* Copyright (c) 2018 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS uKernel */
"
#output the includes
while read name type at busname bus args; do
line=$((line + 1))
#convert comments and ignore empty lines
case "$name" in
"#"*)
echo -n "/* "
[ -n "${name#?}" ] && echo -n "${name#?}"
[ -n "$type" ] && echo -n " $type"
[ -n "$at" ] && echo -n " $at"
[ -n "$busname" ] && echo -n " $busname"
[ -n "$bus" ] && echo -n " $bus"
[ -n "$args" ] && echo -n " $args"
echo " */"
continue
;;
"")
continue
;;
esac
#check for syntax errors
if [ "$at" != "at" -o "$bus" != "bus" ] &&
[ -n "$at" -o -n "$busname" -o -n "$bus" ]; then
_error "Syntax error at line $line"
ret=$?
fi
case "$type" in
bus)
buses="$buses
$name $type $busname $args"
;;
*)
devices="$devices
$name $type $busname $args"
;;
esac
done
#output the includes
echo "#include <stddef.h>"
echo "$devices" | while read name type busname args; do
[ -n "$name" ] || continue
echo "#include \"drivers/${type}.h\""
done
#begin the function
echo "
/* public */
/* functions */
/* platform_init */
void platform_init(void)
{"
#list the buses
echo "$buses" | while read name type busname args; do
[ -n "$name" ] || continue
echo " ukBus * ${name}bus;"
done
#initialize the buses
echo
echo "$buses" | while read name type busname args; do
[ -n "$name" ] || continue
if [ -z "$busname" ]; then
echo " ${name}${type} = ${type}_init(NULL, \"${name}\");"
else
echo " ${name}${type} = ${type}_init(${busname}bus, \"${name}\");"
fi
done
#initialize the devices
echo "$devices" | while read name type busname args; do
[ -n "$name" ] || continue
if [ -z "$busname" ]; then
echo " ${type}_init(NULL, \"${name}\");"
else
echo " ${type}_init(${busname}bus, \"${name}\");"
fi
done
#end the function
echo "}"
return $ret
}
#usage
_usage()
{
echo "Usage: $PROGNAME target" 1>&2
return 1
}
#main
while getopts "cO:P:" name; do
case "$name" in
c)
clean=1
;;
O)
export "${OPTARG%%=*}"="${OPTARG#*=}"
;;
P)
#XXX ignored for compatibility
;;
?)
_usage
exit $?
;;
esac
done
shift $((OPTIND - 1))
if [ $# -ne 1 ]; then
_usage
exit $?
fi
#XXX breaks OBJDIR
_platform < "$PLATFORMCONF" > "$TARGET"

View File

@ -1,11 +1,11 @@
targets=crtbegin.o,crtend.o,crti.o,crtn.o,uKernel.bin
targets=crtbegin.o,crtend.o,crti.o,crtn.o,platform.c,uKernel.bin
cppflags_force=-nostdinc -isystem ../../include -I..
as=$(CC)
asflags_force=$(CFLAGSF) $(CFLAGS) -c
cflags_force=`../../tools/platform.sh -V UKERNEL_CFLAGS -C "$$ARCH"`
cflags=-W -Wall -g -O2
ldflags_force=`../../tools/platform.sh -V UKERNEL_LDFLAGS -C "$$ARCH"`
dist=Makefile
dist=Makefile,platform.conf,platform.sh
#targets
[crtbegin.o]
@ -24,9 +24,14 @@ sources=crti.S
type=object
sources=crtn.S
[platform.c]
type=script
script=./platform.sh
depends=platform.conf,platform.sh
[uKernel.bin]
type=binary
sources=arch.S,gdt.c,idt.c,main.c,multiboot.c,start.S
sources=arch.S,gdt.c,idt.c,main.c,multiboot.c,platform.c,start.S
ldflags=$(OBJDIR)crti.o $(OBJDIR)crtbegin.o $(OBJDIR)../drivers/boot/multiboot.o $(OBJDIR)../drivers/bus.o $(OBJDIR)../drivers/bus/cmos.o $(OBJDIR)../drivers/bus/ioport.o $(OBJDIR)../drivers/bus/vga.o $(OBJDIR)../drivers/clock.o $(OBJDIR)../drivers/clock/cmos.o $(OBJDIR)../drivers/console.o $(OBJDIR)../drivers/console/uart.o $(OBJDIR)../drivers/display.o $(OBJDIR)../drivers/display/vesa.o $(OBJDIR)../drivers/display/vga.o $(OBJDIR)../drivers/pic.o $(OBJDIR)../drivers/pic/i8259a.o $(OBJDIR)../lib/libuKernel.a $(OBJDIR)crtend.o $(OBJDIR)crtn.o `$(CC) -print-libgcc-file-name`
depends=$(OBJDIR)crtbegin.o,$(OBJDIR)crtend.o,$(OBJDIR)crti.o,$(OBJDIR)crtn.o,$(OBJDIR)../drivers/boot/multiboot.o,$(OBJDIR)../drivers/bus.o,$(OBJDIR)../drivers/bus/cmos.o,$(OBJDIR)../drivers/bus/ioport.o,$(OBJDIR)../drivers/bus/vga.o,$(OBJDIR)../drivers/clock.o,$(OBJDIR)../drivers/clock/cmos.o,$(OBJDIR)../drivers/console.o,$(OBJDIR)../drivers/console/uart.o,$(OBJDIR)../drivers/display.o,$(OBJDIR)../drivers/display/vesa.o,$(OBJDIR)../drivers/display/vga.o,$(OBJDIR)../drivers/pic.o,$(OBJDIR)../drivers/pic/i8259a.o,$(OBJDIR)../lib/libuKernel.a,../arch/amd64/uKernel.ld,../arch/i386/uKernel.ld