This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 169
/
Program.cs
128 lines (110 loc) · 5.55 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Linq;
using CommandLine;
using Microsoft.Quantum.QsCompiler.Diagnostics;
namespace Microsoft.Quantum.QsCompiler.CommandLineCompiler
{
public static class ReturnCode
{
/// <summary>
/// Return code indicating that the invoked command to the Q# command line compiler succeeded.
/// </summary>
public const int Success = 0;
/// <summary>
/// Return code indicating that the command to the Q# command line compiler was invoked with invalid arguments.
/// </summary>
public const int InvalidArguments = -1;
/// <summary>
/// Return code indicating that some of the files given to the Q# command line compiler could not be loaded.
/// </summary>
public const int UnresolvedFiles = -2;
/// <summary>
/// Return code indicating that the compilation using the Q# command line compiler failed due to in build errors.
/// </summary>
public const int CompilationErrors = -3;
/// <summary>
/// Return code indicating that generating the necessary functor support for the built compilation failed.
/// </summary>
public const int FunctorGenerationErrors = -4;
/// <summary>
/// Return code indicating that pre-evaluating the built compilation if possible failed.
/// </summary>
public const int PreevaluationErrors = -5;
/// <summary>
/// Return code indicating that generating a binary file with the content of the built compilation failed.
/// </summary>
public const int BinaryGenerationErrors = -6;
/// <summary>
/// Return code indicating that generating a dll containing the compiled binary failed.
/// </summary>
public const int DllGenerationErrors = -7;
/// <summary>
/// Return code indicating that generating formatted Q# code based on the built compilation failed.
/// </summary>
public const int CodeGenerationErrors = -8;
/// <summary>
/// Return code indicating that generating documentation for the built compilation failed.
/// </summary>
public const int DocGenerationErrors = -9;
/// <summary>
/// Return code indicating that invoking the specified compiler plugin(s) failed.
/// </summary>
public const int PluginExecutionErrors = -10;
/// <summary>
/// Return code indicating that executing a target-specific compilation step failed.
/// </summary>
public const int TargetingErrors = -11;
/// <summary>
/// Return code indicating that monomorphizing the compilation failed.
/// </summary>
public const int MonomorphizationErrors = -12;
/// <summary>
/// Return code indicating that an unexpected exception was thrown when executing the invoked command to the Q# command line compiler.
/// </summary>
public const int UnexpectedError = -1000;
public static int Status(CompilationLoader loaded) =>
loaded.SourceFileLoading == CompilationLoader.Status.Failed ? UnresolvedFiles :
loaded.ReferenceLoading == CompilationLoader.Status.Failed ? UnresolvedFiles :
loaded.Validation == CompilationLoader.Status.Failed ? CompilationErrors :
loaded.FunctorSupport == CompilationLoader.Status.Failed ? FunctorGenerationErrors :
loaded.PreEvaluation == CompilationLoader.Status.Failed ? PreevaluationErrors :
loaded.AllLoadedRewriteSteps == CompilationLoader.Status.Failed ? PluginExecutionErrors :
loaded.Monomorphization == CompilationLoader.Status.Failed ? MonomorphizationErrors :
loaded.TargetSpecificReplacements == CompilationLoader.Status.Failed ? TargetingErrors :
loaded.TargetSpecificCompilation == CompilationLoader.Status.Failed ? TargetingErrors :
loaded.Documentation == CompilationLoader.Status.Failed ? DocGenerationErrors :
loaded.BinaryFormat == CompilationLoader.Status.Failed ? BinaryGenerationErrors :
loaded.DllGeneration == CompilationLoader.Status.Failed ? DllGenerationErrors :
loaded.Success ? Success : UnexpectedError;
}
public static class Program
{
private static int Run<T>(Func<T, ConsoleLogger, int> compile, T options)
where T : Options
{
var logger = options.GetLogger();
try
{
var result = compile(options, logger);
logger.ReportSummary(result);
return result;
}
catch (Exception ex)
{
logger.Log(ErrorCode.UnexpectedCommandLineCompilerException, Enumerable.Empty<string>());
logger.Log(ex);
return ReturnCode.UnexpectedError;
}
}
public static int Main(string[] args) =>
Parser.Default
.ParseArguments<BuildCompilation.BuildOptions, DiagnoseCompilation.DiagnoseOptions, FormatCompilation.FormatOptions>(args)
.MapResult(
(BuildCompilation.BuildOptions opts) => Run((c, o) => BuildCompilation.Run(c, o), opts),
(DiagnoseCompilation.DiagnoseOptions opts) => Run((c, o) => DiagnoseCompilation.Run(c, o), opts),
(FormatCompilation.FormatOptions opts) => Run((c, o) => FormatCompilation.Run(c, o), opts),
errs => ReturnCode.InvalidArguments);
}
}