Skip to content

Commit

Permalink
Remove artifacts compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon committed Feb 13, 2024
1 parent 85bcd2f commit 62466f1
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 93 deletions.
9 changes: 0 additions & 9 deletions src/Neo.Compiler.CSharp/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@ namespace Neo.Compiler
{
public class Options
{
public enum GenerateArtifactsKind
{
None,
Source,
Library,
SourceAndLibrary
}

public string? Output { get; set; }
public string? BaseName { get; set; }
public NullableContextOptions Nullable { get; set; }
public bool Checked { get; set; }
public bool Debug { get; set; }
public bool Assembly { get; set; }
public GenerateArtifactsKind GenerateArtifacts { get; set; } = GenerateArtifactsKind.Source;
public bool NoOptimize { get; set; }
public bool NoInline { get; set; }
public byte AddressVersion { get; set; }
Expand Down
74 changes: 3 additions & 71 deletions src/Neo.Compiler.CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
// modifications are permitted.

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using Neo.IO;
using Neo.Json;
using Neo.Optimizer;
Expand All @@ -21,7 +19,6 @@
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using System.ComponentModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
Expand All @@ -42,7 +39,6 @@ static int Main(string[] args)
new Option<bool>("--checked", "Indicates whether to check for overflow and underflow."),
new Option<bool>(new[] { "-d", "--debug" }, "Indicates whether to generate debugging information."),
new Option<bool>("--assembly", "Indicates whether to generate assembly."),
new Option<Options.GenerateArtifactsKind>("--generate-artifacts", "Instruct the compiler how to generate artifacts."),
new Option<bool>("--no-optimize", "Instruct the compiler not to optimize the code."),
new Option<bool>("--no-inline", "Instruct the compiler not to insert inline code."),
new Option<byte>("--address-version", () => ProtocolSettings.Default.AddressVersion, "Indicates the address version used by the compiler.")
Expand Down Expand Up @@ -181,76 +177,12 @@ private static int ProcessOutputs(Options options, string folder, CompilationCon
return 1;
}
Console.WriteLine($"Created {path}");

if (options.GenerateArtifacts != Options.GenerateArtifactsKind.None)
{
var artifact = manifest.Abi.GetArtifactsSource(baseName);

if (options.GenerateArtifacts == Options.GenerateArtifactsKind.SourceAndLibrary || options.GenerateArtifacts == Options.GenerateArtifactsKind.Source)
{
path = Path.Combine(outputFolder, $"{baseName}.artifacts.cs");
File.WriteAllText(path, artifact);
Console.WriteLine($"Created {path}");
}

if (options.GenerateArtifacts == Options.GenerateArtifactsKind.SourceAndLibrary || options.GenerateArtifacts == Options.GenerateArtifactsKind.Library)
{
try
{
// Try to compile the artifacts into a dll

string coreDir = Path.GetDirectoryName(typeof(object).Assembly.Location)!;

var syntaxTree = CSharpSyntaxTree.ParseText(artifact);
var references = new MetadataReference[]
{
MetadataReference.CreateFromFile(Path.Combine(coreDir, "System.Runtime.dll")),
MetadataReference.CreateFromFile(Path.Combine(coreDir, "System.Runtime.InteropServices.dll")),
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(DisplayNameAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Numerics.BigInteger).Assembly.Location),
MetadataReference.CreateFromFile(typeof(UInt160).Assembly.Location),
MetadataReference.CreateFromFile(typeof(SmartContract.Testing.SmartContract).Assembly.Location)
};

var compilation = CSharpCompilation.Create(baseName, new[] { syntaxTree }, references,
new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary,
optimizationLevel: OptimizationLevel.Release,
platform: Platform.AnyCpu,
deterministic: true
));

using var ms = new MemoryStream();
EmitResult result = compilation.Emit(ms);

if (!result.Success)
{
var failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);

foreach (var diagnostic in failures)
{
Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
}
}
else
{
ms.Seek(0, SeekOrigin.Begin);

// Write dll

path = Path.Combine(outputFolder, $"{baseName}.artifacts.dll");
File.WriteAllBytes(path, ms.ToArray());
Console.WriteLine($"Created {path}");
}
}
catch
{
Console.Error.WriteLine("Artifacts compilation error.");
}
}
path = Path.Combine(outputFolder, $"{baseName}.artifacts.cs");
File.WriteAllText(path, artifact);
Console.WriteLine($"Created {path}");
}
if (options.Debug)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Neo.Compiler.CSharp\Neo.Compiler.CSharp.csproj" />
<ProjectReference Include="..\..\src\Neo.SmartContract.Framework\Neo.SmartContract.Framework.csproj">
<Aliases>scfx</Aliases>
</ProjectReference>
<ProjectReference Include="..\..\src\Neo.SmartContract.Testing\Neo.SmartContract.Testing.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Nep17Contract">
<HintPath>templates\neocontractnep17\Artifacts\Nep17Contract.artifacts.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
<None Update="templates\neocontractnep17\Artifacts\Nep17Contract.manifest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -42,7 +32,7 @@
<Exec Command="dotnet new uninstall Neo.SmartContract.Template" />
<Exec Command="dotnet remove &quot;$(SolutionDir)src/Neo.SmartContract.Template/$(OutputPath)/Nep17Contract.csproj&quot; package Neo.SmartContract.Framework" />
<Exec Command="dotnet add &quot;$(SolutionDir)src/Neo.SmartContract.Template/$(OutputPath)/Nep17Contract.csproj&quot; reference &quot;$(SolutionDir)src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj&quot;" />
<Exec Command="dotnet $(SolutionDir)src/Neo.Compiler.CSharp/$(OutputPath)/nccs.dll $(NullableArgument) $(CheckedArgument) $(DebugArgument) &quot;$(SolutionDir)src/Neo.SmartContract.Template/$(OutputPath)/Nep17Contract.csproj&quot; -o &quot;$(SolutionDir)tests/Neo.SmartContract.Template.UnitTests/templates/neocontractnep17/Artifacts&quot; --generate-artifacts library" />
<Exec Command="dotnet $(SolutionDir)src/Neo.Compiler.CSharp/$(OutputPath)/nccs.dll $(NullableArgument) $(CheckedArgument) $(DebugArgument) &quot;$(SolutionDir)src/Neo.SmartContract.Template/$(OutputPath)/Nep17Contract.csproj&quot; -o &quot;$(SolutionDir)tests/Neo.SmartContract.Template.UnitTests/templates/neocontractnep17/Artifacts&quot;" />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace Neo.SmartContract.Template.UnitTests.templates.neocontractnep17
[TestClass]
public class Nep17ContractTests
{
private const string NefFilePath = "templates/neocontractnep17/UtArtifacts/Nep17Contract.nef";
private const string ManifestPath = "templates/neocontractnep17/UtArtifacts/Nep17Contract.manifest.json";
private const string NefFilePath = "templates/neocontractnep17/Artifacts/Nep17Contract.nef";
private const string ManifestPath = "templates/neocontractnep17/Artifacts/Nep17Contract.manifest.json";

private readonly TestEngine Engine;
private readonly Nep17Contract Nep17;
Expand Down

0 comments on commit 62466f1

Please sign in to comment.