From 51eff62931b07b22eca642a870bf343d4c58ac5b Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 7 Feb 2019 11:07:57 +0200 Subject: [PATCH] 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 --- bin/rubyxc | 14 ++++++++++++++ lib/rubyx/rubyxc.rb | 33 +++++++++++++++++++++++++++++++++ test/rubyx/test_rubyxc.rb | 21 +++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100755 bin/rubyxc create mode 100644 lib/rubyx/rubyxc.rb create mode 100644 test/rubyx/test_rubyxc.rb diff --git a/bin/rubyxc b/bin/rubyxc new file mode 100755 index 00000000..6a99acf2 --- /dev/null +++ b/bin/rubyxc @@ -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) diff --git a/lib/rubyx/rubyxc.rb b/lib/rubyx/rubyxc.rb new file mode 100644 index 00000000..f0cecd08 --- /dev/null +++ b/lib/rubyx/rubyxc.rb @@ -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 diff --git a/test/rubyx/test_rubyxc.rb b/test/rubyx/test_rubyxc.rb new file mode 100644 index 00000000..414a903d --- /dev/null +++ b/test/rubyx/test_rubyxc.rb @@ -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