This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathBundler.cs
280 lines (233 loc) · 10.4 KB
/
Bundler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// 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 Microsoft.NET.HostModel.AppHost;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Reflection.PortableExecutable;
namespace Microsoft.NET.HostModel.Bundle
{
/// <summary>
/// Bundler: Functionality to embed the managed app and its dependencies
/// into the host native binary.
/// </summary>
public class Bundler
{
readonly string HostName;
readonly string OutputDir;
readonly bool EmbedPDBs;
readonly string DepsJson;
readonly string RuntimeConfigJson;
readonly string RuntimeConfigDevJson;
readonly Trace trace;
public readonly Manifest BundleManifest;
/// <summary>
/// Align embedded assemblies such that they can be loaded
/// directly from memory-mapped bundle.
///
/// TBD: Determine if this is the minimum required alignment
/// on all platforms and assembly types (and customize the padding accordingly).
/// </summary>
public const int AssemblyAlignment = 4096;
public static string Version => (Manifest.MajorVersion + "." + Manifest.MinorVersion);
public Bundler(string hostName, string outputDir, bool embedPDBs = false, bool diagnosticOutput = false)
{
HostName = hostName;
OutputDir = Path.GetFullPath(string.IsNullOrEmpty(outputDir) ? Environment.CurrentDirectory : outputDir);
string baseName = Path.GetFileNameWithoutExtension(HostName);
DepsJson = baseName + ".deps.json";
RuntimeConfigJson = baseName + ".runtimeconfig.json";
RuntimeConfigDevJson = baseName + ".runtimeconfig.dev.json";
EmbedPDBs = embedPDBs;
trace = new Trace(diagnosticOutput);
BundleManifest = new Manifest();
}
/// <summary>
/// Embed 'file' into 'bundle'
/// </summary>
/// <returns>Returns the offset of the start 'file' within 'bundle'</returns>
long AddToBundle(Stream bundle, Stream file, bool shouldAlign)
{
if (shouldAlign)
{
long misalignment = (bundle.Position % AssemblyAlignment);
if (misalignment != 0)
{
long padding = AssemblyAlignment - misalignment;
bundle.Position += padding;
}
}
file.Position = 0;
long startOffset = bundle.Position;
file.CopyTo(bundle);
return startOffset;
}
bool ShouldEmbed(string fileRelativePath)
{
if (fileRelativePath.Equals(HostName))
{
// The bundle starts with the host, so ignore it while embedding.
return false;
}
if (fileRelativePath.Equals(RuntimeConfigDevJson))
{
// Ignore the machine specific configuration file.
return false;
}
if (Path.GetExtension(fileRelativePath).ToLower().Equals(".pdb"))
{
return EmbedPDBs;
}
return true;
}
FileType InferType(string fileRelativePath, Stream file)
{
if (fileRelativePath.Equals(DepsJson))
{
return FileType.DepsJson;
}
if (fileRelativePath.Equals(RuntimeConfigJson))
{
return FileType.RuntimeConfigJson;
}
try
{
PEReader peReader = new PEReader(file);
CorHeader corHeader = peReader.PEHeaders.CorHeader;
if (corHeader != null)
{
return ((corHeader.Flags & CorFlags.ILOnly) != 0) ? FileType.IL : FileType.Ready2Run;
}
}
catch (BadImageFormatException)
{
}
return FileType.Other;
}
/// <summary>
/// Generate a bundle, given the specification of embedded files
/// </summary>
/// <param name="fileSpecs">
/// An enumeration FileSpecs for the files to be embedded.
/// </param>
/// <returns>
/// The full path the the generated bundle file
/// </returns>
/// <exceptions>
/// ArgumentException if input is invalid
/// IOExceptions and ArgumentExceptions from callees flow to the caller.
/// </exceptions>
public string GenerateBundle(IReadOnlyList<FileSpec> fileSpecs)
{
trace.Log($"Bundler version {Version}");
if (fileSpecs.Any(x => !x.IsValid()))
{
throw new ArgumentException("Invalid input specification: Found entry with empty source-path or bundle-relative-path.");
}
string hostSource;
try
{
hostSource = fileSpecs.Where(x => x.BundleRelativePath.Equals(HostName)).Single().SourcePath;
}
catch (InvalidOperationException)
{
throw new ArgumentException("Invalid input specification: Must specify the host binary");
}
if (fileSpecs.GroupBy(file => file.BundleRelativePath).Where(g => g.Count() > 1).Any())
{
throw new ArgumentException("Invalid input specification: Found multiple entries with the same BundleRelativePath");
}
string bundlePath = Path.Combine(OutputDir, HostName);
if (File.Exists(bundlePath))
{
trace.Log($"Ovewriting existing File {bundlePath}");
}
BinaryUtils.CopyFile(hostSource, bundlePath);
long headerOffset = 0;
using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(bundlePath)))
{
Stream bundle = writer.BaseStream;
bundle.Position = bundle.Length;
// Write the files from the specification into the bundle
// Alignment:
// Assemblies are written aligned at "AssemblyAlignment" bytes to facilitate loading from bundle
// Remaining files (native binaries and other files) are written without alignment.
//
// The unaligned files are written first, followed by the aligned files,
// and finally the bundle manifest.
// TODO: Order file writes to minimize file size.
List<Tuple<FileSpec, FileType>> ailgnedFiles = new List<Tuple<FileSpec, FileType>>();
bool NeedsAlignment(FileType type) => type == FileType.IL || type == FileType.Ready2Run;
foreach (var fileSpec in fileSpecs)
{
if (!ShouldEmbed(fileSpec.BundleRelativePath))
{
trace.Log($"Skip: {fileSpec.BundleRelativePath}");
continue;
}
using (FileStream file = File.OpenRead(fileSpec.SourcePath))
{
FileType type = InferType(fileSpec.BundleRelativePath, file);
if (NeedsAlignment(type))
{
ailgnedFiles.Add(Tuple.Create(fileSpec, type));
continue;
}
long startOffset = AddToBundle(bundle, file, shouldAlign:false);
FileEntry entry = BundleManifest.AddEntry(type, fileSpec.BundleRelativePath, startOffset, file.Length);
trace.Log($"Embed: {entry}");
}
}
foreach (var tuple in ailgnedFiles)
{
var fileSpec = tuple.Item1;
var type = tuple.Item2;
using (FileStream file = File.OpenRead(fileSpec.SourcePath))
{
long startOffset = AddToBundle(bundle, file, shouldAlign: true);
FileEntry entry = BundleManifest.AddEntry(type, fileSpec.BundleRelativePath, startOffset, file.Length);
trace.Log($"Embed: {entry}");
}
}
// Write the bundle manifest
headerOffset = BundleManifest.Write(writer);
trace.Log($"Header Offset={headerOffset}");
trace.Log($"Meta-data Size={writer.BaseStream.Position - headerOffset}");
trace.Log($"Bundle: Path={bundlePath}, Size={bundle.Length}");
}
HostWriter.SetAsBundle(bundlePath, headerOffset);
return bundlePath;
}
string RelativePath(string dirFullPath, string fileFullPath)
{
// This function is used in lieu of Path.GetRelativePath because
// * Path.GetRelativePath() doesn't exist in netstandard2.0
// * This implementation is pretty much only intended for testing.
// SDK integration invokes GenerateBundle(fileSpecs) directly.
//
// In later revisions, we should target netstandard2.1, and replace
// this function with Path.GetRelativePath().
return fileFullPath.Substring(dirFullPath.TrimEnd(Path.DirectorySeparatorChar).Length).TrimStart(Path.DirectorySeparatorChar);
}
/// <summary>
/// Generate a bundle containind the (embeddable) files in sourceDir
/// </summary>
public string GenerateBundle(string sourceDir)
{
// Convert sourceDir to absolute path
sourceDir = Path.GetFullPath(sourceDir);
// Get all files in the source directory and all sub-directories.
string[] sources = Directory.GetFiles(sourceDir, searchPattern: "*", searchOption: SearchOption.AllDirectories);
// Sort the file names to keep the bundle construction deterministic.
Array.Sort(sources, StringComparer.Ordinal);
List<FileSpec> fileSpecs = new List<FileSpec>(sources.Length);
foreach (var file in sources)
{
fileSpecs.Add(new FileSpec(file, RelativePath(sourceDir, file)));
}
return GenerateBundle(fileSpecs);
}
}
}