summaryrefslogtreecommitdiff
path: root/Rakefile
blob: 32157d0bade0c75e2bd0c3799d5977a110c1a509 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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/.
# frozen_string_literal: true

require 'open3'
require 'rake/clean'

STAGES = Dir.glob('boot/stage*.elna').collect { |stage| File.basename stage, '.elna' }.sort

CLEAN.include 'build/boot', 'build/valid'

directory 'build/boot'
directory 'build/valid'

def compile(input, output)
  sh ENV.fetch('CC', 'gcc'), '-nostdlib', '-fpie', '-g', '-o', output, input
end

def run(exe)
  ENV.fetch('QEMU', '').split << exe
end

task default: :boot

desc 'Final stage'
task boot: "build/valid/#{STAGES.last}"
task boot: "build/valid/#{STAGES.last}.s"
task boot: "boot/#{STAGES.last}.elna" do |t|
  groupped = t.prerequisites.group_by { |stage| File.extname stage }.transform_values(&:first)
  exe = groupped['']
  expected = groupped['.s']
  source = groupped['.elna']

  cat_arguments = ['cat', source]
  diff_arguments = ['diff', '-Nur', '--text', expected, '-']
  Open3.pipeline(cat_arguments, run(exe), diff_arguments)
end

desc 'Convert previous stage language into the current stage language'
task :convert do
  File.open('boot/stage9.elna', 'w') do |current_stage|
    File.readlines('boot/stage8.elna').each do |line|
      comment_match = /^(\s*)#(.*)/.match line

      if comment_match.nil?
        current_stage << line
      elsif comment_match[2].empty?
        current_stage << "\n"
      else
        current_stage << "#{comment_match[1]}(* #{comment_match[2].strip} *)\n"
      end
    end
  end
end

rule /^build\/[[:alpha:]]+\/stage[[:digit:]]+$/ => ->(match) {
  "#{match}.s"
} do |t|
  compile(*t.prerequisites, t.name)
end

STAGES.each do |stage|
  previous = stage.delete_prefix('stage').to_i.pred

  file "build/valid/#{stage}.s" => ["build/boot/#{stage}", "boot/#{stage}.elna"] do |t|
    exe, source = t.prerequisites

    cat_arguments = ['cat', source]
    last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, run(exe))

    IO.copy_stream last_stdout, t.name
  end

  file "build/boot/#{stage}.s" => ["build/valid/stage#{previous}", "boot/#{stage}.elna"] do |t|
    exe, source = t.prerequisites

    cat_arguments = ['cat', source]
    last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, run(exe))

    IO.copy_stream last_stdout, t.name
  end
end

#
# Stage 1.
#

file 'build/valid/stage1.s' => ['build/boot/stage1', 'boot/stage1.s', 'build/valid'] do |t|
  source, exe, = t.prerequisites.partition { |prerequisite| prerequisite.end_with? '.s' }

  cat_arguments = ['cat', *source]
  last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, run(exe.first))

  IO.copy_stream last_stdout, t.name
end

file 'build/boot/stage1' => ['build/boot', 'boot/stage1.s'] do |t|
  source = t.prerequisites.select { |prerequisite| prerequisite.end_with? '.s' }

  compile(*source, t.name)
end