Skip to content

Commit

Permalink
Copy Native dependencies to Store
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek committed Oct 6, 2022
1 parent fa0fec1 commit 22551b9
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 34 deletions.
151 changes: 117 additions & 34 deletions build/nuke/Build.Steps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using Extensions;
using Nuke.Common;
Expand Down Expand Up @@ -350,41 +351,123 @@ partial class Build
}
});

Target CopyAdditionalDeps => _ => _
.Unlisted()
.Description("Creates AutoInstrumentation.AdditionalDeps and shared store in tracer-home")
.After(CompileManagedSrc)
.Executes(() =>
{
AdditionalDepsDirectory.GlobFiles("**/*deps.json").ForEach(DeleteFile);
Target CopyAdditionalDeps => _ =>
{
return _
.Unlisted()
.Description("Creates AutoInstrumentation.AdditionalDeps and shared store in tracer-home")
.After(CompileManagedSrc)
.Executes(() =>
{
AdditionalDepsDirectory.GlobFiles("**/*deps.json").ForEach(DeleteFile);

DotNetPublish(s => s
.SetProject(Solution.GetProject(Projects.AutoInstrumentationAdditionalDeps))
.SetConfiguration(BuildConfiguration)
.SetTargetPlatformAnyCPU()
.SetProperty("TracerHomePath", TracerHomeDirectory)
.EnableNoBuild()
.EnableNoRestore()
.CombineWith(TestFrameworks.ExceptNetFramework(), (p, framework) => p
.SetFramework(framework)
// Additional-deps probes the directory using SemVer format.
// Example: For netcoreapp3.1 framework, additional-deps uses 3.1.0 or 3.1.1 and so on.
// Major and Minor version are extracted from framework and default value of 0 is appended for patch.
.SetOutput(AdditionalDepsDirectory / "shared" / "Microsoft.NETCore.App" / framework.ToString().Substring(framework.ToString().Length - 3) + ".0")));

AdditionalDepsDirectory.GlobFiles("**/*.dll", "**/*.pdb", "**/*.xml", "**/*.dylib", "**/*.so").ForEach(DeleteFile);
AdditionalDepsDirectory.GlobDirectories("**/runtimes").ForEach(DeleteDirectory);
AdditionalDepsDirectory.GlobFiles("**/*deps.json")
.ForEach(file =>
{
string depsJsonContent = File.ReadAllText(file);
// Remove OpenTelemetry.Instrumentation.AutoInstrumentationAdditionalDeps entry from target section.
depsJsonContent = Regex.Replace(depsJsonContent, "\"OpenTelemetry(.+)AutoInstrumentation.AdditionalDeps.dll(.+?)}," + Environment.NewLine + "(.+?)\"", "\"", RegexOptions.IgnoreCase | RegexOptions.Singleline);
// Remove OpenTelemetry.Instrumentation.AutoInstrumentationAdditionalDeps entry from library section and write to file.
depsJsonContent = Regex.Replace(depsJsonContent, "\"OpenTelemetry(.+?)}," + Environment.NewLine + "(.+?)\"", "\"", RegexOptions.IgnoreCase | RegexOptions.Singleline);
File.WriteAllText(file, depsJsonContent);
});
});
DotNetPublish(s => s
.SetProject(Solution.GetProject(Projects.AutoInstrumentationAdditionalDeps))
.SetConfiguration(BuildConfiguration)
.SetTargetPlatformAnyCPU()
.SetProperty("TracerHomePath", TracerHomeDirectory)
.EnableNoBuild()
.EnableNoRestore()
.CombineWith(TestFrameworks.ExceptNetFramework(), (p, framework) => p
.SetFramework(framework)
// Additional-deps probes the directory using SemVer format.
// Example: For netcoreapp3.1 framework, additional-deps uses 3.1.0 or 3.1.1 and so on.
// Major and Minor version are extracted from framework and default value of 0 is appended for patch.
.SetOutput(AdditionalDepsDirectory / "shared" / "Microsoft.NETCore.App" / framework.ToString().Substring(framework.ToString().Length - 3) + ".0")));


AdditionalDepsDirectory.GlobFiles("**/*deps.json")
.ForEach(file =>
{
var depsJsonContent = File.ReadAllText(file);
CopyNativeDependenciesToStore(file, depsJsonContent);

RemoveOpenTelemetryAutoInstrumentationAdditionalDepsFromDepsFile(depsJsonContent, file);
});
RemoveFilesFromAdditionalDepsDirectory();

void CopyNativeDependenciesToStore(AbsolutePath file, string depsJsonContent)
{
var depsDirectory = file.Parent;
var targetDirectory = Path.Combine(depsDirectory.Parent.Parent.Parent.Parent, "store");
using var jsonDocument = JsonDocument.Parse(depsJsonContent);

var runtimeName = jsonDocument.RootElement.GetProperty("runtimeTarget").GetProperty("name").GetString();
var folderRuntimeName = MapToFolderName(runtimeName);

string MapToFolderName(string runtimeName)
{
switch (runtimeName)
{
case ".NETCoreApp,Version=v3.1":
return "netcoreapp3.1";
case ".NETCoreApp,Version=v6.0":
return "net6.0";
}

throw new ArgumentOutOfRangeException(nameof(runtimeName), runtimeName,
"This value is not supported. You have probably introduced new .NET version to AutoInstrumentation");
}

foreach (var targetProperty in jsonDocument.RootElement.GetProperty("targets").EnumerateObject())
{
var target = targetProperty.Value;

foreach (var packages in target.EnumerateObject())
{
if (packages.Value.TryGetProperty("runtimeTargets", out var runtimeTargets))
{
foreach (var runtimeDependency in runtimeTargets.EnumerateObject())
{
var sourceFileName = Path.Combine(depsDirectory, runtimeDependency.Name);


var targetFileNameX64 = Path.Combine(targetDirectory, "x64", folderRuntimeName,
packages.Name.ToLowerInvariant(), runtimeDependency.Name);
var targetFileNameX86 = Path.Combine(targetDirectory, "x86", folderRuntimeName,
packages.Name.ToLowerInvariant(), runtimeDependency.Name);

var targetDirectoryX64 = Path.GetDirectoryName(targetFileNameX64);
var targetDirectoryX86 = Path.GetDirectoryName(targetFileNameX86);

if (!Directory.Exists(targetDirectoryX64))
{
Directory.CreateDirectory(targetDirectoryX64);
}

File.Copy(sourceFileName, targetFileNameX64);

if (!Directory.Exists(targetDirectoryX86))
{
Directory.CreateDirectory(targetDirectoryX86);
}

File.Copy(sourceFileName, targetFileNameX86);
}
}
}
}
}

void RemoveOpenTelemetryAutoInstrumentationAdditionalDepsFromDepsFile(string depsJsonContent, AbsolutePath file)
{
// Remove OpenTelemetry.Instrumentation.AutoInstrumentationAdditionalDeps entry from target section.
depsJsonContent = Regex.Replace(depsJsonContent,
"\"OpenTelemetry(.+)AutoInstrumentation.AdditionalDeps.dll(.+?)}," + Environment.NewLine + "(.+?)\"", "\"",
RegexOptions.IgnoreCase | RegexOptions.Singleline);
// Remove OpenTelemetry.Instrumentation.AutoInstrumentationAdditionalDeps entry from library section and write to file.
depsJsonContent = Regex.Replace(depsJsonContent, "\"OpenTelemetry(.+?)}," + Environment.NewLine + "(.+?)\"", "\"",
RegexOptions.IgnoreCase | RegexOptions.Singleline);
File.WriteAllText(file, depsJsonContent);
}

void RemoveFilesFromAdditionalDepsDirectory()
{
AdditionalDepsDirectory.GlobFiles("**/*.dll", "**/*.pdb", "**/*.xml", "**/*.dylib", "**/*.so").ForEach(DeleteFile);
AdditionalDepsDirectory.GlobDirectories("**/runtimes").ForEach(DeleteDirectory);
}
});
};

