Files
elna/Rakefile

104 lines
2.6 KiB
Ruby

require 'pathname'
require 'rake/clean'
require 'open3'
M2C = 'gm2' # Modula-2 compiler.
BOOT_OBJECTS = FileList['boot/*.mod']
.map do |file|
File.join('build', Pathname.new(file).sub_ext('.o'))
end
directory 'build/boot'
CLEAN.include 'build'
rule(/build\/boot\/.+\.o$/ => ->(file) { test_for_out(file) }) do |t|
sources = t.prerequisites.filter { |f| f.end_with? '.mod' }
sh M2C, '-c', '-I', 'boot', '-o', t.name, *sources
end
file 'build/boot/Compiler.o' => ['build/boot', 'boot/Compiler.mod'] do |t|
sources = t.prerequisites.filter { |f| f.end_with? '.mod' }
sh M2C, '-fscaffold-main', '-c', '-I', 'boot', '-o', t.name, *sources
end
file 'build/boot/Compiler' => BOOT_OBJECTS do |t|
sh M2C, '-o', t.name, *t.prerequisites
end
task default: 'build/boot/Compiler'
task :default do |t|
sh t.prerequisites.first
end
def test_for_out(out_file)
path = Pathname.new(out_file).relative_path_from('build')
implementation = path.sub_ext('.mod').to_path
definition = path.sub_ext('.def').to_path
['build/boot', implementation, definition]
end
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']
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
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