3 mruby uses Rake to compile and cross-compile all libraries and
8 To compile mruby out of the source code you need the following tools:
9 * C Compiler (e.g. `gcc` or `clang`)
10 * Linker (e.g. `gcc` or `clang`)
11 * Archive utility (e.g. `ar`)
12 * Parser generator (e.g. `bison`)
13 * Ruby 2.0 or later (e.g. `ruby` or `jruby`)
15 Note that `bison` bundled with MacOS is too old to compile `mruby`.
16 Try `brew install bison` and follow the instuction shown to update
17 the `$PATH` to compile `mruby`.
20 * GIT (to update mruby source and integrate mrbgems easier)
21 * C++ compiler (to use GEMs which include \*.cpp, \*.cxx, \*.cc)
22 * Assembler (to use GEMs which include \*.asm)
26 Inside of the root directory of the mruby source a file exists
27 called *build_config.rb*. This file contains the build configuration
28 of mruby and looks like this for example:
30 MRuby::Build.new do |conf|
35 All tools necessary to compile mruby can be set or modified here. In case
36 you want to maintain an additional *build_config.rb* you can define a
37 customized path using the *$MRUBY_CONFIG* environment variable.
39 To compile just call `rake` inside of the mruby source root. To
40 generate and execute the test tools call `rake test`. To clean
41 all build files call `rake clean`. To see full command line on
42 build, call `rake -v`.
44 ## Build Configuration
46 Inside of the *build_config.rb* the following options can be configured
47 based on your environment.
51 The mruby build system already contains a set of toolchain templates which
52 configure the build environment for specific compiler infrastructures.
56 Toolchain configuration for the GNU C Compiler.
63 Toolchain configuration for the LLVM C Compiler clang. Mainly equal to the
69 #### Visual Studio 2010, 2012 and 2013
71 Toolchain configuration for Visual Studio on Windows. If you use the
72 [Visual Studio Command Prompt](http://msdn.microsoft.com/en-us/library/ms229859\(v=vs.110\).aspx),
73 you normally do not have to specify this manually, since it gets automatically detected by our build process.
80 Toolchain configuration for Android.
85 Requires the custom standalone Android NDK and the toolchain path
86 in `ANDROID_STANDALONE_TOOLCHAIN`.
90 It is possible to select which tools should be compiled during the compilation
91 process. The following tools can be selected:
92 * mruby (mruby interpreter)
93 * mirb (mruby interactive shell)
95 To select them declare conf.gem as follows:
97 conf.gem "#{root}/mrbgems/mruby-bin-mruby"
98 conf.gem "#{root}/mrbgems/mruby-bin-mirb"
103 Some environments require a different file separator character. It is possible to
104 set the character via `conf.file_separator`.
106 conf.file_separator = '/'
111 Configuration of the C compiler binary, flags and include paths.
116 cc.include_paths = ...
118 cc.option_include_path = ...
119 cc.option_define = ...
120 cc.compile_options = ...
124 C Compiler has header searcher to detect installed library.
126 If you need a include path of header file use `search_header_path`:
128 # Searches ```iconv.h```.
129 # If found it will return include path of the header file.
130 # Otherwise it will return nil .
131 fail 'iconv.h not found' unless conf.cc.search_header_path 'iconv.h'
134 If you need a full file name of header file use `search_header`:
136 # Searches ```iconv.h```.
137 # If found it will return full path of the header file.
138 # Otherwise it will return nil .
139 iconv_h = conf.cc.search_header 'iconv.h'
140 print "iconv.h found: #{iconv_h}\n"
143 Header searcher uses compiler's `include_paths` by default.
144 When you are using GCC toolchain (including clang toolchain since its base is gcc toolchain)
145 it will use compiler specific include paths too. (For example `/usr/local/include`, `/usr/include`)
147 If you need a special header search paths define a singleton method `header_search_paths` to C compiler:
149 def conf.cc.header_search_paths
150 ['/opt/local/include'] + include_paths
156 Configuration of the Linker binary, flags and library paths.
158 conf.linker do |linker|
161 linker.flags_before_libraries = ...
162 linker.libraries = ...
163 linker.flags_after_libraries = ...
164 linker.library_paths = ....
165 linker.option_library = ...
166 linker.option_library_path = ...
167 linker.link_options = ...
173 Configuration of the Archiver binary and flags.
175 conf.archiver do |archiver|
176 archiver.command = ...
177 archiver.archive_options = ...
183 Configuration of the Parser Generator binary and flags.
187 yacc.compile_options = ...
193 Configuration of the GPerf binary and flags.
195 conf.gperf do |gperf|
197 gperf.compile_options = ...
205 exts.executable = ...
212 Integrate GEMs in the build process.
214 # Integrate GEM with additional configuration
215 conf.gem 'path/to/gem' do |g|
219 # Integrate GEM without additional configuration
220 conf.gem 'path/to/another/gem'
223 See doc/mrbgems/README.md for more option about mrbgems.
227 Configuration Mrbtest build process.
229 If you want mrbtest.a only, You should set `conf.build_mrbtest_lib_only`
231 conf.build_mrbtest_lib_only
236 Tests for mrbgem tools using CRuby.
237 To have bintests place \*.rb scripts to `bintest/` directory of mrbgems.
238 See `mruby-bin-*/bintest/*.rb` if you need examples.
239 If you want a temporary files use `tempfile` module of CRuby instead of `/tmp/`.
241 You can enable it with following:
248 By default, mruby uses setjmp/longjmp to implement its
249 exceptions. But it doesn't release C++ stack object
250 correctly. To support mrbgems written in C++, mruby can be
251 configured to use C++ exception.
253 There are two levels of C++ exception handling. The one is
254 `enable_cxx_exception` that enables C++ exception, but
255 uses C ABI. The other is `enable_cxx_abi` where all
256 files are compiled by C++ compiler.
258 When you mix C++ code, C++ exception would be enabled automatically.
259 If you need to enable C++ exception explicitly add the following:
261 conf.enable_cxx_exception
264 #### C++ exception disabling.
266 If your compiler does not support C++ and you want to ensure
267 you don't use mrbgem written in C++, you can explicitly disable
268 C++ exception, add following:
270 conf.disable_cxx_exception
272 and you will get an error when you try to use C++ gem.
273 Note that it must be called before `enable_cxx_exception` or `gem` method.
277 To enable debugging mode add the following:
282 When debugging mode is enabled
283 * Macro `MRB_DEBUG` would be defined.
284 * Which means `mrb_assert()` macro is enabled.
285 * Debug information of irep would be generated by `mrbc`.
286 * Because `-g` flag would be added to `mrbc` runner.
287 * You can have better backtrace of mruby scripts with this.
291 mruby can also be cross-compiled from one platform to another. To
292 achieve this the *build_config.rb* needs to contain an instance of
293 `MRuby::CrossBuild`. This instance defines the compilation
294 tools and flags for the target platform. An example could look
297 MRuby::CrossBuild.new('32bit') do |conf|
300 conf.cc.flags << "-m32"
301 conf.linker.flags << "-m32"
305 All configuration options of `MRuby::Build` can also be used
306 in `MRuby::CrossBuild`.
308 ### Mrbtest in Cross-Compilation
310 In cross compilation, you can run `mrbtest` on emulator if
311 you have it by changing configuration of test runner.
313 conf.test_runner do |t|
314 t.command = ... # set emulator. this value must be non nil or false
315 t.flags = ... # set flags of emulator
317 def t.run(bin) # override `run` if you need to change the behavior of it
318 ... # `bin` is the full path of mrbtest
325 During the build process the directory *build* will be created in the
326 root directory. The structure of this directory will look like this:
332 +- bin <- Binaries (mirb, mrbc and mruby)
334 +- lib <- Libraries (libmruby.a and libmruby_core.a)
340 +- test <- mrbtest tool
350 The compilation workflow will look like this:
351 * compile all files under *src* (object files will be stored
353 * generate parser grammar out of *src/parse.y* (generated
354 result will be stored in *build/host/src/y.tab.c*)
355 * compile *build/host/src/y.tab.c* to *build/host/src/y.tab.o*
356 * create *build/host/lib/libmruby_core.a* out of all object files (C only)
357 * create `build/host/bin/mrbc` by compiling *tools/mrbc/mrbc.c* and
358 linking with *build/host/lib/libmruby_core.a*
359 * create *build/host/mrblib/mrblib.c* by compiling all \*.rb files
360 under *mrblib* with `build/host/bin/mrbc`
361 * compile *build/host/mrblib/mrblib.c* to *build/host/mrblib/mrblib.o*
362 * create *build/host/lib/libmruby.a* out of all object files (C and Ruby)
363 * create `build/host/bin/mruby` by compiling *mrbgems/mruby-bin-mruby/tools/mruby/mruby.c* and
364 linking with *build/host/lib/libmruby.a*
365 * create `build/host/bin/mirb` by compiling *mrbgems/mruby-bin-mirb/tools/mirb/mirb.c* and
366 linking with *build/host/lib/libmruby.a*
369 _____ _____ ______ ____ ____ _____ _____ ____
370 | CC |->|GEN |->|AR |->|CC |->|CC |->|AR |->|CC |->|CC |
371 | *.c | |y.tab| |core.a| |mrbc| |*.rb| |lib.a| |mruby| |mirb|
372 ----- ----- ------ ---- ---- ----- ----- ----
375 ### Cross-Compilation
377 In case of a cross-compilation to *i386* the *build* directory structure looks
384 | +- bin <- Native Binaries
386 | +- lib <- Native Libraries
392 | +- test <- Native mrbtest tool
403 +- bin <- Cross-compiled Binaries
405 +- lib <- Cross-compiled Libraries
411 +- test <- Cross-compiled mrbtest tool
421 An extra directory is created for the target platform. In case you
422 compile for *i386* a directory called *i386* is created under the
425 The cross compilation workflow starts in the same way as the normal
426 compilation by compiling all *native* libraries and binaries.
427 Afterwards the cross compilation process proceeds like this:
428 * cross-compile all files under *src* (object files will be stored
430 * generate parser grammar out of *src/parse.y* (generated
431 result will be stored in *build/i386/src/y.tab.c*)
432 * cross-compile *build/i386/src/y.tab.c* to *build/i386/src/y.tab.o*
433 * create *build/i386/mrblib/mrblib.c* by compiling all \*.rb files
434 under *mrblib* with the native `build/host/bin/mrbc`
435 * cross-compile *build/host/mrblib/mrblib.c* to *build/host/mrblib/mrblib.o*
436 * create *build/i386/lib/libmruby.a* out of all object files (C and Ruby)
437 * create `build/i386/bin/mruby` by cross-compiling *mrbgems/mruby-bin-mruby/tools/mruby/mruby.c* and
438 linking with *build/i386/lib/libmruby.a*
439 * create `build/i386/bin/mirb` by cross-compiling *mrbgems/mruby-bin-mirb/tools/mirb/mirb.c* and
440 linking with *build/i386/lib/libmruby.a*
441 * create *build/i386/lib/libmruby_core.a* out of all object files (C only)
442 * create `build/i386/bin/mrbc` by cross-compiling *tools/mrbc/mrbc.c* and
443 linking with *build/i386/lib/libmruby_core.a*
446 _______________________________________________________________
447 | Native Compilation for Host System |
448 | _____ ______ _____ ____ ____ _____ |
449 | | CC | -> |AR | -> |GEN | -> |CC | -> |CC | -> |AR | |
450 | | *.c | |core.a| |y.tab| |mrbc| |*.rb| |lib.a| |
451 | ----- ------ ----- ---- ---- ----- |
452 ---------------------------------------------------------------
456 ________________________________________________________________
457 | Cross Compilation for Target System |
458 | _____ _____ _____ ____ ______ _____ |
459 | | CC | -> |AR | -> |CC | -> |CC | -> |AR | -> |CC | |
460 | | *.c | |lib.a| |mruby| |mirb| |core.a| |mrbc | |
461 | ----- ----- ----- ---- ------ ----- |
462 ----------------------------------------------------------------
465 ## Build Configuration Examples
469 To build a minimal mruby library you need to use the Cross Compiling
470 feature due to the reason that there are functions (e.g. stdio) which
471 can't be disabled for the main build.
474 MRuby::CrossBuild.new('Minimal') do |conf|
477 conf.cc.defines = %w(MRB_DISABLE_STDIO)
482 This configuration defines a cross compile build called 'Minimal' which
483 is using the GCC and compiles for the host machine. It also disables
484 all usages of stdio and doesn't compile any binaries (e.g. mrbc).
488 mruby's build process includes a test environment. In case you start the testing
489 of mruby, a native binary called `mrbtest` will be generated and executed.
490 This binary contains all test cases which are defined under *test/t*. In case
491 of a cross-compilation an additional cross-compiled *mrbtest* binary is
492 generated. You can copy this binary and run on your target system.