diff --git a/lib/vaporware/compiler.rb b/lib/vaporware/compiler.rb index a3b1f7f..5ee940a 100644 --- a/lib/vaporware/compiler.rb +++ b/lib/vaporware/compiler.rb @@ -5,17 +5,18 @@ require_relative "compiler/linker" class Vaporware::Compiler - def self.compile(source, compiler: "gcc", dest: "tmp", debug: false, compiler_options: ["-O0"], shared: false) - s = new(input: source, output: dest, debug:, shared:) - s.compile(compiler_options:) - s.assemble(input: dest.to_s + ".s", assembler: "as", debug:) - s.link + attr_reader *%i(generator assembler linker) + + def self.compile(source, assembler: "as", linker: "ld", dest: "tmp", debug: false, compiler_options: ["-O0"], shared: false) + compiler = new(input: source, output: dest, debug:, shared:, linker:, assembler:) + compiler.compile(compiler_options:) + compiler.assemble(input: dest.to_s + ".s", assembler:, debug:) + compiler.link end def initialize(input:, output: File.basename(input, ".*"), linker: "ld", assembler: "as", debug: false, shared: false) @generator = Vaporware::Compiler::Generator.new(input:, output: output + ".s", debug:, shared:) @assembler = Vaporware::Compiler::Assembler.new(input: @generator.precompile, output: output + ".o", assembler:, debug:) - output = "lib#{output}.so" if shared && output !~ /^lib.+\.so$/ @linker = Vaporware::Compiler::Linker.new(input: @assembler.obj_file, output:, linker:, debug:, shared:) end diff --git a/lib/vaporware/compiler/linker.rb b/lib/vaporware/compiler/linker.rb index 2614300..2c436e5 100644 --- a/lib/vaporware/compiler/linker.rb +++ b/lib/vaporware/compiler/linker.rb @@ -10,7 +10,7 @@ def initialize(input:, output: "a.out", linker: "mold", linker_options: [], shar def link(input: @input, output: @output, debug: @debug, shared: @shared) = IO.popen(link_command).close - def link_command + def link_command(input: @input, output: @output, debug: @debug, shared: @shared) ld_path = [] if @shared ld_path << "--shared" @@ -26,6 +26,7 @@ def link_command # for not static compile ld_path << "#{gcc_libpath}/crtend.o" end + ld_path << "#{libpath}/libc.so" ld_path << "#{libpath}/crtn.o" cmd = [@linker, "-o", @output, "-m", "elf_x86_64", *@options, *ld_path, @input].join(' ')