protected override void AddResponseFileCommands(CommandLineBuilderExtension commandLine) { commandLine.AppendSwitchIfNotNull("/lib:", AdditionalLibPaths, ","); commandLine.AppendSwitchIfNotNull("/loadpaths:", AdditionalLoadPaths, ","); commandLine.AppendSwitchIfNotNull("/imports:", Imports, ";"); Csc.AddReferencesToCommandLine(commandLine, References, isInteractive: true); base.AddResponseFileCommands(commandLine); }
internal void AddResponseFileCommandsForSwitchesSinceInitialReleaseThatAreNeededByTheHost(CommandLineBuilderExtension commandLine) { commandLine.AppendPlusOrMinusSwitch("/deterministic", _store, nameof(Deterministic)); commandLine.AppendPlusOrMinusSwitch("/publicsign", _store, nameof(PublicSign)); AddFeatures(commandLine, Features); }
/// <summary> /// Fills the provided CommandLineBuilderExtension with those switches and other information that can go into a response file. /// </summary> protected internal virtual void AddResponseFileCommands(CommandLineBuilderExtension commandLine) { // If outputAssembly is not specified, then an "/out: <name>" option won't be added to // overwrite the one resulting from the OutputAssembly member of the CompilerParameters class. // In that case, we should set the outputAssembly member based on the first source file. if ( (OutputAssembly == null) && (Sources != null) && (Sources.Length > 0) && (ResponseFiles == null) // The response file may already have a /out: switch in it, so don't try to be smart here. ) { try { OutputAssembly = new TaskItem(Path.GetFileNameWithoutExtension(Sources[0].ItemSpec)); } catch (ArgumentException e) { throw new ArgumentException(e.Message, "Sources"); } if (string.Compare(TargetType, "library", StringComparison.OrdinalIgnoreCase) == 0) { OutputAssembly.ItemSpec += ".dll"; } else if (string.Compare(TargetType, "module", StringComparison.OrdinalIgnoreCase) == 0) { OutputAssembly.ItemSpec += ".netmodule"; } else { OutputAssembly.ItemSpec += ".exe"; } } commandLine.AppendSwitchIfNotNull("/addmodule:", AddModules, ","); commandLine.AppendSwitchWithInteger("/codepage:", _store, nameof(CodePage)); ConfigureDebugProperties(); // The "DebugType" parameter should be processed after the "EmitDebugInformation" parameter // because it's more specific. Order matters on the command-line, and the last one wins. // /debug+ is just a shorthand for /debug:full. And /debug- is just a shorthand for /debug:none. commandLine.AppendPlusOrMinusSwitch("/debug", _store, nameof(EmitDebugInformation)); commandLine.AppendSwitchIfNotNull("/debug:", DebugType); commandLine.AppendPlusOrMinusSwitch("/delaysign", _store, nameof(DelaySign)); commandLine.AppendSwitchWithInteger("/filealign:", _store, nameof(FileAlignment)); commandLine.AppendSwitchIfNotNull("/keycontainer:", KeyContainer); commandLine.AppendSwitchIfNotNull("/keyfile:", KeyFile); // If the strings "LogicalName" or "Access" ever change, make sure to search/replace everywhere in vsproject. commandLine.AppendSwitchIfNotNull("/linkresource:", LinkResources, new string[] { "LogicalName", "Access" }); commandLine.AppendWhenTrue("/nologo", _store, nameof(NoLogo)); commandLine.AppendWhenTrue("/nowin32manifest", _store, nameof(NoWin32Manifest)); commandLine.AppendPlusOrMinusSwitch("/optimize", _store, nameof(Optimize)); commandLine.AppendSwitchIfNotNull("/pathmap:", PathMap); commandLine.AppendSwitchIfNotNull("/out:", OutputAssembly); commandLine.AppendSwitchIfNotNull("/ruleset:", CodeAnalysisRuleSet); commandLine.AppendSwitchIfNotNull("/errorlog:", ErrorLog); commandLine.AppendSwitchIfNotNull("/subsystemversion:", SubsystemVersion); commandLine.AppendWhenTrue("/reportanalyzer", _store, nameof(ReportAnalyzer)); // If the strings "LogicalName" or "Access" ever change, make sure to search/replace everywhere in vsproject. commandLine.AppendSwitchIfNotNull("/resource:", Resources, new string[] { "LogicalName", "Access" }); commandLine.AppendSwitchIfNotNull("/target:", TargetType); commandLine.AppendPlusOrMinusSwitch("/warnaserror", _store, nameof(TreatWarningsAsErrors)); commandLine.AppendWhenTrue("/utf8output", _store, nameof(Utf8Output)); commandLine.AppendSwitchIfNotNull("/win32icon:", Win32Icon); commandLine.AppendSwitchIfNotNull("/win32manifest:", Win32Manifest); AddResponseFileCommandsForSwitchesSinceInitialReleaseThatAreNeededByTheHost(commandLine); AddAnalyzersToCommandLine(commandLine, Analyzers); AddAdditionalFilesToCommandLine(commandLine); // Append the sources. commandLine.AppendFileNamesIfNotNull(Sources, " "); }
private void AddReferencesToCommandLine(CommandLineBuilderExtension commandLine) { if ((this.References == null) || (this.References.Length == 0)) { return; } var references = new List<ITaskItem>(this.References.Length); var links = new List<ITaskItem>(this.References.Length); foreach (ITaskItem reference in this.References) { bool embed = Utilities.TryConvertItemMetadataToBool(reference, "EmbedInteropTypes"); if (embed) { links.Add(reference); } else { references.Add(reference); } } if (links.Count > 0) { commandLine.AppendSwitchIfNotNull("/link:", links.ToArray(), ","); } if (references.Count > 0) { commandLine.AppendSwitchIfNotNull("/reference:", references.ToArray(), ","); } }
internal void InitializeHostObjectSupportForNewSwitches(ITaskHost hostObject, ref string param) { var compilerOptionsHostObject = hostObject as ICompilerOptionsHostObject; if (compilerOptionsHostObject != null) { var commandLineBuilder = new CommandLineBuilderExtension(); AddResponseFileCommandsForSwitchesSinceInitialReleaseThatAreNeededByTheHost(commandLineBuilder); param = "CompilerOptions"; CheckHostObjectSupport(param, compilerOptionsHostObject.SetCompilerOptions(commandLineBuilder.ToString())); } }
/// <summary> /// Adds a "/analyzer:" switch to the command line for each provided analyzer. /// </summary> internal static void AddAnalyzersToCommandLine(CommandLineBuilderExtension commandLine, ITaskItem[] analyzers) { // If there were no analyzers passed in, don't add any /analyzer: switches // on the command-line. if (analyzers == null) { return; } foreach (ITaskItem analyzer in analyzers) { commandLine.AppendSwitchIfNotNull("/analyzer:", analyzer.ItemSpec); } }
/// <summary> /// Fills the provided CommandLineBuilderExtension with those switches and other information that can't go into a response file and /// must go directly onto the command line. /// </summary> protected internal virtual void AddCommandLineCommands(CommandLineBuilderExtension commandLine) { commandLine.AppendWhenTrue("/noconfig", _store, nameof(NoConfig)); }
internal void AddResponseFileCommandsForSwitchesSinceInitialReleaseThatAreNeededByTheHost(CommandLineBuilderExtension commandLine) { commandLine.AppendPlusOrMinusSwitch("/deterministic", _store, nameof(Deterministic)); commandLine.AppendPlusOrMinusSwitch("/publicsign", _store, nameof(PublicSign)); commandLine.AppendSwitchIfNotNull("/runtimemetadataversion:", RuntimeMetadataVersion); commandLine.AppendSwitchIfNotNull("/checksumalgorithm:", ChecksumAlgorithm); commandLine.AppendSwitchWithSplitting("/instrument:", Instrument, ",", ';', ','); commandLine.AppendSwitchIfNotNull("/sourcelink:", SourceLink); AddFeatures(commandLine, Features); AddEmbeddedFilesToCommandLine(commandLine); }
internal void AddResponseFileCommandsForSwitchesSinceInitialReleaseThatAreNeededByTheHost(CommandLineBuilderExtension commandLine) { commandLine.AppendPlusOrMinusSwitch("/deterministic", _store, nameof(Deterministic)); commandLine.AppendPlusOrMinusSwitch("/publicsign", _store, nameof(PublicSign)); commandLine.AppendSwitchIfNotNull("/runtimemetadataversion:", RuntimeMetadataVersion); commandLine.AppendSwitchIfNotNull("/checksumalgorithm:", ChecksumAlgorithm); AddFeatures(commandLine, Features); }
/// <summary> /// Looks at all the parameters that have been set, and builds up the string /// containing all the command-line switches. /// </summary> protected internal override void AddResponseFileCommands(CommandLineBuilderExtension commandLine) { commandLine.AppendSwitchIfNotNull("/baseaddress:", this.GetBaseAddressInHex()); commandLine.AppendSwitchIfNotNull("/libpath:", this.AdditionalLibPaths, ","); commandLine.AppendSwitchIfNotNull("/imports:", this.Imports, ","); // Make sure this /doc+ switch comes *before* the /doc:<file> switch (which is handled in the // ManagedCompiler.cs base class). /doc+ is really just an alias for /doc:<assemblyname>.xml, // and the last /doc switch on the command-line wins. If the user provided a specific doc filename, // we want that one to win. commandLine.AppendPlusOrMinusSwitch("/doc", this._store, "GenerateDocumentation"); commandLine.AppendSwitchIfNotNull("/optioncompare:", this.OptionCompare); commandLine.AppendPlusOrMinusSwitch("/optionexplicit", this._store, "OptionExplicit"); // Make sure this /optionstrict+ switch appears *before* the /optionstrict:xxxx switch below /* twhitney: In Orcas a change was made for devdiv bug 16889 that set Option Strict-, whenever this.DisabledWarnings was * empty. That was clearly the wrong thing to do and we found it when we had a project with all the warning configuration * entries set to WARNING. Because this.DisabledWarnings was empty in that case we would end up sending /OptionStrict- * effectively silencing all the warnings that had been selected. * * Now what we do is: * If option strict+ is specified, that trumps everything and we just set option strict+ * Otherwise, just set option strict:custom. * You may wonder why we don't try to set Option Strict- The reason is that Option Strict- just implies a certain * set of warnings that should be disabled (there's ten of them today) You get the same effect by sending * option strict:custom on along with the correct list of disabled warnings. * Rather than make this code know the current set of disabled warnings that comprise Option strict-, we just send * option strict:custom on with the understanding that we'll get the same behavior as option strict- since we are passing * the /nowarn line on that contains all the warnings OptionStrict- would disable anyway. The IDE knows what they are * and puts them in the project file so we are good. And by not making this code aware of which warnings comprise * Option Strict-, we have one less place we have to keep up to date in terms of what comprises option strict- */ // Decide whether we are Option Strict+ or Option Strict:custom object optionStrictSetting = this._store["OptionStrict"]; bool optionStrict = optionStrictSetting != null ? (bool)optionStrictSetting : false; if (optionStrict) { commandLine.AppendSwitch("/optionstrict+"); } else // OptionStrict+ wasn't specified so use :custom. { commandLine.AppendSwitch("/optionstrict:custom"); } commandLine.AppendSwitchIfNotNull("/optionstrict:", this.OptionStrictType); commandLine.AppendWhenTrue("/nowarn", this._store, "NoWarnings"); commandLine.AppendSwitchWithSplitting("/nowarn:", this.DisabledWarnings, ",", ';', ','); commandLine.AppendPlusOrMinusSwitch("/optioninfer", this._store, "OptionInfer"); commandLine.AppendWhenTrue("/nostdlib", this._store, "NoStandardLib"); commandLine.AppendWhenTrue("/novbruntimeref", this._store, "NoVBRuntimeReference"); commandLine.AppendSwitchIfNotNull("/errorreport:", this.ErrorReport); commandLine.AppendSwitchIfNotNull("/platform:", this.PlatformWith32BitPreference); commandLine.AppendPlusOrMinusSwitch("/removeintchecks", this._store, "RemoveIntegerChecks"); commandLine.AppendSwitchIfNotNull("/rootnamespace:", this.RootNamespace); commandLine.AppendSwitchIfNotNull("/sdkpath:", this.SdkPath); commandLine.AppendSwitchIfNotNull("/langversion:", this.LangVersion); commandLine.AppendSwitchIfNotNull("/moduleassemblyname:", this.ModuleAssemblyName); commandLine.AppendWhenTrue("/netcf", this._store, "TargetCompactFramework"); commandLine.AppendSwitchIfNotNull("/preferreduilang:", this.PreferredUILang); commandLine.AppendPlusOrMinusSwitch("/highentropyva", this._store, "HighEntropyVA"); if (0 == String.Compare(this.VBRuntimePath, this.VBRuntime, StringComparison.OrdinalIgnoreCase)) { commandLine.AppendSwitchIfNotNull("/vbruntime:", this.VBRuntimePath); } else if (this.VBRuntime != null) { string vbRuntimeSwitch = this.VBRuntime; if (0 == String.Compare(vbRuntimeSwitch, "EMBED", StringComparison.OrdinalIgnoreCase)) { commandLine.AppendSwitch("/vbruntime*"); } else if (0 == String.Compare(vbRuntimeSwitch, "NONE", StringComparison.OrdinalIgnoreCase)) { commandLine.AppendSwitch("/vbruntime-"); } else if (0 == String.Compare(vbRuntimeSwitch, "DEFAULT", StringComparison.OrdinalIgnoreCase)) { commandLine.AppendSwitch("/vbruntime+"); } else { commandLine.AppendSwitchIfNotNull("/vbruntime:", vbRuntimeSwitch); } } // Verbosity if ( (this.Verbosity != null) && ( (0 == String.Compare(this.Verbosity, "quiet", StringComparison.OrdinalIgnoreCase)) || (0 == String.Compare(this.Verbosity, "verbose", StringComparison.OrdinalIgnoreCase)) ) ) { commandLine.AppendSwitchIfNotNull("/", this.Verbosity); } commandLine.AppendSwitchIfNotNull("/doc:", this.DocumentationFile); commandLine.AppendSwitchUnquotedIfNotNull("/define:", Vbc.GetDefineConstantsSwitch(this.DefineConstants)); AddReferencesToCommandLine(commandLine); commandLine.AppendSwitchIfNotNull("/win32resource:", this.Win32Resource); // Special case for "Sub Main" (See VSWhidbey 381254) if (0 != String.Compare("Sub Main", this.MainEntryPoint, StringComparison.OrdinalIgnoreCase)) { commandLine.AppendSwitchIfNotNull("/main:", this.MainEntryPoint); } base.AddResponseFileCommands(commandLine); // This should come after the "TreatWarningsAsErrors" flag is processed (in managedcompiler.cs). // Because if TreatWarningsAsErrors=false, then we'll have a /warnaserror- on the command-line, // and then any specific warnings that should be treated as errors should be specified with // /warnaserror+:<list> after the /warnaserror- switch. The order of the switches on the command-line // does matter. // // Note that // /warnaserror+ // is just shorthand for: // /warnaserror+:<all possible warnings> // // Similarly, // /warnaserror- // is just shorthand for: // /warnaserror-:<all possible warnings> commandLine.AppendSwitchWithSplitting("/warnaserror+:", this.WarningsAsErrors, ",", ';', ','); commandLine.AppendSwitchWithSplitting("/warnaserror-:", this.WarningsNotAsErrors, ",", ';', ','); // If not design time build and the globalSessionGuid property was set then add a -globalsessionguid:<guid> bool designTime = false; if (this.HostObject != null) { var vbHost = this.HostObject as IVbcHostObject; designTime = vbHost.IsDesignTime(); } if (!designTime) { if (!string.IsNullOrWhiteSpace(this.VsSessionGuid)) { commandLine.AppendSwitchIfNotNull("/sqmsessionguid:", this.VsSessionGuid); } } // It's a good idea for the response file to be the very last switch passed, just // from a predictability perspective. It also solves the problem that a dogfooder // ran into, which is described in an email thread attached to bug VSWhidbey 146883. // See also bugs 177762 and 118307 for additional bugs related to response file position. if (this.ResponseFiles != null) { foreach (ITaskItem response in this.ResponseFiles) { commandLine.AppendSwitchIfNotNull("@", response.ItemSpec); } } }
/// <summary> /// Adds a "/analyzer:" switch to the command line for each provided analyzer. /// </summary> private void AddAnalyzersToCommandLine(CommandLineBuilderExtension commandLine) { // If there were no analyzers passed in, don't add any /analyzer: switches // on the command-line. if ((this.Analyzers == null) || (this.Analyzers.Length == 0)) { return; } foreach (ITaskItem analyzer in this.Analyzers) { commandLine.AppendSwitchIfNotNull("/analyzer:", analyzer.ItemSpec); } }
/// <summary> /// The C# compiler (starting with Whidbey) supports assembly aliasing for references. /// See spec at http://devdiv/spectool/Documents/Whidbey/VCSharp/Design%20Time/M3%20DCRs/DCR%20Assembly%20aliases.doc. /// This method handles the necessary work of looking at the "Aliases" attribute on /// the incoming "References" items, and making sure to generate the correct /// command-line on csc.exe. The syntax for aliasing a reference is: /// csc.exe /reference:Foo=System.Xml.dll /// /// The "Aliases" attribute on the "References" items is actually a comma-separated /// list of aliases, and if any of the aliases specified is the string "global", /// then we add that reference to the command-line without an alias. /// </summary> internal static void AddReferencesToCommandLine( CommandLineBuilderExtension commandLine, ITaskItem[] references, bool isInteractive = false) { // If there were no references passed in, don't add any /reference: switches // on the command-line. if (references == null) { return; } // Loop through all the references passed in. We'll be adding separate // /reference: switches for each reference, and in some cases even multiple // /reference: switches per reference. foreach (ITaskItem reference in references) { // See if there was an "Alias" attribute on the reference. string aliasString = reference.GetMetadata("Aliases"); string switchName = "/reference:"; if (!isInteractive) { bool embed = Utilities.TryConvertItemMetadataToBool(reference, "EmbedInteropTypes"); if (embed) { switchName = "/link:"; } } if (string.IsNullOrEmpty(aliasString)) { // If there was no "Alias" attribute, just add this as a global reference. commandLine.AppendSwitchIfNotNull(switchName, reference.ItemSpec); } else { // If there was an "Alias" attribute, it contains a comma-separated list // of aliases to use for this reference. For each one of those aliases, // we're going to add a separate /reference: switch to the csc.exe // command-line string[] aliases = aliasString.Split(','); foreach (string alias in aliases) { // Trim whitespace. string trimmedAlias = alias.Trim(); if (alias.Length == 0) { continue; } // The alias should be a valid C# identifier. Therefore it cannot // contain comma, space, semicolon, or double-quote. Let's check for // the existence of those characters right here, and bail immediately // if any are present. There are a whole bunch of other characters // that are not allowed in a C# identifier, but we'll just let csc.exe // error out on those. The ones we're checking for here are the ones // that could seriously screw up the command-line parsing or could // allow parameter injection. if (trimmedAlias.IndexOfAny(new char[] { ',', ' ', ';', '"' }) != -1) { throw Utilities.GetLocalizedArgumentException( ErrorString.Csc_AssemblyAliasContainsIllegalCharacters, reference.ItemSpec, trimmedAlias); } // The alias called "global" is special. It means that we don't // give it an alias on the command-line. if (string.Compare("global", trimmedAlias, StringComparison.OrdinalIgnoreCase) == 0) { commandLine.AppendSwitchIfNotNull(switchName, reference.ItemSpec); } else { // We have a valid (and explicit) alias for this reference. Add // it to the command-line using the syntax: // /reference:Foo=System.Xml.dll commandLine.AppendSwitchAliased(switchName, trimmedAlias, reference.ItemSpec); } } } } }
/// <summary> /// Fills the provided CommandLineBuilderExtension with those switches and other information that can go into a response file. /// </summary> protected internal override void AddResponseFileCommands(CommandLineBuilderExtension commandLine) { commandLine.AppendSwitchIfNotNull("/lib:", AdditionalLibPaths, ","); commandLine.AppendPlusOrMinusSwitch("/unsafe", _store, nameof(AllowUnsafeBlocks)); commandLine.AppendPlusOrMinusSwitch("/checked", _store, nameof(CheckForOverflowUnderflow)); commandLine.AppendSwitchWithSplitting("/nowarn:", DisabledWarnings, ",", ';', ','); commandLine.AppendWhenTrue("/fullpaths", _store, nameof(GenerateFullPaths)); commandLine.AppendSwitchIfNotNull("/langversion:", LangVersion); commandLine.AppendSwitchIfNotNull("/moduleassemblyname:", ModuleAssemblyName); commandLine.AppendSwitchIfNotNull("/pdb:", PdbFile); commandLine.AppendPlusOrMinusSwitch("/nostdlib", _store, nameof(NoStandardLib)); commandLine.AppendSwitchIfNotNull("/platform:", PlatformWith32BitPreference); commandLine.AppendSwitchIfNotNull("/errorreport:", ErrorReport); commandLine.AppendSwitchWithInteger("/warn:", _store, nameof(WarningLevel)); commandLine.AppendSwitchIfNotNull("/doc:", DocumentationFile); commandLine.AppendSwitchIfNotNull("/baseaddress:", BaseAddress); commandLine.AppendSwitchUnquotedIfNotNull("/define:", GetDefineConstantsSwitch(DefineConstants, Log)); commandLine.AppendSwitchIfNotNull("/win32res:", Win32Resource); commandLine.AppendSwitchIfNotNull("/main:", MainEntryPoint); commandLine.AppendSwitchIfNotNull("/appconfig:", ApplicationConfiguration); commandLine.AppendWhenTrue("/errorendlocation", _store, nameof(ErrorEndLocation)); commandLine.AppendSwitchIfNotNull("/preferreduilang:", PreferredUILang); commandLine.AppendPlusOrMinusSwitch("/highentropyva", _store, nameof(HighEntropyVA)); // If not design time build and the globalSessionGuid property was set then add a -globalsessionguid:<guid> bool designTime = false; if (HostObject != null) { var csHost = HostObject as ICscHostObject; designTime = csHost.IsDesignTime(); } if (!designTime) { if (!string.IsNullOrWhiteSpace(VsSessionGuid)) { commandLine.AppendSwitchIfNotNull("/sqmsessionguid:", VsSessionGuid); } } AddReferencesToCommandLine(commandLine, References); base.AddResponseFileCommands(commandLine); // This should come after the "TreatWarningsAsErrors" flag is processed (in managedcompiler.cs). // Because if TreatWarningsAsErrors=false, then we'll have a /warnaserror- on the command-line, // and then any specific warnings that should be treated as errors should be specified with // /warnaserror+:<list> after the /warnaserror- switch. The order of the switches on the command-line // does matter. // // Note that // /warnaserror+ // is just shorthand for: // /warnaserror+:<all possible warnings> // // Similarly, // /warnaserror- // is just shorthand for: // /warnaserror-:<all possible warnings> commandLine.AppendSwitchWithSplitting("/warnaserror+:", WarningsAsErrors, ",", ';', ','); commandLine.AppendSwitchWithSplitting("/warnaserror-:", WarningsNotAsErrors, ",", ';', ','); // It's a good idea for the response file to be the very last switch passed, just // from a predictability perspective. It also solves the problem that a dogfooder // ran into, which is described in an email thread attached to bug VSWhidbey 146883. // See also bugs 177762 and 118307 for additional bugs related to response file position. if (ResponseFiles != null) { foreach (ITaskItem response in ResponseFiles) { commandLine.AppendSwitchIfNotNull("@", response.ItemSpec); } } }
/// <summary> /// Fills the provided CommandLineBuilderExtension with those switches and other information that can't go into a response file and /// must go directly onto the command line. /// </summary> protected virtual void AddCommandLineCommands(CommandLineBuilderExtension commandLine) { }
/// <summary> /// Adds a "/additionalfile:" switch to the command line for each additional file. /// </summary> private void AddAdditionalFilesToCommandLine(CommandLineBuilderExtension commandLine) { if (AdditionalFiles != null) { foreach (ITaskItem additionalFile in AdditionalFiles) { commandLine.AppendSwitchIfNotNull("/additionalfile:", additionalFile.ItemSpec); } } }
protected override string GenerateCommandLineCommands() { CommandLineBuilderExtension commandLineBuilder = new CommandLineBuilderExtension(); AddCommandLineCommands(commandLineBuilder); return commandLineBuilder.ToString(); }
/// <summary> /// Adds a "/embed:" switch to the command line for each pdb embedded file. /// </summary> private void AddEmbeddedFilesToCommandLine(CommandLineBuilderExtension commandLine) { if (EmbeddedFiles != null) { foreach (ITaskItem embeddedFile in EmbeddedFiles) { commandLine.AppendSwitchIfNotNull("/embed:", embeddedFile.ItemSpec); } } }
protected internal override void AddResponseFileCommands(CommandLineBuilderExtension commandLine) { commandLine.AppendSwitchIfNotNull("/lib:", AdditionalLibPaths, ","); commandLine.AppendPlusOrMinusSwitch("/unsafe", _store, nameof(AllowUnsafeBlocks)); commandLine.AppendPlusOrMinusSwitch("/checked", _store, nameof(CheckForOverflowUnderflow)); commandLine.AppendSwitchWithSplitting("/nowarn:", DisabledWarnings, ",", ';', ','); commandLine.AppendWhenTrue("/fullpaths", _store, nameof(GenerateFullPaths)); commandLine.AppendSwitchIfNotNull("/moduleassemblyname:", ModuleAssemblyName); commandLine.AppendSwitchIfNotNull("/pdb:", PdbFile); commandLine.AppendPlusOrMinusSwitch("/nostdlib", _store, nameof(NoStandardLib)); commandLine.AppendSwitchIfNotNull("/platform:", PlatformWith32BitPreference); commandLine.AppendSwitchIfNotNull("/errorreport:", ErrorReport); commandLine.AppendSwitchWithInteger("/warn:", _store, nameof(WarningLevel)); commandLine.AppendSwitchIfNotNull("/doc:", DocumentationFile); commandLine.AppendSwitchIfNotNull("/baseaddress:", BaseAddress); commandLine.AppendSwitchUnquotedIfNotNull("/define:", GetDefineConstantsSwitch(DefineConstants, Log)); commandLine.AppendSwitchIfNotNull("/win32res:", Win32Resource); commandLine.AppendSwitchIfNotNull("/main:", MainEntryPoint); commandLine.AppendSwitchIfNotNull("/appconfig:", ApplicationConfiguration); commandLine.AppendWhenTrue("/errorendlocation", _store, nameof(ErrorEndLocation)); commandLine.AppendSwitchIfNotNull("/preferreduilang:", PreferredUILang); commandLine.AppendPlusOrMinusSwitch("/highentropyva", _store, nameof(HighEntropyVA)); commandLine.AppendSwitchIfNotNull("/nullable:", Nullable); commandLine.AppendWhenTrue("/nosdkpath", _store, nameof(DisableSdkPath)); // If not design time build and the globalSessionGuid property was set then add a -globalsessionguid:<guid> bool designTime = false; if (HostObject is ICscHostObject csHost) { designTime = csHost.IsDesignTime(); } else if (HostObject != null) { throw new InvalidOperationException(string.Format(ErrorString.General_IncorrectHostObject, "Csc", "ICscHostObject")); } if (!designTime) { if (!string.IsNullOrWhiteSpace(VsSessionGuid)) { commandLine.AppendSwitchIfNotNull("/sqmsessionguid:", VsSessionGuid); } } AddReferencesToCommandLine(commandLine, References); base.AddResponseFileCommands(commandLine); // This should come after the "TreatWarningsAsErrors" flag is processed (in managedcompiler.cs). // Because if TreatWarningsAsErrors=false, then we'll have a /warnaserror- on the command-line, // and then any specific warnings that should be treated as errors should be specified with // /warnaserror+:<list> after the /warnaserror- switch. The order of the switches on the command-line // does matter. // // Note that // /warnaserror+ // is just shorthand for: // /warnaserror+:<all possible warnings> // // Similarly, // /warnaserror- // is just shorthand for: // /warnaserror-:<all possible warnings> commandLine.AppendSwitchWithSplitting("/warnaserror+:", WarningsAsErrors, ",", ';', ','); commandLine.AppendSwitchWithSplitting("/warnaserror-:", WarningsNotAsErrors, ",", ';', ','); // It's a good idea for the response file to be the very last switch passed, just // from a predictability perspective. It also solves the problem that a dogfooder // ran into, which is described in an email thread attached to bug VSWhidbey 146883. // See also bugs 177762 and 118307 for additional bugs related to response file position. if (ResponseFiles != null) { foreach (ITaskItem response in ResponseFiles) { commandLine.AppendSwitchIfNotNull("@", response.ItemSpec); } } }
/// <summary> /// Adds a "/features:" switch to the command line for each provided feature. /// </summary> internal static void AddFeatures(CommandLineBuilderExtension commandLine, string features) { if (string.IsNullOrEmpty(features)) { return; } foreach (var feature in CompilerOptionParseUtilities.ParseFeatureFromMSBuild(features)) { commandLine.AppendSwitchIfNotNull("/features:", feature.Trim()); } }
internal static void AddReferencesToCommandLine( CommandLineBuilderExtension commandLine, ITaskItem[]?references, bool isInteractive = false) { // If there were no references passed in, don't add any /reference: switches // on the command-line. if (references == null) { return; } // Loop through all the references passed in. We'll be adding separate // /reference: switches for each reference, and in some cases even multiple // /reference: switches per reference. foreach (ITaskItem reference in references) { // See if there was an "Alias" attribute on the reference. string aliasString = reference.GetMetadata("Aliases"); string switchName = "/reference:"; if (!isInteractive) { bool embed = Utilities.TryConvertItemMetadataToBool(reference, "EmbedInteropTypes"); if (embed) { switchName = "/link:"; } } if (string.IsNullOrEmpty(aliasString)) { // If there was no "Alias" attribute, just add this as a global reference. commandLine.AppendSwitchIfNotNull(switchName, reference.ItemSpec); } else { // If there was an "Alias" attribute, it contains a comma-separated list // of aliases to use for this reference. For each one of those aliases, // we're going to add a separate /reference: switch to the csc.exe // command-line string[] aliases = aliasString.Split(','); foreach (string alias in aliases) { // Trim whitespace. string trimmedAlias = alias.Trim(); if (alias.Length == 0) { continue; } // The alias should be a valid C# identifier. Therefore it cannot // contain comma, space, semicolon, or double-quote. Let's check for // the existence of those characters right here, and bail immediately // if any are present. There are a whole bunch of other characters // that are not allowed in a C# identifier, but we'll just let csc.exe // error out on those. The ones we're checking for here are the ones // that could seriously screw up the command-line parsing or could // allow parameter injection. if (trimmedAlias.IndexOfAny(new char[] { ',', ' ', ';', '"' }) != -1) { throw Utilities.GetLocalizedArgumentException( ErrorString.Csc_AssemblyAliasContainsIllegalCharacters, reference.ItemSpec, trimmedAlias); } // The alias called "global" is special. It means that we don't // give it an alias on the command-line. if (string.Compare("global", trimmedAlias, StringComparison.OrdinalIgnoreCase) == 0) { commandLine.AppendSwitchIfNotNull(switchName, reference.ItemSpec); } else { // We have a valid (and explicit) alias for this reference. Add // it to the command-line using the syntax: // /reference:Goo=System.Xml.dll commandLine.AppendSwitchAliased(switchName, trimmedAlias, reference.ItemSpec); } } } } }
/// <summary> /// Adds a "/additionalfile:" switch to the command line for each additional file. /// </summary> private void AddAdditionalFilesToCommandLine(CommandLineBuilderExtension commandLine) { // If there were no additional files passed in, don't add any /additionalfile: switches // on the command-line. if (AdditionalFiles == null) { return; } foreach (ITaskItem additionalFile in AdditionalFiles) { commandLine.AppendSwitchIfNotNull("/additionalfile:", additionalFile.ItemSpec); } }
protected override string GenerateResponseFileCommands() { var commandLineBuilder = new CommandLineBuilderExtension(); AddResponseFileCommands(commandLineBuilder); return commandLineBuilder.ToString(); }
/// <summary> /// Fills the provided CommandLineBuilderExtension with those switches and other information that can go into a response file. /// </summary> protected virtual void AddResponseFileCommands(CommandLineBuilderExtension commandLine) { commandLine.AppendSwitch("/i-"); ManagedCompiler.AddFeatures(commandLine, Features); if (ResponseFiles != null) { foreach (var response in ResponseFiles) { commandLine.AppendSwitchIfNotNull("@", response.ItemSpec); } } commandLine.AppendFileNameIfNotNull(Source); if (ScriptArguments != null) { foreach (var scriptArgument in ScriptArguments) { commandLine.AppendTextUnquoted(scriptArgument); } } if (ResponseFiles != null) { foreach (var scriptResponse in ScriptResponseFiles) { commandLine.AppendSwitchIfNotNull("@", scriptResponse.ItemSpec); } } }