summaryrefslogtreecommitdiff
path: root/Rakefile
blob: 368abfa425bf38c276018559f25ae77aa5b0385e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require 'open3'
require 'rake/clean'

CLOBBER.include 'build'

CROSS_GCC = '../riscv32-ilp32d--glibc/bin/riscv32-linux-gcc'
SYSROOT = '../riscv32-ilp32d--glibc/riscv32-buildroot-linux-gnu/sysroot'
QEMU = 'qemu-riscv32'

desc 'Final stage'
task default: 'build/stage2'

directory 'build'

desc 'Initial stage'
file 'build/stage1' => ['boot/echo-boot.s', 'boot/common-boot.s', 'build'] do |t|
  assembler = t.prerequisites.filter { |prerequisite| prerequisite.end_with? '.s' }

  sh CROSS_GCC, '-nostdlib', '-o', t.name, *assembler
end

file 'build/stage2.s' => ['build/stage1', 'boot/stage2.elna'] do |t|
  assembler, exe = t.prerequisites.partition { |prerequisite| prerequisite.end_with? '.elna' }
  arguments = [QEMU, '-L', SYSROOT, *exe]

  puts(arguments * ' ')
  puts
  Open3.popen2(*arguments) do |qemu_in, qemu_out|
    qemu_in.write File.read(*assembler)
    qemu_in.close

    File.open t.name, 'w' do |output|
      IO.copy_stream qemu_out, output
    end
    qemu_out.close
  end
end

file 'build/stage2' => ['build/stage2.s', 'boot/common-boot.s'] do |t|
  sh CROSS_GCC, '-nostdlib', '-o', t.name, *t.prerequisites
end