2024-12-11 21:44:32 +01:00
|
|
|
# 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/. -}
|
|
|
|
|
2024-09-06 13:07:18 +02:00
|
|
|
require 'open3'
|
|
|
|
require 'rake/clean'
|
|
|
|
require_relative 'shared'
|
|
|
|
|
|
|
|
CLEAN.include(TMP + 'riscv')
|
|
|
|
|
|
|
|
LINKER = 'build/rootfs/riscv32-unknown-linux-gnu/bin/ld'
|
2024-09-15 23:03:25 +02:00
|
|
|
AS = 'build/rootfs/riscv32-unknown-linux-gnu/bin/as'
|
2024-09-06 13:07:18 +02:00
|
|
|
|
|
|
|
namespace :test do
|
2024-09-15 23:03:25 +02:00
|
|
|
test_sources = FileList['tests/vm/*.elna', 'tests/vm/*.s']
|
2024-09-06 13:07:18 +02:00
|
|
|
compiler = `cabal list-bin elna`.strip
|
|
|
|
object_directory = TMP + 'riscv/tests'
|
|
|
|
root_directory = TMP + 'riscv/root'
|
|
|
|
executable_directory = root_directory + 'tests'
|
|
|
|
expectation_directory = root_directory + 'expectations'
|
|
|
|
init = TMP + 'riscv/root/init'
|
2024-09-15 23:03:25 +02:00
|
|
|
builtin = TMP + 'riscv/builtin.o'
|
2024-09-06 13:07:18 +02:00
|
|
|
|
|
|
|
directory root_directory
|
|
|
|
directory object_directory
|
|
|
|
directory executable_directory
|
|
|
|
directory expectation_directory
|
|
|
|
|
2024-09-15 23:03:25 +02:00
|
|
|
file builtin => ['tools/builtin.s', object_directory] do |task|
|
|
|
|
sh AS, '-o', task.name, task.prerequisites.first
|
|
|
|
end
|
|
|
|
|
2024-09-06 13:07:18 +02:00
|
|
|
test_files = test_sources.flat_map do |test_source|
|
2024-09-15 23:03:25 +02:00
|
|
|
test_basename = File.basename(test_source, '.*')
|
2024-09-06 13:07:18 +02:00
|
|
|
test_object = object_directory + test_basename.ext('.o')
|
|
|
|
|
2024-09-15 23:03:25 +02:00
|
|
|
file test_object => [test_source, object_directory] do |task|
|
|
|
|
case File.extname(task.prerequisites.first)
|
|
|
|
when '.s'
|
|
|
|
sh AS, '-mno-relax', '-o', task.name, task.prerequisites.first
|
|
|
|
when '.elna'
|
|
|
|
sh compiler, '--output', task.name, task.prerequisites.first
|
|
|
|
else
|
|
|
|
raise "Unknown source file extension #{task.prerequisites.first}"
|
|
|
|
end
|
2024-09-06 13:07:18 +02:00
|
|
|
end
|
|
|
|
test_executable = executable_directory + test_basename
|
|
|
|
|
2024-09-15 23:03:25 +02:00
|
|
|
file test_executable => [test_object, executable_directory, builtin] do |task|
|
|
|
|
objects = task.prerequisites.filter { |prerequisite| File.file? prerequisite }
|
|
|
|
|
|
|
|
sh LINKER, '-o', test_executable.to_path, *objects
|
2024-09-06 13:07:18 +02:00
|
|
|
end
|
|
|
|
expectation_name = test_basename.ext '.txt'
|
|
|
|
source_expectation = "tests/expectations/#{expectation_name}"
|
|
|
|
target_expectation = expectation_directory + expectation_name
|
|
|
|
|
|
|
|
file target_expectation => [source_expectation, expectation_directory] do
|
|
|
|
cp source_expectation, target_expectation
|
|
|
|
end
|
|
|
|
|
|
|
|
[test_executable, target_expectation]
|
|
|
|
end
|
|
|
|
|
|
|
|
file init => [root_directory] do |task|
|
|
|
|
cp (TMP + 'tools/init'), task.name
|
|
|
|
end
|
2024-09-08 02:08:13 +02:00
|
|
|
# Directories should come first.
|
|
|
|
test_files.unshift executable_directory, expectation_directory, init
|
2024-09-06 13:07:18 +02:00
|
|
|
|
|
|
|
file (TMP + 'riscv/root.cpio') => test_files do |task|
|
|
|
|
root_files = task.prerequisites
|
|
|
|
.map { |prerequisite| Pathname.new(prerequisite).relative_path_from(root_directory).to_path }
|
|
|
|
|
|
|
|
File.open task.name, 'wb' do |cpio_file|
|
|
|
|
cpio_options = {
|
|
|
|
chdir: root_directory.to_path
|
|
|
|
}
|
|
|
|
cpio_stream = Open3.popen2 'cpio', '-o', '--format=newc', cpio_options do |stdin, stdout, wait_thread|
|
|
|
|
stdin.write root_files.join("\n")
|
|
|
|
stdin.close
|
|
|
|
stdout.each { |chunk| cpio_file.write chunk }
|
|
|
|
wait_thread.value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
task :vm => (TMP + 'riscv/root.cpio') do |task|
|
|
|
|
kernels = FileList.glob(TMP + 'tools/linux-*/arch/riscv/boot/Image')
|
|
|
|
|
|
|
|
sh 'qemu-system-riscv32',
|
|
|
|
'-nographic',
|
|
|
|
'-M', 'virt',
|
|
|
|
'-bios', 'default',
|
|
|
|
'-kernel', kernels.first,
|
|
|
|
'-append', 'quiet panic=1',
|
|
|
|
'-initrd', task.prerequisites.first,
|
|
|
|
'-no-reboot'
|
|
|
|
end
|
|
|
|
end
|