Skip to content

Instantly share code, notes, and snippets.

@luislavena
Created May 22, 2012 21:46
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 luislavena/2771839 to your computer and use it in GitHub Desktop.
Save luislavena/2771839 to your computer and use it in GitHub Desktop.
Mimic 'ruby' executable
require "rake/clean"
require "rbconfig"
def compile_c(source, target)
cflags = RbConfig::CONFIG["CFLAGS"]
cppflags = RbConfig::CONFIG["CPPFLAGS"]
hdr_dir = RbConfig::CONFIG["rubyhdrdir"]
arch_dir = RbConfig::CONFIG["arch"]
include_dirs = "-I#{hdr_dir}/#{arch_dir} -I#{hdr_dir}"
cc = ENV.fetch("CC", RbConfig::CONFIG["CC"])
sh "#{cc} -c #{source} -o #{target} #{cflags} #{cppflags} #{include_dirs}"
end
def link_ruby(objects, target)
libruby_dir = RbConfig::CONFIG["libdir"]
libruby = RbConfig::CONFIG["LIBRUBYARG"]
libs = RbConfig::CONFIG["LIBS"]
libs_dir = "-L#{libruby_dir} #{libruby} #{libs}"
cc = ENV.fetch("CC", RbConfig::CONFIG["CC"])
sh "#{cc} #{objects.join(' ')} -o #{target} #{libs_dir}"
end
rule ".o" => ".c" do |t|
compile_c t.source, t.name
end
SRCS = FileList["*.c"]
OBJS = SRCS.ext(".o")
file "simple-ruby.exe" => OBJS do |t|
link_ruby t.prerequisites, t.name
end
CLEAN.include OBJS
CLEAN.include "simple-ruby.exe"
desc "Compile executable"
task :compile => ["simple-ruby.exe"]
task :default => [:compile]
#include "ruby.h"
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
int
main(int argc, char **argv)
{
#ifdef HAVE_LOCALE_H
setlocale(LC_CTYPE, "");
#endif
ruby_sysinit(&argc, &argv);
{
RUBY_INIT_STACK;
ruby_init();
return ruby_run_node(ruby_options(argc, argv));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment