Added options to compile stdlib and init executable.

This commit is contained in:
Patrick 2024-02-03 02:09:28 +01:00
parent c2a40a8104
commit 3ec4fa78b4
7 changed files with 66 additions and 35 deletions

View File

@ -11,30 +11,18 @@ AddOption(
action = 'store', action = 'store',
default = 'x86_64' default = 'x86_64'
) )
AddOption(
'--target',
dest = 'target',
type = 'choice',
choices = ('os', 'stdlib'),
nargs = 1,
action = 'store',
default = 'os'
)
arch = GetOption('arch') arch = GetOption('arch')
target = GetOption('target')
env = Environment(tools = ['default', 'compilation_db'], ENV = os.environ) # TODO inheriting the environment is not a best practice env = Environment(tools = ['default', 'compilation_db'], ENV = os.environ) # TODO inheriting the environment is not a best practice
env.Append(CCFLAGS = ['-g', '-O0', '-fno-stack-protector']) env.Append(CCFLAGS = ['-g', '-O0', '-fno-stack-protector'])
env.Append(CPPDEFINES = ['BASTL_EXTENSIONS=1']) env.Append(CPPDEFINES = ['BASTL_EXTENSIONS=1'])
env['BUILD_TARGET'] = target
env['TARGET_ARCH'] = arch env['TARGET_ARCH'] = arch
# env.Append(CCFLAGS = ['-O2']) # env.Append(CCFLAGS = ['-O2'])
env['ISO_FILES'] = [] env['ISO_FILES'] = []
env = SConscript('targets/_any/SConscript', exports = 'env') env = SConscript('targets/_any/SConscript', exports = 'env')
if env['BUILD_TARGET'] == 'os': env = SConscript(f'targets/{arch}/SConscript', exports = 'env')
env = SConscript(f'targets/{arch}/SConscript', exports = 'env')
comp_db = env.CompilationDatabase(target = '#compile_commands.json') comp_db = env.CompilationDatabase(target = '#compile_commands.json')
env.Default(comp_db) env.Default(comp_db)

View File

@ -11,5 +11,6 @@ env.Append(KERNEL_SOURCES = [env.File(f) for f in any_target_sources])
env = SConscript('bastl/SConscript', exports = 'env') env = SConscript('bastl/SConscript', exports = 'env')
env = SConscript('kernel/SConscript', exports = 'env') env = SConscript('kernel/SConscript', exports = 'env')
env = SConscript('stdlib/SConscript', exports = 'env') env = SConscript('stdlib/SConscript', exports = 'env')
env = SConscript('init/SConscript', exports = 'env')
Return('env') Return('env')

View File

@ -0,0 +1,23 @@
Import('env')
init_sources = Split('''
src/main.cpp
''')
init_env = env.Clone()
init_env['AS'] = 'x86_64-elf-baos-as'
init_env['CC'] = 'x86_64-elf-baos-gcc'
init_env['CXX'] = 'x86_64-elf-baos-g++'
init_env['LD'] = 'x86_64-elf-baos-g++'
init_env.Append(CXXFLAGS = ['-fno-exceptions', '-fno-rtti', '-std=c++20'])
init_env.Append(CPPPATH = ['#targets/_any/bastl/include', '#targets/_any/stdlib/include', '#targets/_any/kernel/include'])
prog_init = init_env.Program(
target = f'#sysroot_{init_env["TARGET_ARCH"]}/bin/init',
source = init_sources,
LIBS = [init_env.File('#stdlib/libc.a')]
)
init_env.Alias('init', prog_init)
Return('env')

View File

@ -0,0 +1,8 @@
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("Hello world!\n");
return 0;
}

View File

