I have gotten meson working for a very simple os, but I do have several problems with my system.
My meson build file currently cannot do the correct link order for c++ global constructors and it cannot build a iso image in the meson build file.
Here is my current tree
Code: Select all
|-- meson-build/
| \-- [build output and meson generated files]
|-- boot.asm
|-- build-iso.make
|-- cross_file.txt
|-- crti.asm
|-- crto.asm
|-- grub.cfg
|-- kernel.cpp
|-- linker.ld
\-- meson.build
meson.build
Code: Select all
# Specify the project
project('myos', 'c', 'cpp')
# Find where the crtbegin and crtend object files are so we can link to them
find_crtbegin_result = run_command('i686-elf-gcc', ['$CFLAGS', '-print-file-name=crtbegin.o'])
find_crtend_result = run_command('i686-elf-gcc', ['$CFLAGS', '-print-file-name=crtend.o'])
crtbegin_obj = find_crtbegin_result.stdout().strip()
crtend_obj = find_crtend_result.stdout().strip()
# I use nasm for my assembly files, so this is how to use it
# First of all we find nasm
nasm = find_program('nasm')
# We then define a generator that will make object files, if you need different command line
# options to the assembler, then make a new generator
asm_gen = generator(nasm,
output: '@[email protected]',
arguments: ['-felf32', '@INPUT@', '-o', '@OUTPUT@'])
# Generators actually take in a array of files to generate
bootstrapobjs = asm_gen.process(['boot.asm'])
# crti and crtn need to be handled by themselves, because the link order is important to them
crtiobj = asm_gen.process(['crti.asm'])
crtnobj = asm_gen.process(['crtn.asm'])
# note, I currently haven't worked out a way to specify the correct link order, i will do a bug report soon
# to try and figure out if there is a way to do this.
# To verify that the constructors actually worked, I edited the ninja build file
# generated by meson to make sure the constructors worked, but since my current kernel
# doesn't use any, I don't need the constructors to work.
linker_file = files('linker.ld')
# Because crtbegin and crtend are already generated object files, we specify them with the
# objects setting, but because of this, they are last on the command line, breaking global constructors
# We use our linker script by depending on it at link time, and specifying our linker args for our kernel
kernel_exe = executable('myos.elf', bootstrapobjs, crtiobj, 'kernel.cpp', crtnobj,
cpp_args: ['-O2', '-Wall', '-Wextra', '-fno-exceptions', '-fno-rtti', '-nostdlib'],
link_args: ['-O2', '-lgcc', '-nostdlib', '-T', '../linker.ld'],
link_depends: 'linker.ld',
objects: [crtbegin_obj, crtend_obj])
# currently it is easer having a seperate make file just for making the iso image, I am planing
# on finding a better way to do this
grub_file = files('grub.cfg')
iso_builder = find_program('build-iso.make')
iso_gen = custom_target('build-iso',
output: ['myos.iso'],
command: ['make', '-f', iso_builder],
build_by_default: true,
depends: [kernel_exe],
depend_files: 'grub.cfg')
cross_file.txt
Code: Select all
[binaries]
c = 'i686-elf-gcc'
cpp = 'i686-elf-g++'
ar = 'i686-elf-ar'
strip = 'i686-elf-strip'
[properties]
has_function_printf = false
c_args = ['-ffreestanding']
cpp_args = ['-ffreestanding']
c_link_args = ['-ffreestanding', '-nostdlib', '-lgcc']
cpp_link_args = ['-ffreestanding', '-nostdlib', '-lgcc']
[host_machine]
system = 'myos-kernel'
cpu_family = 'x86'
cpu = 'i686'
endian = 'little'
This build setup still needs more work and probably needs changing when I start using a libc.