blob: 3082c0df04a45f5938136929779058d5c317db42 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# mrbgems
mrbgems is a library manager to integrate C and Ruby extension in an easy and
standardised way into mruby.
## GEM Structure
The maximal Gem structure looks like this:
```
+- GEM_NAME <- Name of Gem
|
+- mrblib/ <- Source for Ruby extension
|
+- src/ <- Source for C extension
|
+- test/ <- Test code (Ruby)
|
+- Makefile <- Makefile for Gem
|
+- README.md <- Readme for Gem
```
The folder *mrblib* contains pure Ruby files to extend mruby. The folder *src*
contains C files to extend mruby. The folder *test* contains pure Ruby files
for testing purposes which will be used by mrbtest. The *Makefile* contains
rules to build all C files and integrates them into the normal mruby
build process. *README.md* is a short description of your Gem.
## C Extension
mruby can be extended with C. It is possible by using the C API to integrate C
libraries into mruby. You need to use the folder *src* for all C files. Pay
attention that your *Makefile* has to build the source and also add the object
files to libmruby.a
### Pre-Conditions
mrbgems will automatically call the *gem-all* make target of your Gem. Make
sure that you build all files in this target and that you add your object
files to libmruby.a
mrbgems expects that you have implemented a C method called
*mrb_YOURGEMNAME_gem_init(mrb_state)*. YOURGEMNAME will be replaced
by the name of you Gem. The directory name of your Gem is considered also
as the name! If you call your Gem directory *c_extension_example*, your
initialisation method could look like this:
```
void
mrb_c_extension_example_gem_init(mrb_state* mrb) {
_class_cextension = mrb_define_module(mrb, "CExtension");
mrb_define_class_method(mrb, _class_cextension, "c_method", mrb_c_method, ARGS_NONE());
}
```
mrbgems will also use the *gem-clean* make target to clean up your Gem. Implement
this target with the necessary rules!
### Example
```
+- c_extension_example/
|
+- src/
| |
| +- example.c <- C extension source
|
+- test/
| |
| +- example.rb <- Test code for C extension
|
+- Makefile <- Build rules for C extension
|
+- README.md
```
## Ruby Extension
mruby can be extended with pure Ruby. It is possible to override existing
classes or add new ones in this way. Put all Ruby files into the *mrblib*
folder. At the moment only one directory layer is supported. So don't
use a deeper structure for now!
The *Makefile* is not used for building a Ruby extension. But you still
should maintain this file so that during the build process the progress
can be visualized. If you want to do additional things during the build
process of your Ruby extension you can use the *Makefile* too.
### Example
```
+- ruby_extension_example/
|
+- mrblib/
| |
| +- example.rb <- Ruby extension source
|
+- test/
| |
| +- example.rb <- Test code for Ruby extension
|
+- Makefile
|
+- README.md
```
## Current Limitations
The following limitations are currently existing:
* Gem _MUST NOT_ have a *src* folder in case it doesn't have a
C extension
* Gem _MUST NOT_ have a *mrblib* folder in case it doesn't have a
Ruby extension
* Only Ruby files in the root directory of *mrblib* will be integrated
If you have ideas how to fix these issues without implementing to much
complexity into the code please provide your code or idea.
|