Created
March 6, 2013 09:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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