Very basic cli to compile ruby files
Off course this is basically a cross compiler and the files have to be transferred to an arm machine (and fixed as per note) close #22
This commit is contained in:
parent
e634781d6c
commit
51eff62931
14
bin/rubyxc
Executable file
14
bin/rubyxc
Executable file
@ -0,0 +1,14 @@
|
|||||||
|
#! /usr/bin/env ruby -I lib
|
||||||
|
require 'rubygems'
|
||||||
|
require 'bundler'
|
||||||
|
begin
|
||||||
|
Bundler.setup(:default)
|
||||||
|
rescue Bundler::BundlerError => e
|
||||||
|
$stderr.puts e.message
|
||||||
|
$stderr.puts "Run `bundle install` to install missing gems"
|
||||||
|
exit e.status_code
|
||||||
|
end
|
||||||
|
|
||||||
|
require "rubyx/rubyxc"
|
||||||
|
|
||||||
|
RubyXC.start(ARGV)
|
33
lib/rubyx/rubyxc.rb
Normal file
33
lib/rubyx/rubyxc.rb
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
require "thor"
|
||||||
|
require "rubyx"
|
||||||
|
|
||||||
|
class RubyXC < Thor
|
||||||
|
desc "compile FILE" , "Compile given FILE to binary"
|
||||||
|
long_desc <<-LONGDESC
|
||||||
|
Very basic cli to compile ruby programs.
|
||||||
|
Currently only compile command supported without option.
|
||||||
|
|
||||||
|
Output will be elf object file of the same name, with .o, in root directory.
|
||||||
|
|
||||||
|
Note: Because of Bug #13, you need to run "ld -N file.o" on the file, before
|
||||||
|
executing it. This can be done on a mac by installing a cross linker
|
||||||
|
(brew install arm-linux-gnueabihf-binutils), or on the target arm machine.
|
||||||
|
LONGDESC
|
||||||
|
|
||||||
|
def compile(file)
|
||||||
|
begin
|
||||||
|
ruby = File.read(file)
|
||||||
|
rescue
|
||||||
|
fail MalformattedArgumentError , "No such file #{file}"
|
||||||
|
end
|
||||||
|
puts "compiling #{file}"
|
||||||
|
|
||||||
|
linker = ::RubyX::RubyXCompiler.new.ruby_to_binary( ruby , :arm )
|
||||||
|
writer = Elf::ObjectWriter.new(linker)
|
||||||
|
|
||||||
|
outfile = file.split("/").last.gsub(".rb" , ".o")
|
||||||
|
writer.save outfile
|
||||||
|
|
||||||
|
return outfile
|
||||||
|
end
|
||||||
|
end
|
21
test/rubyx/test_rubyxc.rb
Normal file
21
test/rubyx/test_rubyxc.rb
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
require_relative "helper"
|
||||||
|
require "rubyx/rubyxc"
|
||||||
|
|
||||||
|
module RubyX
|
||||||
|
class TestRubyXCli < MiniTest::Test
|
||||||
|
|
||||||
|
def test_invoke_without_not_work
|
||||||
|
assert_output(nil , /not find/) {RubyXC.start(["list"])}
|
||||||
|
end
|
||||||
|
def test_invoke_compile_works
|
||||||
|
assert_output( "") {RubyXC.start(["compile" ])}
|
||||||
|
end
|
||||||
|
def test_file_fail
|
||||||
|
assert_output(nil , /No such file/) {RubyXC.start(["compile" , "hi"])}
|
||||||
|
end
|
||||||
|
def test_file_open
|
||||||
|
assert_output(/compiling/) {RubyXC.start(["compile" , "test/mains/source/add__4.rb"])}
|
||||||
|
File.delete "add__4.o"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user