[xbuild] Fix warnings.
[mono.git] / mcs / tools / xbuild / SolutionParser.cs
index c2cece8e9afc9c2e1c48f80df261e53b9bfc5f72..b9f5090e2a4e8162ab250abdd8c861d88b932751 100644 (file)
@@ -32,6 +32,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.Linq;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.IO;
@@ -92,7 +93,7 @@ namespace Mono.XBuild.CommandLine {
                static string guidExpression = "{[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}}";
 
                static Regex slnVersionRegex = new Regex (@"Microsoft Visual Studio Solution File, Format Version (\d?\d.\d\d)");
-               static Regex projectRegex = new Regex ("Project\\(\"(" + guidExpression + ")\"\\) = \"(.*?)\", \"(.*?)\", \"(" + guidExpression + ")\"(\\s*?)((\\s*?)ProjectSection\\((.*?)\\) = (.*?)EndProjectSection(\\s*?))*(\\s*?)EndProject?", RegexOptions.Singleline);
+               static Regex projectRegex = new Regex ("Project\\(\"(" + guidExpression + ")\"\\) = \"(.*?)\", \"(.*?)\", \"(" + guidExpression + ")\"(\\s*?)((\\s*?)ProjectSection\\((.*?)\\) = (.*?)EndProjectSection(\\s*?))*(\\s*?)(EndProject)?", RegexOptions.Singleline);
                static Regex projectDependenciesRegex = new Regex ("ProjectSection\\((.*?)\\) = \\w*(.*?)EndProjectSection", RegexOptions.Singleline);
                static Regex projectDependencyRegex = new Regex ("\\s*(" + guidExpression + ") = (" + guidExpression + ")");
                static Regex projectSectionPropertiesRegex = new Regex ("\\s*(?<name>.*) = \"(?<value>.*)\"");
@@ -132,10 +133,13 @@ namespace Mono.XBuild.CommandLine {
                        Dictionary<Guid, ProjectInfo> projectInfos = new Dictionary<Guid, ProjectInfo> ();
                        Dictionary<Guid, ProjectInfo> websiteProjectInfos = new Dictionary<Guid, ProjectInfo> ();
                        List<ProjectInfo>[] infosByLevel = null;
+                       Dictionary<Guid, ProjectInfo> unsupportedProjectInfos = new Dictionary<Guid, ProjectInfo> ();
 
                        Match m = projectRegex.Match (line);
                        while (m.Success) {
-                               ProjectInfo projectInfo = new ProjectInfo (m.Groups[2].Value, m.Groups[3].Value);
+                               ProjectInfo projectInfo = new ProjectInfo (m.Groups[2].Value,
+                                                               Path.GetFullPath (Path.Combine (solutionDir,
+                                                                       m.Groups [3].Value.Replace ('\\', Path.DirectorySeparatorChar))));
                                if (String.Compare (m.Groups [1].Value, solutionFolderGuid,
                                                StringComparison.InvariantCultureIgnoreCase) == 0) {
                                        // Ignore solution folders
@@ -143,10 +147,14 @@ namespace Mono.XBuild.CommandLine {
                                        continue;
                                }
 
+                               projectInfo.Guid = new Guid (m.Groups [4].Value);
+
                                if (String.Compare (m.Groups [1].Value, vcprojGuid,
                                                StringComparison.InvariantCultureIgnoreCase) == 0) {
                                        // Ignore vcproj 
                                        RaiseWarning (0, string.Format("Ignoring vcproj '{0}'.", projectInfo.Name));
+
+                                       unsupportedProjectInfos [projectInfo.Guid] = projectInfo;
                                        m = m.NextMatch ();
                                        continue;
                                }
@@ -155,9 +163,7 @@ namespace Mono.XBuild.CommandLine {
                                                StringComparison.InvariantCultureIgnoreCase) == 0)
                                        websiteProjectInfos.Add (new Guid (m.Groups[4].Value), projectInfo);
                                else
-                                       projectInfos.Add (new Guid (m.Groups[4].Value), projectInfo);
-
-                               projectInfo.Guid = new Guid (m.Groups [4].Value);
+                                       projectInfos.Add (projectInfo.Guid, projectInfo);
 
                                Match projectSectionMatch = projectDependenciesRegex.Match (m.Groups[6].Value);
                                while (projectSectionMatch.Success) {
@@ -189,8 +195,8 @@ namespace Mono.XBuild.CommandLine {
                        }
 
                        foreach (ProjectInfo projectInfo in projectInfos.Values) {
-                               string filename = Path.Combine (solutionDir,
-                                                       projectInfo.FileName.Replace ('\\', Path.DirectorySeparatorChar));
+                               string filename = projectInfo.FileName;
+                               string projectDir = Path.GetDirectoryName (filename);
 
                                if (!File.Exists (filename)) {
                                        RaiseWarning (0, String.Format ("Project file {0} referenced in the solution file, " +
@@ -200,19 +206,53 @@ namespace Mono.XBuild.CommandLine {
 
                                Project currentProject = p.ParentEngine.CreateNewProject ();
                                try {
-                                       currentProject.Load (filename);
+                                       currentProject.Load (filename, ProjectLoadSettings.IgnoreMissingImports);
                                } catch (InvalidProjectFileException e) {
                                        RaiseWarning (0, e.Message);
                                        continue;
                                }
 
                                foreach (BuildItem bi in currentProject.GetEvaluatedItemsByName ("ProjectReference")) {
+                                       ProjectInfo info = null;
                                        string projectReferenceGuid = bi.GetEvaluatedMetadata ("Project");
-                                       Guid guid = new Guid (projectReferenceGuid);
-                                       ProjectInfo info;
-                                       if (projectInfos.TryGetValue (guid, out info))
-                                               // ignore if not found
-                                               projectInfo.Dependencies [guid] = info;
+                                       bool hasGuid = !String.IsNullOrEmpty (projectReferenceGuid);
+
+                                       // try to resolve the ProjectReference by GUID
+                                       // and fallback to project filename
+
+                                       if (hasGuid) {
+                                               Guid guid = new Guid (projectReferenceGuid);
+                                               projectInfos.TryGetValue (guid, out info);
+                                               if (info == null && unsupportedProjectInfos.TryGetValue (guid, out info)) {
+                                                       RaiseWarning (0, String.Format (
+                                                                       "{0}: ProjectReference '{1}' is of an unsupported type. Ignoring.",
+                                                                       filename, bi.Include));
+                                                       continue;
+                                               }
+                                       }
+
+                                       if (info == null || !hasGuid) {
+                                               // Project not found by guid or guid not available
+                                               // Try to find by project file
+
+                                               string fullpath = Path.GetFullPath (Path.Combine (projectDir, bi.Include.Replace ('\\', Path.DirectorySeparatorChar)));
+                                               info = projectInfos.Values.FirstOrDefault (pi => pi.FileName == fullpath);
+
+                                               if (info == null) {
+                                                       if (unsupportedProjectInfos.Values.Any (pi => pi.FileName == fullpath))
+                                                               RaiseWarning (0, String.Format (
+                                                                               "{0}: ProjectReference '{1}' is of an unsupported type. Ignoring.",
+                                                                               filename, bi.Include));
+                                                       else
+                                                               RaiseWarning (0, String.Format (
+                                                                               "{0}: ProjectReference '{1}' not found, neither by guid '{2}' nor by project file name '{3}'.",
+                                                                               filename, bi.Include, projectReferenceGuid.Replace ("{", "").Replace ("}", ""), fullpath));
+                                               }
+
+                                       }
+
+                                       if (info != null)
+                                               projectInfo.Dependencies [info.Guid] = info;
                                }
                        }
 
@@ -250,6 +290,8 @@ namespace Mono.XBuild.CommandLine {
                                                break;
                                        case "NestedProjects":
                                                break;
+                                       case "MonoDevelopProperties":
+                                               break;
                                        default:
                                                RaiseWarning (0, string.Format("Don't know how to handle GlobalSection {0}, Ignoring.", sectionType));
                                                break;
@@ -271,7 +313,6 @@ namespace Mono.XBuild.CommandLine {
 
                 string GetSlnFileVersion (StreamReader reader)
                 {
-                        string strVersion = null;
                         string strInput = null;
                         Match match;
 
@@ -709,20 +750,10 @@ namespace Mono.XBuild.CommandLine {
                                        string target_name = GetTargetNameForProject (project.Name, buildTarget);
                                        Target target = p.Targets.AddNewTarget (target_name);
                                        target.Condition = "'$(CurrentSolutionConfigurationContents)' != ''"; 
-
-                                       if (project.Dependencies.Count > 0) {
-                                               StringBuilder dependencies = new StringBuilder ();
-                                               foreach (ProjectInfo dependentInfo in project.Dependencies.Values) {
-                                                       if (dependencies.Length > 0)
-                                                               dependencies.Append (";");
-                                                       if (IsBuildTargetName (dependentInfo.Name))
-                                                               dependencies.Append ("Solution:");
-                                                       dependencies.Append (dependentInfo.Name);
-                                                       if (buildTarget != "Build")
-                                                               dependencies.Append (":" + buildTarget);
-                                               }
-                                               target.DependsOnTargets = dependencies.ToString ();
-                                       }
+                                       if (project.Dependencies.Count > 0)
+                                               target.DependsOnTargets = String.Join (";",
+                                                               project.Dependencies.Values.Select (
+                                                                       di => GetTargetNameForProject (di.Name, buildTarget)).ToArray ());
 
                                        foreach (TargetInfo targetInfo in solutionTargets) {
                                                BuildTask task = null;