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/11.1.0', '-L/opt/riscv/riscv32-unknown-elf/lib', '/opt/riscv/riscv32-unknown-elf/lib/crt0.o', '/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/crtbegin.o', t.source, '--start-group', '-lc', '-lgloss', '--end-group', '/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.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-12') 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/') .read .to_i puts "Running #{test}" if test.include? '/riscv/' system('/opt/riscv/bin/spike', '/opt/riscv/riscv32-unknown-elf/bin/pk', test, { out: '/dev/null' }) else raise 'Unsupported test platform' end actual = $?.exitstatus fail "#{test}: Expected #{expected}, got #{actual}" unless expected == actual end end desc 'Run unittest blocks' task unittest: SOURCES do |t| sh('dub', 'test', '--compiler=gdc-12') 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