Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Implement Single-file Bundler #5286

Merged
merged 10 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Microsoft.DotNet.CoreSetup.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Depend
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtils", "src\test\TestUtils\TestUtils.csproj", "{D6676666-D14D-4DFA-88FB-76E3E823E2E1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Build.Bundle", "src\managed\Microsoft.DotNet.Build.Bundle\Microsoft.DotNet.Build.Bundle.csproj", "{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Build.Bundle.Tests", "src\test\Microsoft.DotNet.Build.Bundle.Tests\Microsoft.DotNet.Build.Bundle.Tests.csproj", "{89374278-6DEB-48EF-9AFC-7D1BF340C49D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -115,6 +119,38 @@ Global
{D6676666-D14D-4DFA-88FB-76E3E823E2E1}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{D6676666-D14D-4DFA-88FB-76E3E823E2E1}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{D6676666-D14D-4DFA-88FB-76E3E823E2E1}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.Debug|x64.ActiveCfg = Debug|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.Debug|x64.Build.0 = Debug|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.MinSizeRel|x64.Build.0 = Debug|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.Release|Any CPU.Build.0 = Release|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.Release|x64.ActiveCfg = Release|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.Release|x64.Build.0 = Release|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.Debug|x64.ActiveCfg = Debug|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.Debug|x64.Build.0 = Debug|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.MinSizeRel|x64.Build.0 = Debug|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.Release|Any CPU.Build.0 = Release|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.Release|x64.ActiveCfg = Release|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.Release|x64.Build.0 = Release|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{89374278-6DEB-48EF-9AFC-7D1BF340C49D}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -125,6 +161,8 @@ Global
{23F4AB97-D15C-4C51-A641-DF5C5D5EF70F} = {5CE8410C-3100-4F41-8FA9-E6B4132D9703}
{D86A859D-E6FA-4E73-A255-5776FC473A25} = {5CE8410C-3100-4F41-8FA9-E6B4132D9703}
{D6676666-D14D-4DFA-88FB-76E3E823E2E1} = {5CE8410C-3100-4F41-8FA9-E6B4132D9703}
{6C49FB1B-2C7D-49C5-8471-FFA4861D30DD} = {FAA448DA-7D1C-4481-915D-5765BF906332}
{89374278-6DEB-48EF-9AFC-7D1BF340C49D} = {5CE8410C-3100-4F41-8FA9-E6B4132D9703}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {28B9726D-802B-478D-AF7A-B9243B9E180B}
Expand Down
20 changes: 20 additions & 0 deletions src/managed/Microsoft.DotNet.Build.Bundle/BundleException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace Microsoft.DotNet.Build.Bundle
{
/// <summary>
/// This exception is thrown when a bundle/extraction
/// operation fails due known user errors.
/// </summary>
public class BundleException : Exception
{
public BundleException(string message) :
base(message)
{
}
}
}

220 changes: 220 additions & 0 deletions src/managed/Microsoft.DotNet.Build.Bundle/Bundler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;
using System.Reflection.PortableExecutable;

namespace Microsoft.DotNet.Build.Bundle
{
/// <summary>
/// Bundler: Functionality to embed the managed app and its dependencies
/// into the host native binary.
/// </summary>
public class Bundler
{
string HostName;
string SourceDir;
string OutputDir;
bool EmbedPDBs;

string Application;
string DepsJson;
string RuntimeConfigJson;
string RuntimeConfigDevJson;

/// <summary>
/// Align embedded assemblies such that they can be loaded
/// directly from memory-mapped bundle.
/// TBD: Set the correct value of alignment while working on
/// the runtime changes to load the embedded assemblies.
/// </summary>
const int AssemblyAlignment = 16;

public static string Version => (Manifest.MajorVersion + "." + Manifest.MinorVersion);

public Bundler(string hostName, string sourceDir, string outputDir, bool embedPDBs)
{
SourceDir = sourceDir;
OutputDir = outputDir;
HostName = hostName;
EmbedPDBs = embedPDBs;
}

void ValidateFiles()
{
// Check required directories
if (!Directory.Exists(SourceDir))
{
throw new BundleException("Dirctory not found: " + SourceDir);
}
if (!Directory.Exists(OutputDir))
{
throw new BundleException("Dirctory not found: " + OutputDir);
}

// Set default names
string baseName = Path.GetFileNameWithoutExtension(HostName);
Application = baseName + ".dll";
DepsJson = baseName + ".deps.json";
RuntimeConfigJson = baseName + ".runtimeconfig.json";
RuntimeConfigDevJson = baseName + ".runtimeconfig.dev.json";

// Check that required files exist on disk.
Action<string> checkFileExists = (string name) =>
{
string path = Path.Combine(SourceDir, name);
if (!File.Exists(path))
{
throw new BundleException("File not found: " + path);
}
};

checkFileExists(HostName);
checkFileExists(Application);
// The *.json files may or may not exist.
}

/// <summary>
/// Embed 'file' into 'bundle'
/// </summary>
/// <returns>Returns the offset of the start 'file' within 'bundle'</returns>

long AddToBundle(Stream bundle, Stream file, FileType type = FileType.Extract)
{
// Allign assemblies, since they are loaded directly from bundle
if (type == FileType.Assembly)
{
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;
}

if (fileRelativePath.Equals(Application))
{
return FileType.Application;
}

try
{
PEReader peReader = new PEReader(file);
CorHeader corHeader = peReader.PEHeaders.CorHeader;
if ((corHeader != null) && ((corHeader.Flags & CorFlags.ILOnly) != 0))
{
return FileType.Assembly;
}
}
catch (BadImageFormatException)
{
}

return FileType.Extract;
}

void GenerateBundle()
{
string bundlePath = Path.Combine(OutputDir, HostName);

if (File.Exists(bundlePath))
{
Program.Log($"Ovewriting existing File {bundlePath}");
}

// Start with a copy of the host executable.
// Copy the file to preserve its permissions.
File.Copy(Path.Combine(SourceDir, HostName), bundlePath, overwrite: true);

using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(bundlePath)))
{
Stream bundle = writer.BaseStream;
Manifest manifest = new Manifest();

bundle.Position = bundle.Length;
int sourceDirLen = Path.GetFullPath(SourceDir).Length + 1;

// 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);

foreach (string filePath in sources)
{
// filePath is the full-path of files within source directory, and any of its sub-directories.
// We only need the relative paths with respect to the source directory.
string relativePath = filePath.Substring(sourceDirLen);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: indentation of comments


if (!ShouldEmbed(relativePath))
{
Program.Log($"Skip: {relativePath}");
continue;
}

using (FileStream file = File.OpenRead(filePath))
{
FileType type = InferType(relativePath, file);
long startOffset = AddToBundle(bundle, file, type);
FileEntry entry = new FileEntry(type, relativePath, startOffset, file.Length);
manifest.Files.Add(entry);
Program.Log($"Embed: {entry}");
}
}

manifest.Write(writer);
Program.Log($"Bundle: Path={bundlePath} Size={bundle.Length}");
}
}

public void MakeBundle()
{
ValidateFiles();
GenerateBundle();
}
}
}

76 changes: 76 additions & 0 deletions src/managed/Microsoft.DotNet.Build.Bundle/Extractor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;

namespace Microsoft.DotNet.Build.Bundle
{
/// <summary>
/// Extractor: The functionality to extract the files embedded
/// within a bundle to sepearte files.
/// </summary>
public class Extractor
{
string OutputDir;
string BundlePath;

public Extractor(string bundlePath, string outputDir)
{
BundlePath = bundlePath;
OutputDir = outputDir;
}

public void Spill()
{
try
{
if (!File.Exists(BundlePath))
{
throw new BundleException("File not found: " + BundlePath);
}

using (BinaryReader reader = new BinaryReader(File.OpenRead(BundlePath)))
{
Manifest manifest = Manifest.Read(reader);

foreach (FileEntry entry in manifest.Files)
{
Program.Log($"Spill: {entry}");

string fileRelativePath = entry.RelativePath.Replace(Manifest.DirectorySeparatorChar, Path.DirectorySeparatorChar);
string filePath = Path.Combine(OutputDir, fileRelativePath);
string fileDir = Path.GetDirectoryName(filePath);

if ((fileDir != null) && !fileDir.Equals(String.Empty))
{
Directory.CreateDirectory(fileDir);
}

reader.BaseStream.Position = entry.Offset;
using (BinaryWriter file = new BinaryWriter(File.Create(filePath)))
{
long size = entry.Size;
do
{
int copySize = (int)(size % int.MaxValue);
file.Write(reader.ReadBytes(copySize));
size -= copySize;
} while (size > 0);
}
}
}
}
catch (IOException)
{
throw new BundleException("Malformed Bundle");
}
catch (ArgumentOutOfRangeException)
{
// Trying to set file-stream position to an invalid value
throw new BundleException("Malformed Bundle");
}
}
}
}

Loading