1
/**
2
 * Defines the help texts for the CLI options offered by DMD.
3
 *
4
 * This file is not shared with other compilers which use the DMD front-end.
5
 * However, this file will be used to generate the
6
 * $(LINK2 https://dlang.org/dmd-linux.html, online documentation) and MAN pages.
7
 *
8
 * Copyright:   Copyright (C) 1999-2020 by The D Language Foundation, All Rights Reserved
9
 * Authors:     $(LINK2 http://www.digitalmars.com, Walter Bright)
10
 * License:     $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
11
 * Source:      $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/cli.d, _cli.d)
12
 * Documentation:  https://dlang.org/phobos/dmd_cli.html
13
 * Coverage:    https://codecov.io/gh/dlang/dmd/src/master/src/dmd/cli.d
14
 */
15
module dmd.cli;
16

17
/// Bit decoding of the TargetOS
18
enum TargetOS
19
{
20
    all = int.max,
21
    linux = 1,
22
    windows = 2,
23
    macOS = 4,
24
    freeBSD = 8,
25
    solaris = 16,
26
    dragonFlyBSD = 32,
27
}
28

29
// Detect the current TargetOS
30
version (linux)
31
{
32
    private enum targetOS = TargetOS.linux;
33
}
34
else version(Windows)
35
{
36
    private enum targetOS = TargetOS.windows;
37
}
38
else version(OSX)
39
{
40
    private enum targetOS = TargetOS.macOS;
41
}
42
else version(FreeBSD)
43
{
44
    private enum targetOS = TargetOS.freeBSD;
45
}
46
else version(DragonFlyBSD)
47
{
48
    private enum targetOS = TargetOS.dragonFlyBSD;
49
}
50
else version(Solaris)
51
{
52
    private enum targetOS = TargetOS.solaris;
53
}
54
else
55
{
56
    private enum targetOS = TargetOS.all;
57
}
58

59
/**
60
Checks whether `os` is the current $(LREF TargetOS).
61
For `TargetOS.all` it will always return true.
62

63
Params:
64
    os = $(LREF TargetOS) to check
65

66
Returns: true iff `os` contains the current targetOS.
67
*/
68
bool isCurrentTargetOS(TargetOS os)
69
{
70 0
    return (os & targetOS) > 0;
71
}
72

73
/**
74
Capitalize a the first character of a ASCII string.
75
Params:
76
    w = ASCII i string to capitalize
77
Returns: capitalized string
78
*/
79
static string capitalize(string w)
80
{
81 0
    char[] result = cast(char[]) w;
82 0
    char c1 = w.length ? w[0] : '\0';
83

84 0
    if (c1 >= 'a' && c1 <= 'z')
85
    {
86
        enum adjustment = 'A' - 'a';
87

88 0
        result = new char[] (w.length);
89 0
        result[0] = cast(char) (c1 + adjustment);
90 0
        result[1 .. $] = w[1 .. $];
91
    }
92

93 0
    return cast(string) result;
94
}
95

