diff options
author | Michal Strehovský <MichalStrehovsky@users.noreply.github.com> | 2018-07-17 20:59:53 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-17 20:59:53 +0300 |
commit | 2e533d85ee6c7922c97e2e59945d86d2d696ebcd (patch) | |
tree | e0a818350ca888f562b1f28a113ee05a7368c5ec /src/ILCompiler.Compiler | |
parent | c1e97380862b2281763b34462627a86187f47bea (diff) |
Add support for embedding runtime configuration (#6103)
Runtime configuration (e.g. whether to use server GC) can currently be provided either through environment variables at runtime, or through a RhConfig.ini file placed next to the executable. This adds another channel where we burn a blob similar to RhConfig.ini format into the executable. The order of precedence is environment variable > RhConfig.ini > embedded config.
The `ServerGarbageCollection` project property that is used to determine whether to link with a runtime that supports server GC will be also used to control whether to generate a configuration that enables server GC at runtime by default.
Fixes #6100.
Diffstat (limited to 'src/ILCompiler.Compiler')
4 files changed, 65 insertions, 0 deletions
diff --git a/src/ILCompiler.Compiler/src/Compiler/Compilation.cs b/src/ILCompiler.Compiler/src/Compiler/Compilation.cs index 7df723cb5..6a307aac4 100644 --- a/src/ILCompiler.Compiler/src/Compiler/Compilation.cs +++ b/src/ILCompiler.Compiler/src/Compiler/Compilation.cs @@ -421,6 +421,11 @@ namespace ILCompiler { _graph.AddRoot(_factory.ModuleMetadata(module), reason); } + + public void RootReadOnlyDataBlob(byte[] data, int alignment, string reason, string exportName) + { + _graph.AddRoot(_factory.ReadOnlyDataBlob(exportName, data, alignment), reason); + } } } diff --git a/src/ILCompiler.Compiler/src/Compiler/IRootingServiceProvider.cs b/src/ILCompiler.Compiler/src/Compiler/IRootingServiceProvider.cs index 0895464cc..1a462e9fc 100644 --- a/src/ILCompiler.Compiler/src/Compiler/IRootingServiceProvider.cs +++ b/src/ILCompiler.Compiler/src/Compiler/IRootingServiceProvider.cs @@ -18,5 +18,6 @@ namespace ILCompiler void RootNonGCStaticBaseForType(TypeDesc type, string reason); void RootVirtualMethodForReflection(MethodDesc method, string reason); void RootModuleMetadata(ModuleDesc module, string reason); + void RootReadOnlyDataBlob(byte[] data, int alignment, string reason, string exportName); } } diff --git a/src/ILCompiler.Compiler/src/Compiler/RuntimeConfigurationRootProvider.cs b/src/ILCompiler.Compiler/src/Compiler/RuntimeConfigurationRootProvider.cs new file mode 100644 index 000000000..e4be79fc1 --- /dev/null +++ b/src/ILCompiler.Compiler/src/Compiler/RuntimeConfigurationRootProvider.cs @@ -0,0 +1,58 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; + +namespace ILCompiler +{ + /// <summary> + /// A root provider that provides a runtime configuration blob that influences runtime behaviors. + /// See RhConfigValues.h for allowed values. + /// </summary> + public class RuntimeConfigurationRootProvider : ICompilationRootProvider + { + private readonly IEnumerable<string> _runtimeOptions; + + public RuntimeConfigurationRootProvider(IEnumerable<string> runtimeOptions) + { + _runtimeOptions = runtimeOptions; + } + + void ICompilationRootProvider.AddCompilationRoots(IRootingServiceProvider rootProvider) + { + rootProvider.RootReadOnlyDataBlob(GetRuntimeOptionsBlob(), 4, "Runtime configuration information", "g_compilerEmbeddedSettingsBlob"); + } + + protected byte[] GetRuntimeOptionsBlob() + { + const int HeaderSize = 4; + + ArrayBuilder<byte> options = new ArrayBuilder<byte>(); + + // Reserve space for the header + options.ZeroExtend(HeaderSize); + + foreach (string option in _runtimeOptions) + { + byte[] optionBytes = System.Text.Encoding.ASCII.GetBytes(option); + options.Append(optionBytes); + + // Emit a null to separate the next option + options.Add(0); + } + + byte[] result = options.ToArray(); + + int length = options.Count - HeaderSize; + + // Encode the size of the blob into the header + result[0] = (byte)length; + result[1] = (byte)(length >> 8); + result[2] = (byte)(length >> 0x10); + result[3] = (byte)(length >> 0x18); + + return result; + } + } +} diff --git a/src/ILCompiler.Compiler/src/ILCompiler.Compiler.csproj b/src/ILCompiler.Compiler/src/ILCompiler.Compiler.csproj index 7b86399ea..5c84847c2 100644 --- a/src/ILCompiler.Compiler/src/ILCompiler.Compiler.csproj +++ b/src/ILCompiler.Compiler/src/ILCompiler.Compiler.csproj @@ -333,6 +333,7 @@ <Compile Include="Compiler\ExportsFileWriter.cs" /> <Compile Include="Compiler\PrecomputedMetadataManager.cs" /> <Compile Include="Compiler\ReadyToRun.cs" /> + <Compile Include="Compiler\RuntimeConfigurationRootProvider.cs" /> <Compile Include="Compiler\RyuJitCompilation.cs" /> <Compile Include="Compiler\RyuJitCompilationBuilder.cs" /> <Compile Include="Compiler\SingleFileCompilationModuleGroup.cs" /> |