# This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at https://mozilla.org/MPL/2.0/. # frozen_string_literal: true require 'open3' require 'rake/clean' CROSS_GCC = '../eugenios/build/rootfs/bin/riscv32-unknown-linux-gnu-gcc' SYSROOT = '../eugenios/build/sysroot' QEMU = 'qemu-riscv32' CLEAN.include 'build/boot', 'build/valid' directory 'build/boot' directory 'build/valid' desc 'Final stage' task default: ['build/valid/stage3', 'build/valid/stage3.s', 'boot/stage3.elna'] do |t| exe, expected, source = t.prerequisites cat_arguments = ['cat', source] compiler_arguments = [QEMU, '-L', SYSROOT, exe] diff_arguments = ['diff', '-Nur', '--text', expected, '-'] Open3.pipeline(cat_arguments, compiler_arguments, diff_arguments) end desc 'Convert stage2 language into the stage3 language' task :convert do File.open('boot/stage3.elna', 'w') do |stage2| li_value = nil File.readlines('boot/stage2.elna').each do |line| if line.start_with?("\tj ") stage2 << "\tgoto " + line.chomp.delete_prefix("\tj ") + ";\n" li_value = nil elsif line.match?(/^\tli a0, [[:digit:]]/) li_value = line.delete_prefix("\tli a0, ").chomp elsif line == "\tli a0, '\\n'\n" li_value = "'\\n'" elsif !li_value.nil? && line.start_with?("\t_") stage2 << "\t" + line[1..-4] + li_value + ");\n" li_value = nil else stage2 << "\tli a0, #{li_value}\n" unless li_value.nil? stage2 << line li_value = nil end end end end # # Stage 3. # file 'build/valid/stage3' => 'build/valid/stage3.s' do |t| sh CROSS_GCC, '-nostdlib', '-o', t.name, *t.prerequisites end file 'build/valid/stage3.s' => ['build/boot/stage3', 'boot/stage3.elna'] do |t| exe, source = t.prerequisites cat_arguments = ['cat', source] compiler_arguments = [QEMU, '-L', SYSROOT, exe] last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, compiler_arguments) IO.copy_stream last_stdout, t.name end file 'build/boot/stage3' => 'build/boot/stage3.s' do |t| sh CROSS_GCC, '-nostdlib', '-o', t.name, *t.prerequisites end file 'build/boot/stage3.s' => ['build/valid/stage2', 'boot/stage3.elna'] do |t| exe, source = t.prerequisites cat_arguments = ['cat', source] compiler_arguments = [QEMU, '-L', SYSROOT, exe] last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, compiler_arguments) IO.copy_stream last_stdout, t.name end # # Stage 2. # file 'build/valid/stage2' => 'build/valid/stage2.s' do |t| sh CROSS_GCC, '-nostdlib', '-o', t.name, *t.prerequisites end file 'build/valid/stage2.s' => ['build/boot/stage2', 'boot/stage2.elna'] do |t| exe, source = t.prerequisites cat_arguments = ['cat', source] compiler_arguments = [QEMU, '-L', SYSROOT, exe] last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, compiler_arguments) IO.copy_stream last_stdout, t.name end file 'build/boot/stage2' => 'build/boot/stage2.s' do |t| sh CROSS_GCC, '-nostdlib', '-o', t.name, *t.prerequisites end file 'build/boot/stage2.s' => ['build/valid/stage1', 'boot/stage2.elna'] do |t| exe, source = t.prerequisites cat_arguments = ['cat', source] compiler_arguments = [QEMU, '-L', SYSROOT, exe] last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, compiler_arguments) IO.copy_stream last_stdout, t.name end # # Stage 1. # file 'build/valid/stage1' => ['build/valid', 'build/valid/stage1.s'] do |t| source = t.prerequisites.select { |prerequisite| prerequisite.end_with? '.s' } sh CROSS_GCC, '-nostdlib', '-o', t.name, *source end file 'build/valid/stage1.s' => ['build/boot/stage1', 'boot/stage1.s', 'build/valid'] do |t| source, exe, = t.prerequisites.partition { |prerequisite| prerequisite.end_with? '.s' } cat_arguments = ['cat', *source] compiler_arguments = [QEMU, '-L', SYSROOT, *exe] last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, compiler_arguments) IO.copy_stream last_stdout, t.name end file 'build/boot/stage1' => ['build/boot', 'boot/stage1.s'] do |t| source = t.prerequisites.select { |prerequisite| prerequisite.end_with? '.s' } sh CROSS_GCC, '-nostdlib', '-o', t.name, *source end