96
/**
97
Contains all available CLI $(LREF Usage.Option)s.
98

99
See_Also: $(LREF Usage.Option)
100
*/
101
struct Usage
102
{
103
    /**
104
    * Representation of a CLI `Option`
105
    *
106
    * The DDoc description `ddoxText` is only available when compiled with `-version=DdocOptions`.
107
    */
108
    struct Option
109
    {
110
        string flag; /// The CLI flag without leading `-`, e.g. `color`
111
        string helpText; /// A detailed description of the flag
112
        TargetOS os = TargetOS.all; /// For which `TargetOS` the flags are applicable
113

114
        // Needs to be version-ed to prevent the text ending up in the binary
115
        // See also: https://issues.dlang.org/show_bug.cgi?id=18238
116
        version(DdocOptions) string ddocText; /// Detailed description of the flag (in Ddoc)
117

118
        /**
119
        * Params:
120
        *  flag = CLI flag without leading `-`, e.g. `color`
121
        *  helpText = detailed description of the flag
122
        *  os = for which `TargetOS` the flags are applicable
123
        *  ddocText = detailed description of the flag (in Ddoc)
124
        */
125 0
        this(string flag, string helpText, TargetOS os = TargetOS.all)
126
        {
127 0
            this.flag = flag;
128 0
            this.helpText = helpText;
129
            version(DdocOptions) this.ddocText = helpText;
130 0
            this.os = os;
131
        }
132

133
        /// ditto
134 0
        this(string flag, string helpText, string ddocText, TargetOS os = TargetOS.all)
135
        {
136 0
            this.flag = flag;
137 0
            this.helpText = helpText;
138
            version(DdocOptions) this.ddocText = ddocText;
139 0
            this.os = os;
140
        }
141
    }
142

143
    /// Returns all available CLI options
144
    static immutable options = [
145
        Option("allinst",
146
            "generate code for all template instantiations"
147
        ),
148
        Option("betterC",
149
            "omit generating some runtime information and helper functions",
150
            "Adjusts the compiler to implement D as a $(LINK2 $(ROOT_DIR)spec/betterc.html, better C):
151
            $(UL
152
                $(LI Predefines `D_BetterC` $(LINK2 $(ROOT_DIR)spec/version.html#predefined-versions, version).)
153
                $(LI $(LINK2 $(ROOT_DIR)spec/expression.html#AssertExpression, Assert Expressions), when they fail,
154
                call the C runtime library assert failure function
155
                rather than a function in the D runtime.)
156
                $(LI $(LINK2 $(ROOT_DIR)spec/arrays.html#bounds, Array overflows)
157
                call the C runtime library assert failure function
158
                rather than a function in the D runtime.)
159
                $(LI $(LINK2 spec/statement.html#final-switch-statement/, Final switch errors)
160
                call the C runtime library assert failure function
161
                rather than a function in the D runtime.)
162
                $(LI Does not automatically link with phobos runtime library.)
163
                $(UNIX
164
                $(LI Does not generate Dwarf `eh_frame` with full unwinding information, i.e. exception tables
165
                are not inserted into `eh_frame`.)
166
                )
167
                $(LI Module constructors and destructors are not generated meaning that
168
                $(LINK2 $(ROOT_DIR)spec/class.html#StaticConstructor, static) and
169
                $(LINK2 $(ROOT_DIR)spec/class.html#SharedStaticConstructor, shared static constructors) and
170
                $(LINK2 $(ROOT_DIR)spec/class.html#StaticDestructor, destructors)
171
                will not get called.)
172
                $(LI `ModuleInfo` is not generated.)
173
                $(LI $(LINK2 $(ROOT_DIR)phobos/object.html#.TypeInfo, `TypeInfo`)
174
                instances will not be generated for structs.)
175
            )",
176
        ),
177
        Option("boundscheck=[on|safeonly|off]",
178
            "bounds checks on, in @safe only, or off",
179
            `Controls if bounds checking is enabled.
180
                $(UL
181
                    $(LI $(I on): Bounds checks are enabled for all code. This is the default.)
182
                    $(LI $(I safeonly): Bounds checks are enabled only in $(D @safe) code.
183
                                        This is the default for $(SWLINK -release) builds.)
184
                    $(LI $(I off): Bounds checks are disabled completely (even in $(D @safe)
185
                                   code). This option should be used with caution and as a
186
                                   last resort to improve performance. Confirm turning off
187
                                   $(D @safe) bounds checks is worthwhile by benchmarking.)
188
                )`
189
        ),
190
        Option("c",
191
            "compile only, do not link"
192
        ),
193
        Option("check=[assert|bounds|in|invariant|out|switch][=[on|off]]",
194
            "enable or disable specific checks",
195
            `Overrides default, -boundscheck, -release and -unittest options to enable or disable specific checks.
196
                $(UL
197
                    $(LI $(B assert): assertion checking)
198
                    $(LI $(B bounds): array bounds)
199
                    $(LI $(B in): in contracts)
200
                    $(LI $(B invariant): class/struct invariants)
201
                    $(LI $(B out): out contracts)
202
                    $(LI $(B switch): finalswitch failure checking)
203
                )
204
                $(UL
205
                    $(LI $(B on) or not specified: specified check is enabled.)
206
                    $(LI $(B off): specified check is disabled.)
207
                )`
208
        ),
209
        Option("check=[h|help|?]",
210
            "list information on all available checks"
211
        ),
212
        Option("checkaction=[D|C|halt|context]",
213
            "behavior on assert/boundscheck/finalswitch failure",
214
            `Sets behavior when an assert fails, and array boundscheck fails,
215
             or a final switch errors.
216
                $(UL
217
                    $(LI $(B D): Default behavior, which throws an unrecoverable $(D AssertError).)
218
                    $(LI $(B C): Calls the C runtime library assert failure function.)
219
                    $(LI $(B halt): Executes a halt instruction, terminating the program.)
220
                    $(LI $(B context): Prints the error context as part of the unrecoverable $(D AssertError).)
221
                )`
222
        ),
223
        Option("checkaction=[h|help|?]",
224
            "list information on all available check actions"
225
        ),
226
        Option("color",
227
            "turn colored console output on"
228
        ),
229
        Option("color=[on|off|auto]",
230
            "force colored console output on or off, or only when not redirected (default)",
231
            `Show colored console output. The default depends on terminal capabilities.
232
            $(UL
233
                $(LI $(B auto): use colored output if a tty is detected (default))
234
                $(LI $(B on): always use colored output.)
235
                $(LI $(B off): never use colored output.)
236
            )`
237
        ),
238
        Option("conf=<filename>",
239
            "use config file at filename"
240
        ),
241
        Option("cov",
242
            "do code coverage analysis"
243
        ),
244
        Option("cov=ctfe", "Include code executed during CTFE in coverage report"),
245
        Option("cov=<nnn>",
246
            "require at least nnn% code coverage",
247
            `Perform $(LINK2 $(ROOT_DIR)code_coverage.html, code coverage analysis) and generate
248
            $(TT .lst) file with report.)
249
---
250
dmd -cov -unittest myprog.d
251
---
252
            `,
253
        ),
254
        Option("D",
255
            "generate documentation",
256
            `$(P Generate $(LINK2 $(ROOT_DIR)spec/ddoc.html, documentation) from source.)
257
            $(P Note: mind the $(LINK2 $(ROOT_DIR)spec/ddoc.html#security, security considerations).)
258
            `,
259
        ),
260
        Option("Dd<directory>",
261
            "write documentation file to directory",
262
            `Write documentation file to $(I directory) . $(SWLINK -op)
263
            can be used if the original package hierarchy should
264
            be retained`,
265
        ),
266
        Option("Df<filename>",
267
            "write documentation file to filename"
268
        ),
269
        Option("d",
270
            "silently allow deprecated features and symbols",
271
            `Silently allow $(DDLINK deprecate,deprecate,deprecated features) and use of symbols with
272
            $(DDSUBLINK $(ROOT_DIR)spec/attribute, deprecated, deprecated attributes).`,
273
        ),
274
        Option("de",
275
            "issue an error when deprecated features or symbols are used (halt compilation)"
276
        ),
277
        Option("dw",
278
            "issue a message when deprecated features or symbols are used (default)"
279
        ),
280
        Option("debug",
281
            "compile in debug code",
282
            `Compile in $(LINK2 spec/version.html#debug, debug) code`,
283
        ),
284
        Option("debug=<level>",
285
            "compile in debug code <= level",
286
            `Compile in $(LINK2 spec/version.html#debug, debug level) &lt;= $(I level)`,
287
        ),
288
        Option("debug=<ident>",
289
            "compile in debug code identified by ident",
290
            `Compile in $(LINK2 spec/version.html#debug, debug identifier) $(I ident)`,
291
        ),
292
        Option("debuglib=<name>",
293
            "set symbolic debug library to name",
294
            `Link in $(I libname) as the default library when
295
            compiling for symbolic debugging instead of $(B $(LIB)).
296
            If $(I libname) is not supplied, then no default library is linked in.`
297
        ),
298
        Option("defaultlib=<name>",
299
            "set default library to name",
300
            `Link in $(I libname) as the default library when
301
            not compiling for symbolic debugging instead of $(B $(LIB)).
302
            If $(I libname) is not supplied, then no default library is linked in.`,
303
        ),
304
        Option("deps",
305
            "print module dependencies (imports/file/version/debug/lib)"
306
        ),
307
        Option("deps=<filename>",
308
            "write module dependencies to filename (only imports)",
309
            `Without $(I filename), print module dependencies
310
            (imports/file/version/debug/lib).
311
            With $(I filename), write module dependencies as text to $(I filename)
312
            (only imports).`,
313
        ),
314
        Option("extern-std=<standard>",
315
            "set C++ name mangling compatibility with <standard>",
316
            "Standards supported are:
317
            $(UL
318
                $(LI $(I c++98) (default): Use C++98 name mangling,
319
                    Sets `__traits(getTargetInfo, \"cppStd\")` to `199711`)
320
                $(LI $(I c++11): Use C++11 name mangling,
321
                    Sets `__traits(getTargetInfo, \"cppStd\")` to `201103`)
322
                $(LI $(I c++14): Use C++14 name mangling,
323
                    Sets `__traits(getTargetInfo, \"cppStd\")` to `201402`)
324
                $(LI $(I c++17): Use C++17 name mangling,
325
                    Sets `__traits(getTargetInfo, \"cppStd\")` to `201703`)
326
            )",
327
        ),
328
        Option("extern-std=[h|help|?]",
329
            "list all supported standards"
330
        ),
331
        Option("fPIC",
332
            "generate position independent code",
333
            TargetOS.all & ~(TargetOS.windows | TargetOS.macOS)
334
        ),
335
        Option("g",
336
            "add symbolic debug info",
337
            `$(WINDOWS
338
                Add CodeView symbolic debug info. See
339
                $(LINK2 http://dlang.org/windbg.html, Debugging on Windows).
340
            )
341
            $(UNIX
342
                Add symbolic debug info in Dwarf format
343
                for debuggers such as
344
                $(D gdb)
345
            )`,
346
        ),
347
        Option("gf",
348
            "emit debug info for all referenced types",
349
            `Symbolic debug info is emitted for all types referenced by the compiled code,
350
             even if the definition is in an imported file not currently being compiled.`,
351
        ),
352
        Option("gs",
353
            "always emit stack frame"
354
        ),
355
        Option("gx",
356
            "add stack stomp code",
357
            `Adds stack stomp code, which overwrites the stack frame memory upon function exit.`,
358
        ),
359
        Option("H",
360
            "generate 'header' file",
361
            `Generate $(RELATIVE_LINK2 $(ROOT_DIR)interface-files, D interface file)`,
362
        ),
363
        Option("Hd=<directory>",
364
            "write 'header' file to directory",
365
            `Write D interface file to $(I dir) directory. $(SWLINK -op)
366
            can be used if the original package hierarchy should
367
            be retained.`,
368
        ),
369
        Option("Hf=<filename>",
370
            "write 'header' file to filename"
371
        ),
372
        Option("HC",
373
            "generate C++ 'header' file"
374
        ),
375
        Option("HCd=<directory>",
376
            "write C++ 'header' file to directory"
377
        ),
378
        Option("HCf=<filename>",
379
            "write C++ 'header' file to filename"
380
        ),
381
        Option("-help",
382
            "print help and exit"
383
        ),
384
        Option("I=<directory>",
385
            "look for imports also in directory"
386
        ),
387
        Option("i[=<pattern>]",
388
            "include imported modules in the compilation",
389
            q"{$(P Enables "include imports" mode, where the compiler will include imported
390
             modules in the compilation, as if they were given on the command line. By default, when
391
             this option is enabled, all imported modules are included except those in
392
             druntime/phobos. This behavior can be overriden by providing patterns via `-i=<pattern>`.
393
             A pattern of the form `-i=<package>` is an "inclusive pattern", whereas a pattern
394
             of the form `-i=-<package>` is an "exclusive pattern". Inclusive patterns will include
395
             all module's whose names match the pattern, whereas exclusive patterns will exclude them.
396
             For example. all modules in the package `foo.bar` can be included using `-i=foo.bar` or excluded
397
             using `-i=-foo.bar`. Note that each component of the fully qualified name must match the
398
             pattern completely, so the pattern `foo.bar` would not match a module named `foo.barx`.)
399

400
             $(P The default behavior of excluding druntime/phobos is accomplished by internally adding a
401
             set of standard exclusions, namely, `-i=-std -i=-core -i=-etc -i=-object`. Note that these
402
             can be overriden with `-i=std -i=core -i=etc -i=object`.)
403

404
             $(P When a module matches multiple patterns, matches are prioritized by their component length, where
405
             a match with more components takes priority (i.e. pattern `foo.bar.baz` has priority over `foo.bar`).)
406

407
             $(P By default modules that don't match any pattern will be included. However, if at
408
             least one inclusive pattern is given, then modules not matching any pattern will
409
             be excluded. This behavior can be overriden by usig `-i=.` to include by default or `-i=-.` to
410
             exclude by default.)
411

412
             $(P Note that multiple `-i=...` options are allowed, each one adds a pattern.)}"
413
        ),
414
        Option("ignore",
415
            "ignore unsupported pragmas"
416
        ),
417
        Option("inline",
418
            "do function inlining",
419
            `Inline functions at the discretion of the compiler.
420
            This can improve performance, at the expense of making
421
            it more difficult to use a debugger on it.`,
422
        ),
423
        Option("J=<directory>",
424
            "look for string imports also in directory",
425
            `Where to look for files for
426
            $(LINK2 $(ROOT_DIR)spec/expression.html#ImportExpression, $(I ImportExpression))s.
427
            This switch is required in order to use $(I ImportExpression)s.
428
            $(I path) is a ; separated
429
            list of paths. Multiple $(B -J)'s can be used, and the paths
430
            are searched in the same order.`,
431
        ),
432
        Option("L=<linkerflag>",
433
            "pass linkerflag to link",
434
            `Pass $(I linkerflag) to the
435
            $(WINDOWS linker $(OPTLINK))
436
            $(UNIX linker), for example, ld`,
437
        ),
438
        Option("lib",
439
            "generate library rather than object files",
440
            `Generate library file as output instead of object file(s).
441
            All compiled source files, as well as object files and library
442
            files specified on the command line, are inserted into
443
            the output library.
444
            Compiled source modules may be partitioned into several object
445
            modules to improve granularity.
446
            The name of the library is taken from the name of the first
447
            source module to be compiled. This name can be overridden with
448
            the $(SWLINK -of) switch.`,
449
        ),
450
        Option("lowmem",
451
            "enable garbage collection for the compiler",
452
            `Enable the garbage collector for the compiler, reducing the
453
            compiler memory requirements but increasing compile times.`,
454
        ),
455
        Option("m32",
456
            "generate 32 bit code",
457
            `$(UNIX Compile a 32 bit executable. This is the default for the 32 bit dmd.)
458
            $(WINDOWS Compile a 32 bit executable. This is the default.
459
            The generated object code is in OMF and is meant to be used with the
460
            $(LINK2 http://www.digitalmars.com/download/freecompiler.html, Digital Mars C/C++ compiler)).`,
461
            (TargetOS.all & ~TargetOS.dragonFlyBSD)  // available on all OS'es except DragonFly, which does not support 32-bit binaries
462
        ),
463
        Option("m32mscoff",
464
            "generate 32 bit code and write MS-COFF object files",
465
            TargetOS.windows
466
        ),
467
        Option("m64",
468
            "generate 64 bit code",
469
            `$(UNIX Compile a 64 bit executable. This is the default for the 64 bit dmd.)
470
            $(WINDOWS The generated object code is in MS-COFF and is meant to be used with the
471
            $(LINK2 https://msdn.microsoft.com/en-us/library/dd831853(v=vs.100).aspx, Microsoft Visual Studio 10)
472
            or later compiler.`,
473
        ),
474
        Option("main",
475
            "add default main() (e.g. for unittesting)",
476
            `Add a default $(D main()) function when compiling. This is useful when
477
            unittesting a library, as it enables running the unittests
478
            in a library without having to manually define an entry-point function.`,
479
        ),
480
        Option("man",
481
            "open web browser on manual page",
482
            `$(WINDOWS
483
                Open default browser on this page
484
            )
485
            $(LINUX
486
                Open browser specified by the $(B BROWSER)
487
                environment variable on this page. If $(B BROWSER) is
488
                undefined, $(B x-www-browser) is assumed.
489
            )
490
            $(FREEBSD
491
                Open browser specified by the $(B BROWSER)
492
                environment variable on this page. If $(B BROWSER) is
493
                undefined, $(B x-www-browser) is assumed.
494
            )
495
            $(OSX
496
                Open browser specified by the $(B BROWSER)
497
                environment variable on this page. If $(B BROWSER) is
498
                undefined, $(B Safari) is assumed.
499
            )`,
500
        ),
501
        Option("map",
502
            "generate linker .map file",
503
            `Generate a $(TT .map) file`,
504
        ),
505
        Option("mcpu=<id>",
506
            "generate instructions for architecture identified by 'id'",
507
            `Set the target architecture for code generation,
508
            where:
509
            $(DL
510
            $(DT help)$(DD list alternatives)
511
            $(DT baseline)$(DD the minimum architecture for the target platform (default))
512
            $(DT avx)$(DD
513
            generate $(LINK2 https://en.wikipedia.org/wiki/Advanced_Vector_Extensions, AVX)
514
            instructions instead of $(LINK2 https://en.wikipedia.org/wiki/Streaming_SIMD_Extensions, SSE)
515
            instructions for vector and floating point operations.
516
            Not available for 32 bit memory models other than OSX32.
517
            )
518
            $(DT native)$(DD use the architecture the compiler is running on)
519
            )`,
520
        ),
521
        Option("mcpu=[h|help|?]",
522
            "list all architecture options"
523
        ),
524
        Option("mixin=<filename>",
525
            "expand and save mixins to file specified by <filename>"
526
        ),
527
        Option("mscrtlib=<libname>",
528
            "MS C runtime library to reference from main/WinMain/DllMain",
529
            "If building MS-COFF object files with -m64 or -m32mscoff, embed a reference to
530
            the given C runtime library $(I libname) into the object file containing `main`,
531
            `DllMain` or `WinMain` for automatic linking. The default is $(TT libcmt)
532
            (release version with static linkage), the other usual alternatives are
533
            $(TT libcmtd), $(TT msvcrt) and $(TT msvcrtd).
534
            If no Visual C installation is detected, a wrapper for the redistributable
535
            VC2010 dynamic runtime library and mingw based platform import libraries will
536
            be linked instead using the LLD linker provided by the LLVM project.
537
            The detection can be skipped explicitly if $(TT msvcrt120) is specified as
538
            $(I libname).
539
            If $(I libname) is empty, no C runtime library is automatically linked in.",
540
            TargetOS.windows,
541
        ),
542
        Option("mv=<package.module>=<filespec>",
543
            "use <filespec> as source file for <package.module>",
544
            `Use $(I path/filename) as the source file for $(I package.module).
545
            This is used when the source file path and names are not the same
546
            as the package and module hierarchy.
547
            The rightmost components of the  $(I path/filename) and $(I package.module)
548
            can be omitted if they are the same.`,
549
        ),
550
        Option("noboundscheck",
551
            "no array bounds checking (deprecated, use -boundscheck=off)",
552
            `Turns off all array bounds checking, even for safe functions. $(RED Deprecated
553
            (use $(TT $(SWLINK -boundscheck)=off) instead).)`,
554
        ),
555
        Option("O",
556
            "optimize",
557
            `Optimize generated code. For fastest executables, compile
558
            with the $(TT $(SWLINK -O) $(SWLINK -release) $(SWLINK -inline) $(SWLINK -boundscheck)=off)
559
            switches together.`,
560
        ),
561
        Option("o-",
562
            "do not write object file",
563
            `Suppress generation of object file. Useful in
564
            conjuction with $(SWLINK -D) or $(SWLINK -H) flags.`
565
        ),
566
        Option("od=<directory>",
567
            "write object & library files to directory",
568
            `Write object files relative to directory $(I objdir)
569
            instead of to the current directory. $(SWLINK -op)
570
            can be used if the original package hierarchy should
571
            be retained`,
572
        ),
573
        Option("of=<filename>",
574
            "name output file to filename",
575
            `Set output file name to $(I filename) in the output
576
            directory. The output file can be an object file,
577
            executable file, or library file depending on the other
578
            switches.`
579
        ),
580
        Option("op",
581
            "preserve source path for output files",
582
            `Normally the path for $(B .d) source files is stripped
583
            off when generating an object, interface, or Ddoc file
584
            name. $(SWLINK -op) will leave it on.`,
585
        ),
586
        Option("preview=<name>",
587
            "enable an upcoming language change identified by 'name'",
588
            `Preview an upcoming language change identified by $(I id)`,
589
        ),
590
        Option("preview=[h|help|?]",
591
            "list all upcoming language changes"
592
        ),
593
        Option("profile",
594
            "profile runtime performance of generated code"
595
        ),
596
        Option("profile=gc",
597
            "profile runtime allocations",
598
            `$(LINK2 http://www.digitalmars.com/ctg/trace.html, profile)
599
            the runtime performance of the generated code.
600
            $(UL
601
                $(LI $(B gc): Instrument calls to memory allocation and write a report
602
                to the file $(TT profilegc.log) upon program termination.)
603
            )`,
604
        ),
605
        Option("release",
606
            "compile release version",
607
            `Compile release version, which means not emitting run-time
608
            checks for contracts and asserts. Array bounds checking is not
609
            done for system and trusted functions, and assertion failures
610
            are undefined behaviour.`
611
        ),
612
        Option("revert=<name>",
613
            "revert language change identified by 'name'",
614
            `Revert language change identified by $(I id)`,
615
        ),
616
        Option("revert=[h|help|?]",
617
            "list all revertable language changes"
618
        ),
619
        Option("run <srcfile>",
620
            "compile, link, and run the program srcfile",
621
            `Compile, link, and run the program $(I srcfile) with the
622
            rest of the
623
            command line, $(I args...), as the arguments to the program.
624
            No .$(OBJEXT) or executable file is left behind.`
625
        ),
626
        Option("shared",
627
            "generate shared library (DLL)",
628
            `$(UNIX Generate shared library)
629
             $(WINDOWS Generate DLL library)`,
630
        ),
631
        Option("transition=<name>",
632
            "help with language change identified by 'name'",
633
            `Show additional info about language change identified by $(I id)`,
634
        ),
635
        Option("transition=[h|help|?]",
636
            "list all language changes"
637
        ),
638
        Option("unittest",
639
            "compile in unit tests",
640
            `Compile in $(LINK2 spec/unittest.html, unittest) code, turns on asserts, and sets the
641
             $(D unittest) $(LINK2 spec/version.html#PredefinedVersions, version identifier)`,
642
        ),
643
        Option("v",
644
            "verbose",
645
            `Enable verbose output for each compiler pass`,
646
        ),
647
        Option("vcolumns",
648
            "print character (column) numbers in diagnostics"
649
        ),
650
        Option("verror-style=[digitalmars|gnu]",
651
            "set the style for file/line number annotations on compiler messages",
652
            `Set the style for file/line number annotations on compiler messages,
653
            where:
654
            $(DL
655
            $(DT digitalmars)$(DD 'file(line[,column]): message'. This is the default.)
656
            $(DT gnu)$(DD 'file:line[:column]: message', conforming to the GNU standard used by gcc and clang.)
657
            )`,
658
        ),
659
        Option("verrors=<num>",
660
            "limit the number of error messages (0 means unlimited)"
661
        ),
662
        Option("verrors=context",
663
            "show error messages with the context of the erroring source line"
664
        ),
665
        Option("verrors=spec",
666
            "show errors from speculative compiles such as __traits(compiles,...)"
667
        ),
668
        Option("-version",
669
            "print compiler version and exit"
670
        ),
671
        Option("version=<level>",
672
            "compile in version code >= level",
673
            `Compile in $(LINK2 $(ROOT_DIR)spec/version.html#version, version level) >= $(I level)`,
674
        ),
675
        Option("version=<ident>",
676
            "compile in version code identified by ident",
677
            `Compile in $(LINK2 $(ROOT_DIR)spec/version.html#version, version identifier) $(I ident)`
678
        ),
679
        Option("vgc",
680
            "list all gc allocations including hidden ones"
681
        ),
682
        Option("vtls",
683
            "list all variables going into thread local storage"
684
        ),
685
        Option("vtemplates",
686
            "list statistics on template instantiations"
687
        ),
688
        Option("w",
689
            "warnings as errors (compilation will halt)",
690
            `Enable $(LINK2 $(ROOT_DIR)articles/warnings.html, warnings)`
691
        ),
692
        Option("wi",
693
            "warnings as messages (compilation will continue)",
694
            `Enable $(LINK2 $(ROOT_DIR)articles/warnings.html, informational warnings (i.e. compilation
695
            still proceeds normally))`,
696
        ),
697
        Option("X",
698
            "generate JSON file"
699
        ),
700
        Option("Xf=<filename>",
701
            "write JSON file to filename"
702
        ),
703
        Option("Xcc=<driverflag>",
704
            "pass driverflag to linker driver (cc)",
705
            "Pass $(I driverflag) to the linker driver (`$CC` or `cc`)",
706
            TargetOS.all & ~TargetOS.windows
707
        ),
708
    ];
709

710
    /// Representation of a CLI feature
711
    struct Feature
712
    {
713
        string name; /// name of the feature
714
        string paramName; // internal transition parameter name
715
        string helpText; // detailed description of the feature
716
        bool documented = true; // whether this option should be shown in the documentation
717
        bool deprecated_; /// whether the feature is still in use
718
    }
719

720
    /// Returns all available transitions
721
    static immutable transitions = [
722
        Feature("field", "vfield",
723
            "list all non-mutable fields which occupy an object instance"),
724
        Feature("complex", "vcomplex",
725
            "give deprecation messages about all usages of complex or imaginary types"),
726
        Feature("tls", "vtls",
727
            "list all variables going into thread local storage"),
728
        Feature("vmarkdown", "vmarkdown",
729
            "list instances of Markdown replacements in Ddoc"),
730
    ];
731

732
    /// Returns all available reverts
733
    static immutable reverts = [
734
        Feature("dip25", "noDIP25", "revert DIP25 changes https://github.com/dlang/DIPs/blob/master/DIPs/archive/DIP25.md"),
735
    ];
736

737
    /// Returns all available previews
738
    static immutable previews = [
739
        Feature("dip25", "useDIP25",
740
            "implement https://github.com/dlang/DIPs/blob/master/DIPs/archive/DIP25.md (Sealed references)"),
741
        Feature("dip1000", "vsafe",
742
            "implement https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1000.md (Scoped Pointers)"),
743
        Feature("dip1008", "ehnogc",
744
            "implement https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1008.md (@nogc Throwable)"),
745
        Feature("dip1021", "useDIP1021",
746
            "implement https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1021.md (Mutable function arguments)"),
747
        Feature("fieldwise", "fieldwise", "use fieldwise comparisons for struct equality"),
748
        Feature("markdown", "markdown", "enable Markdown replacements in Ddoc"),
749
        Feature("fixAliasThis", "fixAliasThis",
750
            "when a symbol is resolved, check alias this scope before going to upper scopes"),
751
        Feature("intpromote", "fix16997",
752
            "fix integral promotions for unary + - ~ operators"),
753
        Feature("dtorfields", "dtorFields",
754
            "destruct fields of partially constructed objects"),
755
        Feature("rvaluerefparam", "rvalueRefParam",
756
            "enable rvalue arguments to ref parameters"),
757
        Feature("nosharedaccess", "noSharedAccess",
758
            "disable access to shared memory objects"),
759
        Feature("in", "previewIn",
760
            "in means scope const"),
761
    ];
762
}
763

764
/**
765
Formats the `Options` for CLI printing.
766
*/
767
struct CLIUsage
768
{
769
    /**
770
    Returns a string of all available CLI options for the current targetOS.
771
    Options are separated by newlines.
772
    */
773
    static string usage()
774
    {
775
        enum maxFlagLength = 18;
776
        enum s = () {
777
            char[] buf;
778
            foreach (option; Usage.options)
779
            {
780
                if (option.os.isCurrentTargetOS)
781
                {
782
                    buf ~= "  -" ~ option.flag;
783
                    // create new lines if the flag name is too long
784
                    if (option.flag.length >= 17)
785
                    {
786
                            buf ~= "\n                    ";
787
                    }
788
                    else if (option.flag.length <= maxFlagLength)
789
                    {
790
                        const spaces = maxFlagLength - option.flag.length - 1;
791
                        buf.length += spaces;
792
                        buf[$ - spaces .. $] = ' ';
793
                    }
794
                    else
795
                    {
796
                            buf ~= "  ";
797
                    }
798
                    buf ~= option.helpText;
799
                    buf ~= "\n";
800
                }
801
            }
802
            return cast(string) buf;
803
        }();
804 1
        return s;
805
    }
806

807
    /// CPU architectures supported -mcpu=id
808
    enum mcpuUsage = "CPU architectures supported by -mcpu=id:
809
  =[h|help|?]    list information on all available choices
810
  =baseline      use default architecture as determined by target
811
  =avx           use AVX 1 instructions
812
  =avx2          use AVX 2 instructions
813
  =native        use CPU architecture that this compiler is running on
814
";
815

816
    static string generateFeatureUsage(const Usage.Feature[] features, string flagName, string description)
817
    {
818
        enum maxFlagLength = 20;
819 0
        auto buf = description.capitalize ~ " listed by -"~flagName~"=name:
820
";
821 0
        auto allTransitions = [Usage.Feature("all", null,
822
            "Enables all available " ~ description)] ~ features;
823 0
        foreach (t; allTransitions)
824
        {
825 0
            if (t.deprecated_)
826 0
                continue;
827 0
            if (!t.documented)
828 0
                continue;
829 0
            buf ~= "  =";
830 0
            buf ~= t.name;
831 0
            auto lineLength = 3 + t.name.length;
832 0
            foreach (i; lineLength .. maxFlagLength)
833 0
                buf ~= " ";
834 0
            buf ~= t.helpText;
835 0
            buf ~= "\n";
836
        }
837 0
        return buf;
838
    }
839

840
    /// Language changes listed by -transition=id
841
    enum transitionUsage = generateFeatureUsage(Usage.transitions, "transition", "language transitions");
842

843
    /// Language changes listed by -revert
844
    enum revertUsage = generateFeatureUsage(Usage.reverts, "revert", "revertable language changes");
845

846
    /// Language previews listed by -preview
847
    enum previewUsage = generateFeatureUsage(Usage.previews, "preview", "upcoming language changes");
848

849
    /// Options supported by -checkaction=
850
    enum checkActionUsage = "Behavior on assert/boundscheck/finalswitch failure:
851
  =[h|help|?]    List information on all available choices
852
  =D             Usual D behavior of throwing an AssertError
853
  =C             Call the C runtime library assert failure function
854
  =halt          Halt the program execution (very lightweight)
855
  =context       Use D assert with context information (when available)
856
";
857

858
    /// Options supported by -check
859
    enum checkUsage = "Enable or disable specific checks:
860
  =[h|help|?]           List information on all available choices
861
  =assert[=[on|off]]    Assertion checking
862
  =bounds[=[on|off]]    Array bounds checking
863
  =in[=[on|off]]        Generate In contracts
864
  =invariant[=[on|off]] Class/struct invariants
865
  =out[=[on|off]]       Out contracts
866
  =switch[=[on|off]]    Final switch failure checking
867
  =on                   Enable all assertion checking
868
                        (default for non-release builds)
869
  =off                  Disable all assertion checking
870
";
871

872
    /// Options supported by -extern-std
873
    enum externStdUsage = "Available C++ standards:
874
  =[h|help|?]           List information on all available choices
875
  =c++98                Sets `__traits(getTargetInfo, \"cppStd\")` to `199711`
876
  =c++11                Sets `__traits(getTargetInfo, \"cppStd\")` to `201103`
877
  =c++14                Sets `__traits(getTargetInfo, \"cppStd\")` to `201402`
878
  =c++17                Sets `__traits(getTargetInfo, \"cppStd\")` to `201703`
879
";
880
}

Read our documentation on viewing source code .

Loading