require 'pathname' require 'rake/clean' require 'open3' DFLAGS = ['--warn-no-deprecated', '-L/usr/lib64/gcc-12'] BINARY = 'build/bin/elna' TESTS = FileList['tests/*.eln'].flat_map do |test| build = Pathname.new 'build' test_basename = Pathname.new(test).basename('') [build + 'riscv' + test_basename].map { |path| path.sub_ext('').to_path } end SOURCES = FileList['source/**/*.d'] directory 'build' CLEAN.include 'build' CLEAN.include '.dub' rule(/build\/riscv\/[^\/\.]+$/ => ->(file) { test_for_out(file, '.o') }) do |t| sh '/opt/riscv/bin/riscv32-unknown-elf-ld', '-o', t.name, '-L/opt/riscv/lib/gcc/riscv32-unknown-elf/13.2.0/', '-L/opt/riscv/riscv32-unknown-elf/lib', '/opt/riscv/riscv32-unknown-elf/lib/crt0.o', '/opt/riscv/lib/gcc/riscv32-unknown-elf/13.2.0/crtbegin.o', t.source, '--start-group', '-lgcc', '-lc', '-lgloss', '--end-group', '/opt/riscv/lib/gcc/riscv32-unknown-elf/13.2.0/crtend.o' end rule(/build\/riscv\/.+\.o$/ => ->(file) { test_for_object(file, '.eln') }) do |t| Pathname.new(t.name).dirname.mkpath sh BINARY, '-o', t.name, t.source end file BINARY => SOURCES do |t| sh({ 'DFLAGS' => (DFLAGS * ' ') }, 'dub', 'build', '--compiler=gdc') end task default: BINARY desc 'Run all tests and check the results' task test: TESTS task test: BINARY do TESTS.each do |test| expected = Pathname .new(test) .sub_ext('.txt') .sub(/^build\/[[:alpha:]]+\//, 'tests/expectations/') .to_path puts "Running #{test}" if test.include? '/riscv/' spike = [ '/opt/riscv/bin/spike', '--isa=RV32IMAC', '/opt/riscv/riscv32-unknown-elf/bin/pk', test ] diff = ['diff', '-Nur', '--color', expected, '-'] tail = ['tail', '-n', '1'] last_stdout, wait_threads = Open3.pipeline_r spike, tail, diff else raise 'Unsupported test platform' end print last_stdout.read last_stdout.close fail unless wait_threads.last.value.exitstatus.zero? end end def test_for_object(out_file, extension) test_source = Pathname .new(out_file) .sub_ext(extension) .sub(/^build\/[[:alpha:]]+\//, 'tests/') .to_path [test_source, BINARY] end def test_for_out(out_file, extension) Pathname .new(out_file) .sub_ext(extension) .to_path end