Browse files

Use Rake instead of Make

  • Loading branch information...
1 parent 8d34941 commit d58989f784fb93936bffa1429b3ac3b45e7266db @RyanScottLewis committed Dec 19, 2012
Showing with 74 additions and 27 deletions.
  1. +0 −15 Makefile
  2. +2 −0 Rakefile
  3. +11 −12 lib/lua-mruby.c
  4. +61 −0 lib/rake/lua_mruby_tasks.rb
View
15 Makefile
@@ -1,15 +0,0 @@
-MRUBY_ROOT = /Users/ryguy/Code/C/Source/mruby
-LUA_ROOT = /usr/local/Cellar/lua/5.1.4
-
-INCLUDES = -I$(MRUBY_ROOT)/include -I$(MRUBY_ROOT)/src -I$(LUA_ROOT)/include
-LIB = -lm $(MRUBY_ROOT)/mrblib/mrblib.o -lm $(MRUBY_ROOT)/lib/libmruby.a
-CFLAGS = $(INCLUDES) -llua -O3 -g -Wall -Werror-implicit-function-declaration -shared -O
-
-CC = gcc
-
-all :
- mkdir pkg
- $(CC) $(CFLAGS) -o pkg/mruby.so lib/lua-mruby.c $(LIB)
-
-clean :
- rm -rf pkg
View
2 Rakefile
@@ -0,0 +1,2 @@
+require_relative 'lib/rake/lua_mruby_tasks'
+Rake::LuaMRubyTasks.new
View
23 lib/lua-mruby.c
@@ -7,17 +7,17 @@
#include "mruby/compile.h"
static int lua_mruby_run(lua_State *L) {
- // mrb_state *mrb = mrb_open();
- // const char *mrb_code = lua_tostring(L, 1);
- // struct mrb_parser_state* parser = mrb_parse_string(mrb, mrb_code, NULL);
- // int mrb_generated_code = mrb_generate_code(mrb, parser->tree);
- //
- // mrb_run( mrb, mrb_proc_new(mrb, mrb->irep[mrb_generated_code]), mrb_nil_value() );
- //
- // int lua_return = mrb_generated_code > 0 ? 0 : 1;
- // lua_pushnumber(L, lua_return);
- //
- // mrb_close(mrb);
+ mrb_state *mrb = mrb_open();
+ const char *mrb_code = lua_tostring(L, 1);
+ struct mrb_parser_state *p = mrb_parse_string(mrb, mrb_code, NULL);
+ int mrb_generated_code = mrb_generate_code(mrb, p-> tree);
+
+ mrb_run( mrb, mrb_proc_new(mrb, mrb->irep[mrb_generated_code]), mrb_nil_value() );
+
+ int lua_return = mrb_generated_code > 0 ? 0 : 1;
+ lua_pushnumber(L, lua_return);
+
+ mrb_close(mrb);
return 1;
}
@@ -28,7 +28,6 @@ static const struct luaL_reg mrubylib[] = {
};
int luaopen_mruby(lua_State *L) {
- // luaL_openlib(L, "mruby", mrubylib, 0);
luaL_register(L, "mruby", mrubylib);
return 1;
}
View
61 lib/rake/lua_mruby_tasks.rb
@@ -0,0 +1,61 @@
+require 'rake/tasklib'
+require 'pathname'
+Pathname.send(:alias_method, :/, :join)
+
+class Rake::LuaMRubyTasks < Rake::TaskLib
+
+ def initialize
+
+ desc "Compile lua-mruby"
+ # task(:compile, &:compile)
+ task(:compile) { compile }
+
+ desc "Clean up the current build"
+ # task(:clean, &:clean)
+ task(:clean) { clean }
+
+ task :default => [:clean, :compile]
+
+ end
+
+ define_method(:root) { (Pathname.new(__FILE__) / '..' / '..' / '..').expand_path }
+ define_method(:compiler) { '/usr/bin/gcc' }
+ define_method(:mruby) { Pathname.new('/Users/ryguy/Code/C/Source/mruby') }
+ define_method(:lua) { Pathname.new('/usr/local/Cellar/lua/5.1.4') }
+ define_method(:mruby_include) { mruby / 'include' }
+ define_method(:mruby_src) { mruby / 'src' }
+ define_method(:lua_include) { lua / 'include' }
+ define_method(:package) { root / 'pkg' }
+ define_method(:output) { "-o #{package / 'mruby.so'}" }
+
+ def includes
+ [mruby_include, mruby_src, lua_include].collect { |path| "-I#{path}" }.join(' ')
+ end
+
+ def libraries
+ [
+ root / 'lib' / 'lua-mruby.c',
+ '-lm',
+ mruby / 'mrblib' / 'mrblib.o',
+ mruby / 'lib' / 'libmruby.a'
+ ].join(' ')
+ end
+
+ def flags
+ '-llua -O3 -g -Wall -Werror-implicit-function-declaration -shared -O'
+ end
+
+ def command
+ [compiler, includes, flags, output, libraries].join(' ')
+ end
+
+ def compile
+ package.mkdir
+ system(command)
+ end
+
+ def clean
+ package.rmtree rescue nil
+ end
+
+end

0 comments on commit d58989f

Please sign in to comment.