Skip to content

Commit

Permalink
change default assembler gcc to (g)as
Browse files Browse the repository at this point in the history
  • Loading branch information
katsyoshi committed Jan 8, 2024
1 parent 603d848 commit 95c3ee7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
13 changes: 7 additions & 6 deletions lib/vaporware/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/vaporware/compiler/linker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(' ')
Expand Down
2 changes: 1 addition & 1 deletion test/vaporware/test_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_sample_while
def test_sample_call_method
@generated = "libtmp.so"
@file = "sample/method.rb"
@vaporware = Vaporware::Compiler.compile(@file, shared: true)
@vaporware = Vaporware::Compiler.compile(@file, dest: "./libtmp.so", shared: true)
require './sample/fiddle.rb'
assert_equal(10, X.aibo)
end
Expand Down

0 comments on commit 95c3ee7

Please sign in to comment.