Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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