Permalink
Browse files

Add MRUBY_CFLAGS / MRUBY_LDFLAGS to mrbgems

  • Loading branch information...
1 parent 4293598 commit dee0b2283c180c4e2584ff27dcb241a80fb7cfc2 @masuidrive masuidrive committed Dec 21, 2012
View
3 .gitignore
@@ -9,6 +9,9 @@
*.sav
*.swp
*.d
+*.tmp
+*.ctmp
+*.rbtmp
*~
.DS_Store
.ccmalloc
View
11 Rakefile
@@ -36,8 +36,13 @@ end
LDFLAGS = [ENV['LDFLAGS']]
LIBS = [ENV['LIBS'] || '-lm']
+if ENABLE_GEMS
+ require './mrbgems/build_tasks'
+ Rake::Task[:load_mrbgems_flags].invoke
+else
+ CFLAGS << "-DDISABLE_GEMS"
+end
-CFLAGS << "-DDISABLE_GEMS" unless ENABLE_GEMS
CFLAGS << "-Wall" << "-Werror-implicit-function-declaration" << "-I#{MRUBY_ROOT}/include"
if ENV['OS'] == 'Windows_NT'
@@ -58,10 +63,6 @@ CAT = ENV['CAT'] ||= 'cat'
##############################
# generic build targets, rules
-if ENABLE_GEMS
- require './mrbgems/build_tasks'
-end
-
task :default => :all
desc "build all targets, install (locally) in-repo"
View
7 doc/mrbgems/c_and_ruby_extension_example/.gitignore
@@ -1,3 +1,8 @@
-gem_mixlib.c
+# mrbgems
+*.tmp
*.ctmp
*.rbtmp
+gem_mixlib.c
+gem_mrblib.c
+gem_srclib.c
+
View
5 doc/mrbgems/c_and_ruby_extension_example/Makefile
@@ -7,6 +7,11 @@ GEM_OBJECTS := $(patsubst %.c, %.o, $(GEM_C_FILES))
GEM_RB_FILES := $(wildcard $(MRB_DIR)/*.rb)
+MRUBY_CFLAGS :=
+MRUBY_LDFLAGS :=
+
gem-all : $(GEM_OBJECTS) gem-c-and-rb-files
gem-clean : gem-clean-c-and-rb-files
+
+gem-test : gem-test-c-and-rb-files
View
7 doc/mrbgems/c_extension_example/.gitignore
@@ -1,3 +1,8 @@
-gem_srclib.c
+# mrbgems
+*.tmp
*.ctmp
*.rbtmp
+gem_mixlib.c
+gem_mrblib.c
+gem_srclib.c
+
View
5 doc/mrbgems/c_extension_example/Makefile
@@ -5,6 +5,11 @@ include $(MAKEFILE_4_GEM)
GEM_C_FILES := $(wildcard $(SRC_DIR)/*.c)
GEM_OBJECTS := $(patsubst %.c, %.o, $(GEM_C_FILES))
+MRUBY_CFLAGS :=
+MRUBY_LDFLAGS :=
+
gem-all : $(GEM_OBJECTS) gem-c-files
gem-clean : gem-clean-c-files
+
+gem-test : gem-test-c-files
View
9 doc/mrbgems/c_extension_example/test/example.c
@@ -0,0 +1,9 @@
+/*
+ if *.c exists in test directory, auto generate this file
+*/
+
+void
+mrb_c_extension_example_gem_test_init(mrb_state *mrb)
+{
+ /* test initializer in C */
+}
View
7 doc/mrbgems/ruby_extension_example/.gitignore
@@ -1,3 +1,8 @@
-gem_mrblib.c
+# mrbgems
+*.tmp
*.ctmp
*.rbtmp
+gem_mixlib.c
+gem_mrblib.c
+gem_srclib.c
+
View
5 doc/mrbgems/ruby_extension_example/Makefile
@@ -4,6 +4,11 @@ include $(MAKEFILE_4_GEM)
GEM_RB_FILES := $(wildcard $(MRB_DIR)/*.rb)
+MRUBY_CFLAGS :=
+MRUBY_LDFLAGS :=
+
gem-all : gem-rb-files
gem-clean : gem-clean-rb-files
+
+gem-test : gem-test-rb-files
View
1 minirake
@@ -389,7 +389,6 @@ class RakeApp
if $show_tasks
display_tasks
else
- p ARGV
tasks = []
ARGV.each do |task_name|
if /^(\w+)=(.*)/.match(task_name)
View
52 mrbgems/Makefile4gem
@@ -12,8 +12,19 @@ CC := gcc
RM := rm -f
AR := ar
+# mruby compiler
+ifeq ($(OS),Windows_NT)
+MRBC = $(MRUBY_ROOT)/bin/mrbc.exe
+else
+MRBC = $(MRUBY_ROOT)/bin/mrbc
+endif
+
SRC_DIR := src
MRB_DIR := mrblib
+TEST_DIR := test
+
+GEM_TEST_RB_FILES := $(wildcard $(TEST_DIR)/*.rb)
+GEM_TEST_C_FILES := $(wildcard $(TEST_DIR)/*.c)
# LIBR can be manipulated with command line arguments
ifeq ($(strip $(LIBR)),)
@@ -29,7 +40,7 @@ ifeq ($(strip $(ACTIVE_GEMS)),)
endif
MAKEFILE_GEM_LIST := $(MRUBY_ROOT)/mrbgems/g/MakefileGemList
-include $(MAKEFILE_GEM_LIST)
+-include $(MAKEFILE_GEM_LIST)
# Default rules which are calling the
# gem specific gem-all and gem-clean
@@ -72,24 +83,55 @@ gem_mrblib_init.ctmp :
ruby $(MRUBY_ROOT)/mrbgems/generate_gem_mrblib.rb '$(GEM)' > $@
gem_mrblib_irep.ctmp : gem_mrblib.rbtmp
- $(MRUBY_ROOT)/bin/mrbc -Bgem_mrblib_irep_$(subst -,_,$(GEM)) -o$@ $<
+ $(MRBC) -Bgem_mrblib_irep_$(subst -,_,$(GEM)) -o$@ $<
gem_mrblib.rbtmp :
- cat $(GEM_RB_FILES) > $@
+ cat $(GEM_RB_FILES) > $@
+
+
+gem_test.ctmp : gem_test.rbtmp $(GEM_TEST_C_FILES)
+ $(MRBC) -Bgem_mrblib_irep_$(subst -,_,$(GEM))_test -o$@ $<
+ifeq ($(GEM_TEST_C_FILES),)
+ echo "void mrb_$(subst -,_,$(GEM))_gem_test_init(mrb_state* mrb) { }" >> $@
+else
+ cat $(GEM_TEST_C_FILES) >> $@
+endif
+
+gem_test.rbtmp : $(GEM_TEST_RB_FILES)
+ifeq ($(GEM_TEST_RB_FILES),)
+ echo > $@
+else
+ cat $(GEM_TEST_RB_FILES) > $@
+endif
+
+gem-test-c-and-rb-files : gem_test.ctmp
+
+gem-test-c-files : gem_test.ctmp
+
+gem-test-rb-files : gem_test.ctmp
+
gem-clean-c-and-rb-files :
-$(RM) $(GEM_PACKAGE) gem_mixlib.o gem_mixlib.c gem_mrblib_header.ctmp gem_mrblib_irep.ctmp gem_mixlib_init.ctmp gem_mrblib.rbtmp
- -$(RM) gem_srclib.c gem_srclib.o $(GEM_OBJECTS)
+ -$(RM) gem_srclib.c gem_srclib.o $(GEM_OBJECTS) gem_test.ctmp gem_test.rbtmp gem-cflags.tmp gem-ldflags.tmp
gem-clean-c-files :
- -$(RM) $(GEM_PACKAGE) gem_srclib.c gem_srclib.o $(GEM_OBJECTS)
+ -$(RM) $(GEM_PACKAGE) gem_srclib.c gem_srclib.o $(GEM_OBJECTS) gem_test.ctmp gem_test.rbtmp gem-cflags.tmp gem-ldflags.tmp
gem-clean-rb-files :
-$(RM) $(GEM_PACKAGE) gem_mrblib.o gem_mrblib.c gem_mrblib_header.ctmp gem_mrblib_init.ctmp gem_mrblib_irep.ctmp gem_mrblib.rbtmp
+ -$(RM) gem_test.ctmp gem_test.rbtmp gem-cflags.tmp gem-ldflags.tmp
+
%.o : %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $(GEM_INCLUDE_LIST) $< -o $@
.PHONY : clean
clean : gem-clean
@echo "Gem '$(GEM)' is clean"
+
+.PHONY : gem-flags
+gem-flags :
+ @echo "$(MRUBY_CFLAGS) -I`pwd`/include" > gem-cflags.tmp
+ @echo "$(MRUBY_LDFLAGS)" > gem-ldflags.tmp
+
View
43 mrbgems/build_tasks.rb
@@ -5,29 +5,56 @@
GEM_MAKEFILE_LIST = "#{MRBGEMS_PATH}/g/MakefileGemList"
MAKEFILE_4_GEM = "#{MRUBY_ROOT}/mrbgems/Makefile4gem"
-if ENV['OS'] == 'Windows_NT'
-GEM_MAKE_FLAGS = "#{MAKE_FLAGS} MAKEFILE_4_GEM=\"#{MAKEFILE_4_GEM}\""
-else
-GEM_MAKE_FLAGS = "#{MAKE_FLAGS} MAKEFILE_4_GEM='#{MAKEFILE_4_GEM}'"
+def gem_make_flags
+ if ENV['OS'] == 'Windows_NT'
+ "#{MAKE_FLAGS rescue ''} MAKEFILE_4_GEM=\"#{MAKEFILE_4_GEM}\""
+ else
+ "#{MAKE_FLAGS rescue ''} MAKEFILE_4_GEM='#{MAKEFILE_4_GEM}'"
+ end
end
task :mrbgems_all => ["#{GEM_INIT}.a", :mrbgems_generate_gem_makefile_list] do
for_each_gem do |path, gemname|
- sh "#{MAKE} -C #{path} #{GEM_MAKE_FLAGS}"
+ sh "#{MAKE} -C #{path} #{gem_make_flags}"
+ end
+end
+
+task :load_mrbgems_flags do
+ for_each_gem do |path, gemname|
+ sh "#{MAKE} gem-flags -C #{path} #{gem_make_flags}"
+ CFLAGS << File.read("#{path}/gem-cflags.tmp").chomp
+ LDFLAGS << File.read("#{path}/gem-ldflags.tmp").chomp
end
end
task :mrbgems_clean do
sh "cd #{MRUBY_ROOT}/mrbgems && #{RM_F} *.c *.d *.a *.o"
sh "cd #{MRUBY_ROOT}/mrbgems/g && #{RM_F} *.c *.d *.rbtmp *.ctmp *.o mrbtest"
for_each_gem do |path, gemname|
- sh "#{MAKE} clean -C #{path} #{GEM_MAKE_FLAGS}"
+ sh "#{MAKE} gem-clean -C #{path} #{gem_make_flags}"
end
end
task :mrbgems_prepare_test do
- sh "#{CAT} #{for_each_gem{|path, gemname| "#{path}/test/*.rb "}} > #{MRUBY_ROOT}/mrbgems/g/mrbgemtest.rbtmp"
- sh "#{MRUBY_ROOT}/bin/mrbc -Bmrbgemtest_irep -o#{MRUBY_ROOT}/mrbgems/g/mrbgemtest.ctmp #{MRUBY_ROOT}/mrbgems/g/mrbgemtest.rbtmp"
+ for_each_gem do |path, gemname, escaped_gemname|
+ sh "#{MAKE} gem-test -C #{path} #{gem_make_flags}"
+ end
+ open("#{MRUBY_ROOT}/mrbgems/g/mrbgemtest.ctmp", 'w') do |f|
+ for_each_gem do |path, gemname, escaped_gemname|
+ f.puts "void mrb_#{escaped_gemname}_gem_test_init(mrb_state *mrb);"
+ f.puts "extern const char gem_mrblib_irep_#{escaped_gemname}_test[];"
+ end
+ f.puts "void mrbgemtest_init(mrb_state* mrb) {"
+ for_each_gem do |path, gemname, escaped_gemname|
+ f.puts "mrb_#{escaped_gemname}_gem_test_init(mrb);"
+ end
+ for_each_gem do |path, gemname, escaped_gemname|
+ f.puts "mrb_load_irep(mrb, gem_mrblib_irep_#{escaped_gemname}_test);"
+ end
+ f.puts "}"
+
+ end
+ sh "#{CAT} #{for_each_gem{|path, gemname| "#{path}/gem_test.ctmp "}} >> #{MRUBY_ROOT}/mrbgems/g/mrbgemtest.ctmp"
end
file "#{GEM_INIT}.a" => ["#{GEM_INIT}.c", "#{GEM_INIT}.o"] do |t|
View
22 test/Makefile
@@ -52,14 +52,6 @@ ifeq ($(strip $(COMPILE_MODE)),)
COMPILE_MODE = debug
endif
-ifeq ($(COMPILE_MODE),debug)
- CFLAGS = -g -O3
-else ifeq ($(COMPILE_MODE),release)
- CFLAGS = -O3
-else ifeq ($(COMPILE_MODE),small)
- CFLAGS = -Os
-endif
-
ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS)
ifeq ($(OS),Windows_NT)
MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS='$(ALL_CFLAGS)' LDFLAGS='$(LDFLAGS)'
@@ -85,7 +77,7 @@ RAKE = ../minirake
# generic build targets, rules
.PHONY : test
-all : $(EXE) $(MRUBY) $(TESTRB) $(TESTMRB)
+all : $(EXE) $(MRUBY) $(TESTRB) $(TESTMRB)
@echo "# exec mrbtest"
./$(EXE)
@echo
@@ -97,20 +89,22 @@ all : $(EXE) $(MRUBY) $(TESTRB) $(TESTMRB)
@echo
# executable constructed using linker from object files
-$(EXE) : $(OBJS) $(LIBR) $(GEM_ARCHIVE_FILES)
+$(EXE) : generate-mrbgems-test $(OBJS) $(LIBR) $(GEM_ARCHIVE_FILES)
$(LL) -o $@ $(LDFLAGS) $(OBJS) $(LIBR) $(GEM_ARCHIVE_FILES) $(LIBS)
+generate-mrbgems-test :
+ifeq ($(ENABLE_GEMS),true)
+ @$(RAKE) mrbgems_prepare_test
+endif
+
-include $(OBJS:.o=.d)
# objects compiled from source
$(OBJS) : %.o : %.c
$(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $< -o $@
# Compile C source from merged mruby source
-$(CLIB) : $(DLIB) $(INIT)
-ifeq ($(ENABLE_GEMS),true)
- @$(RAKE) mrbgems_prepare_test
-endif
+$(CLIB) : $(DLIB) $(INIT) $(GEMDLIB)
$(CAT) $(INIT) $(DLIB) $(GEMDLIB) > $@
$(DLIB) : $(RLIB) $(MRBC)
View
5 test/init_mrbtest.c
@@ -5,14 +5,15 @@
#include "mruby/proc.h"
extern const char mrbtest_irep[];
-extern const char mrbgemtest_irep[];
+
+void mrbgemtest_init(mrb_state* mrb);
void
mrb_init_mrbtest(mrb_state *mrb)
{
mrb_load_irep(mrb, mrbtest_irep);
#ifndef DISABLE_GEMS
- mrb_load_irep(mrb, mrbgemtest_irep);
+ mrbgemtest_init(mrb);
#endif
if (mrb->exc) {
mrb_p(mrb, mrb_obj_value(mrb->exc));

0 comments on commit dee0b22

Please sign in to comment.