2015-11-20 17:23:19 -08:00
using System ;
using System.Collections.Generic ;
using System.CommandLine ;
namespace Microsoft.DotNet.Tools.Compiler.Native
{
internal static class ArgumentsParser
{
internal static ArgValues Parse ( IEnumerable < string > args )
{
string inputAssembly = null ;
string outputDirectory = null ;
string temporaryOutputDirectory = null ;
string configuration = null ;
BuildConfiguration ? buildConfiguration = null ;
string mode = null ;
NativeIntermediateMode ? nativeMode = null ;
string ilcArgs = null ;
string ilcPath = null ;
2015-12-19 12:14:46 -08:00
string ilcSdkPath = null ;
2015-11-20 17:23:19 -08:00
string appDepSdk = null ;
string logPath = null ;
2015-12-16 15:45:24 -08:00
var help = false ;
string helpText = null ;
var returnCode = 0 ;
2015-12-20 20:46:28 -08:00
string cppCompilerFlags = null ;
2015-11-20 17:23:19 -08:00
IReadOnlyList < string > references = Array . Empty < string > ( ) ;
2015-12-16 15:45:24 -08:00
IReadOnlyList < string > linklib = Array . Empty < string > ( ) ;
2015-11-20 17:23:19 -08:00
try
{
ArgumentSyntax . Parse ( args , syntax = >
{
2015-12-16 15:45:24 -08:00
syntax . HandleHelp = false ;
syntax . HandleErrors = false ;
2015-11-20 17:23:19 -08:00
syntax . DefineOption ( "output" , ref outputDirectory , "Output Directory for native executable." ) ;
syntax . DefineOption ( "temp-output" , ref temporaryOutputDirectory , "Directory for intermediate files." ) ;
syntax . DefineOption ( "configuration" , ref configuration ,
"debug/release build configuration. Defaults to debug." ) ;
syntax . DefineOption ( "mode" , ref mode , "Code Generation mode. Defaults to ryujit." ) ;
syntax . DefineOptionList ( "reference" , ref references ,
"Use to specify Managed DLL references of the app." ) ;
// Custom Extensibility Points to support CoreRT workflow TODO better descriptions
syntax . DefineOption ( "ilcargs" , ref ilcArgs , "Use to specify custom arguments for the IL Compiler." ) ;
2015-12-19 12:14:46 -08:00
syntax . DefineOption ( "ilcpath" , ref ilcPath , "Use to specify a custom build of IL Compiler." ) ;
syntax . DefineOption ( "ilcsdkpath" , ref ilcSdkPath , "Use to specify a custom build of IL Compiler SDK" ) ;
2015-11-20 17:23:19 -08:00
syntax . DefineOptionList ( "linklib" , ref linklib , "Use to link in additional static libs" ) ;
// TEMPORARY Hack until CoreRT compatible Framework Libs are available
syntax . DefineOption ( "appdepsdk" , ref appDepSdk , "Use to plug in custom appdepsdk path" ) ;
// Optional Log Path
syntax . DefineOption ( "logpath" , ref logPath , "Use to dump Native Compilation Logs to a file." ) ;
2015-12-20 20:46:28 -08:00
// Optional flags to be passed to the native compiler
syntax . DefineOption ( "cppcompilerflags" , ref cppCompilerFlags , "Additional flags to be passed to the native compiler." ) ;
2015-12-16 15:45:24 -08:00
syntax . DefineOption ( "h|help" , ref help , "Help for compile native." ) ;
2015-11-20 17:23:19 -08:00
syntax . DefineParameter ( "INPUT_ASSEMBLY" , ref inputAssembly ,
"The managed input assembly to compile to native." ) ;
2015-12-16 15:45:24 -08:00
helpText = syntax . GetHelpText ( ) ;
2015-11-20 17:23:19 -08:00
if ( string . IsNullOrWhiteSpace ( inputAssembly ) )
{
syntax . ReportError ( "Input Assembly is a required parameter." ) ;
2015-12-16 15:45:24 -08:00
help = true ;
2015-11-20 17:23:19 -08:00
}
if ( ! string . IsNullOrEmpty ( configuration ) )
{
try
{
buildConfiguration = EnumExtensions . Parse < BuildConfiguration > ( configuration ) ;
}
catch ( ArgumentException )
{
syntax . ReportError ( $"Invalid Configuration Option: {configuration}" ) ;
2015-12-16 15:45:24 -08:00
help = true ;
2015-11-20 17:23:19 -08:00
}
}
if ( ! string . IsNullOrEmpty ( mode ) )
{
try
{
nativeMode = EnumExtensions . Parse < NativeIntermediateMode > ( mode ) ;
}
catch ( ArgumentException )
{
syntax . ReportError ( $"Invalid Mode Option: {mode}" ) ;
2015-12-16 15:45:24 -08:00
help = true ;
2015-11-20 17:23:19 -08:00
}
}
} ) ;
}
2015-12-16 15:45:24 -08:00
catch ( ArgumentSyntaxException exception )
2015-11-20 17:23:19 -08:00
{
2015-12-16 15:45:24 -08:00
Console . Error . WriteLine ( exception . Message ) ;
help = true ;
returnCode = 1 ;
}
if ( help )
{
Console . WriteLine ( helpText ) ;
return new ArgValues
{
IsHelp = help ,
ReturnCode = returnCode
} ;
2015-11-20 17:23:19 -08:00
}
Console . WriteLine ( $"Input Assembly: {inputAssembly}" ) ;
return new ArgValues
{
InputManagedAssemblyPath = inputAssembly ,
OutputDirectory = outputDirectory ,
IntermediateDirectory = temporaryOutputDirectory ,
Architecture = ArchitectureMode . x64 ,
BuildConfiguration = buildConfiguration ,
NativeMode = nativeMode ,
ReferencePaths = references ,
IlcArgs = ilcArgs ,
IlcPath = ilcPath ,
2015-12-19 12:14:46 -08:00
IlcSdkPath = ilcSdkPath ,
2015-11-20 17:23:19 -08:00
LinkLibPaths = linklib ,
AppDepSDKPath = appDepSdk ,
2015-12-20 20:46:28 -08:00
LogPath = logPath ,
CppCompilerFlags = cppCompilerFlags
2015-11-20 17:23:19 -08:00
} ;
}
}
}