Created
May 22, 2012 21:46
-
-
Save luislavena/2771839 to your computer and use it in GitHub Desktop.
Mimic 'ruby' executable
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
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] |
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
#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