3 mrbgems is a library manager to integrate C and Ruby extension in an easy and
4 standardised way into mruby.
8 By
default mrbgems is currently deactivated. As soon as you add a GEM to your
9 build configuration (
i.e. *build_config.rb*), mrbgems will be activated and the
12 To add a GEM into the *build_config.rb* add the following line
for example:
14 conf.gem
'/path/to/your/gem/dir'
16 You can also use a relative path which would be relative from the mruby root:
18 conf.gem
'examples/mrbgems/ruby_extension_example'
20 A remote GIT repository location
for a GEM is also supported:
22 conf.gem :git =>
'https://github.com/masuidrive/mrbgems-example.git', :branch =>
'master'
24 conf.gem :github =>
'masuidrive/mrbgems-example', :branch =>
'master'
26 conf.gem :bitbucket =>
'mruby/mrbgems-example', :branch =>
'master'
28 To pull all gems from remote GIT repository on build, call ```./minirake -p```,
29 or ```./minirake --pull-gems```.
31 NOTE: `:bitbucket` option supports only git. Hg is unsupported in
this version.
35 There are instances when you wish to add a collection of mrbgems into mruby at
36 once, or be able to substitute mrbgems based on configuration, without having to
37 add each gem to the *build_config.rb* file. A packaged collection of mrbgems
38 is called a GemBox. A GemBox is a file that contains a list of mrbgems to load
39 into mruby, in the same format as
if you were adding them to *build_config.rb*
40 via `config.gem`, but wrapped in an `MRuby::GemBox`
object. GemBoxes are
41 loaded into mruby via `config.gembox
'boxname'`.
43 Below we have created a GemBox containing *mruby-time* and *mrbgems-example*:
45 MRuby::GemBox.new
do |conf|
46 conf.gem
"#{root}/mrbgems/mruby-time"
47 conf.gem :github =>
'masuidrive/mrbgems-example'
50 As mentioned, the GemBox uses the same conventions as `MRuby::Build`. The GemBox
51 must be saved with a *.gembox* extension inside the *mrbgems* directory to to be
54 To use
this example GemBox, we save it as `custom.gembox` inside the *mrbgems*
55 directory in mruby, and add the following to our *build_config.rb* file inside
60 This will cause the *custom* GemBox to be read in during the build process,
61 adding *mruby-time* and *mrbgems-example* to the build.
63 If you want, you can put GemBox outside of mruby directory. In that
case you must
64 specify absolute path like below.
66 conf.gembox
"#{ENV["HOME
"]}/mygemboxes/custom"
68 There are two GemBoxes that ship with mruby: [
default](../../mrbgems/
default.gembox)
69 and [full-core](../../mrbgems/full-core.gembox). The [
default](../../mrbgems/
default.gembox) GemBox
70 contains several core components of mruby, and [full-core](../../mrbgems/full-core.gembox)
71 contains every gem found in the *mrbgems* directory.
75 The maximal GEM structure looks like
this:
77 +- GEM_NAME <- Name of GEM
79 +- mrblib/ <- Source
for Ruby extension
81 +- src/ <- Source
for C extension
83 +- test/ <- Test code (Ruby)
85 +- mrbgem.rake <- GEM Specification
87 +- README.md <- Readme
for GEM
89 The folder *mrblib* contains pure Ruby
files to extend mruby. The folder *src*
90 contains C
files to extend mruby. The folder *test* contains C and pure Ruby
files
91 for testing purposes which will be used by `mrbtest`. *mrbgem.rake* contains
92 the specification to compile C and Ruby
files. *README.md* is a
short description
97 mrbgems expects a specification file called *mrbgem.rake* inside of your
98 GEM directory. A typical GEM specification could look like
this for example:
100 MRuby::Gem::Specification.new(
'c_and_ruby_extension_example')
do |spec|
102 spec.author =
'mruby developers'
105 The mrbgems build process will use
this specification to compile Object and Ruby
106 files. The compilation results will be added to *lib/libmruby.a*. This file exposes
107 the GEM functionality to tools like `mruby` and `mirb`.
109 The following properties can be
set inside of your `MRuby::Gem::Specification`
for
112 * `spec.license` or `spec.licenses` (A single
license or a list of them under which
this GEM is licensed)
113 * `spec.author` or `spec.authors` (Developer name or a list of them)
114 * `spec.version` (Current
version)
116 * `spec.summary` (Short summary)
117 * `spec.homepage` (Homepage)
118 * `spec.requirements` (External requirements as information
for user)
122 In
case your GEM is depending on other GEMs please use
123 `spec.add_dependency(gem, *requirements)` like:
125 MRuby::Gem::Specification.new(
'c_and_ruby_extension_example')
do |spec|
127 spec.author =
'mruby developers'
129 # add GEM dependency mruby-parser.
130 # Version has to be between 1.0.0 and 1.5.2
131 spec.add_dependency(
'mruby-parser',
'> 1.0.0',
'< 1.5.2')
134 The usage of versions is optional.
137 The dependency system is currently (May 2013) under development and doesn't check
138 or resolve dependencies!
140 In case your GEM has more complex build requirements you can use
141 the following
options additionally inside of your GEM specification:
143 * `spec.
cflags` (C compiler flags)
144 * `spec.mruby_cflags` (global C compiler flags for everything)
145 * `spec.mruby_ldflags` (global linker flags for everything)
146 * `spec.mruby_libs` (global libraries for everything)
147 * `spec.mruby_includes` (global includes for everything)
148 * `spec.rbfiles` (Ruby
files to compile)
149 * `spec.objs` (Object
files to compile)
150 * `spec.test_rbfiles` (Ruby test
files for integration into mrbtest)
151 * `spec.test_objs` (Object test
files for integration into mrbtest)
152 * `spec.test_preload` (Initialization
files for mrbtest)
156 mruby can be extended with C. This is possible by
using the C API to
157 integrate C libraries into mruby.
161 mrbgems expects that you have implemented a C method called
162 `mrb_YOURGEMNAME_gem_init(
mrb_state)`. `YOURGEMNAME` will be replaced
163 by the name of your GEM. If you call your GEM *c_extension_example*, your
164 initialisation method could look like
this:
174 mrbgems expects that you have implemented a C method called
175 `mrb_YOURGEMNAME_gem_final(
mrb_state)`. `YOURGEMNAME` will be replaced
176 by the name of your GEM. If you call your GEM *c_extension_example*, your
177 finalizer method could look like
this:
186 +- c_extension_example/
190 | +- example.c <- C extension source
194 | +- example.rb <- Test code
for C extension
196 +- mrbgem.rake <- GEM specification
202 mruby can be extended with pure Ruby. It is possible to
override existing
203 classes or add
new ones in
this way. Put all Ruby
files into the *mrblib*
212 +- ruby_extension_example/
216 | +- example.rb <- Ruby extension source
220 | +- example.rb <- Test code
for Ruby extension
222 +- mrbgem.rake <- GEM specification
226 ## C and Ruby Extension
228 mruby can be extended with C and Ruby at the same time. It is possible to
229 override existing classes or add
new ones in
this way. Put all Ruby
files
230 into the *mrblib* folder and all C
files into the *src* folder.
234 See C and Ruby example.
238 +- c_and_ruby_extension_example/
242 | +- example.rb <- Ruby extension source
246 | +- example.c <- C extension source
250 | +- example.rb <- Test code
for C and Ruby extension
252 +- mrbgem.rake <- GEM specification