2 Configuration options for druntime.
4 The default way to configure the runtime is by passing command line arguments
5 starting with `--DRT-` and followed by the option name, e.g. `--DRT-gcopt` to
7 When command line parsing is enabled, command line options starting
8 with `--DRT-` are filtered out before calling main, so the program
9 will not see them. They are still available via `rt_args()`.
11 Configuration via the command line can be disabled by declaring a variable for the
12 linker to pick up before using it's default from the runtime:
15 extern(C) __gshared bool rt_cmdline_enabled = false;
18 Likewise, declare a boolean rt_envvars_enabled to enable configuration via the
19 environment variable `DRT_` followed by the option name, e.g. `DRT_GCOPT`:
22 extern(C) __gshared bool rt_envvars_enabled = true;
25 Setting default configuration properties in the executable can be done by specifying an
26 array of options named `rt_options`:
29 extern(C) __gshared string[] rt_options = [ "gcopt=precise:1 profile:1"];
32 Evaluation order of options is `rt_options`, then environment variables, then command
33 line arguments, i.e. if command line arguments are not disabled, they can override
34 options specified through the environment or embedded in the executable.
36 Copyright: Copyright Digital Mars 2014.
37 License: Distributed under the
38 $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
39 (See accompanying file LICENSE)
40 Authors: Rainer Schuetze
41 Source: $(DRUNTIMESRC rt/_config.d)
46 // put each variable in its own COMDAT by making them template instances
47 template rt_envvars_enabled()
49 extern(C) pragma(mangle, "rt_envvars_enabled") __gshared bool rt_envvars_enabled = false;
51 template rt_cmdline_enabled()
53 extern(C) pragma(mangle, "rt_cmdline_enabled") __gshared bool rt_cmdline_enabled = true;
57 extern(C) pragma(mangle, "rt_options") __gshared string[] rt_options = [];
60 import core.stdc.ctype : toupper;
61 import core.stdc.stdlib : getenv;
62 import core.stdc.string : strlen;
64 extern extern(C) string[] rt_args() @nogc nothrow @system;
66 alias rt_configCallBack = string delegate(string) @nogc nothrow;
69 * get a druntime config option using standard configuration options
70 * opt name of the option to retrieve
71 * dg if non-null, passes the option through this
72 * delegate and only returns its return value if non-null
73 * reverse reverse the default processing order cmdline/envvar/rt_options
74 * to allow overwriting settings in the delegate with values
75 * from higher priority
77 * returns the options' value if
78 * - set on the command line as "--DRT-<opt>=value" (rt_cmdline_enabled enabled)
79 * - the environment variable "DRT_<OPT>" is set (rt_envvars_enabled enabled)
80 * - rt_options[] contains an entry "<opt>=value"
83 string rt_configOption(string opt, scope rt_configCallBack dg = null, bool reverse = false) @nogc nothrow
88 string s = (reverse ? rt_linkOption(opt, dg) : rt_cmdlineOption(opt, dg));
91 s = rt_envvarsOption(opt, dg);
94 s = (reverse ? rt_cmdlineOption(opt, dg) : rt_linkOption(opt, dg));
98 string rt_cmdlineOption(string opt, scope rt_configCallBack dg) @nogc nothrow
100 if (rt_cmdline_enabled!())
107 if (a.length >= opt.length + 7 && a[0..6] == "--DRT-" &&
108 a[6 .. 6 + opt.length] == opt && a[6 + opt.length] == '=')
110 string s = dg(a[7 + opt.length .. $]);
119 string rt_envvarsOption(string opt, scope rt_configCallBack dg) @nogc nothrow
121 if (rt_envvars_enabled!())
123 if (opt.length >= 32)
127 var[0 .. 4] = "DRT_";
129 var[4 + i] = cast(char) toupper(c);
130 var[4 + opt.length] = 0;
132 auto p = getenv(var.ptr);
135 string s = dg(cast(string) p[0 .. strlen(p)]);
143 string rt_linkOption(string opt, scope rt_configCallBack dg) @nogc nothrow
145 foreach (a; rt_options!())
147 if (a.length > opt.length && a[0..opt.length] == opt && a[opt.length] == '=')
149 string s = dg(a[opt.length + 1 .. $]);