Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
configure options at link time
Browse files Browse the repository at this point in the history
  • Loading branch information
rainers committed Oct 22, 2014
1 parent 46c8da1 commit 7d12df1
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions src/gc/config.d
Expand Up @@ -7,25 +7,27 @@

module gc.config;

// The default way to confige the GC is by passing command line argument --DRT-gcopt to
// The default way to configure the GC is by passing command line argument --DRT-gcopt to
// the executable, use --DRT-gcopt=help for a list of available options.
//
// Configuration can be disabled by building this module with version noinitGCFromCommandline
// and linking it with your executable:
// dmd -version=noinitGCFromCommandline main.d /path/to/druntime/src/gc/config.d
// Configuration via the command line can be disabled by declaring a variable for the
// linker to pick up before using it's defult from the runtime:
//
// If you want to allow configuration by an environment variable DRT_GCOPT aswell, compile
// gc.config with version initGCFromEnvironment:
// dmd -version=initGCFromEnvironment main.d /path/to/druntime/src/gc/config.d

//version = initGCFromEnvironment; // read settings from environment variable DRT_GCOPT
version(noinitGCFromCommandline) {} else
version = initGCFromCommandLine; // read settings from command line argument "--DRT-gcopt=options"

version(initGCFromEnvironment)
version = configurable;
version(initGCFromCommandLine)
version = configurable;
// extern(C) __gshared bool drt_cmdline_enabled = false;
//
// Likewise, declare a boolean drt_envvars_enabled to enable configuration via the
// environment variable DRT_GCOPT:
//
// extern(C) __gshared bool drt_envvars_enabled = true;
//
// Setting default configuration properties in the executable can be done by specifying an
// array of options named drt_args:
//
// extern(C) __gshared string[] drt_args = [ "gcopt=precise=1 profile=1"];
//
// Evaluation order of options is drt_args, then environment variables, then command
// line arguments, i.e. if command line arguments are not disabled, they can override
// options specified through the environment or embedded in the executable.

import core.stdc.stdlib;
import core.stdc.stdio;
Expand All @@ -35,6 +37,20 @@ import core.vararg;

extern extern(C) string[] rt_args();

// put each variable in its own COMDAT by making them template instances
template drt_envvars_enabled()
{
pragma(mangle,"drt_envvars_enabled") extern(C) __gshared bool drt_envvars_enabled = false;
}
template drt_cmdline_enabled()
{
pragma(mangle,"drt_cmdline_enabled") extern(C) __gshared bool drt_cmdline_enabled = true;
}
template drt_args()
{
pragma(mangle,"drt_args") extern(C) __gshared string[] drt_args = [];
}

struct Config
{
bool disable; // start disabled
Expand All @@ -49,14 +65,20 @@ struct Config

bool initialize(...) // avoid inlining
{
version(initGCFromEnvironment)
foreach (a; drt_args!())
{
if(a.length >= 6 && a[0..6] == "gcopt=")
if (!parseOptions(a[6 .. $]))
return false;
}
if(drt_envvars_enabled!())
{
auto p = getenv("DRT_GCOPT");
if (p)
if (!parseOptions(p[0 .. strlen(p)]))
return false;
}
version(initGCFromCommandLine)
if(drt_cmdline_enabled!())
{
foreach (a; rt_args)
{
Expand All @@ -68,8 +90,6 @@ struct Config
return true;
}

version (configurable):

string help() @nogc
{
return "GC options are specified as white space separated assignments:
Expand Down

0 comments on commit 7d12df1

Please sign in to comment.