| 1 |
# mrbgems |
| 2 |
|
| 3 |
mrbgems is a library manager to integrate C and Ruby extensions in an easy and |
| 4 |
standardised way into mruby. Conventionally, each mrbgem name is prefixed by |
| 5 |
`mruby-`, e.g. `mruby-time` for a gem that provides `Time` class functionality. |
| 6 |
|
| 7 |
## Usage |
| 8 |
|
| 9 |
You have to activate mrbgems explicitly in your build configuration. To add |
| 10 |
a gem, add the following line to your build configuration file, for example: |
| 11 |
|
| 12 |
```ruby |
| 13 |
conf.gem '/path/to/your/gem/dir' |
| 14 |
``` |
| 15 |
|
| 16 |
You can also use a relative path to specify a gem. |
| 17 |
|
| 18 |
```ruby |
| 19 |
conf.gem 'examples/mrbgems/ruby_extension_example' |
| 20 |
``` |
| 21 |
|
| 22 |
In that case, |
| 23 |
|
| 24 |
- if your build configuration file is in the `build_config` directory, it's |
| 25 |
relative from `MRUBY_ROOT`. |
| 26 |
- otherwise, it is relative from the directory where your build configuration is. |
| 27 |
|
| 28 |
A remote GIT repository location for a GEM is also supported: |
| 29 |
|
| 30 |
```ruby |
| 31 |
conf.gem :git => 'https://github.com/masuidrive/mrbgems-example.git', :branch => 'master' |
| 32 |
conf.gem :github => 'masuidrive/mrbgems-example', :branch => 'master' |
| 33 |
conf.gem :bitbucket => 'mruby/mrbgems-example', :branch => 'master' |
| 34 |
``` |
| 35 |
|
| 36 |
NOTE: `:bitbucket` option supports only git. Hg is unsupported in this |
| 37 |
version. |
| 38 |
|
| 39 |
You can specify the subdirectory of the repository with `:path` option: |
| 40 |
|
| 41 |
```ruby |
| 42 |
conf.gem github: 'mruby/mruby', path: 'mrbgems/mruby-socket' |
| 43 |
``` |
| 44 |
|
| 45 |
To use mrbgem from [mgem-list](https://github.com/mruby/mgem-list) use `:mgem` option: |
| 46 |
|
| 47 |
```ruby |
| 48 |
conf.gem :mgem => 'mruby-yaml' |
| 49 |
conf.gem :mgem => 'yaml' # 'mruby-' prefix could be omitted |
| 50 |
``` |
| 51 |
|
| 52 |
For specifying the commit hash to checkout use `:checksum_hash` option: |
| 53 |
|
| 54 |
```ruby |
| 55 |
conf.gem mgem: 'mruby-redis', checksum_hash: '3446d19fc4a3f9697b5ddbf2a904f301c42f2f4e' |
| 56 |
``` |
| 57 |
|
| 58 |
If there are missing dependencies, mrbgem dependencies solver will reference |
| 59 |
mrbgem from the core or mgem-list. |
| 60 |
|
| 61 |
Note that if more than one git-based gem has the same base name |
| 62 |
(i.e. the default checkout directory name), it is (now) an error |
| 63 |
**UNLESS** they have the same repository URL, branch name and |
| 64 |
commit-id (i.e. checksum hash). You can bypass this by explicitly |
| 65 |
importing your preferred version **first** and setting the |
| 66 |
`canonical:` option to `true`: |
| 67 |
|
| 68 |
```ruby |
| 69 |
conf.gem github: 'me/mruby-yaml', branch: 'my-hacked-branch', canonical: true |
| 70 |
``` |
| 71 |
|
| 72 |
If you do this, the system will (mostly) silently ignore other |
| 73 |
attempts to clone a gem with this name. |
| 74 |
|
| 75 |
Note that this only affects cloning the gem from git. It does not |
| 76 |
resolve version conflicts. If the version as specified in the gem's |
| 77 |
rakefile is incompatible with a dependency, your build will still |
| 78 |
fail. |
| 79 |
|
| 80 |
### Tweak the gem in your build configuration file |
| 81 |
|
| 82 |
You can give blocks in the `conf.gem` call to make adjustments for |
| 83 |
environments where the original gem does not expect them: |
| 84 |
|
| 85 |
```ruby |
| 86 |
conf.gem core: "mruby-bin-mirb" do |g| |
| 87 |
# For cross build to NetBSD |
| 88 |
g.linker.libraries = %w(edit termcap) |
| 89 |
end |
| 90 |
``` |
| 91 |
|
| 92 |
However, it should be used with caution, as it may deviate from the intent |
| 93 |
of the gem's author. |
| 94 |
|
| 95 |
### Gem Testing |
| 96 |
|
| 97 |
If you enable unit tests in your build with `enable_test`, tests will be |
| 98 |
generated for all gems and their dependencies by default. If necessary, it is |
| 99 |
possible to suppress tests for a specific gem like so: |
| 100 |
|
| 101 |
```ruby |
| 102 |
conf.gem 'mruby-noisygem' do |g| |
| 103 |
g.skip_test = true |
| 104 |
end |
| 105 |
``` |
| 106 |
|
| 107 |
However, it is considered best practice to leave all tests enabled whenever |
| 108 |
possible. A warning message will be generated for each gem with disabled tests. |
| 109 |
|
| 110 |
## GemBox |
| 111 |
|
| 112 |
There are instances when you wish to add a collection of mrbgems into mruby at |
| 113 |
once, or be able to substitute mrbgems based on configuration, without having to |
| 114 |
add each gem to your build configuration file. A packaged collection of mrbgems |
| 115 |
is called a GemBox. A GemBox is a file that contains a list of mrbgems to load |
| 116 |
into mruby, in the same format as if you were adding them to the build config |
| 117 |
via `config.gem`, but wrapped in an `MRuby::GemBox` object. GemBoxes are |
| 118 |
loaded into mruby via `config.gembox 'boxname'`. |
| 119 |
|
| 120 |
Below we have created a GemBox containing `mruby-time` and `mrbgems-example`: |
| 121 |
|
| 122 |
```ruby |
| 123 |
MRuby::GemBox.new do |conf| |
| 124 |
conf.gem "#{root}/mrbgems/mruby-time" |
| 125 |
conf.gem :github => 'masuidrive/mrbgems-example' |
| 126 |
end |
| 127 |
``` |
| 128 |
|
| 129 |
As mentioned, the GemBox uses the same conventions as `MRuby::Build`. The GemBox |
| 130 |
must be saved with a `.gembox` extension inside the `mrbgems` directory to be |
| 131 |
picked up by mruby. |
| 132 |
|
| 133 |
To use this example GemBox, we save it as `custom.gembox` inside the `mrbgems` |
| 134 |
directory in mruby, and add the following to your build configuration file inside |
| 135 |
the build block: |
| 136 |
|
| 137 |
```ruby |
| 138 |
conf.gembox 'custom' |
| 139 |
``` |
| 140 |
|
| 141 |
This will cause the `custom` GemBox to be read in during the build process, |
| 142 |
adding `mruby-time` and `mrbgems-example` to the build. |
| 143 |
|
| 144 |
If you want, you can put GemBox outside the mruby directory. In that case you must |
| 145 |
specify an absolute path like below. |
| 146 |
|
| 147 |
```ruby |
| 148 |
conf.gembox "#{ENV["HOME"]}/mygemboxes/custom" |
| 149 |
``` |
| 150 |
|
| 151 |
There are two GemBoxes that ship with mruby: [default](../../mrbgems/default.gembox) |
| 152 |
and [full-core](../../mrbgems/full-core.gembox). The [default](../../mrbgems/default.gembox) GemBox |
| 153 |
contains several core components of mruby, and [full-core](../../mrbgems/full-core.gembox) |
| 154 |
contains every gem found in the `mrbgems` directory. |
| 155 |
|
| 156 |
## GEM Structure |
| 157 |
|
| 158 |
The maximal GEM structure looks like this: |
| 159 |
|
| 160 |
``` |
| 161 |
+- GEM_NAME <- Name of GEM |
| 162 |
| |
| 163 |
+- README.md <- Readme for GEM |
| 164 |
| |
| 165 |
+- mrbgem.rake <- GEM Specification |
| 166 |
| |
| 167 |
+- include/ <- Header for Ruby extension (will exported) |
| 168 |
| |
| 169 |
+- mrblib/ <- Source for Ruby extension |
| 170 |
| |
| 171 |
+- src/ <- Source for C extension |
| 172 |
| |
| 173 |
+- tools/ <- Source for Executable (in C) |
| 174 |
| |
| 175 |
+- test/ <- Test code (Ruby) |
| 176 |
``` |
| 177 |
|
| 178 |
The `mrblib` directory contains pure Ruby files to extend mruby. The `src` directory |
| 179 |
contains C/C++ files to extend mruby. The `include` directory contains C/C++ header |
| 180 |
files. The `test` directory contains C/C++ and pure Ruby files for testing purposes |
| 181 |
which will be used by `mrbtest`. `mrbgem.rake` contains the specification |
| 182 |
to compile C and Ruby files. `README.md` is a short description of your GEM. |
| 183 |
|
| 184 |
## Build process |
| 185 |
|
| 186 |
mrbgems expects a specification file called `mrbgem.rake` inside of your |
| 187 |
GEM directory. A typical GEM specification could look like this for example: |
| 188 |
|
| 189 |
```ruby |
| 190 |
MRuby::Gem::Specification.new('c_and_ruby_extension_example') do |spec| |
| 191 |
spec.license = 'MIT' |
| 192 |
spec.author = 'mruby developers' |
| 193 |
spec.summary = 'Example mrbgem using C and Ruby' |
| 194 |
end |
| 195 |
``` |
| 196 |
|
| 197 |
The mrbgems build process will use this specification to compile Object and Ruby |
| 198 |
files. The compilation results will be added to `lib/libmruby.a`. This file exposes |
| 199 |
the GEM functionality to tools like `mruby` and `mirb`. |
| 200 |
|
| 201 |
The following properties can be set inside your `MRuby::Gem::Specification` for |
| 202 |
information purpose: |
| 203 |
|
| 204 |
- `spec.license` or `spec.licenses` (A single license or a list of them under which this GEM is licensed) |
| 205 |
- `spec.author` or `spec.authors` (Developer name or a list of them) |
| 206 |
- `spec.version` (Current version) |
| 207 |
- `spec.description` (Detailed description) |
| 208 |
- `spec.summary` |
| 209 |
- One line short description of mrbgem. |
| 210 |
- Printed in build summary of rake when set. |
| 211 |
- `spec.homepage` (Homepage) |
| 212 |
- `spec.requirements` (External requirements as information for user) |
| 213 |
|
| 214 |
The `license` and `author` properties are required in every GEM! |
| 215 |
|
| 216 |
In case your GEM is depending on other GEMs please use |
| 217 |
`spec.add_dependency(gem, *requirements[, default_get_info])` like: |
| 218 |
|
| 219 |
```ruby |
| 220 |
MRuby::Gem::Specification.new('c_and_ruby_extension_example') do |spec| |
| 221 |
spec.license = 'MIT' |
| 222 |
spec.author = 'mruby developers' |
| 223 |
|
| 224 |
# Add GEM dependency mruby-parser. |
| 225 |
# The version must be between 1.0.0 and 1.5.2 . |
| 226 |
spec.add_dependency('mruby-parser', '>= 1.0.0', '<= 1.5.2') |
| 227 |
|
| 228 |
# Use any version of mruby-uv from GitHub. |
| 229 |
spec.add_dependency('mruby-uv', '>= 0.0.0', :github => 'mattn/mruby-uv') |
| 230 |
|
| 231 |
# Use latest mruby-onig-regexp from GitHub. (version requirements can be omitted) |
| 232 |
spec.add_dependency('mruby-onig-regexp', :github => 'mattn/mruby-onig-regexp') |
| 233 |
|
| 234 |
# You can add extra mgems active only on test |
| 235 |
spec.add_test_dependency('mruby-process', :github => 'iij/mruby-process') |
| 236 |
end |
| 237 |
``` |
| 238 |
|
| 239 |
The version requirements and default gem information are optional. |
| 240 |
|
| 241 |
Version requirement supports following operators: |
| 242 |
|
| 243 |
- '=': is equal |
| 244 |
- '!=': is not equal |
| 245 |
- '>': is greater |
| 246 |
- '<': is lesser |
| 247 |
- '>=': is equal or greater |
| 248 |
- '<=': is equal or lesser |
| 249 |
- '~>': is equal or greater and is lesser than the next major version |
| 250 |
- example 1: '~> 2.2.2' means '>= 2.2.2' and '< 2.3.0' |
| 251 |
- example 2: '~> 2.2' means '>= 2.2.0' and '< 3.0.0' |
| 252 |
|
| 253 |
When more than one version requirements is passed, the dependency must satisfy all of it. |
| 254 |
|
| 255 |
You can have default gem to use as dependency when it's not defined in your build configuration. |
| 256 |
When the last argument of `add_dependency` call is `Hash`, it will be treated as default gem information. |
| 257 |
Its format is same as argument of method `MRuby::Build#gem`, expect that it can't be treated as path gem location. |
| 258 |
|
| 259 |
When a special version of dependency is required, |
| 260 |
use `MRuby::Build#gem` in the build configuration to override default gem. |
| 261 |
|
| 262 |
If you have conflicting GEMs use the following method: |
| 263 |
|
| 264 |
- `spec.add_conflict(gem, *requirements)` |
| 265 |
- The `requirements` argument is same as in `add_dependency` method. |
| 266 |
|
| 267 |
like following code: |
| 268 |
|
| 269 |
```ruby |
| 270 |
MRuby::Gem::Specification.new 'some-regexp-binding' do |spec| |
| 271 |
spec.license = 'BSD' |
| 272 |
spec.author = 'John Doe' |
| 273 |
|
| 274 |
spec.add_conflict 'mruby-onig-regexp', '> 0.0.0' |
| 275 |
spec.add_conflict 'mruby-hs-regexp' |
| 276 |
spec.add_conflict 'mruby-pcre-regexp' |
| 277 |
spec.add_conflict 'mruby-regexp-pcre' |
| 278 |
end |
| 279 |
``` |
| 280 |
|
| 281 |
In case your GEM has more complex build requirements you can use |
| 282 |
the following options additionally inside your GEM specification: |
| 283 |
|
| 284 |
- `spec.cc.flags` (C compiler flags) |
| 285 |
- `spec.cc.defines` (C compiler defines) |
| 286 |
- `spec.cc.include_paths` (C compiler include paths) |
| 287 |
- `spec.linker.flags` (Linker flags) |
| 288 |
- `spec.linker.libraries` (Linker libraries) |
| 289 |
- `spec.linker.library_paths` (Linker additional library path) |
| 290 |
- `spec.bins` (Generate binary file) |
| 291 |
- `spec.rbfiles` (Ruby files to compile) |
| 292 |
- `spec.objs` (Object files to compile) |
| 293 |
- `spec.test_rbfiles` (Ruby test files for integration into mrbtest) |
| 294 |
- `spec.test_objs` (Object test files for integration into mrbtest) |
| 295 |
- `spec.test_preload` (Initialization files for mrbtest) |
| 296 |
|
| 297 |
You also can use `spec.mruby.cc` and `spec.mruby.linker` to add extra global parameters for the compiler and linker. |
| 298 |
|
| 299 |
### include_paths and dependency |
| 300 |
|
| 301 |
Your GEM can export include paths to another GEMs that depends on your GEM. |
| 302 |
By default, `/...absolute path.../{GEM_NAME}/include` will be exported. |
| 303 |
So it is recommended not to put GEM's local header files on include/. |
| 304 |
|
| 305 |
These exports are retroactive. |
| 306 |
For example: when B depends on C and A depends on B, A will get include paths exported by C. |
| 307 |
|
| 308 |
Exported include_paths are automatically appended to GEM local include_paths by rake. |
| 309 |
You can use `spec.export_include_paths` accessor if you want more complex build. |
| 310 |
|
| 311 |
## C Extension |
| 312 |
|
| 313 |
mruby can be extended with C. This is possible by using the C API to |
| 314 |
integrate C libraries into mruby. |
| 315 |
|
| 316 |
### Preconditions |
| 317 |
|
| 318 |
mrbgems expects that you have implemented a C method called |
| 319 |
`mrb_YOURGEMNAME_gem_init(mrb_state)`. `YOURGEMNAME` will be replaced |
| 320 |
by the name of your GEM. If you call your GEM `c_extension_example`, your |
| 321 |
initialisation method could look like this: |
| 322 |
|
| 323 |
```c |
| 324 |
void |
| 325 |
mrb_c_extension_example_gem_init(mrb_state* mrb) { |
| 326 |
struct RClass *class_cextension = mrb_define_module(mrb, "CExtension"); |
| 327 |
mrb_define_class_method(mrb, class_cextension, "c_method", mrb_c_method, MRB_ARGS_NONE()); |
| 328 |
} |
| 329 |
``` |
| 330 |
|
| 331 |
### Finalize |
| 332 |
|
| 333 |
mrbgems expects that you have implemented a C method called |
| 334 |
`mrb_YOURGEMNAME_gem_final(mrb_state)`. `YOURGEMNAME` will be replaced |
| 335 |
by the name of your GEM. If you call your GEM `c_extension_example`, your |
| 336 |
finalizer method could look like this: |
| 337 |
|
| 338 |
```c |
| 339 |
void |
| 340 |
mrb_c_extension_example_gem_final(mrb_state* mrb) { |
| 341 |
free(someone); |
| 342 |
} |
| 343 |
``` |
| 344 |
|
| 345 |
### Example |
| 346 |
|
| 347 |
``` |
| 348 |
+- c_extension_example/ |
| 349 |
| |
| 350 |
+- README.md (Optional) |
| 351 |
| |
| 352 |
+- src/ |
| 353 |
| | |
| 354 |
| +- example.c <- C extension source |
| 355 |
| |
| 356 |
+- test/ |
| 357 |
| | |
| 358 |
| +- example.rb <- Test code for C extension |
| 359 |
| |
| 360 |
+- mrbgem.rake <- GEM specification |
| 361 |
``` |
| 362 |
|
| 363 |
## Ruby Extension |
| 364 |
|
| 365 |
mruby can be extended with pure Ruby. It is possible to override existing |
| 366 |
classes or add new ones in this way. Put all Ruby files into the `mrblib` |
| 367 |
directory. |
| 368 |
|
| 369 |
### Pre-Conditions |
| 370 |
|
| 371 |
none |
| 372 |
|
| 373 |
### Example |
| 374 |
|
| 375 |
``` |
| 376 |
+- ruby_extension_example/ |
| 377 |
| |
| 378 |
+- README.md (Optional) |
| 379 |
| |
| 380 |
+- mrblib/ |
| 381 |
| | |
| 382 |
| +- example.rb <- Ruby extension source |
| 383 |
| |
| 384 |
+- test/ |
| 385 |
| | |
| 386 |
| +- example.rb <- Test code for Ruby extension |
| 387 |
| |
| 388 |
+- mrbgem.rake <- GEM specification |
| 389 |
``` |
| 390 |
|
| 391 |
## C and Ruby Extension |
| 392 |
|
| 393 |
mruby can be extended with C and Ruby at the same time. It is possible to |
| 394 |
override existing classes or add new ones in this way. Put all Ruby files |
| 395 |
into the `mrblib` directory and all C files into the `src` directory. |
| 396 |
|
| 397 |
mruby codes under `mrblib` directory would be executed after gem init C |
| 398 |
function is called. Make sure *mruby script* depends on *C code* and |
| 399 |
*C code* doesn't depend on *mruby script*. |
| 400 |
|
| 401 |
### Pre-Conditions |
| 402 |
|
| 403 |
See C and Ruby example. |
| 404 |
|
| 405 |
### Example |
| 406 |
|
| 407 |
``` |
| 408 |
+- c_and_ruby_extension_example/ |
| 409 |
| |
| 410 |
+- README.md (Optional) |
| 411 |
| |
| 412 |
+- mrblib/ |
| 413 |
| | |
| 414 |
| +- example.rb <- Ruby extension source |
| 415 |
| |
| 416 |
+- src/ |
| 417 |
| | |
| 418 |
| +- example.c <- C extension source |
| 419 |
| |
| 420 |
+- test/ |
| 421 |
| | |
| 422 |
| +- example.rb <- Test code for C and Ruby extension |
| 423 |
| |
| 424 |
+- mrbgem.rake <- GEM specification |
| 425 |
``` |
| 426 |
|
| 427 |
## Binary gems |
| 428 |
|
| 429 |
Some gems can generate executables under `bin` directory. Those gems are called |
| 430 |
binary gems. Names of binary gems are conventionally prefixed by `mruby-bin`, |
| 431 |
e.g. `mruby-bin-mirb` and `mruby-bin-strip`. |
| 432 |
|
| 433 |
To specify the name of executable, you need to specify `spec.bins` in the |
| 434 |
`mrbgem.rake`. The entry point `main()` should be in the C source file under |
| 435 |
`tools/<bin>/*.c` where `<bin>` is a name of the executable. C files under the |
| 436 |
`<bin>` directory are compiled and linked to the executable, but not included in |
| 437 |
`libmruby.a`, whereas files under `mrblib` and `src` are. |
| 438 |
|
| 439 |
It is strongly recommended not to include `mrblib` and `src` directories in the |
| 440 |
binary gems, to separate normal gems and binary gems. |
| 441 |
|
| 442 |
### Example |
| 443 |
|
| 444 |
``` |
| 445 |
+- mruby-bin-example/ |
| 446 |
| |
| 447 |
+- README.md (Optional) |
| 448 |
| |
| 449 |
+- bintest/ |
| 450 |
| | |
| 451 |
| +- example.rb <- Test code for binary gem |
| 452 |
| |
| 453 |
+- mrbgem.rake <- Gem specification |
| 454 |
| |
| 455 |
+- mrblib/ <- Source for Ruby extension (Optional) |
| 456 |
| |
| 457 |
+- src/ <- Source for C extension (Optional) |
| 458 |
| |
| 459 |
+- tools/ |
| 460 |
| |
| 461 |
+- example/ <- Executable name directory |
| 462 |
| |
| 463 |
+- example.c <- Source for Executable (includes main) |
| 464 |
``` |