@ -16,24 +16,23 @@ stdlib_sources = Split(f'''
env.Append(KERNEL_SOURCES = [env.File(f) for f in stdlib_sources]) env.Append(KERNEL_SOURCES = [env.File(f) for f in stdlib_sources])
if env['BUILD_TARGET'] == 'stdlib': ### Stdlib
### Stdlib stdlib_env = env.Clone()
stdlib_env = env.Clone() stdlib_env['AS'] = 'x86_64-elf-baos-as'
stdlib_env['AS'] = 'x86_64-elf-baos-as' stdlib_env['CC'] = 'x86_64-elf-baos-gcc'
stdlib_env['CC'] = 'x86_64-elf-baos-gcc' stdlib_env['CXX'] = 'x86_64-elf-baos-g++'
stdlib_env['CXX'] = 'x86_64-elf-baos-g++' stdlib_env['LD'] = 'x86_64-elf-baos-g++'
stdlib_env['LD'] = 'x86_64-elf-baos-g++' stdlib_env['OBJSUFFIX'] = f'.baos{stdlib_env["OBJSUFFIX"]}'
stdlib_env['OBJSUFFIX'] = f'.baos{stdlib_env["OBJSUFFIX"]}' stdlib_env.Append(CXXFLAGS = ['-fno-exceptions', '-fno-rtti', '-std=c++20'])
stdlib_env.Append(CXXFLAGS = ['-fno-exceptions', '-fno-rtti', '-std=c++20']) stdlib_env.Append(CPPPATH = ['#targets/_any/bastl/include', '#targets/_any/stdlib/include', '#targets/_any/kernel/include'])
stdlib_env.Append(CPPPATH = ['#targets/_any/bastl/include', '#targets/_any/stdlib/include', '#targets/_any/kernel/include'])
lib_stdlib = stdlib_env.StaticLibrary( lib_stdlib = stdlib_env.StaticLibrary(
target = stdlib_env.File('#stdlib/libc.a'), target = stdlib_env.File('#stdlib/libc.a'),
source = stdlib_sources source = stdlib_sources
) )
crti_o = stdlib_env.Object(target = '#stdlib/crti.o', source = stdlib_env['CRTI_PATH']) crti_o = stdlib_env.Object(target = '#stdlib/crti.o', source = stdlib_env['CRTI_PATH'])
crtn_o = stdlib_env.Object(target = '#stdlib/crtn.o', source = stdlib_env['CRTN_PATH']) crtn_o = stdlib_env.Object(target = '#stdlib/crtn.o', source = stdlib_env['CRTN_PATH'])
crt0_o = stdlib_env.Object(target = '#stdlib/crt0.o', source = stdlib_env['CRT0_PATH']) crt0_o = stdlib_env.Object(target = '#stdlib/crt0.o', source = stdlib_env['CRT0_PATH'])
stdlib_env.Default([lib_stdlib, crti_o, crtn_o, crt0_o]) stdlib_env.Alias('stdlib', [lib_stdlib, crti_o, crtn_o, crt0_o])
Return('env') Return('env')

View File

@ -2,9 +2,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h> #include <stddef.h>
#if defined(__baos_kernel_source__)
#include "os/memory.hpp" #include "os/memory.hpp"
#include "os/syscall.hpp"
#include "os/tty.hpp" #include "os/tty.hpp"
#else
#include "os/syscall.hpp"
#endif
namespace namespace
{ {
@ -39,16 +43,21 @@ extern "C"
{ {
void abort() void abort()
{ {
#if defined(__baos_kernel_source__)
tty::write("Abort called!"); tty::write("Abort called!");
__asm__ volatile("hlt"); __asm__ volatile("hlt");
while(true) {}; while(true) {};
__builtin_unreachable(); __builtin_unreachable();
#else
exit(-1); // TODO
#endif
} }
void exit(int exitCode) noexcept void exit(int exitCode) noexcept
{ {
#if !defined(__baos_kernel_source__) #if !defined(__baos_kernel_source__)
baos::doSyscall(baos::Syscall::EXIT, exitCode); baos::doSyscall(baos::Syscall::EXIT, exitCode);
__builtin_unreachable();
#else #else
abort(); // no exit in kernel! abort(); // no exit in kernel!
#endif #endif
@ -93,6 +102,7 @@ void* malloc(size_t size) noexcept
prevBlock = block; prevBlock = block;
} }
#if defined(__baos_kernel_source__)
baos::PageRange pages = baos::allocatePages({ baos::PageRange pages = baos::allocatePages({
.numPages = std::max(1ul, baos::bytesToPages(size, /* bigPages = */ true)), .numPages = std::max(1ul, baos::bytesToPages(size, /* bigPages = */ true)),
.bigPages = true .bigPages = true
@ -108,6 +118,9 @@ void* malloc(size_t size) noexcept
gNextBlock = newBlock; gNextBlock = newBlock;
return malloc(size); return malloc(size);
#else
return nullptr; // TODO!
#endif
} }
void free(void* memory) noexcept void free(void* memory) noexcept

View File

@ -42,7 +42,6 @@ prog_kernel = kernel_env.Program(
source = kernel_sources source = kernel_sources
) )
kernel_env.Depends(prog_kernel, [crti_o, crtn_o] + kernel_sources) kernel_env.Depends(prog_kernel, [crti_o, crtn_o] + kernel_sources)
kernel_env.Default(prog_kernel)
x86_64_iso_files = [ x86_64_iso_files = [
{ {
@ -80,7 +79,6 @@ prog_loader = uefi_env.Program(
target = loader_target, target = loader_target,
source = loader_sources source = loader_sources
) )
uefi_env.Default(prog_loader)
### Bootable Image ### Bootable Image
def runcmd(*args, **kwargs): def runcmd(*args, **kwargs):
@ -125,7 +123,8 @@ cmd_iso = env.Command(
source = [cmd_image], source = [cmd_image],
action = build_iso action = build_iso
) )
env.Default(cmd_iso) alias_os = env.Alias('os', [cmd_iso])
env.Default(alias_os)
# finally update the environment # finally update the environment
env.Append(ISO_FILES = x86_64_iso_files) env.Append(ISO_FILES = x86_64_iso_files)