Use ninja for build

This commit is contained in:
2026-01-04 17:14:00 +01:00
parent f86d06236c
commit 955161b36e

113
Rakefile
View File

@@ -17,10 +17,6 @@ CLEAN.include 'build/boot', 'build/valid'
CLEAN.include 'doc/*.pdf' CLEAN.include 'doc/*.pdf'
CLOBBER.include 'build' CLOBBER.include 'build'
def compile(*arguments)
sh(ENV.fetch('CC', 'gcc'), '-fpie', '-g', *arguments)
end
def run(exe) def run(exe)
ENV.fetch('QEMU', '').split << exe ENV.fetch('QEMU', '').split << exe
end end
@@ -50,75 +46,74 @@ task :convert do
end end
end end
rule /^build\/[[:alpha:]]+\/stage[[:digit:]]+\/cl$/ => ->(match) { file 'build/build.ninja' => ['build'] do |t|
"#{match}.o" File.open t.name, 'w' do |f|
} do |t| f << <<~NINJA
arguments_path = Pathname.new('boot') + Pathname.new(t.name).dirname.basename + 'linker.arg' builddir = build
if arguments_path.exist? cflags = -fpie -g
arguments1 = ['--dynamic-linker', '/lib32/ld-linux-riscv32-ilp32d.so.1', '/usr/lib/crt1.o', '/usr/lib/crti.o', '-lc']
arguments2 = ['/usr/lib/crtn.o']
else
arguments1 = arguments2 = []
end
sh(ENV.fetch('LD', 'ld'), '-o', t.name, *arguments1, *t.prerequisites, *arguments2) rule cc
end command = gcc $cflags -nostdlib -o $out $in
rule /^build\/[[:alpha:]]+\/stage[[:digit:]]+\/cl.o$/ => ->(match) { rule as
match.ext('.s') command = gcc $cflags -c -o $out $in
} do |t|
compile('-c', '-o', t.name, *t.prerequisites)
end
rule link1
command = ld -o $out $in
rule link2
command = ld -o $out --dynamic-linker /lib32/ld-linux-riscv32-ilp32d.so.1 /usr/lib/crt1.o /usr/lib/crti.o -lc $in /usr/lib/crtn.o
rule boot1
command = build/boot/stage1/cl < \$in > \$out
rule valid1
command = build/valid/stage1/cl < \$in > \$out
NINJA
STAGES.each do |stage| STAGES.each do |stage|
previous = stage.delete_prefix('stage').to_i.pred stage_number = stage.delete_prefix('stage').to_i
directory "build/valid/#{stage}" f << <<~NINJA
directory "build/boot/#{stage}"
file "build/valid/#{stage}/cl.s" => ["build/boot/#{stage}/cl", "boot/#{stage}/cl.elna", "build/valid/#{stage}"] do |t| rule valid#{stage_number}
exe, source = t.prerequisites command = build/valid/stage#{stage_number}/cl < \$in > \$out
cat_arguments = ['cat', source] rule boot#{stage_number}
last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, run(exe)) command = build/boot/stage#{stage_number}/cl < \$in > \$out
NINJA
IO.copy_stream last_stdout, t.name
end end
f << <<~NINJA
file "build/boot/#{stage}/cl.s" => ["build/valid/stage#{previous}/cl", "boot/#{stage}/cl.elna", "build/boot/#{stage}"] do |t| build build/boot/stage1/cl: cc boot/stage1.s
exe, source = t.prerequisites
cat_arguments = ['cat', source] build build/valid/stage1/cl.s: boot1 boot/stage1.s | build/boot/stage1/cl
last_stdout, wait_threads = Open3.pipeline_r(cat_arguments, run(exe)) build build/valid/stage1/cl.o: as build/valid/stage1/cl.s
build build/valid/stage1/cl: link1 build/valid/stage1/cl.o
NINJA
STAGES.each do |stage|
stage_number = stage.delete_prefix('stage').to_i
IO.copy_stream last_stdout, t.name arguments_path = Pathname.new('boot') + stage + 'linker.arg'
if arguments_path.exist?
link = 'link2'
else
link = 'link1'
end end
boot_stage = "build/boot/stage#{stage_number}"
valid_stage = "build/valid/stage#{stage_number}"
f << <<~NINJA
build #{boot_stage}/cl.s: valid#{stage_number.pred} boot/stage#{stage_number}/cl.elna | build/valid/stage#{stage_number.pred}/cl
build #{boot_stage}/cl.o: as #{boot_stage}/cl.s
build #{boot_stage}/cl: #{link} #{boot_stage}/cl.o
build #{valid_stage}/cl.s: boot#{stage_number} boot/stage#{stage_number}/cl.elna | #{boot_stage}/cl
build #{valid_stage}/cl.o: as #{valid_stage}/cl.s
build #{valid_stage}/cl: #{link} #{valid_stage}/cl.o
NINJA
end end
f << "\ndefault build/valid/stage18/cl\n"
#
# Stage 1.
#
directory 'build/valid/stage1'
directory 'build/boot/stage1'
file 'build/valid/stage1/cl' => ['build/valid/stage1.s'] do |t|
compile('-nostdlib', '-o', t.name, *t.prerequisites)
end end
file 'build/valid/stage1.s' => ['build/boot/stage1/cl', 'boot/stage1.s', 'build/valid/stage1'] 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/cl' => ['build/boot/stage1', 'boot/stage1.s'] do |t|
source = t.prerequisites.select { |prerequisite| prerequisite.end_with? '.s' }
compile('-nostdlib', '-o', t.name, *source)
end end
rule '.pdf' => '.adoc' do |t| rule '.pdf' => '.adoc' do |t|