Skip to content

Instantly share code, notes, and snippets.

@undecided
Created March 6, 2013 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save undecided/5098096 to your computer and use it in GitHub Desktop.
Save undecided/5098096 to your computer and use it in GitHub Desktop.
Quick and dirty script for compiling a ruby file with mruby. Run it with ruby ./compile_mruby.rb my_mruby_file.rb
#!/usr/bin/env ruby
require 'fileutils'
def shell(cmd)
puts "Running #{cmd}"
`#{cmd}`
end
infile = ARGV[0]
name = ARGV[0][/^(.+)\.rb/, 1]
cdatafile = name + '.c'
cfile = name + '_launcher.c'
ofile = name + '_launcher.o'
execfile = name
shell %Q[../bin/mrbc -B#{name} #{infile}] # Produces cdatafile
File.open(cfile, 'w') do |f|
f.puts <<-END
#include "mruby.h"
#include "mruby/irep.h"
#include "mruby/proc.h"
int main(void)
{
#{File.read cdatafile}
/* new interpreter instance */
mrb_state *mrb;
mrb = mrb_open();
/* read and execute compiled symbols */
int n = mrb_read_irep(mrb, #{name});
mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
mrb_close(mrb);
return 0;
}
END
end
shell %Q{ gcc -I../src -I../include -c #{cfile} }
shell %Q{ gcc -o #{execfile} #{ofile} ../lib/libmruby.a }
FileUtils.rm ofile
FileUtils.rm cfile
FileUtils.rm cdatafile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment