Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
/home/maru/tmp/groonga/vendor/mruby-eeac4be/doc/mrbgems/README.md
Go to the documentation of this file.
1 # mrbgems
2 
3 mrbgems is a library manager to integrate C and Ruby extension in an easy and
4 standardised way into mruby.
5 
6 ## Usage
7 
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
10 extension integrated.
11 
12 To add a GEM into the *build_config.rb* add the following line for example:
13 
14  conf.gem '/path/to/your/gem/dir'
15 
16 You can also use a relative path which would be relative from the mruby root:
17 
18  conf.gem 'examples/mrbgems/ruby_extension_example'
19 
20 A remote GIT repository location for a GEM is also supported:
21 
22  conf.gem :git => 'https://github.com/masuidrive/mrbgems-example.git', :branch => 'master'
23 
24  conf.gem :github => 'masuidrive/mrbgems-example', :branch => 'master'
25 
26  conf.gem :bitbucket => 'mruby/mrbgems-example', :branch => 'master'
27 
28 To pull all gems from remote GIT repository on build, call ```./minirake -p```,
29 or ```./minirake --pull-gems```.
30 
31 NOTE: `:bitbucket` option supports only git. Hg is unsupported in this version.
32 
33 ## GemBox
34 
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'`.
42 
43 Below we have created a GemBox containing *mruby-time* and *mrbgems-example*:
44 
45  MRuby::GemBox.new do |conf|
46  conf.gem "#{root}/mrbgems/mruby-time"
47  conf.gem :github => 'masuidrive/mrbgems-example'
48  end
49 
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
52 picked up by mruby.
53 
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
56 the build block:
57 
58  conf.gembox 'custom'
59 
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.
62 
63 If you want, you can put GemBox outside of mruby directory. In that case you must
64 specify absolute path like below.
65 
66  conf.gembox "#{ENV["HOME"]}/mygemboxes/custom"
67 
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.
72 
73 ## GEM Structure
74 
75 The maximal GEM structure looks like this:
76 
77  +- GEM_NAME <- Name of GEM
78  |
79  +- mrblib/ <- Source for Ruby extension
80  |
81  +- src/ <- Source for C extension
82  |
83  +- test/ <- Test code (Ruby)
84  |
85  +- mrbgem.rake <- GEM Specification
86  |
87  +- README.md <- Readme for GEM
88 
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
93 of your GEM.
94 
95 ## Build process
96 
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:
99 
100  MRuby::Gem::Specification.new('c_and_ruby_extension_example') do |spec|
101  spec.license = 'MIT'
102  spec.author = 'mruby developers'
103  end
104 
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`.
108 
109 The following properties can be set inside of your `MRuby::Gem::Specification` for
110 information purpose:
111 
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)
115 * `spec.description` (Detailed description)
116 * `spec.summary` (Short summary)
117 * `spec.homepage` (Homepage)
118 * `spec.requirements` (External requirements as information for user)
119 
120 The license and author properties are required in every GEM!
121 
122 In case your GEM is depending on other GEMs please use
123 `spec.add_dependency(gem, *requirements)` like:
124 
125  MRuby::Gem::Specification.new('c_and_ruby_extension_example') do |spec|
126  spec.license = 'MIT'
127  spec.author = 'mruby developers'
128 
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')
132  end
133 
134 The usage of versions is optional.
135 
136 __ATTENTION:__
137 The dependency system is currently (May 2013) under development and doesn't check
138 or resolve dependencies!
139 
140 In case your GEM has more complex build requirements you can use
141 the following options additionally inside of your GEM specification:
142 
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)
153 
154 ## C Extension
155 
156 mruby can be extended with C. This is possible by using the C API to
157 integrate C libraries into mruby.
158 
159 ### Preconditions
160 
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:
165 
166  void
168  struct RClass *class_cextension = mrb_define_module(mrb, "CExtension");
169  mrb_define_class_method(mrb, class_cextension, "c_method", mrb_c_method, MRB_ARGS_NONE());
170  }
171 
172 ### Finalize
173 
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:
178 
179  void
181  free(someone);
182  }
183 
184 ### Example
185 
186  +- c_extension_example/
187  |
188  +- src/
189  | |
190  | +- example.c <- C extension source
191  |
192  +- test/
193  | |
194  | +- example.rb <- Test code for C extension
195  |
196  +- mrbgem.rake <- GEM specification
197  |
198  +- README.md
199 
200 ## Ruby Extension
201 
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*
204 folder.
205 
206 ### Pre-Conditions
207 
208 none
209 
210 ### Example
211 
212  +- ruby_extension_example/
213  |
214  +- mrblib/
215  | |
216  | +- example.rb <- Ruby extension source
217  |
218  +- test/
219  | |
220  | +- example.rb <- Test code for Ruby extension
221  |
222  +- mrbgem.rake <- GEM specification
223  |
224  +- README.md
225 
226 ## C and Ruby Extension
227 
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.
231 
232 ### Pre-Conditions
233 
234 See C and Ruby example.
235 
236 ### Example
237 
238  +- c_and_ruby_extension_example/
239  |
240  +- mrblib/
241  | |
242  | +- example.rb <- Ruby extension source
243  |
244  +- src/
245  | |
246  | +- example.c <- C extension source
247  |
248  +- test/
249  | |
250  | +- example.rb <- Test code for C and Ruby extension
251  |
252  +- mrbgem.rake <- GEM specification
253  |
254  +- README.md