// NAnt - A .NET build tool
// Copyright (C) 2001-2002 Gerry Shaw
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Gerry Shaw (gerry_shaw@yahoo.com)
// Mike Krueger (mike@icsharpcode.net)
using System.IO;
using System.Text.RegularExpressions;
using NAnt.Core;
using NAnt.Core.Attributes;
using NAnt.Core.Types;
namespace NAnt.DotNet.Tasks {
///
/// Compiles C# programs.
///
///
/// Compile helloworld.cs to helloworld.exe.
///
///
///
///
///
///
/// ]]>
///
///
[TaskName("csc")]
public class CscTask : FXCompilerBase {
#region Private Instance Fields
string _doc = null;
bool _nostdlib = false;
bool _noconfig = false;
bool _checked = false;
bool _unsafe = false;
bool _optimize = false;
string _warningLevel = null;
string _noWarn = null;
string _codepage = null;
string _lib = null;
#endregion Private Instance Fields
#region Public Instance Properties
///
/// Folders to look in for references resolution.
///
///
///
/// Corresponds with the /lib: flag.
///
///
[TaskAttribute("lib")]
public string Lib { get { return _lib; } set {_lib = value; } }
///
/// The name of the XML documentation file to generate.
///
///
///
/// Corresponds with the /doc: flag.
///
///
[TaskAttribute("doc")]
public string Doc { get { return _doc; } set {_doc = value; } }
///
/// Instructs the compiler not to import mscorlib.dll (true/false). Default is "false".
///
///
///
/// Corresponds with the /nostdlib[+|-] flag.
///
///
[TaskAttribute("nostdlib")]
public bool NoStdLib { get { return _nostdlib; } set {_nostdlib = value; } }
///
/// Instructs the compiler not to use implicit references to assemblies (true/false). Default is "false".
///
///
///
/// Corresponds with the /noconfig flag.
///
///
[TaskAttribute("noconfig")]
public bool NoConfig { get { return _noconfig; } set {_noconfig = value; } }
///
/// Specifies whether an integer arithmetic statement that is not in the scope of the
/// checked or unchecked keywords and that results in a value outside the
/// range of the data type should cause a run-time exception (true/false).
/// Default is "false".
///
///
/// Corresponds with the /checked[+|-] flag.
///
///
[TaskAttribute("checked")]
public bool Checked { get { return _checked; } set {_checked = value; } }
///
/// Instructs the compiler to allow code that uses the unsafe keyword
/// (true/false). Default is "false".
///
///
///
/// Corresponds with the /unsafe[+|-] flag.
///
///
[TaskAttribute("unsafe")]
public bool Unsafe { get { return _unsafe; } set {_unsafe = value; } }
///
/// Specifies whether the compiler should perform optimizations to the make
/// output files smaller, faster, and more effecient.
///
///
/// The value of this attribute must be either true or false.
/// If false, the switch is omitted.
///
///
///
/// Corresponds with the /optimize[+|-] flag.
///
///
[TaskAttribute("optimize")]
[BooleanValidator()]
public bool Optimize{ get { return _optimize; } set {_optimize = value;}}
///
/// Specifies the warning level for the compiler to display. Valid values are 0-4. Default is 4.
///
/// The warning level for the compiler to display.
///
///
/// Corresponds with the /warn flag.
///
///
[TaskAttribute("warninglevel")]
[Int32Validator(0, 4)]
public string WarningLevel { get { return _warningLevel; } set {_warningLevel = value;}}
///
/// Specifies a comma-separated list of warnings that should be suppressed
/// by the compiler.
///
/// Comma-separated list of warnings that should be suppressed by the compiler.
///
///
/// Corresponds with the /nowarn flag.
///
///
[TaskAttribute("nowarn")]
public string NoWarn { get { return _noWarn; } set {_noWarn = value;}}
///
/// Specifies the code page to use for all source code files in the compilation.
///
///
///
/// Corresponds with the /codepage flag.
///
///
[TaskAttribute("codepage")]
public string Codepage {
get { return _codepage; }
set { _codepage = value; }
}
#endregion Public Instance Properties
#region Override implementation of ExternalProgramBase
///
/// Gets the name of the executable that should be used to launch the
/// external program.
///
///
/// The name of the executable that should be used to launch the
/// external program.
///
///
/// If a current framework is defined, the name of the executable will
/// be retrieved from the configuration of the framework; otherwise the
/// will be used.
///
protected override string ExeName {
get {
if (Project.CurrentFramework != null) {
return Project.CurrentFramework.CSharpCompilerName;
} else {
return Name;
}
}
}
///
/// Gets a value indicating whether the external program should be executed
/// using a runtime engine, if configured.
///
///
/// true if the program should be executed using a runtime engine;
/// otherwise, false.
///
protected override bool UsesRuntimeEngine {
get {
if (Project.CurrentFramework != null) {
// TO-DO : find better of doing this than relying on the name of the framework
if (Project.CurrentFramework.Name.IndexOf("mono", 0) != -1) {
return true;
}
}
return false;
}
}
#endregion Override implementation of ExternalProgramBase
#region Override implementation of CompilerBase
///
/// Writes the compiler options to the specified .
///
/// to which the compiler options should be written.
protected override void WriteOptions(TextWriter writer) {
WriteOption(writer, "fullpaths");
if (Doc != null) {
WriteOption(writer, "doc", this.Doc);
}
if (Lib != null) {
WriteOption(writer, "lib", this.Lib);
}
if (Debug) {
WriteOption(writer, "debug");
WriteOption(writer, "define", "DEBUG");
WriteOption(writer, "define", "TRACE");
}
if (NoStdLib) {
WriteOption(writer, "nostdlib");
}
if (Checked) {
WriteOption(writer, "checked");
}
if (Unsafe) {
WriteOption(writer, "unsafe");
}
if (Optimize) {
WriteOption(writer, "optimize");
}
if (WarningLevel != null) {
WriteOption(writer, "warn", WarningLevel);
}
if (NoWarn != null) {
WriteOption(writer, "nowarn", NoWarn);
}
if (Codepage != null) {
WriteOption(writer, "codepage", Codepage);
}
if (NoConfig && !Arguments.Contains("/noconfig")) {
Arguments.Add(new Argument("/noconfig"));
}
}
///
/// Gets the file extension required by the current compiler.
///
/// For the C# compiler, the file extension is always cs.
protected override string Extension {
get { return "cs"; }
}
#endregion Override implementation of CompilerBase
}
}