summaryrefslogtreecommitdiff
path: root/Rakefile
blob: 86ca2e111b1f80154fa8e8ff0741b7075f6faddd (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
42
43
44
45
46
47
48
49
50
51
52
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'

def assemble_stage(output, compiler, source)
  arguments = [QEMU, '-L', SYSROOT, *compiler]

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

    IO.copy_stream qemu_out, output
    qemu_out.close
  end
end

desc 'Final stage'
task default: ['build/stage2', 'boot/stage2.elna'] do |t|
  assembler, exe = t.prerequisites.partition { |prerequisite| prerequisite.end_with? '.elna' }

  File.open File::NULL, 'w' do |output|
    assemble_stage output, exe, assembler
  end
end

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' }

  File.open t.name, 'w' do |output|
    assemble_stage output, exe, assembler
  end
end

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