Ported MSBuild task to .NET 2.0 and included the project in the VS solution.

This commit is contained in:
jfrijters 2012-12-13 10:11:17 +00:00
Родитель a9356752fd
Коммит caf6b6d0c4
7 изменённых файлов: 109 добавлений и 229 удалений

Просмотреть файл

@ -38,6 +38,8 @@
<delete failonerror="false">
<fileset>
<include name="bin/IKVM.AWT.WinForms.dll" />
<include name="bin/IKVM.MSBuild.dll" />
<include name="bin/IKVM.MSBuild.Java.Targets" />
<include name="bin/IKVM.Reflection.dll" />
<include name="bin/IKVM.Runtime.dll" />
<include name="bin/IKVM.Runtime.JNI.dll" />

Просмотреть файл

@ -16,6 +16,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IKVM.Runtime.JNI.8", "runti
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IKVM.Reflection", "reflect\IKVM.Reflection.csproj", "{4CB170EF-DFE6-4A56-9E1B-A85449E827A7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IKVM.MSBuild", "msbuild\IKVM.MSBuild.csproj", "{D7FF23BC-49C2-490D-B702-8EEE8F5AF296}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -63,6 +65,11 @@ Global
{4CB170EF-DFE6-4A56-9E1B-A85449E827A7}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{4CB170EF-DFE6-4A56-9E1B-A85449E827A7}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{4CB170EF-DFE6-4A56-9E1B-A85449E827A7}.Debug|Win32.ActiveCfg = Debug|Any CPU
{D7FF23BC-49C2-490D-B702-8EEE8F5AF296}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D7FF23BC-49C2-490D-B702-8EEE8F5AF296}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7FF23BC-49C2-490D-B702-8EEE8F5AF296}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{D7FF23BC-49C2-490D-B702-8EEE8F5AF296}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{D7FF23BC-49C2-490D-B702-8EEE8F5AF296}.Debug|Win32.ActiveCfg = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Просмотреть файл

@ -37,4 +37,4 @@ using System.Runtime.InteropServices;
[assembly: AssemblyTitle("IKVM.MSBuild")]
[assembly: AssemblyDescription("Java build task for .NET")]
[assembly: System.Security.AllowPartiallyTrustedCallers]
//[assembly: System.Security.AllowPartiallyTrustedCallers]

Просмотреть файл

@ -12,11 +12,10 @@ IKVM.Java.targets
<Target Name="CreateManifestResourceNames"></Target>
<Target Name="CoreCompile">
<JavaTask
<Target Name="CoreCompile" DependsOnTargets="ResolveProjectReferences;ResolveAssemblyReferences">
<JavaTask
EmitDebugInformation="$(DebugSymbols)"
References="@(Reference)"
TargetFrameworkVersion="$(TargetFrameworkVersion)"
References="@(_ResolveAssemblyReferenceResolvedFiles)"
Sources="@(Compile)"
Platform="$(PlatformTarget)"
Configuration="$(Configuration)"
@ -27,10 +26,6 @@ IKVM.Java.targets
/>
</Target>
<PropertyGroup>
<CopyBuildOutputToOutputDirectory >false</CopyBuildOutputToOutputDirectory>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.Common.targets"/>
</Project>

Просмотреть файл

@ -1,33 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<Project ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<ProjectGuid>{D7FF23BC-49C2-490D-B702-8EEE8F5AF296}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<OutputType>Library</OutputType>
<RootNamespace>IKVM.MSBuild</RootNamespace>
<AssemblyName>IKVM.MSBuild</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<AppDesignerFolder>Properties</AppDesignerFolder>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'x86' ">
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<OutputPath>bin\Debug\</OutputPath>
<DebugSymbols>True</DebugSymbols>
<DebugType>Full</DebugType>
<Optimize>False</Optimize>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>bin\Release\</OutputPath>
<DebugSymbols>False</DebugSymbols>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>TRACE</DefineConstants>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="IKVM.OpenJDK.Core">
@ -38,10 +30,8 @@
</Reference>
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Build.Tasks" />
<Reference Include="Microsoft.Build.Utilities.v4.0" />
<Reference Include="Microsoft.Build.Utilities" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
@ -49,7 +39,7 @@
</ItemGroup>
<ItemGroup>
<None Include="IKVM.MSBuild.Java.Targets">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />

Просмотреть файл

