-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathProgram.cs
214 lines (204 loc) · 9.29 KB
/
Program.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
// Copyright (C) 2015-2023 The Neo Project.
//
// The Neo.Compiler.CSharp is free software distributed under the MIT
// software license, see the accompanying file LICENSE in the main directory
// of the project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using Microsoft.CodeAnalysis;
using Neo.IO;
using Neo.Json;
using Neo.Optimizer;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.TestEngine;
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
namespace Neo.Compiler
{
class Program
{
static int Main(string[] args)
{
RootCommand rootCommand = new(Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>()!.Title)
{
new Argument<string[]>("paths", "The path of the project file, project directory or source files."),
new Option<string>(new[] { "-o", "--output" }, "Specifies the output directory."),
new Option<string>("--base-name", "Specifies the base name of the output files."),
new Option<NullableContextOptions>("--nullable", () => NullableContextOptions.Annotations, "Represents the default state of nullable analysis in this compilation."),
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<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.")
};
rootCommand.Handler = CommandHandler.Create<RootCommand, Options, string[], InvocationContext>(Handle);
return rootCommand.Invoke(args);
}
private static void Handle(RootCommand command, Options options, string[] paths, InvocationContext context)
{
if (paths is null || paths.Length == 0)
{
// catch Unhandled exception: System.Reflection.TargetInvocationException
try
{
context.ExitCode = ProcessDirectory(options, Environment.CurrentDirectory);
if (context.ExitCode == 2)
{
// Display help without args
command.Invoke("--help");
}
}
catch (UnauthorizedAccessException)
{
Console.Error.WriteLine("Unauthorized to access the project directory, or no project is specified. Please ensure you have the proper permissions and a project is specified.");
}
return;
}
paths = paths.Select(Path.GetFullPath).ToArray();
if (paths.Length == 1)
{
string path = paths[0];
if (Directory.Exists(path))
{
context.ExitCode = ProcessDirectory(options, path);
return;
}
if (File.Exists(path) && Path.GetExtension(path).ToLowerInvariant() == ".csproj")
{
context.ExitCode = ProcessCsproj(options, path);
return;
}
}
foreach (string path in paths)
{
if (Path.GetExtension(path).ToLowerInvariant() != ".cs")
{
Console.Error.WriteLine("The files must have a .cs extension.");
context.ExitCode = 1;
return;
}
if (!File.Exists(path))
{
Console.Error.WriteLine($"The file \"{path}\" doesn't exist.");
context.ExitCode = 1;
return;
}
}
context.ExitCode = ProcessSources(options, Path.GetDirectoryName(paths[0])!, paths);
}
private static int ProcessDirectory(Options options, string path)
{
string? csproj = Directory.EnumerateFiles(path, "*.csproj", SearchOption.TopDirectoryOnly).FirstOrDefault();
if (csproj is null)
{
string obj = Path.Combine(path, "obj");
string[] sourceFiles = Directory.EnumerateFiles(path, "*.cs", SearchOption.AllDirectories).Where(p => !p.StartsWith(obj)).ToArray();
if (sourceFiles.Length == 0)
{
Console.Error.WriteLine($"No .cs file is found in \"{path}\".");
return 2;
}
return ProcessSources(options, path, sourceFiles);
}
else
{
return ProcessCsproj(options, csproj);
}
}
private static int ProcessCsproj(Options options, string path)
{
return ProcessOutputs(options, Path.GetDirectoryName(path)!, CompilationContext.CompileProject(path, options));
}
private static int ProcessSources(Options options, string folder, string[] sourceFiles)
{
return ProcessOutputs(options, folder, CompilationContext.CompileSources(sourceFiles, options));
}
private static int ProcessOutputs(Options options, string folder, CompilationContext context)
{
foreach (Diagnostic diagnostic in context.Diagnostics)
{
if (diagnostic.Severity == DiagnosticSeverity.Error)
Console.Error.WriteLine(diagnostic.ToString());
else
Console.WriteLine(diagnostic.ToString());
}
if (context.Success)
{
string outputFolder = options.Output ?? Path.Combine(folder, "bin", "sc");
string path = outputFolder;
string baseName = options.BaseName ?? context.ContractName!;
NefFile nef = context.CreateExecutable();
ContractManifest manifest = context.CreateManifest();
JToken debugInfo = context.CreateDebugInformation(folder);
if (!options.NoOptimize)
{
(nef, manifest, debugInfo) = Reachability.RemoveUncoveredInstructions(nef, manifest, debugInfo);
}
try
{
Directory.CreateDirectory(outputFolder);
path = Path.Combine(path, $"{baseName}.nef");
File.WriteAllBytes(path, nef.ToArray());
}
catch (Exception ex)
{
Console.Error.WriteLine($"Can't create {path}. {ex.Message}.");
return 1;
}
Console.WriteLine($"Created {path}");
path = Path.Combine(outputFolder, $"{baseName}.manifest.json");
try
{
File.WriteAllBytes(path, manifest.ToJson().ToByteArray(false));
}
catch (Exception ex)
{
Console.Error.WriteLine($"Can't create {path}. {ex.Message}.");
return 1;
}
Console.WriteLine($"Created {path}");
// Create artifacts
{
path = Path.Combine(outputFolder, $"{baseName}.artifacts.cs");
File.WriteAllText(path, Artifacts.CreateSourceFromManifest(baseName, manifest.Abi));
Console.WriteLine($"Created {path}");
}
if (options.Debug)
{
path = Path.Combine(outputFolder, $"{baseName}.nefdbgnfo");
using FileStream fs = new(path, FileMode.Create, FileAccess.Write);
using ZipArchive archive = new(fs, ZipArchiveMode.Create);
using Stream stream = archive.CreateEntry($"{baseName}.debug.json").Open();
stream.Write(debugInfo.ToByteArray(false));
Console.WriteLine($"Created {path}");
}
if (options.Assembly)
{
path = Path.Combine(outputFolder, $"{baseName}.asm");
File.WriteAllText(path, context.CreateAssembly());
Console.WriteLine($"Created {path}");
path = Path.Combine(outputFolder, $"{baseName}.nef.txt");
File.WriteAllText(path, DumpNef.GenerateDumpNef(nef, debugInfo));
Console.WriteLine($"Created {path}");
}
Console.WriteLine("Compilation completed successfully.");
return 0;
}
else
{
Console.Error.WriteLine("Compilation failed.");
return 1;
}
}
}
}