* xbuild/Microsoft.Common.targets: Emit list of files written
[mcs.git] / tools / xbuild / SolutionParser.cs
bloba9fecfd74ceb6d1fe9c8c88620242b98765f10f2
1 //
2 // SolutionParser.cs: Generates a project file from a solution file.
3 //
4 // Author:
5 // Jonathan Chambers (joncham@gmail.com)
6 // Ankit Jain <jankit@novell.com>
7 // Lluis Sanchez Gual <lluis@novell.com>
8 //
9 // (C) 2009 Jonathan Chambers
10 // Copyright 2008, 2009 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #if NET_2_0
33 using System;
34 using System.Collections.Generic;
35 using System.Text;
36 using System.Text.RegularExpressions;
37 using System.IO;
38 using Microsoft.Build.BuildEngine;
40 namespace Mono.XBuild.CommandLine {
41 class ProjectInfo {
42 public string Name;
43 public string FileName;
44 public Guid Guid;
46 public ProjectInfo (string name, string fileName)
48 Name = name;
49 FileName = fileName;
52 public Dictionary<TargetInfo, TargetInfo> TargetMap = new Dictionary<TargetInfo, TargetInfo> ();
53 public Dictionary<Guid, ProjectInfo> Dependencies = new Dictionary<Guid, ProjectInfo> ();
54 public Dictionary<string, ProjectSection> ProjectSections = new Dictionary<string, ProjectSection> ();
55 public List<string> AspNetConfigurations = new List<string> ();
58 class ProjectSection {
59 public string Name;
60 public Dictionary<string, string> Properties = new Dictionary<string, string> ();
62 public ProjectSection (string name)
64 Name = name;
68 struct TargetInfo {
69 public string Configuration;
70 public string Platform;
71 public bool Build;
73 public TargetInfo (string configuration, string platform)
74 : this (configuration, platform, false)
78 public TargetInfo (string configuration, string platform, bool build)
80 Configuration = configuration;
81 Platform = platform;
82 Build = build;
87 internal delegate void RaiseWarningHandler (int errorNumber, string message);
89 class SolutionParser {
90 static string[] buildTargets = new string[] { "Build", "Clean", "Rebuild", "Publish" };
92 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}}";
94 static Regex projectRegex = new Regex ("Project\\(\"(" + guidExpression + ")\"\\) = \"(.*?)\", \"(.*?)\", \"(" + guidExpression + ")\"(\\s*?)((\\s*?)ProjectSection\\((.*?)\\) = (.*?)EndProjectSection(\\s*?))*(\\s*?)EndProject?", RegexOptions.Singleline);
95 static Regex projectDependenciesRegex = new Regex ("ProjectSection\\((.*?)\\) = \\w*(.*?)EndProjectSection", RegexOptions.Singleline);
96 static Regex projectDependencyRegex = new Regex ("\\s*(" + guidExpression + ") = (" + guidExpression + ")");
97 static Regex projectSectionPropertiesRegex = new Regex ("\\s*(?<name>.*) = \"(?<value>.*)\"");
99 static Regex globalRegex = new Regex ("Global(.*)EndGlobal", RegexOptions.Singleline);
100 static Regex globalSectionRegex = new Regex ("GlobalSection\\((.*?)\\) = \\w*(.*?)EndGlobalSection", RegexOptions.Singleline);
102 static Regex solutionConfigurationRegex = new Regex ("\\s*(.*?)\\|(.*?) = (.*?)\\|(.+)");
103 static Regex projectConfigurationActiveCfgRegex = new Regex ("\\s*(" + guidExpression + ")\\.(.+?)\\|(.+?)\\.ActiveCfg = (.+?)\\|(.+)");
104 static Regex projectConfigurationBuildRegex = new Regex ("\\s*(" + guidExpression + ")\\.(.*?)\\|(.*?)\\.Build\\.0 = (.*?)\\|(.+)");
106 static string solutionFolderGuid = "{2150E333-8FDC-42A3-9474-1A3956D46DE8}";
107 static string vcprojGuid = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}";
108 static string websiteProjectGuid = "{E24C65DC-7377-472B-9ABA-BC803B73C61A}";
110 RaiseWarningHandler RaiseWarning;
112 public void ParseSolution (string file, Project p, RaiseWarningHandler RaiseWarning)
114 this.RaiseWarning = RaiseWarning;
115 AddGeneralSettings (file, p);
117 StreamReader reader = new StreamReader (file);
118 string line = reader.ReadToEnd ();
119 line = line.Replace ("\r\n", "\n");
120 string solutionDir = Path.GetDirectoryName (file);
122 List<TargetInfo> solutionTargets = new List<TargetInfo> ();
123 Dictionary<Guid, ProjectInfo> projectInfos = new Dictionary<Guid, ProjectInfo> ();
124 Dictionary<Guid, ProjectInfo> websiteProjectInfos = new Dictionary<Guid, ProjectInfo> ();
125 List<ProjectInfo>[] infosByLevel = null;
127 Match m = projectRegex.Match (line);
128 while (m.Success) {
129 ProjectInfo projectInfo = new ProjectInfo (m.Groups[2].Value, m.Groups[3].Value);
130 if (String.Compare (m.Groups [1].Value, solutionFolderGuid,
131 StringComparison.InvariantCultureIgnoreCase) == 0) {
132 // Ignore solution folders
133 m = m.NextMatch ();
134 continue;
137 if (String.Compare (m.Groups [1].Value, vcprojGuid,
138 StringComparison.InvariantCultureIgnoreCase) == 0) {
139 // Ignore vcproj
140 RaiseWarning (0, string.Format("Ignoring vcproj '{0}'.", projectInfo.Name));
141 m = m.NextMatch ();
142 continue;
145 if (String.Compare (m.Groups [1].Value, websiteProjectGuid,
146 StringComparison.InvariantCultureIgnoreCase) == 0)
147 websiteProjectInfos.Add (new Guid (m.Groups[4].Value), projectInfo);
148 else
149 projectInfos.Add (new Guid (m.Groups[4].Value), projectInfo);
151 projectInfo.Guid = new Guid (m.Groups [4].Value);
153 Match projectSectionMatch = projectDependenciesRegex.Match (m.Groups[6].Value);
154 while (projectSectionMatch.Success) {
155 string section_name = projectSectionMatch.Groups [1].Value;
156 if (String.Compare (section_name, "ProjectDependencies") == 0) {
157 Match projectDependencyMatch = projectDependencyRegex.Match (projectSectionMatch.Value);
158 while (projectDependencyMatch.Success) {
159 // we might not have projectInfo available right now, so
160 // set it to null, and fill it in later
161 projectInfo.Dependencies [new Guid (projectDependencyMatch.Groups[1].Value)] = null;
162 projectDependencyMatch = projectDependencyMatch.NextMatch ();
164 } else {
165 ProjectSection section = new ProjectSection (section_name);
166 Match propertiesMatch = projectSectionPropertiesRegex.Match (
167 projectSectionMatch.Groups [2].Value);
168 while (propertiesMatch.Success) {
169 section.Properties [propertiesMatch.Groups ["name"].Value] =
170 propertiesMatch.Groups ["value"].Value;
172 propertiesMatch = propertiesMatch.NextMatch ();
175 projectInfo.ProjectSections [section_name] = section;
177 projectSectionMatch = projectSectionMatch.NextMatch ();
179 m = m.NextMatch ();
182 foreach (ProjectInfo projectInfo in projectInfos.Values) {
183 string filename = Path.Combine (solutionDir,
184 projectInfo.FileName.Replace ('\\', Path.DirectorySeparatorChar));
186 if (!File.Exists (filename)) {
187 RaiseWarning (0, String.Format ("Project file {0} referenced in the solution file, " +
188 "not found. Ignoring.", filename));
189 continue;
192 Project currentProject = p.ParentEngine.CreateNewProject ();
193 try {
194 currentProject.Load (filename);
195 } catch (InvalidProjectFileException e) {
196 RaiseWarning (0, e.Message);
197 continue;
200 foreach (BuildItem bi in currentProject.GetEvaluatedItemsByName ("ProjectReference")) {
201 string projectReferenceGuid = bi.GetEvaluatedMetadata ("Project");
202 Guid guid = new Guid (projectReferenceGuid);
203 ProjectInfo info;
204 if (projectInfos.TryGetValue (guid, out info))
205 // ignore if not found
206 projectInfo.Dependencies [guid] = info;
210 // fill in the project info for deps found in the .sln file
211 foreach (ProjectInfo projectInfo in projectInfos.Values) {
212 List<Guid> missingInfos = new List<Guid> ();
213 foreach (KeyValuePair<Guid, ProjectInfo> dependency in projectInfo.Dependencies) {
214 if (dependency.Value == null)
215 missingInfos.Add (dependency.Key);
218 foreach (Guid guid in missingInfos) {
219 ProjectInfo info;
220 if (projectInfos.TryGetValue (guid, out info))
221 projectInfo.Dependencies [guid] = info;
222 else
223 projectInfo.Dependencies.Remove (guid);
227 Match globalMatch = globalRegex.Match (line);
228 Match globalSectionMatch = globalSectionRegex.Match (globalMatch.Groups[1].Value);
229 while (globalSectionMatch.Success) {
230 string sectionType = globalSectionMatch.Groups[1].Value;
231 switch (sectionType) {
232 case "SolutionConfigurationPlatforms":
233 ParseSolutionConfigurationPlatforms (globalSectionMatch.Groups[2].Value, solutionTargets);
234 break;
235 case "ProjectConfigurationPlatforms":
236 ParseProjectConfigurationPlatforms (globalSectionMatch.Groups[2].Value,
237 projectInfos, websiteProjectInfos);
238 break;
239 case "SolutionProperties":
240 ParseSolutionProperties (globalSectionMatch.Groups[2].Value);
241 break;
242 case "NestedProjects":
243 break;
244 default:
245 RaiseWarning (0, string.Format("Don't know how to handle GlobalSection {0}, Ignoring.", sectionType));
246 break;
248 globalSectionMatch = globalSectionMatch.NextMatch ();
251 int num_levels = AddBuildLevels (p, solutionTargets, projectInfos, ref infosByLevel);
253 AddCurrentSolutionConfigurationContents (p, solutionTargets, projectInfos, websiteProjectInfos);
254 AddWebsiteProperties (p, websiteProjectInfos, projectInfos);
255 AddValidateSolutionConfiguration (p);
257 AddGetFrameworkPathTarget (p);
258 AddWebsiteTargets (p, websiteProjectInfos, projectInfos, infosByLevel, solutionTargets);
259 AddProjectTargets (p, solutionTargets, projectInfos);
260 AddSolutionTargets (p, num_levels, websiteProjectInfos.Values);
263 void AddGeneralSettings (string solutionFile, Project p)
265 p.DefaultTargets = "Build";
266 p.InitialTargets = "ValidateSolutionConfiguration";
267 p.AddNewUsingTaskFromAssemblyName ("CreateTemporaryVCProject", "Microsoft.Build.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
268 p.AddNewUsingTaskFromAssemblyName ("ResolveVCProjectOutput", "Microsoft.Build.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
270 string solutionFilePath = Path.GetFullPath (solutionFile);
271 BuildPropertyGroup solutionPropertyGroup = p.AddNewPropertyGroup (true);
272 solutionPropertyGroup.AddNewProperty ("SolutionDir", Path.GetDirectoryName (solutionFilePath) + Path.DirectorySeparatorChar);
273 solutionPropertyGroup.AddNewProperty ("SolutionExt", Path.GetExtension (solutionFile));
274 solutionPropertyGroup.AddNewProperty ("SolutionFileName", Path.GetFileName (solutionFile));
275 solutionPropertyGroup.AddNewProperty ("SolutionName", Path.GetFileNameWithoutExtension (solutionFile));
276 solutionPropertyGroup.AddNewProperty ("SolutionPath", solutionFilePath);
279 void ParseSolutionConfigurationPlatforms (string section, List<TargetInfo> solutionTargets)
281 Match solutionConfigurationPlatform = solutionConfigurationRegex.Match (section);
282 while (solutionConfigurationPlatform.Success) {
283 string solutionConfiguration = solutionConfigurationPlatform.Groups[1].Value;
284 string solutionPlatform = solutionConfigurationPlatform.Groups[2].Value;
285 solutionTargets.Add (new TargetInfo (solutionConfiguration, solutionPlatform));
286 solutionConfigurationPlatform = solutionConfigurationPlatform.NextMatch ();
290 // ignores the website projects, in the websiteProjectInfos
291 void ParseProjectConfigurationPlatforms (string section, Dictionary<Guid, ProjectInfo> projectInfos,
292 Dictionary<Guid, ProjectInfo> websiteProjectInfos)
294 List<Guid> missingGuids = new List<Guid> ();
295 Match projectConfigurationPlatform = projectConfigurationActiveCfgRegex.Match (section);
296 while (projectConfigurationPlatform.Success) {
297 Guid guid = new Guid (projectConfigurationPlatform.Groups[1].Value);
298 ProjectInfo projectInfo;
299 if (!projectInfos.TryGetValue (guid, out projectInfo)) {
300 if (!missingGuids.Contains (guid)) {
301 if (!websiteProjectInfos.ContainsKey (guid))
302 // ignore website projects
303 RaiseWarning (0, string.Format("Failed to find project {0}", guid));
304 missingGuids.Add (guid);
306 projectConfigurationPlatform = projectConfigurationPlatform.NextMatch ();
307 continue;
309 string solConf = projectConfigurationPlatform.Groups[2].Value;
310 string solPlat = projectConfigurationPlatform.Groups[3].Value;
311 string projConf = projectConfigurationPlatform.Groups[4].Value;
312 string projPlat = projectConfigurationPlatform.Groups[5].Value;
313 // hack, what are they doing here?
314 if (projPlat == "Any CPU")
315 projPlat = "AnyCPU";
316 projectInfo.TargetMap.Add (new TargetInfo (solConf, solPlat), new TargetInfo (projConf, projPlat));
317 projectConfigurationPlatform = projectConfigurationPlatform.NextMatch ();
319 Match projectConfigurationPlatformBuild = projectConfigurationBuildRegex.Match (section);
320 while (projectConfigurationPlatformBuild.Success) {
321 Guid guid = new Guid (projectConfigurationPlatformBuild.Groups[1].Value);
322 ProjectInfo projectInfo;
323 if (!projectInfos.TryGetValue (guid, out projectInfo)) {
324 if (!missingGuids.Contains (guid)) {
325 RaiseWarning (0, string.Format("Failed to find project {0}", guid));
326 missingGuids.Add (guid);
328 projectConfigurationPlatformBuild = projectConfigurationPlatformBuild.NextMatch ();
329 continue;
331 string solConf = projectConfigurationPlatformBuild.Groups[2].Value;
332 string solPlat = projectConfigurationPlatformBuild.Groups[3].Value;
333 string projConf = projectConfigurationPlatformBuild.Groups[4].Value;
334 string projPlat = projectConfigurationPlatformBuild.Groups[5].Value;
335 // hack, what are they doing here?
336 if (projPlat == "Any CPU")
337 projPlat = "AnyCPU";
338 projectInfo.TargetMap[new TargetInfo (solConf, solPlat)] = new TargetInfo (projConf, projPlat, true);
339 projectConfigurationPlatformBuild = projectConfigurationPlatformBuild.NextMatch ();
343 void ParseSolutionProperties (string section)
347 void AddCurrentSolutionConfigurationContents (Project p, List<TargetInfo> solutionTargets,
348 Dictionary<Guid, ProjectInfo> projectInfos,
349 Dictionary<Guid, ProjectInfo> websiteProjectInfos)
351 TargetInfo default_target_info = new TargetInfo ("Debug", "Any CPU");
352 if (solutionTargets.Count > 0) {
353 bool found = false;
354 foreach (TargetInfo tinfo in solutionTargets) {
355 if (String.Compare (tinfo.Platform, "Mixed Platforms") == 0) {
356 default_target_info = tinfo;
357 found = true;
358 break;
362 if (!found)
363 default_target_info = solutionTargets [0];
366 AddDefaultSolutionConfiguration (p, default_target_info);
368 foreach (TargetInfo solutionTarget in solutionTargets) {
369 BuildPropertyGroup platformPropertyGroup = p.AddNewPropertyGroup (false);
370 platformPropertyGroup.Condition = string.Format (
371 " ('$(Configuration)' == '{0}') and ('$(Platform)' == '{1}') ",
372 solutionTarget.Configuration,
373 solutionTarget.Platform
376 StringBuilder solutionConfigurationContents = new StringBuilder ();
377 solutionConfigurationContents.Append ("<SolutionConfiguration xmlns=\"\">");
378 foreach (KeyValuePair<Guid, ProjectInfo> projectInfo in projectInfos) {
379 AddProjectConfigurationItems (projectInfo.Key, projectInfo.Value, solutionTarget, solutionConfigurationContents);
381 solutionConfigurationContents.Append ("</SolutionConfiguration>");
383 platformPropertyGroup.AddNewProperty ("CurrentSolutionConfigurationContents",
384 solutionConfigurationContents.ToString ());
388 void AddProjectConfigurationItems (Guid guid, ProjectInfo projectInfo, TargetInfo solutionTarget,
389 StringBuilder solutionConfigurationContents)
391 foreach (KeyValuePair<TargetInfo, TargetInfo> targetInfo in projectInfo.TargetMap) {
392 if (solutionTarget.Configuration == targetInfo.Key.Configuration &&
393 solutionTarget.Platform == targetInfo.Key.Platform) {
394 solutionConfigurationContents.AppendFormat (
395 "<ProjectConfiguration Project=\"{0}\">{1}|{2}</ProjectConfiguration>",
396 guid.ToString ("B").ToUpper (), targetInfo.Value.Configuration, targetInfo.Value.Platform);
401 void AddDefaultSolutionConfiguration (Project p, TargetInfo target)
403 BuildPropertyGroup configurationPropertyGroup = p.AddNewPropertyGroup (true);
404 configurationPropertyGroup.Condition = " '$(Configuration)' == '' ";
405 configurationPropertyGroup.AddNewProperty ("Configuration", target.Configuration);
407 BuildPropertyGroup platformPropertyGroup = p.AddNewPropertyGroup (true);
408 platformPropertyGroup.Condition = " '$(Platform)' == '' ";
409 platformPropertyGroup.AddNewProperty ("Platform", target.Platform);
411 // emit default for AspNetConfiguration also
412 BuildPropertyGroup aspNetConfigurationPropertyGroup = p.AddNewPropertyGroup (true);
413 aspNetConfigurationPropertyGroup.Condition = " ('$(AspNetConfiguration)' == '') ";
414 aspNetConfigurationPropertyGroup.AddNewProperty ("AspNetConfiguration", "$(Configuration)");
417 void AddWarningForMissingProjectConfiguration (Target target, string slnConfig, string slnPlatform, string projectName)
419 BuildTask task = target.AddNewTask ("Warning");
420 task.SetParameterValue ("Text",
421 String.Format ("The project configuration for project '{0}' corresponding " +
422 "to the solution configuration '{1}|{2}' was not found in the solution file.",
423 projectName, slnConfig, slnPlatform));
424 task.Condition = String.Format ("('$(Configuration)' == '{0}') and ('$(Platform)' == '{1}')",
425 slnConfig, slnPlatform);
429 // Website project methods
431 void AddWebsiteProperties (Project p, Dictionary<Guid, ProjectInfo> websiteProjectInfos,
432 Dictionary<Guid, ProjectInfo> projectInfos)
434 var propertyGroupByConfig = new Dictionary<string, BuildPropertyGroup> ();
435 foreach (KeyValuePair<Guid, ProjectInfo> infoPair in websiteProjectInfos) {
436 ProjectInfo info = infoPair.Value;
437 string projectGuid = infoPair.Key.ToString ();
439 ProjectSection section;
440 if (!info.ProjectSections.TryGetValue ("WebsiteProperties", out section)) {
441 RaiseWarning (0, String.Format ("Website project '{0}' does not have the required project section: WebsiteProperties. Ignoring project.", info.Name));
442 return;
445 //parse project references
446 string [] ref_guids = null;
447 string references;
448 if (section.Properties.TryGetValue ("ProjectReferences", out references)) {
449 ref_guids = references.Split (new char [] {';'}, StringSplitOptions.RemoveEmptyEntries);
450 for (int i = 0; i < ref_guids.Length; i ++) {
451 // "{guid}|foo.dll"
452 ref_guids [i] = ref_guids [i].Split ('|') [0];
454 Guid r_guid = new Guid (ref_guids [i]);
455 ProjectInfo ref_info;
456 if (projectInfos.TryGetValue (r_guid, out ref_info))
457 // ignore if not found
458 info.Dependencies [r_guid] = ref_info;
462 foreach (KeyValuePair<string, string> pair in section.Properties) {
463 //looking for -- ConfigName.AspNetCompiler.PropName
464 string [] parts = pair.Key.Split ('.');
465 if (parts.Length != 3 || String.Compare (parts [1], "AspNetCompiler") != 0)
466 continue;
468 string config = parts [0];
469 string propertyName = parts [2];
471 BuildPropertyGroup bpg;
472 if (!propertyGroupByConfig.TryGetValue (config, out bpg)) {
473 bpg = p.AddNewPropertyGroup (true);
474 bpg.Condition = String.Format (" '$(AspNetConfiguration)' == '{0}' ", config);
475 propertyGroupByConfig [config] = bpg;
478 bpg.AddNewProperty (String.Format ("Project_{0}_AspNet{1}", projectGuid, propertyName),
479 pair.Value);
481 if (!info.AspNetConfigurations.Contains (config))
482 info.AspNetConfigurations.Add (config);
487 // For WebSite projects
488 // The main "Build" target:
489 // 1. builds all non-website projects
490 // 2. calls target for website project
491 // - gets target path for the referenced projects
492 // - Resolves dependencies, satellites etc for the
493 // referenced project assemblies, and copies them
494 // to bin/ folder
495 void AddWebsiteTargets (Project p, Dictionary<Guid, ProjectInfo> websiteProjectInfos,
496 Dictionary<Guid, ProjectInfo> projectInfos, List<ProjectInfo>[] infosByLevel,
497 List<TargetInfo> solutionTargets)
499 foreach (ProjectInfo w_info in websiteProjectInfos.Values) {
500 // gets a linear list of dependencies
501 List<ProjectInfo> depInfos = new List<ProjectInfo> ();
502 foreach (List<ProjectInfo> pinfos in infosByLevel) {
503 foreach (ProjectInfo pinfo in pinfos)
504 if (w_info.Dependencies.ContainsKey (pinfo.Guid))
505 depInfos.Add (pinfo);
508 foreach (string buildTarget in new string [] {"Build", "Rebuild"})
509 AddWebsiteTarget (p, w_info, projectInfos, depInfos, solutionTargets, buildTarget);
511 // clean/publish are not supported for website projects
512 foreach (string buildTarget in new string [] {"Clean", "Publish"})
513 AddWebsiteUnsupportedTarget (p, w_info, depInfos, buildTarget);
517 void AddWebsiteTarget (Project p, ProjectInfo webProjectInfo,
518 Dictionary<Guid, ProjectInfo> projectInfos, List<ProjectInfo> depInfos,
519 List<TargetInfo> solutionTargets, string buildTarget)
521 string w_guid = webProjectInfo.Guid.ToString ().ToUpper ();
523 Target target = p.Targets.AddNewTarget (GetTargetNameForProject (webProjectInfo.Name, buildTarget));
524 target.Condition = "'$(CurrentSolutionConfigurationContents)' != ''";
525 target.DependsOnTargets = GetWebsiteDependsOnTarget (depInfos, buildTarget);
527 // this item collects all the references
528 string final_ref_item = String.Format ("Project_{0}_References{1}", w_guid,
529 buildTarget != "Build" ? "_" + buildTarget : String.Empty);
531 foreach (TargetInfo targetInfo in solutionTargets) {
532 int ref_num = 0;
533 foreach (ProjectInfo depInfo in depInfos) {
534 TargetInfo projectTargetInfo;
535 if (!depInfo.TargetMap.TryGetValue (targetInfo, out projectTargetInfo))
536 // Ignore, no config, so no target path
537 continue;
539 // GetTargetPath from the referenced project
540 AddWebsiteMSBuildTaskForReference (target, depInfo, projectTargetInfo, targetInfo,
541 final_ref_item, ref_num);
542 ref_num ++;
546 // resolve the references
547 AddWebsiteResolveAndCopyReferencesTasks (target, webProjectInfo, final_ref_item, w_guid);
550 // emits the MSBuild task to GetTargetPath for the referenced project
551 void AddWebsiteMSBuildTaskForReference (Target target, ProjectInfo depInfo, TargetInfo projectTargetInfo,
552 TargetInfo solutionTargetInfo, string final_ref_item, int ref_num)
554 BuildTask task = target.AddNewTask ("MSBuild");
555 task.SetParameterValue ("Projects", depInfo.FileName);
556 task.SetParameterValue ("Targets", "GetTargetPath");
558 task.SetParameterValue ("Properties", string.Format ("Configuration={0}; Platform={1}; BuildingSolutionFile=true; CurrentSolutionConfigurationContents=$(CurrentSolutionConfigurationContents); SolutionDir=$(SolutionDir); SolutionExt=$(SolutionExt); SolutionFileName=$(SolutionFileName); SolutionName=$(SolutionName); SolutionPath=$(SolutionPath)", projectTargetInfo.Configuration, projectTargetInfo.Platform));
559 task.Condition = string.Format (" ('$(Configuration)' == '{0}') and ('$(Platform)' == '{1}') ", solutionTargetInfo.Configuration, solutionTargetInfo.Platform);
561 string ref_item = String.Format ("{0}_{1}",
562 final_ref_item, ref_num);
564 task.AddOutputItem ("TargetOutputs", ref_item);
566 task = target.AddNewTask ("CreateItem");
567 task.SetParameterValue ("Include", String.Format ("@({0})", ref_item));
568 task.SetParameterValue ("AdditionalMetadata", String.Format ("Guid={{{0}}}",
569 depInfo.Guid.ToString ().ToUpper ()));
570 task.AddOutputItem ("Include", final_ref_item);
573 void AddWebsiteResolveAndCopyReferencesTasks (Target target, ProjectInfo webProjectInfo,
574 string final_ref_item, string w_guid)
576 BuildTask task = target.AddNewTask ("ResolveAssemblyReference");
577 task.SetParameterValue ("Assemblies", String.Format ("@({0}->'%(FullPath)')", final_ref_item));
578 task.SetParameterValue ("TargetFrameworkDirectories", "$(TargetFrameworkPath)");
579 task.SetParameterValue ("SearchPaths", "{RawFileName};{TargetFrameworkDirectory};{GAC}");
580 task.SetParameterValue ("FindDependencies", "true");
581 task.SetParameterValue ("FindSatellites", "true");
582 task.SetParameterValue ("FindRelatedFiles", "true");
583 task.Condition = String.Format ("Exists ('%({0}.Identity)')", final_ref_item);
585 string copylocal_item = String.Format ("{0}_CopyLocalFiles", final_ref_item);
586 task.AddOutputItem ("CopyLocalFiles", copylocal_item);
588 // Copy the references
589 task = target.AddNewTask ("Copy");
590 task.SetParameterValue ("SourceFiles", String.Format ("@({0})", copylocal_item));
591 task.SetParameterValue ("DestinationFiles", String.Format (
592 "@({0}->'$(Project_{1}_AspNetPhysicalPath)\\Bin\\%(DestinationSubDirectory)%(Filename)%(Extension)')",
593 copylocal_item, w_guid));
595 // AspNetConfiguration, is config for the website project, useful
596 // for overriding from command line
597 StringBuilder cond = new StringBuilder ();
598 foreach (string config in webProjectInfo.AspNetConfigurations) {
599 if (cond.Length > 0)
600 cond.Append (" or ");
601 cond.AppendFormat (" ('$(AspNetConfiguration)' == '{0}') ", config);
603 task.Condition = cond.ToString ();
605 task = target.AddNewTask ("Message");
606 cond = new StringBuilder ();
607 foreach (string config in webProjectInfo.AspNetConfigurations) {
608 if (cond.Length > 0)
609 cond.Append (" and ");
610 cond.AppendFormat (" ('$(AspNetConfiguration)' != '{0}') ", config);
612 task.Condition = cond.ToString ();
613 task.SetParameterValue ("Text", "Skipping as the '$(AspNetConfiguration)' configuration is " +
614 "not supported by this website project.");
617 void AddWebsiteUnsupportedTarget (Project p, ProjectInfo webProjectInfo, List<ProjectInfo> depInfos,
618 string buildTarget)
620 Target target = p.Targets.AddNewTarget (GetTargetNameForProject (webProjectInfo.Name, buildTarget));
621 target.DependsOnTargets = GetWebsiteDependsOnTarget (depInfos, buildTarget);
623 BuildTask task = target.AddNewTask ("Message");
624 task.SetParameterValue ("Text", String.Format (
625 "Target '{0}' not support for website projects", buildTarget));
628 string GetWebsiteDependsOnTarget (List<ProjectInfo> depInfos, string buildTarget)
630 StringBuilder deps = new StringBuilder ();
631 foreach (ProjectInfo pinfo in depInfos) {
632 if (deps.Length > 0)
633 deps.Append (";");
634 deps.Append (GetTargetNameForProject (pinfo.Name, buildTarget));
636 deps.Append (";GetFrameworkPath");
637 return deps.ToString ();
640 void AddGetFrameworkPathTarget (Project p)
642 Target t = p.Targets.AddNewTarget ("GetFrameworkPath");
643 BuildTask task = t.AddNewTask ("GetFrameworkPath");
644 task.AddOutputProperty ("Path", "TargetFrameworkPath");
647 void AddValidateSolutionConfiguration (Project p)
649 Target t = p.Targets.AddNewTarget ("ValidateSolutionConfiguration");
650 BuildTask task = t.AddNewTask ("Error");
651 task.SetParameterValue ("Text", "Invalid solution configuration and platform: \"$(Configuration)|$(Platform)\".");
652 task.Condition = "('$(CurrentSolutionConfigurationContents)' == '') and ('$(SkipInvalidConfigurations)' != 'true')";
653 task = t.AddNewTask ("Warning");
654 task.SetParameterValue ("Text", "Invalid solution configuration and platform: \"$(Configuration)|$(Platform)\".");
655 task.Condition = "('$(CurrentSolutionConfigurationContents)' == '') and ('$(SkipInvalidConfigurations)' == 'true')";
656 task = t.AddNewTask ("Message");
657 task.SetParameterValue ("Text", "Building solution configuration \"$(Configuration)|$(Platform)\".");
658 task.Condition = "'$(CurrentSolutionConfigurationContents)' != ''";
661 void AddProjectTargets (Project p, List<TargetInfo> solutionTargets, Dictionary<Guid, ProjectInfo> projectInfos)
663 foreach (KeyValuePair<Guid, ProjectInfo> projectInfo in projectInfos) {
664 ProjectInfo project = projectInfo.Value;
665 foreach (string buildTarget in buildTargets) {
666 string target_name = GetTargetNameForProject (project.Name, buildTarget);
667 Target target = p.Targets.AddNewTarget (target_name);
668 target.Condition = "'$(CurrentSolutionConfigurationContents)' != ''";
670 if (project.Dependencies.Count > 0) {
671 StringBuilder dependencies = new StringBuilder ();
672 foreach (ProjectInfo dependentInfo in project.Dependencies.Values) {
673 if (dependencies.Length > 0)
674 dependencies.Append (";");
675 if (IsBuildTargetName (dependentInfo.Name))
676 dependencies.Append ("Solution:");
677 dependencies.Append (dependentInfo.Name);
678 if (buildTarget != "Build")
679 dependencies.Append (":" + buildTarget);
681 target.DependsOnTargets = dependencies.ToString ();
684 foreach (TargetInfo targetInfo in solutionTargets) {
685 BuildTask task = null;
686 TargetInfo projectTargetInfo;
687 if (!project.TargetMap.TryGetValue (targetInfo, out projectTargetInfo)) {
688 AddWarningForMissingProjectConfiguration (target, targetInfo.Configuration,
689 targetInfo.Platform, project.Name);
690 continue;
692 if (projectTargetInfo.Build) {
693 task = target.AddNewTask ("MSBuild");
694 task.SetParameterValue ("Projects", project.FileName);
696 if (buildTarget != "Build")
697 task.SetParameterValue ("Targets", buildTarget);
698 task.SetParameterValue ("Properties", string.Format ("Configuration={0}; Platform={1}; BuildingSolutionFile=true; CurrentSolutionConfigurationContents=$(CurrentSolutionConfigurationContents); SolutionDir=$(SolutionDir); SolutionExt=$(SolutionExt); SolutionFileName=$(SolutionFileName); SolutionName=$(SolutionName); SolutionPath=$(SolutionPath)", projectTargetInfo.Configuration, projectTargetInfo.Platform));
699 } else {
700 task = target.AddNewTask ("Message");
701 task.SetParameterValue ("Text", string.Format ("Project \"{0}\" is disabled for solution configuration \"{1}|{2}\".", project.Name, targetInfo.Configuration, targetInfo.Platform));
703 task.Condition = string.Format (" ('$(Configuration)' == '{0}') and ('$(Platform)' == '{1}') ", targetInfo.Configuration, targetInfo.Platform);
710 string GetTargetNameForProject (string projectName, string buildTarget)
712 //FIXME: hack
713 projectName = projectName.Replace ("\\", "/").Replace (".", "_");
714 string target_name = projectName +
715 (buildTarget == "Build" ? string.Empty : ":" + buildTarget);
717 if (IsBuildTargetName (projectName))
718 target_name = "Solution:" + target_name;
720 return target_name;
723 bool IsBuildTargetName (string name)
725 foreach (string tgt in buildTargets)
726 if (name == tgt)
727 return true;
728 return false;
731 // returns number of levels
732 int AddBuildLevels (Project p, List<TargetInfo> solutionTargets, Dictionary<Guid, ProjectInfo> projectInfos,
733 ref List<ProjectInfo>[] infosByLevel)
735 infosByLevel = TopologicalSort<ProjectInfo> (projectInfos.Values);
737 foreach (TargetInfo targetInfo in solutionTargets) {
738 BuildItemGroup big = p.AddNewItemGroup ();
739 big.Condition = String.Format (" ('$(Configuration)' == '{0}') and ('$(Platform)' == '{1}') ",
740 targetInfo.Configuration, targetInfo.Platform);
742 //FIXME: every level has projects that can be built in parallel.
743 // levels are ordered on the basis of the dependency graph
745 for (int i = 0; i < infosByLevel.Length; i ++) {
746 string build_level = String.Format ("BuildLevel{0}", i);
747 string skip_level = String.Format ("SkipLevel{0}", i);
748 string missing_level = String.Format ("MissingConfigLevel{0}", i);
750 foreach (ProjectInfo projectInfo in infosByLevel [i]) {
751 TargetInfo projectTargetInfo;
752 if (!projectInfo.TargetMap.TryGetValue (targetInfo, out projectTargetInfo)) {
753 // missing project config
754 big.AddNewItem (missing_level, projectInfo.Name);
755 continue;
758 if (projectTargetInfo.Build) {
759 BuildItem item = big.AddNewItem (build_level, projectInfo.FileName);
760 item.SetMetadata ("Configuration", projectTargetInfo.Configuration);
761 item.SetMetadata ("Platform", projectTargetInfo.Platform);
762 } else {
763 // build disabled
764 big.AddNewItem (skip_level, projectInfo.Name);
770 return infosByLevel.Length;
773 void AddSolutionTargets (Project p, int num_levels, IEnumerable<ProjectInfo> websiteProjectInfos)
775 foreach (string buildTarget in buildTargets) {
776 Target t = p.Targets.AddNewTarget (buildTarget);
777 t.Condition = "'$(CurrentSolutionConfigurationContents)' != ''";
779 BuildTask task = null;
780 for (int i = 0; i < num_levels; i ++) {
781 string level_str = String.Format ("BuildLevel{0}", i);
782 task = t.AddNewTask ("MSBuild");
783 task.SetParameterValue ("Condition", String.Format ("'@({0})' != ''", level_str));
784 task.SetParameterValue ("Projects", String.Format ("@({0})", level_str));
785 task.SetParameterValue ("Properties",
786 string.Format ("Configuration=%(Configuration); Platform=%(Platform); BuildingSolutionFile=true; CurrentSolutionConfigurationContents=$(CurrentSolutionConfigurationContents); SolutionDir=$(SolutionDir); SolutionExt=$(SolutionExt); SolutionFileName=$(SolutionFileName); SolutionName=$(SolutionName); SolutionPath=$(SolutionPath)"));
787 if (buildTarget != "Build")
788 task.SetParameterValue ("Targets", buildTarget);
789 //FIXME: change this to BuildInParallel=true, when parallel
790 // build support gets added
791 task.SetParameterValue ("RunEachTargetSeparately", "true");
793 level_str = String.Format ("SkipLevel{0}", i);
794 task = t.AddNewTask ("Message");
795 task.Condition = String.Format ("'@({0})' != ''", level_str);
796 task.SetParameterValue ("Text",
797 String.Format ("The project '%({0}.Identity)' is disabled for solution " +
798 "configuration '$(Configuration)|$(Platform)'.", level_str));
800 level_str = String.Format ("MissingConfigLevel{0}", i);
801 task = t.AddNewTask ("Warning");
802 task.Condition = String.Format ("'@({0})' != ''", level_str);
803 task.SetParameterValue ("Text",
804 String.Format ("The project configuration for project '%({0}.Identity)' " +
805 "corresponding to the solution configuration " +
806 "'$(Configuration)|$(Platform)' was not found.", level_str));
809 // "build" website projects also
810 StringBuilder w_targets = new StringBuilder ();
811 foreach (ProjectInfo info in websiteProjectInfos) {
812 if (w_targets.Length > 0)
813 w_targets.Append (";");
814 w_targets.Append (GetTargetNameForProject (info.Name, buildTarget));
817 task = t.AddNewTask ("CallTarget");
818 task.SetParameterValue ("Targets", w_targets.ToString ());
819 task.SetParameterValue ("RunEachTargetSeparately", "true");
823 // Sorts the ProjectInfo dependency graph, to obtain
824 // a series of build levels with projects. Projects
825 // in each level can be run parallel (no inter-dependency).
826 static List<T>[] TopologicalSort<T> (IEnumerable<T> items) where T: ProjectInfo
828 IList<T> allItems;
829 allItems = items as IList<T>;
830 if (allItems == null)
831 allItems = new List<T> (items);
833 bool[] inserted = new bool[allItems.Count];
834 bool[] triedToInsert = new bool[allItems.Count];
835 int[] levels = new int [allItems.Count];
837 int maxdepth = 0;
838 for (int i = 0; i < allItems.Count; ++i) {
839 int d = Insert<T> (i, allItems, levels, inserted, triedToInsert);
840 if (d > maxdepth)
841 maxdepth = d;
844 // Separate out the project infos by build level
845 List<T>[] infosByLevel = new List<T>[maxdepth];
846 for (int i = 0; i < levels.Length; i ++) {
847 int level = levels [i] - 1;
848 if (infosByLevel [level] == null)
849 infosByLevel [level] = new List<T> ();
851 infosByLevel [level].Add (allItems [i]);
854 return infosByLevel;
857 // returns level# for the project
858 static int Insert<T> (int index, IList<T> allItems, int[] levels, bool[] inserted, bool[] triedToInsert)
859 where T: ProjectInfo
861 if (inserted [index])
862 return levels [index];
864 if (triedToInsert[index])
865 throw new InvalidOperationException (String.Format (
866 "Cyclic dependency involving project {0} found in the project dependency graph",
867 allItems [index].Name));
869 triedToInsert[index] = true;
870 ProjectInfo insertItem = allItems[index];
872 int maxdepth = 0;
873 foreach (ProjectInfo dependency in insertItem.Dependencies.Values) {
874 for (int j = 0; j < allItems.Count; ++j) {
875 ProjectInfo checkItem = allItems [j];
876 if (dependency.FileName == checkItem.FileName) {
877 int d = Insert (j, allItems, levels, inserted, triedToInsert);
878 maxdepth = d > maxdepth ? d : maxdepth;
879 break;
883 levels [index] = maxdepth + 1;
884 inserted [index] = true;
886 return levels [index];
891 #endif