243c83a1dd33c04149372297c0c897f65c7baaf4
[nit.git] / share / man / nitc.md
1 # NAME
2
3 nitc - compiles Nit programs.
4
5
6 # SYNOPSIS
7
8 nitc [*options*] FILE...
9
10 nitc [*options*] --run FILE [ARG]...
11
12
13 # DESCRIPTION
14
15 nitc is the current official Nit compiler.
16 It takes the main module of a Nit program as argument and produces an executable file.
17
18 By default, the generated executables are produced in the current directory.
19 (see `--dir` for details.)
20
21 Internally, nitc rely on the presence of a C compiler. Usually gcc (but nitc was successfully tested with clang).
22 A compilation directory is therefore created and (re-)used.
23 By default, the compilation directory is named `nit_compile` and is removed after the compilation.
24 (see `--compile-dir` for details.)
25
26 Currently, because Nit is still in heavy development, the compilation directory is not cleaned after the compilation.
27
28 By default, the compilation process tries to have a good trade-off between the compilation time and the performance of produced executables.
29 To produce more optimized executables, the current best option is `--semi-global`.
30
31 To improve the compilation time and simplify the compilation of multiple programs, more than one file can be given.
32 Each one will be compiled into a distinct executable.
33
34     $ nitc prog1.nit prog2.nit
35
36 To combine files into a single program, use the `-m` option.
37
38     $ nitc prog1.nit -m other_module.nit
39
40 nitc can produces executables for various platforms when specific modules are used.
41 Currently, android and emscripten are supported.
42 See the documentation of these specific modules for details.
43
44
45 # OPTIONS
46
47 ## MESSAGES
48
49 ### `-W`, `--warn`
50 Show additional warnings (advices).
51
52 By default, only important warnings are displayed.
53 May be overridden by `-w`.
54
55 Important warnings are displayed by default. A warning is considered important when:
56
57 * There is a simple correction.
58 * There is no reason to let the code this way.
59 * There is always a real issue (no false positive).
60
61 Other warnings, called advices, are not displayed by default to avoid filling the terminal with
62 unwanted information.
63 A warning is considered an advice when:
64
65 * The correction could be complex. e.g. require a refactorisation or an API change.
66 * The correction cannot be done. e.g. Code that use a deprecated API for some compatibility reason.
67 * There is not a real issue (false positive). Note that this should be unlikely.
68 * Transitional: While a real important warning, it fires a lot in current code, so a transition is needed
69 in order to let people fix them before promoting the advice to an important warning.
70
71 ### `-w`, `--warning`
72 Show/hide a specific warning.
73
74 Each type of warning can be individually displayed or hidden.
75 The `-w` option takes the name of a warning (displayed at the end of the warning message, between parentheses) to activate it;
76 and "no-{name}" to disable it.
77 It has precedence over -q and -W.
78 Multiple `-w` can be given.
79
80 To show only `missing-doc` warnings in standard"
81
82     $ nitc -q -w missing-doc standard
83
84 To show all warnings and advices, except `missing-doc`:
85
86     $ nitc -W -w no-missing-doc standard
87
88 To show important warnings except `useless-type-test`, but not advice except `missing-doc`:
89
90     $ nitc -w missing-doc -w no-useless-type-test standard
91
92 ### `-q`, `--quiet`
93 Do not show warnings.
94 May be overridden by `-w`
95
96 ### `--stop-on-first-error`
97 Just display the first encountered error then stop.
98
99 By default, nitc tries to detect and display more than one error before aborting the compilation.
100
101 ### `--no-color`
102 Do not use color to display errors and warnings.
103
104 Also, do not echo the line.
105 This options is mainly used by scripts and tools that need parsable error messages.
106
107 ### `-v`, `--verbose`
108 Additional messages from the tool.
109 Multiple `-v` can be given to improve the verbosity.
110
111 With one `-v`, there is constant number of lines.
112 With two `-v`, the number of lines is proportional to the number of modules.
113 With three `-v`, the number of lines is proportional to the number of definition of classes.
114 With four `-v`, the number of lines is proportional to the number of definition of properties.
115
116 ### `--log`
117 Generate various log files.
118
119 The tool will generate some files in the logging directory (see `--log-dir`).
120 These files are intended to the advanced user and the developers of the tools.
121
122 ### `--log-dir`
123 Directory where to generate log files.
124
125 By default the directory is called `logs` in the working directory.
126
127
128 ### `-h`, `-?`, `--help`
129 Show Help (the list of options).
130
131 ### `--version`
132 Show version and exit.
133
134
135 ## PATHS
136
137 ### `-I`, `--path`
138 Add an additional include path.
139
140 This option is used to indicate an additional path of a directory containing Nit libraries.
141
142 The path added with `-I` are searched before those added by the environment variable `NIT_PATH`.
143
144 May be used more than once.
145
146 ### `-o`, `--output`
147 Filename of the generated executable.
148
149 Indicates the path and name of the produced executable.
150
151 Note: it is better to use `--dir` if only the directory is important.
152 This way, the platform extension will be correctly set.
153
154 `-o` is not usable if multiple programs are compiled at once.
155
156 ### `--dir`
157 Output directory.
158
159 Produce the executables in the given directory instead of the current directory.
160
161 ### `--nit-dir`
162 Base directory of the Nit installation.
163
164 Has precedence over the environment variable `NIT_DIR`.
165
166 ## COMPILATION
167
168 ### `--run`
169 Execute the binary after the compilation.
170
171 The binary is generated as expected then it is executed directly.
172 After the execution, the binary is not removed.
173
174 When `--run` is used, the first argument is the program to compile, the rest of the arguments are the arguments of the program.
175 Note that you MUST use `--` before the program arguments if one of them is an option starting with a `-`.
176
177 ~~~bash
178 $ nitc --run foo.nit arg       # compile foo.nit, then executes `./foo arg`
179 $ nitc --run foo.nit arg -W    # compile foo.nit with warnings, then executes `./foo arg`
180 $ nitc --run foo.nit -- arg -W # compile foo.nit, then executes `./foo arg -W`
181 ~~~
182
183 ### `--compile-dir`
184 Directory used to generate temporary files.
185
186 By default, it is named `nit_compile` and created in the current directory and destroyed after the compilation.
187
188 If the option `--compile_dir` or `--no-cc` is used, then the directory is not destroyed and let as is.
189
190 ### `--no-cc`
191 Do not invoke the C compiler.
192
193 Files in the compilation directory are generated but the C compiler is not invoked.
194
195 This option is mainly used to produce C files distributable then compilable on system that do not have a Nit compiler (e.g. embedded system).
196 In this case, it is suggested to also use the options `--dir`, `--compile-dir` and `--semi-global`.
197
198     $ nitc examples/hello_world.nit --no-cc --dir hello --compile-dir hello --semi-global
199
200 Will produce a `hello` directory that contains the required C files to finish the compilation.
201 Only the C files required for the program are generated.
202 The final binary will be generated in the same directory.
203
204 Note that, to be useful, the compilation directory is not destroyed when `--no-cc` is used.
205
206 ### `-m`, `--mixin`
207 Additional module to mix-in.
208
209 Additional modules are imported and refine the main module of the program.
210 This has basically the same effect than implementing a specific module that imports the main module of the program then each one of the mix-in modules.
211 May be used more than once.
212
213 This is option is used to weave additional behaviors to existing programs.
214 Modules designated to bring features to programs by refining basic or specialized services, without any intervention of the main program, are good candidates to be used with the `-m` option.
215 E.g. `hash_debug`.
216
217 An other usage of the `-m` option is to compile program to a specific platform. E.g. `emscripten`  or `android`.
218
219 A last usage is to develop programs as product lines with a main basic module (vanilla) and specific distinct features as flavor modules, then to combine them at compile-time.
220
221     $ nitc prog_vanilla.nit -m feature_chocolate.nit -m feature_cherry.nit
222
223 ### `-D`, `--define`
224 Define a specific property.
225
226 The `-D` option allows to refine a top-level method at compile-time.
227 This has basically the same effect than implementing a specific module that imports the main module of the program and refines the designated methods.
228
229 The designated method must be top-level function with no parameters that returns a Bool, an Int or a String.
230
231 The argument of the `-D` option is "{name}={value}".
232 For Bool, the argument can also be just "{name}", in this case, the value is considered to be `true`.
233
234     $ nitc foo.nit -D prefix=/opt/foo -D port=8080 -D with_ssl
235
236 ### `--release`
237 Compile in release mode and finalize application.
238
239 Currently, this only affect the android platform.
240
241 ### `-g`, `--debug`
242 Compile in debug mode.
243
244 Currently removes gcc optimizations.
245 Also preserves the source-files directory for C-debuggers.
246
247 For more debugging-related options, see also `--hardening` and `NIT_GC_OPTION`
248
249 ## COMPILATION MODES
250
251 ### `nitc` includes distinct compilation modes.
252
253 ### `--separate`
254 Use separate compilation (default mode).
255
256 In separate compilation, modules are compiled independently of their programs.
257 This makes the recompilation of programs faster since only the modified files need to be recompiled.
258
259 ### `--global`
260 Use global compilation.
261
262 The produced executables may become huge and the compilation time is prohibitive.
263 But sometime, they are faster.
264
265 In practice, `--semi-global` produces nearly as fast but smaller executables.
266
267 ### `--erasure`
268 Erase generic types.
269
270 Like `--separate` but use an erasure dynamic typing policy for generics and virtual types.
271 Usually you do not need this, even if you understand the previous sentence.
272
273
274 ## SEMI-GLOBAL OPTIMIZATIONS
275
276 In `--separate` and in `--erasure` modes, some optimization can be gained by relaxing the constraint about
277 the independence on programs.
278
279 Therefore, with these options, the produced executables may be faster and smaller but the recompilation time
280 will increase.
281
282 ### `--semi-global`
283 Enable all semi-global optimizations.
284
285 ### `--rta`
286 Activate RTA (Rapid Type Analysis).
287
288 This option only make sense in `--erasure` to enable some semi-global optimizations.
289
290 RTA is implicitly enabled in `--separate` and `--global`.
291
292 ### `--inline-coloring-numbers`
293 Inline colors and ids (semi-global).
294
295 ### `--inline-some-methods`
296 Allow the separate compiler to inline some methods (semi-global).
297 Need `--rta`.
298
299 ### `--direct-call-monomorph`
300 Allow the separate compiler to direct call monomorphic sites (semi-global).
301 Need `--rta`.
302
303 ### `--direct-call-monomorph0`
304 Allow the separate compiler to direct call monomorphic sites (semi-global).
305 Need `--rta`.
306
307 The difference with the non-zero option is internal:
308 with this option, the monomorphism is looked-at on the mmethod level and not at the callsite level.
309
310 ### `--skip-dead-methods`
311 Do not compile dead methods (semi-global).
312 Need `--rta`.
313
314 ## LINK-BOOST OPTIMIZATIONS
315
316 In `--separate` and in `--erasure` modes, some optimization can be gained by hijacking the linker process.
317
318 Warning: these optimisations are not portable since they use extra features of the GNU linker `ld`.
319 However, there is very few reasons to not use them if GNU `ld` is available.
320
321 ### `--link-boost`
322 Enable all link-boost optimizations.
323
324 ### `--colors-are-symbols`
325 Store colors as symbols instead of static data.
326
327 By default, the various identifiers used to implement OO-mechanisms are stored as genuine constant static variables.
328
329 This option uses linker symbols to encode these identifiers.
330 This makes the compiled program faster since less indirections are required to get the values.
331 It also produces executables that are a little bit smaller since static memory does not have to store the colors.
332
333 ### `--substitute-monomorph`
334 Replace monomorphic trampolines with direct calls.
335
336 Late-binding is implemented with *trampolines*, that are small functions that just select and jump the to right implementations.
337 If, at link-time, is it known that the target will always by the same implementation then all calls to the trampoline are replaced by
338 direct calls to this single implementation.
339
340 Note that using trampolines as indirection slows down the executable.
341 However, it is expected that the gain of monomorphic direct-calls overcompensates the additional indirections in polymorphic trampoline-calls.
342
343 Note: automatically enable option `--trampoline-call`.
344
345 ## POTENTIAL OPTIMIZATIONS
346
347 These optimisation are not enabled by default as they are counter-effective in most cases.
348
349 ### `--guard-call`
350 Guard VFT calls with a direct call.
351
352 ### `--type-poset`
353 Build a poset of types to create more condensed tables.
354
355 The drawback is that more time and memory are used by the compilation process.
356
357
358 ## DANGEROUS OPTIMIZATIONS
359
360 The following optimizations disable runtime checks.
361 It means that correct (non-buggy) programs may be slightly faster.
362 It also means that incorrect (buggy) programs may have unspecified behaviors
363 (e.g. formatting your hard drive or killing your cat).
364
365 In fact, these options are mainly used to bench the compilation results.
366
367 ### `--no-check-all`
368 Disable all tests (dangerous).
369
370 ### `--no-check-covariance`
371 Disable type tests of covariant parameters (dangerous).
372
373 ### `--no-check-attr-isset`
374 Disable isset tests before each attribute access (dangerous).
375
376 ### `--no-check-assert`
377 Disable the evaluation of explicit `assert` and `as` (dangerous).
378
379 ### `--no-check-autocast`
380 Disable implicit casts on unsafe expression usage (dangerous).
381
382 ### `--no-check-null`
383 Disable tests of null receiver (dangerous).
384
385 ### `--no-check-erasure-cast`
386 Disable implicit casts on unsafe return with erasure-typing policy (dangerous).
387
388
389 ## UNOPTIMIZATIONS
390
391 These options are used to debug or to bench the compilation results.
392 Usually you do not need them since they make the generated code slower.
393
394 ### `--no-shortcut-range`
395 Always instantiate a range and its iterator on 'for' loops.
396
397 ### `--no-union-attribute`
398 Put primitive attributes in a box instead of an union.
399
400 ### `--no-shortcut-equal`
401 Always call == in a polymorphic way.
402
403 ### `--no-tag-primitives`
404 Use only boxes for primitive types.
405
406 The separate compiler uses tagged values to encode common primitive types like Int, Bool and Char.
407 This option disables tags and forces such primitive values to be boxed.
408 The drawback is that each boxing costs a memory allocation thus increases the amount of work for the garbage collector.
409
410 However, in some cases, it is possible that this option improves performance since the absence of tags simplify the implementation
411 of OO mechanisms like method calls or equality tests.
412
413 ### `--no-inline-intern`
414 Do not inline call to intern methods.
415
416 ### `--colo-dead-methods`
417 Force colorization of dead methods.
418
419 ### `--no-gcc-directive`
420 Disable advanced gcc directives for optimization.
421
422 ### `--trampoline-call`
423 Use an indirection when calling.
424
425 Just add the trampolines of `--substitute-monomorph` without doing any additional optimizations.
426
427
428 ### DEBUGGING
429
430 ### `--no-stacktrace`
431 Disable the generation of stack traces.
432
433 With this option, the compiled program will not display stack traces on runtime errors.
434
435 Because stack traces rely on libunwind, this option might be useful in order to generate more portable binaries
436 since libunwind might be non available on the runtime system (or available with an ABI incompatible version).
437
438 The generated C is API-portable and can be reused, distributed and compiled on any supported system.
439 If the option `--no-stacktrace` is not used but the development files of the library `libunwind` are not available, then a warning will be displayed
440 and stack trace will be disabled.
441
442 Note that the `--no-stacktrace` option (or this absence) can be toggled manually in the generated Makefile (search `NO_STACKTRACE` in the Makefile).
443 Moreover, the environment variable `NIT_NO_STACK` (see bellow) can also be used at runtime to disable stack traces.
444
445 ### `--trace-memory`
446 Enable dynamic measure of memory usage.
447
448 Compiled programs will generate a large `memory.log` file that logs all memory allocations.
449 This logs file can then be analyzed with the tool `memplot` from contrib.
450
451 ### `--hardening`
452 Generate contracts in the C code against bugs in the compiler.
453
454
455 ## INTERNAL OPTIONS
456
457 These options can be used to control the fine behavior of the tool.
458 They are useless for a normal user.
459
460 ### `--disable-phase`
461 Disable a specific phase; use `list` to get the list.
462
463 ### `--only-parse`
464 Only proceed to parse files.
465
466 ### `--only-metamodel`
467 Stop after meta-model processing.
468
469 ### `--ignore-visibility`
470 Do not check, and produce errors, on visibility issues.
471
472 ### `--no-main`
473 Do not generate main entry point.
474
475 ### `--max-c-lines`
476 Maximum number of lines in generated C files. Use 0 for unlimited.
477
478 ### `--group-c-files`
479 Group all generated code in the same series of files.
480
481 ### `--make-flags`
482 Additional options to the `make` command.
483
484       $ nitc foo.nit --make-flags 'CC=clang' --make-flags 'CFLAGS="-O0 -g"'
485
486 ### `--typing-test-metrics`
487 Enable static and dynamic count of all type tests.
488
489 ### `--invocation-metrics`
490 Enable static and dynamic count of all method invocations.
491
492 ### `--isset-checks-metrics`
493 Enable static and dynamic count of isset checks before attributes access.
494
495 ### `--tables-metrics`
496 Enable static size measuring of tables used for vft, typing and resolution.
497
498 ### `--set-dummy-tool`
499 Set toolname and version to DUMMY. Useful for testing.
500
501 ### `--bash-completion`
502 Generate bash_completion file for this program.
503
504 ### `--stub-man`
505 Generate a stub manpage in pandoc markdown format.
506
507 ### `--keep-going`
508 Continue after errors, whatever the consequences.
509
510 The tool does not stop after some errors but continue until it produces incorrect result, crashes, erases the hard drive, or just continue forever in an infinite loop.
511 This option is used to test the robustness of the tools by allowing phases to progress on incorrect data.
512
513 ### `--sloppy`
514 Force lazy semantic analysis of the source-code.
515
516 Analysis of methods is thus done only when required.
517 This option breaks the behavior of most of the tools since errors in methods are undetected until the method is required in some processing.
518
519 # ENVIRONMENT VARIABLES
520
521 ### `NIT_DIR`
522 Base directory of the Nit installation.
523
524 When the `NIT_DIR` environment variable is set then it specifies the path of the Nit install directory.
525
526 This directory is used to locate binaries, shared files and the common libraries.
527
528 When unset, the directory is guessed according to some heuristic.
529
530 The `--nit-dir` option also set the base directory of the Nit installation but has precedence.
531
532 ### `NIT_PATH`
533 Additional include paths.
534
535 The `NIT_PATH` environment variable contains paths of directories containing Nit libraries.
536 Each path is separated with a column (`:`).
537
538 The `-I` option also add additional paths.
539
540 ### `NIT_GC_OPTION`
541 Runtime control of the garbage collector.
542
543 The behavior of the GC of the executables produced by nitc can be tuned with this environment variable.
544
545 The environment variable is used when programs are executed, not when they are compiled.
546 Thus, you do not need to recompile programs in order to tweak their GC options.
547
548 Available values are:
549
550 * boehm: use the Boehm-Demers-Weiser's conservative garbage collector (default).
551 * malloc: disable the GC and just use `malloc` without doing any `free`.
552 * large: disable the GC and just allocate a large memory area to use for all instantiation.
553 * help: show the list of available options.
554
555 ### `NIT_NO_STACK`
556 Runtime control of stack traces.
557
558 By default, stack traces are printed when a runtime errors occurs during the execution of a compiled program.
559 When setting this environment variable to a non empty value, such stack traces are disabled.
560
561 The environment variable is used when programs are executed, not when they are compiled.
562 Thus, you do not need to recompile programs in order to disable generated stack traces.
563
564 Note that stack traces require that, during the compilation, development files of the library `libunwind` are available.
565 If they are not available, then programs are compiled without any stack trace support.
566
567 To completely disable stack traces, see the option `--no-stacktrace`.
568
569 # SEE ALSO
570
571 The Nit language documentation and the source code of its tools and libraries may be downloaded from <http://nitlanguage.org>