public virtual ProjectOptions GetProjectOptions(string config = null)
{
if (string.IsNullOrEmpty(config))
{
var automationObject = GetAutomationObject() as EnvDTE.Project;
try
{
config = Utilities.GetActiveConfigurationName(automationObject);
}
catch (InvalidOperationException)
{
// Can't figure out the active configuration. Perhaps during solution load, or in a unit test.
}
catch (COMException)
{
// We may be in solution load and don't have an active config yet.
}
}
// Even though the options are the same, when this config was originally set, it may have been before
// the project system was ready to set up its configuration, so go ahead and call through to SetConfiguration
// anyway -- it should do effectively nothing if the config is the same and all the initialization has
// already occurred.
if (this.options != null && string.Equals(this.options.Config, config, StringComparison.OrdinalIgnoreCase))
{
if (config != null)
{
// Fancy dance with the options required because SetConfiguration nulls out this.options
// even if the configuration itself has not changed; whereas we're only calling SetConfiguration
// for purposes of initializing some other fields here; since we know the config is the same, it
// should be perfectly safe to keep the same options as before.
var currentOptions = this.options;
SetConfiguration(config);
this.options = currentOptions;
}
return this.options;
}
var options = CreateProjectOptions();
options.Config = config;
var targetFrameworkMoniker = GetProjectProperty("TargetFrameworkMoniker", false);
if (!string.IsNullOrEmpty(targetFrameworkMoniker))
{
try
{
options.TargetFrameworkMoniker = new FrameworkName(targetFrameworkMoniker);
}
catch (ArgumentException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
}
if (config == null)
{
this.options = options;
return options;
}
options.GenerateExecutable = true;
SetConfiguration(config);
var outputPath = GetOutputPath(CurrentConfig);
if (!string.IsNullOrEmpty(outputPath))
{
// absolutize relative to project folder location
outputPath = Path.Combine(ProjectFolder, outputPath);
}
// Set some default values
options.OutputAssembly = outputPath + Caption + ".exe";
options.ModuleKind = ModuleKindFlags.ConsoleApplication;
options.RootNamespace = GetProjectProperty(ProjectFileConstants.RootNamespace, false);
options.OutputAssembly = outputPath + GetAssemblyName(config);
var outputtype = GetProjectProperty(ProjectFileConstants.OutputType, false);
if (!string.IsNullOrEmpty(outputtype))
{
outputtype = outputtype.ToLower(CultureInfo.InvariantCulture);
}
if (outputtype == "library")
{
options.ModuleKind = ModuleKindFlags.DynamicallyLinkedLibrary;
options.GenerateExecutable = false; // DLL's have no entry point.
}
else if (outputtype == "winexe")
options.ModuleKind = ModuleKindFlags.WindowsApplication;
else
options.ModuleKind = ModuleKindFlags.ConsoleApplication;
options.Win32Icon = GetProjectProperty("ApplicationIcon", false);
options.MainClass = GetProjectProperty("StartupObject", false);
// other settings from CSharp we may want to adopt at some point...
// AssemblyKeyContainerName = "" //This is the key file used to sign the interop assembly generated when importing a com object via add reference
// AssemblyOriginatorKeyFile = ""
// DelaySign = "false"
// DefaultClientScript = "JScript"
// DefaultHTMLPageLayout = "Grid"
// DefaultTargetSchema = "IE50"
// PreBuildEvent = ""
// PostBuildEvent = ""
// RunPostBuildEvent = "OnBuildSuccess"
// transfer all config build options...
if (GetBoolAttr(CurrentConfig, "AllowUnsafeBlocks"))
{
options.AllowUnsafeCode = true;
}
if (GetProjectProperty("BaseAddress", false) != null)
{
try
{
options.BaseAddress = long.Parse(GetProjectProperty("BaseAddress", false),
CultureInfo.InvariantCulture);
}
catch (ArgumentNullException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
catch (ArgumentException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
catch (FormatException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
catch (OverflowException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
}
if (GetBoolAttr(CurrentConfig, "CheckForOverflowUnderflow"))
{
options.CheckedArithmetic = true;
}
if (GetProjectProperty("DefineConstants", false) != null)
{
options.DefinedPreprocessorSymbols = new StringCollection();
foreach (var s in GetProjectProperty("DefineConstants", false).Replace(" \t\r\n", "").Split(';'))
{
options.DefinedPreprocessorSymbols.Add(s);
}
}
var docFile = GetProjectProperty("DocumentationFile", false);
if (!string.IsNullOrEmpty(docFile))
{
options.XmlDocFileName = Path.Combine(ProjectFolder, docFile);
}
if (GetBoolAttr(CurrentConfig, "DebugSymbols"))
{
options.IncludeDebugInformation = true;
}
if (GetProjectProperty("FileAlignment", false) != null)
{
try
{
options.FileAlignment = int.Parse(GetProjectProperty("FileAlignment", false),
CultureInfo.InvariantCulture);
}
catch (ArgumentNullException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
catch (ArgumentException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
catch (FormatException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
catch (OverflowException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
}
if (GetBoolAttr(CurrentConfig, "IncrementalBuild"))
{
options.IncrementalCompile = true;
}
if (GetBoolAttr(CurrentConfig, "Optimize"))
{
options.Optimize = true;
}
if (GetBoolAttr(CurrentConfig, "RegisterForComInterop"))
{
}
if (GetBoolAttr(CurrentConfig, "RemoveIntegerChecks"))
{
}
if (GetBoolAttr(CurrentConfig, "TreatWarningsAsErrors"))
{
options.TreatWarningsAsErrors = true;
}
if (GetProjectProperty("WarningLevel", false) != null)
{
try
{
options.WarningLevel = int.Parse(GetProjectProperty("WarningLevel", false),
CultureInfo.InvariantCulture);
}
catch (ArgumentNullException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
catch (ArgumentException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
catch (FormatException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
catch (OverflowException e)
{
Trace.WriteLine("Exception : " + e.Message);
}
}
this.options = options; // do this AFTER setting configuration so it doesn't clear it.
return options;
}