Debian Codegraph @

vendor/mruby-source/doc/guides/compile.md

raw
1 # Compile
2
3 mruby uses Rake to compile and cross-compile all libraries and
4 binaries.
5
6 ## Prerequisites
7
8 To compile mruby out of the source code you need the following tools:
9
10 - C Compiler (e.g. `gcc` or `clang`)
11 - Linker (e.g. `gcc` or `clang`)
12 - Archive utility (e.g. `ar`)
13 - Ruby 2.5 or later (e.g. `ruby` or `jruby`)
14
15 Optional:
16
17 - Git (to update mruby source and integrate mrbgems easier)
18 - C++ compiler (to use mrbgems which include `*.cpp`, `*.cxx`, `*.cc`)
19 - Bison (to compile `mrbgems/mruby-compiler/core/parse.y`)
20 - gperf (to compile `mrbgems/mruby-compiler/core/keywords`)
21
22 Note that `bison` bundled with macOS is too old to compile `mruby`.
23 Try `brew install bison` and follow the instruction shown to update
24 the `$PATH` to compile `mruby`. We also encourage you to upgrade `ruby`
25 on macOS in similar manner.
26
27 ## Build
28
29 To compile `mruby` with the default build configuration, just invoke `rake`
30 inside of the mruby source root. To generate and execute the test tools call
31 `rake test`. To clean all build files call `rake clean`. To see full command
32 line on build, call `rake -v`.
33
34 You can specify your own configuration file by the `MRUBY_CONFIG` environment
35 variable (you can use `CONFIG` for shorthand for `MRUBY_CONFIG`). If the path
36 doesn't exist, `build_config/${MRUBY_CONFIG}.rb` is used. The default
37 configuration is defined in the `build_config/default.rb` file.
38
39 Those build configuration files contain the build configuration of mruby, for
40 example:
41
42 ```ruby
43 MRuby::Build.new do |conf|
44 conf.toolchain :gcc
45 end
46 ```
47
48 All tools necessary to compile mruby can be set or modified here.
49
50 ## Build Configuration
51
52 We wish you submit a pull-request to `build_config/PLATFORM.rb`, once you
53 created a new configuration for a new platform.
54
55 Inside the configuration file, the following options can be
56 configured based on your environment.
57
58 ### Toolchains
59
60 The mruby build system already contains a set of toolchain templates which
61 configure the build environment for specific compiler infrastructures.
62
63 #### GCC
64
65 Toolchain configuration for the GNU C Compiler.
66
67 ```ruby
68 conf.toolchain :gcc
69 ```
70
71 #### clang
72
73 Toolchain configuration for the LLVM C Compiler clang. Mainly equal to the
74 GCC toolchain.
75
76 ```ruby
77 conf.toolchain :clang
78 ```
79
80 #### Visual Studio 2010, 2012 and 2013
81
82 Toolchain configuration for Visual Studio on Windows. If you use the
83 [Visual Studio Command Prompt](https://msdn.microsoft.com/en-us/library/ms229859\(v=vs.110\).aspx),
84 you normally do not have to specify this manually, since it gets automatically detected by our build process.
85
86 ```ruby
87 conf.toolchain :visualcpp
88 ```
89
90 #### Android
91
92 Toolchain configuration for Android.
93
94 ```ruby
95 conf.toolchain :android
96 ```
97
98 Requires the custom standalone Android NDK and the toolchain path
99 in `ANDROID_STANDALONE_TOOLCHAIN`.
100
101 ### Binaries
102
103 It is possible to select which tools should be compiled during the compilation
104 process. For example,
105
106 - `mruby`
107 - `mirb`
108
109 The configuration are done via `mrbgems`. See `Mrbgems` section.
110
111 ### File Separator
112
113 Some environments require a different file separator character. It is possible to
114 set the character via `conf.file_separator`.
115
116 ```ruby
117 conf.file_separator = '/'
118 ```
119
120 ### C Compiler
121
122 Configuration of the C compiler binary, flags and include paths.
123
124 ```ruby
125 conf.cc do |cc|
126 cc.command = ...
127 cc.flags = ...
128 cc.include_paths = ...
129 cc.defines = ...
130 cc.option_include_path = ...
131 cc.option_define = ...
132 cc.compile_options = ...
133 end
134 ```
135
136 C Compiler has header searcher to detect installed library.
137
138 If you need an include path of header file use `search_header_path`:
139
140 ```ruby
141 # Searches `iconv.h`.
142 # If found it will return include path of the header file.
143 # Otherwise it will return nil.
144 fail 'iconv.h not found' unless conf.cc.search_header_path 'iconv.h'
145 ```
146
147 If you need a full file name of header file use `search_header`:
148
149 ```ruby
150 # Searches `iconv.h`.
151 # If found it will return full path of the header file.
152 # Otherwise it will return nil.
153 iconv_h = conf.cc.search_header 'iconv.h'
154 print "iconv.h found: #{iconv_h}\n"
155 ```
156
157 Header searcher uses compiler's `include_paths` by default.
158 When you are using GCC toolchain (including clang toolchain since its base is gcc toolchain)
159 it will use compiler specific include paths too. (For example `/usr/local/include`, `/usr/include`)
160
161 If you need a special header search paths define a singleton method `header_search_paths` to C compiler:
162
163 ```ruby
164 def conf.cc.header_search_paths
165 ['/opt/local/include'] + include_paths
166 end
167 ```
168
169 ### Linker
170
171 Configuration of the Linker binary, flags and library paths.
172
173 ```ruby
174 conf.linker do |linker|
175 linker.command = ...
176 linker.flags = ...
177 linker.flags_before_libraries = ...
178 linker.libraries = ...
179 linker.flags_after_libraries = ...
180 linker.library_paths = ...
181 linker.option_library = ...
182 linker.option_library_path = ...
183 linker.link_options = ...
184 end
185 ```
186
187 ### Archiver
188
189 Configuration of the Archiver binary and flags.
190
191 ```ruby
192 conf.archiver do |archiver|
193 archiver.command = ...
194 archiver.archive_options = ...
195 end
196 ```
197
198 ### Parser Generator
199
200 Configuration of the Parser Generator binary and flags.
201
202 ```ruby
203 conf.yacc do |yacc|
204 yacc.command = ...
205 yacc.compile_options = ...
206 end
207 ```
208
209 ### GPerf
210
211 Configuration of the GPerf binary and flags.
212
213 ```ruby
214 conf.gperf do |gperf|
215 gperf.command = ...
216 gperf.compile_options = ...
217 end
218 ```
219
220 ### File Extensions
221
222 ```ruby
223 conf.exts do |exts|
224 exts.object = ...
225 exts.executable = ...
226 exts.library = ...
227 end
228 ```
229
230 ### Preallocated Symbols
231
232 By far, preallocated symbols are highly compatible with the previous versions, so
233 we expect you won't see any problem with them. But just in case you face any
234 issue, you can disable preallocated symbols by specifying `conf.disable_presym`.
235
236 In the build process, `mrbc` under cross compiling environment will be compiled
237 with this configuration.
238
239 ### Mrbgems
240
241 `mruby` comes with the (sort of) packaging system named `mrbgems`. To
242 specify `gem`, you can use `conf.gem` in the configuration file.
243
244 ```ruby
245 # Integrate a bundled Gem you see in `mrbgems` directory
246 conf.gem :core => 'mruby-something'
247
248 # Integrate a Gem from GitHub
249 conf.gem :github => 'someone/mruby-another'
250
251 # Integrate a mruby binary Gem
252 conf.gem :core => 'mruby-bin-mruby'
253
254 # Integrate a interactive mruby binary Gem
255 conf.gem :core => 'mruby-bin-mirb'
256
257 # Integrate GemBox (set of Gems)
258 conf.gembox "default"
259 ```
260
261 A GemBox is a set of Gems defined in `mrbgems/default.gembox` for example.
262 It's just a set of `mrbgem` configurations.
263
264 There is a `RubyGem` (gem for CRuby) named `mgem` that help you to
265 manage `mrbgems`. Try `gem install mgem`. `mgem` can show you the list
266 of registered `mrbgems`.
267
268 See [doc/guides/mrbgems.md](mrbgems.md) for more option about mrbgems.
269
270 ### Mrbtest
271
272 Configuration Mrbtest build process.
273
274 If you want `mrbtest.a` only, You should set `conf.build_mrbtest_lib_only`
275
276 ```ruby
277 conf.build_mrbtest_lib_only
278 ```
279
280 ### Bintest
281
282 Tests for mrbgem tools using CRuby.
283 To have bintests place `*.rb` scripts to `bintest/` directory of mrbgems.
284 See `mruby-bin-*/bintest/*.rb` if you need examples.
285 If you want a temporary files use `tempfile` module of CRuby instead of `/tmp/`.
286
287 You can enable it with following:
288
289 ```ruby
290 conf.enable_bintest
291 ```
292
293 ### C++ ABI
294
295 By default, mruby uses setjmp/longjmp to implement its
296 exceptions. But it doesn't release C++ stack object
297 correctly. To support mrbgems written in C++, mruby can be
298 configured to use C++ exception.
299
300 There are two levels of C++ exception handling. The one is
301 `enable_cxx_exception` that enables C++ exception, but
302 uses C ABI. The other is `enable_cxx_abi` where all
303 files are compiled by C++ compiler.
304
305 When you mix C++ code, C++ exception would be enabled automatically.
306 If you need to enable C++ exception explicitly add the following:
307
308 ```ruby
309 conf.enable_cxx_exception
310 ```
311
312 #### C++ exception disabling
313
314 If your compiler does not support C++, and you want to ensure
315 you don't use mrbgem written in C++, you can explicitly disable
316 C++ exception, add following:
317
318 ```ruby
319 conf.disable_cxx_exception
320 ```
321
322 and you will get an error when you try to use C++ gem.
323 Note that it must be called before `enable_cxx_exception` or `gem` method.
324
325 ### Debugging mode
326
327 To enable debugging mode add the following:
328
329 ```ruby
330 conf.enable_debug
331 ```
332
333 When debugging mode is enabled
334
335 - Macro `MRB_DEBUG` would be defined.
336 - Which means `mrb_assert()` macro is enabled.
337 - Debug information of irep would be generated by `mrbc`.
338 - Because `-g` flag would be added to `mrbc` runner.
339 - You can have better backtrace of mruby scripts with this.
340
341 ## Cross-Compilation
342
343 mruby can also be cross-compiled from one platform to another. To achieve
344 cross-compilation, the build configuration needs to contain an instance of
345 `MRuby::CrossBuild`. This instance defines the compilation tools and flags
346 for the target platform. An example could look like this:
347
348 ```ruby
349 MRuby::CrossBuild.new('32bit') do |conf|
350 conf.toolchain :gcc
351
352 conf.cc.flags << "-m32"
353 conf.linker.flags << "-m32"
354 end
355 ```
356
357 All configuration options of `MRuby::Build` can also be used in
358 `MRuby::CrossBuild`. You can find examples under the `build_config`
359 directory.
360
361 ### Mrbtest in Cross-Compilation
362
363 In cross compilation, you can run `mrbtest` on an emulator if
364 you have it by changing configuration of test runner.
365
366 ```ruby
367 conf.test_runner do |t|
368 t.command = ... # set emulator. this value must be non nil or false
369 t.flags = ... # set flags of emulator
370
371 def t.run(bin) # override `run` if you need to change the behavior of it
372 ... # `bin` is the full path of mrbtest
373 end
374 end
375 ```
376
377 ## Build process
378
379 During the build process the `build` directory will be created in the
380 root directory. The structure of this directory will look like this:
381
382 ```
383 +- build
384 |
385 +- host
386 |
387 +- LEGAL <- License description
388 |
389 +- bin <- Binaries (mirb, mrbc and mruby)
390 |
391 +- lib <- Libraries (libmruby.a and libmruby_core.a)
392 |
393 +- mrbc <- Minimal mrbc place
394 |
395 +- mrbgems <- Compilation result from mrbgems
396 |
397 +- mrblib <- Compilation result from mrblib
398 |
399 +- src <- Compilation result from C sources
400 ```
401
402 The compilation workflow will look like this:
403
404 - compile minimal `mrbc` from `src` and `mrblib` sources
405 - compile all files under `src` (object files will be stored in `build/host/mrbc/src`)
406 - compile `mruby-compiler` gem
407 - create `build/host/mrbc/lib/libmruby_core.a` out of all object files (C only)
408 - create `build/host/mrbc/bin/mrbc` via `mruby-bin-mrbc` gem
409 - compile all files under `src` and store result in `build/host/src`
410 - create `build/host/mrblib/mrblib.c` by compiling all `*.rb` files under `mrblib` with `build/host/mrbc/bin/mrbc`
411 - compile `build/host/mrblib/mrblib.c` to `build/host/mrblib/mrblib.o`
412 - create `build/host/lib/libmruby.a` out of all object files (C and Ruby)
413 - compile (normal) mrbgems specified in the configuration file
414 - create `build/host/lib/libmruby.a` from object files from gems and `libmruby_core.a`
415 - create binary commands according to binary gems (e.g. `mirb` and `mruby`)
416 - copy binaries under `build/host/bin` to `bin` directory
417
418 ```
419 _____ _____ ______ ____ ____ _____ _____ ____
420 | CC |->|GEN |->|AR |->|CC |->|CC |->|AR |->|CC |->|CC |
421 | *.c | |y.tab| |core.a| |mrbc| |*.rb| |lib.a| |mruby| |mirb|
422 ----- ----- ------ ---- ---- ----- ----- ----
423 ```
424
425 ### Cross-Compilation
426
427 In case of a cross-compilation to `i386` the `build` directory structure looks
428 like this:
429
430 ```
431 +- build
432 |
433 +- host
434 | |
435 | +- bin <- Native Binaries
436 | |
437 | +- lib <- Native Libraries
438 | |
439 | +- mrbgems
440 | |
441 | +- src
442 |
443 +- i386
444 |
445 +- bin <- Cross-compiled Binaries
446 |
447 +- include <- Header Directory
448 |
449 +- lib <- Cross-compiled Libraries
450 |
451 +- mrbgems
452 |
453 +- mrblib
454 |
455 +- src
456 ```
457
458 An extra directory is created for the target platform. In case you
459 compile for `i386` a directory called `i386` is created under the
460 build directory.
461
462 The cross compilation workflow starts in the same way as the normal
463 compilation by compiling all *native* libraries and binaries, except
464 for we don't have `host/mrbc` directory (`host` directory itself works
465 as placeholder for `mrbc`). Afterwards the cross compilation process
466 proceeds like this:
467
468 - cross-compile all files under `src` and store result in `build/i386/src`
469 - create `build/i386/lib/libmruby_core.a` out of C object files
470 - create `build/i386/mrblib/mrblib.c` by compiling all `*.rb` files under `mrblib` with native `build/host/bin/mrbc`
471 - cross-compile `build/i386/mrblib/mrblib.c` to `build/i386/mrblib/mrblib.o`
472 - create `build/i386/lib/libmruby.a` from object files from gems and `libmruby_core.a`
473 - create binary commands according to binary gems (e.g. `mirb` and `mruby`)
474 - copy binaries under `build/host/bin` to `bin` directory
475
476 ```
477 _______________________________________________________________
478 | Native Compilation for Host System |
479 | _____ ______ _____ ____ ____ _____ |
480 | | CC | -> |AR | -> |GEN | -> |CC | -> |CC | -> |AR | |
481 | | *.c | |core.a| |y.tab| |mrbc| |*.rb| |lib.a| |
482 | ----- ------ ----- ---- ---- ----- |
483 ---------------------------------------------------------------
484 ||
485 \||/
486 \/
487 ________________________________________________________________
488 | Cross Compilation for Target System |
489 | _____ _____ _____ ____ ______ _____ |
490 | | CC | -> |AR | -> |CC | -> |CC | -> |AR | -> |CC | |
491 | | *.c | |lib.a| |mruby| |mirb| |core.a| |mrbc | |
492 | ----- ----- ----- ---- ------ ----- |
493 ----------------------------------------------------------------
494 ```
495
496 ## Build Configuration Examples
497
498 ### Minimal Library
499
500 To build a minimal mruby library you need to use the Cross Compiling
501 feature due to the reason that there are functions (e.g. stdio) which
502 can't be disabled for the main build.
503
504 ```ruby
505 MRuby::CrossBuild.new('minimal') do |conf|
506 conf.toolchain :gcc
507 conf.cc.defines << 'MRB_NO_STDIO'
508 end
509 ```
510
511 This configuration defines a cross compile build called 'minimal' which
512 is using the GCC and compiles for the host machine. It also disables
513 all usages of stdio and doesn't compile any binaries (e.g. `mrbc`).
514
515 ## Test Environment
516
517 mruby's build process includes a test environment. In case you start the testing
518 of mruby, a native binary called `mrbtest` will be generated and executed.
519 This binary contains all test cases which are defined under `test/t`. In case
520 of a cross-compilation an additional cross-compiled `mrbtest` binary is
521 generated. You can copy this binary and run on your target system.
522
523 ## Embedding `mruby` in Your Application
524
525 After the build, you will get `libmruby.a`. You can link it to your application.
526
527 For compiler options and library path, you can use `mruby-config` command for
528 convenience. `mruby-config` command prints the configuration used for `libmruby.a`.
529
530 ```console
531 $ mruby-config --help
532 Usage: mruby-config [switches]
533 switches:
534 --cc print compiler name
535 --cflags print flags passed to compiler
536 --ld print linker name
537 --ldflags print flags passed to linker
538 --ldflags-before-libs print flags passed to linker before linked libraries
539 --libs print linked libraries
540 --libmruby-path print libmruby path
541 --help print this help
542 ```
543
544 For example, when you have a C source file (`c.c`) and try to
545 compile and link it with `libmruby.a`, you can run the following command,
546
547 ```
548 `mruby-config --cc --cflags` c.c `mruby-config --ldflags --libs`
549 ```
550
551 When you use `make`, add following lines in `Makefile`
552
553 ```
554 MRB_CONFIG = <path-to-mruby-config>
555 CFLAGS = `$(MRB_CONFIG) --cflags`
556 LDFLAGS = `$(MRB_CONFIG) --ldflags`
557 LIBS = `$(MRB_CONFIG) --libs`
558 ```
559
560 ## Install
561
562 To install the files in the `bin`, `include` and `lib` directories generated by the "host" build target into a system directory, do the following:
563
564 ```console
565 $ rake install
566 ```
567
568 If there are multiple build targets in the build configuration file, to install the products of all build targets, do the following:
569
570 ```console
571 $ rake install:full
572 ```
573
574 To install only one of several build targets, e.g., the "its-mine" build target, do the following:
575
576 ```console
577 $ rake install:full:its-mine
578 ```
579
580 To install only the executable files, do the following:
581
582 ```console
583 $ rake install_bin # only "host" build target
584 $ rake install:bin # all build targets
585 $ rake install:bin:its-mine # only "its-mine" build target
586 ```
587
588 ### Installation Directory
589
590 The installation directory is `/usr/local` for the "host" build target and `/usr/local/mruby/<build-name>` for the others.
591 To change them, you can set the environment variable `PREFIX` or use `MRuby::Build#install_prefix = dir` in your build configuration file.
592
593 The `PREFIX` environment variable affects all build targets and changes the `/usr/local` part.
594
595 The `MRuby::Build#install_prefix` can be set for each individual build target.
596 In this case, the environment variable `PREFIX` is ignored.
597
598 Also, if the environment variable `DESTDIR` is set, it will prepend to the path obtained by `install_prefix` to determine the final write directory.
599 This is intended for temporary file expansion by the user's package work.
600
601 ---
602
603 To summarize:
604
605 - The default value of the environment variable `PREFIX` is `/usr/local`.
606 - For the "host" build target, the default value of `MRuby::Build#install_prefix` is `<PREFIX>`.
607 - For a build target other than "host", the default value of `MRuby::Build#install_prefix` is `<PREFIX>/mruby/<build-name>`.
608 - If the environment variable `DESTDIR` is set, the actual write directory is `<DESTDIR>/<MRuby::Build#install_prefix>`.
609
610 ## Tips
611
612 - If you see compilation troubles, try `rake clean` first.