@ -1,5 +1,6 @@
/*
Copyright (C) 2012 Volker Berlin (i-net software)
Copyright (C) 2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -39,25 +40,18 @@ namespace IKVM.MSBuild
/// <summary>
/// Java compiler task.
/// </summary>
public class JavaTask : ToolTask
public sealed class JavaTask : ToolTask
{
private ITaskItem[] sources;
private ITaskItem[] references;
private string[] references;
private ITaskItem[] resources;
private string targetFrameworkVersion;
private string configuration;
private string configuration = "Debug";
private string targetType;
private string outputPath;
private string mainFile;
private string outputAssembly;
private bool emitDebugInformation;
private string platform;
private string temp;
public JavaTask()
{
}
private string platform = "x86";
/// <summary>
/// Gets or sets the source files that will be compiled. Is called from script.
@ -144,52 +138,48 @@ namespace IKVM.MSBuild
/// <summary>
/// Gets or sets the assembly references. Is called from script.
/// </summary>
public ITaskItem[] References
public string[] References
{
get { return references; }
set { references = value; }
}
/// <summary>
/// Gets or sets the target framework version. Is called from script.
/// </summary>
public string TargetFrameworkVersion
{
get { return targetFrameworkVersion; }
set { if (value != null && value.StartsWith("v")) targetFrameworkVersion = value.Substring(1); }
}
protected override string ToolName
{
get
{
return "ikvmc.exe";
}
get { return "ikvmc.exe"; }
}
protected override string GenerateFullPathToTool()
{
string location = Assembly.GetAssembly(this.GetType()).Location;
location = new FileInfo(location).DirectoryName;
string path = Path.GetFullPath(Path.Combine(location, ToolName));
Log.LogWarning(path);
return path;
return Path.Combine(GetAssemblyPath(), ToolName);
}
private static string GetAssemblyPath()
{
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
private string GetIntermediatePath()
{
return Path.GetFullPath(Path.Combine(Path.Combine("obj", platform), configuration));
}
protected override string GenerateCommandLineCommands()
{
CommandLineBuilder commandLine = new CommandLineBuilder();
if (EmitDebugInformation)
commandLine.AppendSwitch("-nologo");
if (EmitDebugInformation)
{
commandLine.AppendSwitch("-debug");
}
commandLine.AppendSwitch("-nostdlib");
if (((outputAssembly == null) && (Sources != null)) && ((Sources.Length > 0)))
if (((outputAssembly == null) && (sources != null)) && ((sources.Length > 0)))
{
outputAssembly = Path.GetFileNameWithoutExtension(this.Sources[0].ItemSpec);
outputAssembly = Path.GetFileNameWithoutExtension(sources[0].ItemSpec);
}
if (string.Equals(this.TargetType, "library", StringComparison.OrdinalIgnoreCase))
{
@ -202,87 +192,33 @@ namespace IKVM.MSBuild
if (references != null)
{
HashSet<string> addedReferences = new HashSet<string>();
foreach (ITaskItem item in references)
foreach (string reference in references)
{
string fileName = item.ItemSpec;
if (IsIkvmStandardLibrary(fileName))
if (IsIkvmStandardLibrary(reference))
{
continue;
}
string hintPath = item.GetMetadata("HintPath");
if (hintPath != null && hintPath.Length != 0)
{
fileName = Path.GetFullPath(Path.Combine(GetCurrentFolder(), hintPath));
}
else
{
string versionStr;
string requiredTargetFramework = item.GetMetadata("RequiredTargetFramework");
if (requiredTargetFramework != null && requiredTargetFramework.Length != 0)
{
versionStr = requiredTargetFramework;
}
else
{
versionStr = targetFrameworkVersion;
}
IList<String> pathes = ToolLocationHelper.GetPathToReferenceAssemblies(".NETFramework", versionStr, "");
foreach (String path in pathes)
{
string frameworkFileName = Path.Combine(path, fileName + ".dll");
if (File.Exists(frameworkFileName))
{
fileName = frameworkFileName;
break;
}
}
}
if (addedReferences.Add(fileName))
{
commandLine.AppendSwitchIfNotNull("-reference:", fileName);
}
commandLine.AppendSwitchIfNotNull("-reference:", reference);
}
}
if (Resources != null)
{
foreach (ITaskItem item in Resources)
{
commandLine.AppendSwitch("-resource:" + item.ItemSpec + "=" + GetFullPath(item.ItemSpec));
}
}
if (resources != null)
{
foreach (ITaskItem item in resources)
{
commandLine.AppendSwitch("-resource:" + item.ItemSpec + "=" + Path.GetFullPath(item.ItemSpec));
}
}
commandLine.AppendSwitchIfNotNull("-out:", Path.Combine(outputPath, OutputAssembly));
commandLine.AppendSwitchIfNotNull("-out:", Path.Combine(GetIntermediatePath(), OutputAssembly));
if (TargetType != null)
{
switch (TargetType.ToLower())
{
case "library":
commandLine.AppendSwitch("-target:library");
break;
case "module":
commandLine.AppendSwitch("-target:module");
break;
case "exe":
commandLine.AppendSwitch("-target:exe");
break;
case "winexe":
commandLine.AppendSwitch("-target:winexe");
break;
}
commandLine.AppendSwitch("-target:" + TargetType.ToLower());
}
commandLine.AppendSwitch("-recurse:" + Path.Combine(temp, "*.*"));
Log.LogWarning(commandLine.ToString(), null);
commandLine.AppendSwitch("-recurse:" + Path.Combine(GetIntermediatePath(), "*.*"));
//Log.LogWarning(commandLine.ToString(), null);
return commandLine.ToString();
}
@ -303,26 +239,25 @@ namespace IKVM.MSBuild
private bool RunJavac()
{
List<String> paramList = new List<String>();
List<string> paramList = new List<string>();
temp = GetFullPath(Path.Combine("obj", platform, configuration));
paramList.Add("-d");
paramList.Add(temp);
paramList.Add(GetIntermediatePath());
if (sources != null)
{
for (int i = 0; i < sources.Length; i++)
{
string sourceFile = GetFullPath(sources[i].ItemSpec);
string sourceFile = Path.GetFullPath(sources[i].ItemSpec);
RemoveBOM(sourceFile);
paramList.Add(sourceFile);
}
}
String[] parameters = paramList.ToArray();
PrintWriter pw = new PrintWriter(new LogWriter(Log), true);
int result = Javac.compile(parameters, pw);
pw.close();
return result == 0;
using (PrintWriter pw = new PrintWriter(new LogWriter(Log), true))
{
return Javac.compile(paramList.ToArray(), pw) == 0;
}
}
/// <summary>
@ -330,14 +265,10 @@ namespace IKVM.MSBuild
/// </summary>
private void CopyIkvm()
{
string location = Assembly.GetAssembly(this.GetType()).Location;
FileInfo[] ikvmFiles = new FileInfo(location).Directory.GetFiles("*.dll");
foreach (FileInfo file in ikvmFiles)
foreach (FileInfo file in new DirectoryInfo(GetAssemblyPath()).GetFiles("*.dll"))
{
string name = file.Name;
if (IsIkvmStandardLibrary(name.Substring(0, name.Length - 4)))
if (IsIkvmStandardLibrary(name))
{
FileInfo target = new FileInfo(Path.Combine(outputPath, name));
if (!target.Exists || file.Length != target.Length || file.CreationTime != target.CreationTime)
@ -363,84 +294,60 @@ namespace IKVM.MSBuild
/// <returns></returns>
private bool IsIkvmStandardLibrary(string fileName)
{
if (fileName.Equals("IKVM.Runtime"))
string name = Path.GetFileNameWithoutExtension(fileName);
if (name == "IKVM.Runtime")
{
return true;
}
if (fileName.Equals("IKVM.OpenJDK.Tools"))
if (name == "IKVM.OpenJDK.Tools")
{
return false;
}
if (fileName.StartsWith("IKVM.OpenJDK."))
if (name.StartsWith("IKVM.OpenJDK."))
{
return true;
}
return false;
}
/// <summary>
/// Gets the current folder where this task is being executed from.
/// </summary>
private string GetCurrentFolder()
{
return Directory.GetCurrentDirectory();
}
/// <summary>
/// Takes a relative path to a file and turns it into the full path using the current folder
/// as the base directory.
/// </summary>
private string GetFullPath(string fileName)
{
if (!Path.IsPathRooted(fileName))
{
return Path.GetFullPath(Path.Combine(GetCurrentFolder(), fileName));
}
return fileName;
}
/// <summary>
/// Java does not like a BOM at start of UTF8 coded files that we remove it
/// </summary>
/// <param name="fileName">The name of a Java source file</param>
private void RemoveBOM(string fileName)
{
FileStream original = File.OpenRead(fileName);
if (original.ReadByte() == 0xef && original.ReadByte() == 0xbb && original.ReadByte() == 0xbf)
{
//BOM detected
string copyName = fileName + ".nobom";
FileStream copy = File.OpenWrite(copyName);
byte[] buffer = new byte[4096];
int count;
while ((count = original.Read(buffer, 0, buffer.Length)) > 0)
{
copy.Write(buffer, 0, count);
}
copy.Close();
original.Close();
File.Delete(fileName + ".withbom");
File.Move(fileName, fileName + ".withbom");
File.Move(copyName, fileName);
File.Delete(fileName + ".withbom");
return;
}
original.Close();
using (FileStream original = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.Read))
{
if (original.ReadByte() == 0xef && original.ReadByte() == 0xbb && original.ReadByte() == 0xbf)
{
//BOM detected
string copyName = fileName + ".nobom";
using (FileStream copy = File.OpenWrite(copyName))
{
byte[] buffer = new byte[4096];
int count;
while ((count = original.Read(buffer, 0, buffer.Length)) > 0)
{
copy.Write(buffer, 0, count);
}
}
File.Delete(fileName + ".withbom");
File.Move(fileName, fileName + ".withbom");
File.Move(copyName, fileName);
File.Delete(fileName + ".withbom");
}
}
}
/// <summary>
/// Redirect the output of the Java Compiler to the MSBUILD output
/// </summary>
private class LogWriter : java.io.Writer
private sealed class LogWriter : java.io.Writer
{
private readonly StringBuilder builder = new StringBuilder();
private readonly TaskLoggingHelper log;
private string fileName;
private int lineNr;
private bool error = true;
internal LogWriter(TaskLoggingHelper log)
: base()
{
this.log = log;
}
@ -454,7 +361,7 @@ namespace IKVM.MSBuild
{
if (builder.Length > 0)
{
String msg = builder.ToString();
string msg = builder.ToString();
if (msg.EndsWith("\r\n"))
{
msg = msg.Substring(0, msg.Length - 2);
@ -465,50 +372,35 @@ namespace IKVM.MSBuild
int idx = msg.IndexOf(':', 2);
if (idx > 0)
{
fileName = msg.Substring(0, idx);
string fileName = msg.Substring(0, idx);
idx++;
int idx2 = msg.IndexOf(':', idx);
if (idx2 > 0)
{
int lineNr;
if (Int32.TryParse(msg.Substring(idx, idx2 - idx), out lineNr))
{
msg = msg.Substring(idx2 + 1);
idx = msg.IndexOf("error:");
if (idx >= 0)
{
error = true;
msg = msg.Substring(idx + 6).Trim();
}
log.LogError(null, null, null, fileName, lineNr, 0, lineNr, 0, msg);
}
else
{
idx = msg.IndexOf("warning:");
if (idx >= 0)
{
error = false;
msg = msg.Substring(idx + 8).Trim();
}
log.LogWarning(null, null, null, fileName, lineNr, 0, lineNr, 0, msg);
}
}
}
else
{
lineNr = 0;
}
}
else
{
lineNr = 0;
}
}
}
if (error)
{
log.LogError(null, null, null, fileName, lineNr, 0, lineNr, 0, msg);
}
else
{
log.LogWarning(null, null, null, fileName, lineNr, 0, lineNr, 0, msg);
}
builder.Clear();
builder.Length = 0;
}
}
@ -518,5 +410,4 @@ namespace IKVM.MSBuild
}
}
}
}

Просмотреть файл

@ -1,11 +1,7 @@
<?xml version="1.0"?>
<project name="msbuild" default="msbuild">
<target name="msbuild">
<property name="defs" value="STUB_GENERATOR;TRACE" />
<if test="${property::exists('signed')}">
<property name="defs" value="${defs};${signed}" />
</if>
<csc target="library" output="../bin/IKVM.MsBuild.dll" define="${defs}">
<csc target="library" output="../bin/IKVM.MSBuild.dll">
<sources>
<include name="AssemblyInfo.cs" />
<include name="JavaTask.cs" />
@ -14,12 +10,11 @@
<include name="../bin/IKVM.OpenJDK.Core.dll" asis="true" />
<include name="../bin/IKVM.OpenJDK.Tools.dll" asis="true" />
<include name="Microsoft.Build.Framework.dll" asis="true" />
<include name="Microsoft.Build.Tasks.v4.0.dll" asis="true" />
<include name="Microsoft.Build.Utilities.v4.0.dll" asis="true" />
<include name="Microsoft.Build.Tasks.dll" asis="true" />
<include name="Microsoft.Build.Utilities.dll" asis="true" />
<include name="System.dll" asis="true" />
</references>
</csc>
<copy file="IKVM.MSBuild.Java.Targets" tofile="../bin/IKVM.MSBuild.Java.Targets" overwrite="true">
</copy>
<copy file="IKVM.MSBuild.Java.Targets" tofile="../bin/IKVM.MSBuild.Java.Targets" overwrite="true" />
</target>
</project>