Target InstallDocumentationTools => _ => _
.Description("Installs markdownlint-cli and cspell locally. npm is required")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@
/store/x64/net6.0/mongodb.bson/2.13.3/lib/netstandard2.1/MongoDB.Bson.dll,
/store/x64/net6.0/mongodb.driver.core.extensions.diagnosticsources/1.2.0/lib/netstandard2.0/MongoDB.Driver.Core.Extensions.DiagnosticSources.dll,
/store/x64/net6.0/mongodb.driver.core/2.13.3/lib/netstandard2.1/MongoDB.Driver.Core.dll,
/store/x64/net6.0/mongodb.driver.core/2.13.3/runtimes/win/native/libzstd.dll,
/store/x64/net6.0/mongodb.driver.core/2.13.3/runtimes/win/native/snappy32.dll,
/store/x64/net6.0/mongodb.driver.core/2.13.3/runtimes/win/native/snappy64.dll,
/store/x64/net6.0/mongodb.libmongocrypt/1.2.2/lib/netstandard2.1/MongoDB.Libmongocrypt.dll,
/store/x64/net6.0/mongodb.libmongocrypt/1.2.2/runtimes/linux/native/libmongocrypt.so,
/store/x64/net6.0/mongodb.libmongocrypt/1.2.2/runtimes/osx/native/libmongocrypt.dylib,
/store/x64/net6.0/mongodb.libmongocrypt/1.2.2/runtimes/win/native/mongocrypt.dll,
/store/x64/net6.0/sharpcompress/0.23.0/lib/netstandard2.0/SharpCompress.dll,
/store/x64/netcoreapp3.1/artifact.xml,
/store/x64/netcoreapp3.1/dnsclient/1.4.0/lib/netstandard2.1/DnsClient.dll,
Expand All @@ -91,7 +97,13 @@
/store/x64/netcoreapp3.1/mongodb.bson/2.13.3/lib/netstandard2.1/MongoDB.Bson.dll,
/store/x64/netcoreapp3.1/mongodb.driver.core.extensions.diagnosticsources/1.2.0/lib/netstandard2.0/MongoDB.Driver.Core.Extensions.DiagnosticSources.dll,
/store/x64/netcoreapp3.1/mongodb.driver.core/2.13.3/lib/netstandard2.1/MongoDB.Driver.Core.dll,
/store/x64/netcoreapp3.1/mongodb.driver.core/2.13.3/runtimes/win/native/libzstd.dll,
/store/x64/netcoreapp3.1/mongodb.driver.core/2.13.3/runtimes/win/native/snappy32.dll,
/store/x64/netcoreapp3.1/mongodb.driver.core/2.13.3/runtimes/win/native/snappy64.dll,
/store/x64/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/lib/netstandard2.1/MongoDB.Libmongocrypt.dll,
/store/x64/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/runtimes/linux/native/libmongocrypt.so,
/store/x64/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/runtimes/osx/native/libmongocrypt.dylib,
/store/x64/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/runtimes/win/native/mongocrypt.dll,
/store/x64/netcoreapp3.1/sharpcompress/0.23.0/lib/netstandard2.0/SharpCompress.dll,
/store/x64/netcoreapp3.1/system.diagnostics.diagnosticsource/6.0.0/lib/netstandard2.0/System.Diagnostics.DiagnosticSource.dll,
/store/x64/netcoreapp3.1/system.runtime.compilerservices.unsafe/6.0.0/lib/netcoreapp3.1/System.Runtime.CompilerServices.Unsafe.dll,
Expand All @@ -111,7 +123,13 @@
/store/x86/net6.0/mongodb.bson/2.13.3/lib/netstandard2.1/MongoDB.Bson.dll,
/store/x86/net6.0/mongodb.driver.core.extensions.diagnosticsources/1.2.0/lib/netstandard2.0/MongoDB.Driver.Core.Extensions.DiagnosticSources.dll,
/store/x86/net6.0/mongodb.driver.core/2.13.3/lib/netstandard2.1/MongoDB.Driver.Core.dll,
/store/x86/net6.0/mongodb.driver.core/2.13.3/runtimes/win/native/libzstd.dll,
/store/x86/net6.0/mongodb.driver.core/2.13.3/runtimes/win/native/snappy32.dll,
/store/x86/net6.0/mongodb.driver.core/2.13.3/runtimes/win/native/snappy64.dll,
/store/x86/net6.0/mongodb.libmongocrypt/1.2.2/lib/netstandard2.1/MongoDB.Libmongocrypt.dll,
/store/x86/net6.0/mongodb.libmongocrypt/1.2.2/runtimes/linux/native/libmongocrypt.so,
/store/x86/net6.0/mongodb.libmongocrypt/1.2.2/runtimes/osx/native/libmongocrypt.dylib,
/store/x86/net6.0/mongodb.libmongocrypt/1.2.2/runtimes/win/native/mongocrypt.dll,
/store/x86/net6.0/sharpcompress/0.23.0/lib/netstandard2.0/SharpCompress.dll,
/store/x86/netcoreapp3.1/artifact.xml,
/store/x86/netcoreapp3.1/dnsclient/1.4.0/lib/netstandard2.1/DnsClient.dll,
Expand All @@ -129,7 +147,13 @@
/store/x86/netcoreapp3.1/mongodb.bson/2.13.3/lib/netstandard2.1/MongoDB.Bson.dll,
/store/x86/netcoreapp3.1/mongodb.driver.core.extensions.diagnosticsources/1.2.0/lib/netstandard2.0/MongoDB.Driver.Core.Extensions.DiagnosticSources.dll,
/store/x86/netcoreapp3.1/mongodb.driver.core/2.13.3/lib/netstandard2.1/MongoDB.Driver.Core.dll,
/store/x86/netcoreapp3.1/mongodb.driver.core/2.13.3/runtimes/win/native/libzstd.dll,
/store/x86/netcoreapp3.1/mongodb.driver.core/2.13.3/runtimes/win/native/snappy32.dll,
/store/x86/netcoreapp3.1/mongodb.driver.core/2.13.3/runtimes/win/native/snappy64.dll,
/store/x86/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/lib/netstandard2.1/MongoDB.Libmongocrypt.dll,
/store/x86/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/runtimes/linux/native/libmongocrypt.so,
/store/x86/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/runtimes/osx/native/libmongocrypt.dylib,
/store/x86/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/runtimes/win/native/mongocrypt.dll,
/store/x86/netcoreapp3.1/sharpcompress/0.23.0/lib/netstandard2.0/SharpCompress.dll,
/store/x86/netcoreapp3.1/system.diagnostics.diagnosticsource/6.0.0/lib/netstandard2.0/System.Diagnostics.DiagnosticSource.dll,
/store/x86/netcoreapp3.1/system.runtime.compilerservices.unsafe/6.0.0/lib/netcoreapp3.1/System.Runtime.CompilerServices.Unsafe.dll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@
/store/x64/net6.0/mongodb.bson/2.13.3/lib/netstandard2.1/MongoDB.Bson.dll,
/store/x64/net6.0/mongodb.driver.core.extensions.diagnosticsources/1.2.0/lib/netstandard2.0/MongoDB.Driver.Core.Extensions.DiagnosticSources.dll,
/store/x64/net6.0/mongodb.driver.core/2.13.3/lib/netstandard2.1/MongoDB.Driver.Core.dll,
/store/x64/net6.0/mongodb.driver.core/2.13.3/runtimes/win/native/libzstd.dll,
/store/x64/net6.0/mongodb.driver.core/2.13.3/runtimes/win/native/snappy32.dll,
/store/x64/net6.0/mongodb.driver.core/2.13.3/runtimes/win/native/snappy64.dll,
/store/x64/net6.0/mongodb.libmongocrypt/1.2.2/lib/netstandard2.1/MongoDB.Libmongocrypt.dll,
/store/x64/net6.0/mongodb.libmongocrypt/1.2.2/runtimes/linux/native/libmongocrypt.so,
/store/x64/net6.0/mongodb.libmongocrypt/1.2.2/runtimes/osx/native/libmongocrypt.dylib,
/store/x64/net6.0/mongodb.libmongocrypt/1.2.2/runtimes/win/native/mongocrypt.dll,
/store/x64/net6.0/sharpcompress/0.23.0/lib/netstandard2.0/SharpCompress.dll,
/store/x64/netcoreapp3.1/artifact.xml,
/store/x64/netcoreapp3.1/dnsclient/1.4.0/lib/netstandard2.1/DnsClient.dll,
Expand All @@ -91,7 +97,13 @@
/store/x64/netcoreapp3.1/mongodb.bson/2.13.3/lib/netstandard2.1/MongoDB.Bson.dll,
/store/x64/netcoreapp3.1/mongodb.driver.core.extensions.diagnosticsources/1.2.0/lib/netstandard2.0/MongoDB.Driver.Core.Extensions.DiagnosticSources.dll,
/store/x64/netcoreapp3.1/mongodb.driver.core/2.13.3/lib/netstandard2.1/MongoDB.Driver.Core.dll,
/store/x64/netcoreapp3.1/mongodb.driver.core/2.13.3/runtimes/win/native/libzstd.dll,
/store/x64/netcoreapp3.1/mongodb.driver.core/2.13.3/runtimes/win/native/snappy32.dll,
/store/x64/netcoreapp3.1/mongodb.driver.core/2.13.3/runtimes/win/native/snappy64.dll,
/store/x64/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/lib/netstandard2.1/MongoDB.Libmongocrypt.dll,
/store/x64/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/runtimes/linux/native/libmongocrypt.so,
/store/x64/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/runtimes/osx/native/libmongocrypt.dylib,
/store/x64/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/runtimes/win/native/mongocrypt.dll,
/store/x64/netcoreapp3.1/sharpcompress/0.23.0/lib/netstandard2.0/SharpCompress.dll,
/store/x64/netcoreapp3.1/system.diagnostics.diagnosticsource/6.0.0/lib/netstandard2.0/System.Diagnostics.DiagnosticSource.dll,
/store/x64/netcoreapp3.1/system.runtime.compilerservices.unsafe/6.0.0/lib/netcoreapp3.1/System.Runtime.CompilerServices.Unsafe.dll,
Expand All @@ -111,7 +123,13 @@
/store/x86/net6.0/mongodb.bson/2.13.3/lib/netstandard2.1/MongoDB.Bson.dll,
/store/x86/net6.0/mongodb.driver.core.extensions.diagnosticsources/1.2.0/lib/netstandard2.0/MongoDB.Driver.Core.Extensions.DiagnosticSources.dll,
/store/x86/net6.0/mongodb.driver.core/2.13.3/lib/netstandard2.1/MongoDB.Driver.Core.dll,
/store/x86/net6.0/mongodb.driver.core/2.13.3/runtimes/win/native/libzstd.dll,
/store/x86/net6.0/mongodb.driver.core/2.13.3/runtimes/win/native/snappy32.dll,
/store/x86/net6.0/mongodb.driver.core/2.13.3/runtimes/win/native/snappy64.dll,
/store/x86/net6.0/mongodb.libmongocrypt/1.2.2/lib/netstandard2.1/MongoDB.Libmongocrypt.dll,
/store/x86/net6.0/mongodb.libmongocrypt/1.2.2/runtimes/linux/native/libmongocrypt.so,
/store/x86/net6.0/mongodb.libmongocrypt/1.2.2/runtimes/osx/native/libmongocrypt.dylib,
/store/x86/net6.0/mongodb.libmongocrypt/1.2.2/runtimes/win/native/mongocrypt.dll,
/store/x86/net6.0/sharpcompress/0.23.0/lib/netstandard2.0/SharpCompress.dll,
/store/x86/netcoreapp3.1/artifact.xml,
/store/x86/netcoreapp3.1/dnsclient/1.4.0/lib/netstandard2.1/DnsClient.dll,
Expand All @@ -129,7 +147,13 @@
/store/x86/netcoreapp3.1/mongodb.bson/2.13.3/lib/netstandard2.1/MongoDB.Bson.dll,
/store/x86/netcoreapp3.1/mongodb.driver.core.extensions.diagnosticsources/1.2.0/lib/netstandard2.0/MongoDB.Driver.Core.Extensions.DiagnosticSources.dll,
/store/x86/netcoreapp3.1/mongodb.driver.core/2.13.3/lib/netstandard2.1/MongoDB.Driver.Core.dll,
/store/x86/netcoreapp3.1/mongodb.driver.core/2.13.3/runtimes/win/native/libzstd.dll,
/store/x86/netcoreapp3.1/mongodb.driver.core/2.13.3/runtimes/win/native/snappy32.dll,
/store/x86/netcoreapp3.1/mongodb.driver.core/2.13.3/runtimes/win/native/snappy64.dll,
/store/x86/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/lib/netstandard2.1/MongoDB.Libmongocrypt.dll,
/store/x86/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/runtimes/linux/native/libmongocrypt.so,
/store/x86/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/runtimes/osx/native/libmongocrypt.dylib,
/store/x86/netcoreapp3.1/mongodb.libmongocrypt/1.2.2/runtimes/win/native/mongocrypt.dll,
/store/x86/netcoreapp3.1/sharpcompress/0.23.0/lib/netstandard2.0/SharpCompress.dll,
/store/x86/netcoreapp3.1/system.diagnostics.diagnosticsource/6.0.0/lib/netstandard2.0/System.Diagnostics.DiagnosticSource.dll,
/store/x86/netcoreapp3.1/system.runtime.compilerservices.unsafe/6.0.0/lib/netcoreapp3.1/System.Runtime.CompilerServices.Unsafe.dll
Expand Down
Loading

0 comments on commit 22551b9

Please sign in to comment.