Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SolutionMakefileHandler.cs « MonoDevelop.Autotools « Extras - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2a7ce588eac8e635dc1a0812aed3c07c9106c241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Copyright (C) 2006  Matthias Braun <matze@braunis.de>
					Scott Ellington <scott.ellington@gmail.com>
 
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/

using System;
using System.IO;
using System.Text;
using System.Collections;
using MonoDevelop.Projects;
using MonoDevelop.Core;

namespace MonoDevelop.Autotools
{
	public class SolutionMakefileHandler : IMakefileHandler
	{
		// Recurses into children and tests if they are deployable.
		public bool CanDeploy ( CombineEntry entry )
		{
			return entry is Combine;
		}

		public Makefile Deploy ( AutotoolsContext ctx, CombineEntry entry, IProgressMonitor monitor )
		{
			monitor.BeginTask ( GettextCatalog.GetString  ("Creating Makefile.am for Solution {0}", entry.Name), 1 );			
			Makefile mfile = new Makefile ();

			try
			{
				if ( !CanDeploy ( entry ) )
					throw new Exception ( GettextCatalog.GetString ("Not a deployable solution.") );

				Combine combine = entry as Combine;

				StringBuilder subdirs = new StringBuilder();
				subdirs.Append ("#Warning: This is an automatically generated file, do not edit!\n");

				Set children = new Set ();
				foreach ( CombineConfiguration config in combine.Configurations )
				{
					if ( !ctx.IsSupportedConfiguration ( config.Name ) ) continue;
					
					subdirs.AppendFormat ( "if {0}\n", "ENABLE_" + config.Name.ToUpper () );
					subdirs.Append (" SUBDIRS = ");
					
					foreach ( CombineEntry ce in CalculateSubDirOrder ( config ) )
					{
						if ( combine.BaseDirectory == ce.BaseDirectory )
							throw new Exception ( GettextCatalog.GetString ("Child projects / solutions cannot be in the same directory as their parent") );

						if ( !ce.BaseDirectory.StartsWith (combine.BaseDirectory) )
							throw new Exception ( GettextCatalog.GetString ("Child projects / solutions must be in sub-directories of their parent") );
						
						// add the subdirectory to the list
						string path = Path.GetDirectoryName (ce.RelativeFileName);
						if (path.StartsWith ("." + Path.DirectorySeparatorChar) )
							path = path.Substring (2);
						subdirs.Append (" ");
						subdirs.Append ( AutotoolsContext.EscapeStringForAutomake (path) );

						children.Add ( ce );
					}
					subdirs.Append ( "\nendif\n" );
				}
				mfile.Append ( subdirs.ToString () );

				// deploy recursively
				foreach ( CombineEntry ce in children )
				{
					IMakefileHandler handler = AutotoolsContext.GetMakefileHandler ( ce );
					Makefile makefile;
					if ( handler != null && handler.CanDeploy ( ce ) )
					{
						makefile = handler.Deploy ( ctx, ce, monitor );
						string outpath = Path.Combine(Path.GetDirectoryName(ce.FileName), "Makefile");
						StreamWriter writer = new StreamWriter ( outpath + ".am" );
						makefile.Write ( writer );
						writer.Close ();
						ctx.AddAutoconfFile ( outpath );
					}
					else {
						monitor.Log .WriteLine("Project '{0}' skipped.", ce.Name); 
					}
				}

				monitor.Step (1);
			}
			finally
			{
				monitor.EndTask ();
			}
			return mfile;
		}

		// utility function for finding the correct order to process directories
		ArrayList CalculateSubDirOrder ( CombineConfiguration config )
		{
			ArrayList resultOrder = new ArrayList();
			Set dependenciesMet = new Set();
			Set inResult = new Set();

			bool added;
			string notMet;
			do 
			{
				added = false;
				notMet = null;

				foreach (CombineConfigurationEntry centry in config.Entries) 
				{
					if ( !centry.Build ) continue;
					
					CombineEntry entry = centry.Entry;
					
					if ( inResult.Contains (entry) ) continue;

					Set references, provides;
					if (entry is Project)
					{
						Project project = entry as Project;

						references = GetReferencedProjects (project);
						provides = new Set();
						provides.Add(project.Name);
					} 
					else if (entry is Combine) 
					{
						CombineConfiguration cc = (entry as Combine).Configurations[config.Name] as CombineConfiguration;
						if ( cc == null ) continue;
						GetAllProjects ( cc, out provides, out references);
					}
					else continue;

					if (dependenciesMet.ContainsSet (references) ) 
					{
						resultOrder.Add (entry);
						dependenciesMet.Union(provides);
						inResult.Add(entry);
						added = true;
					} 
					else notMet = entry.Name;
				}
			} while (added == true);

			if (notMet != null) 
				throw new Exception("Impossible to find a solution order that satisfies project references for '" + notMet + "'");

			return resultOrder;
		}

		// cache references
		Hashtable projectReferences = new Hashtable();		
		/**
		 * returns a set of all monodevelop projects that a give
		 * projects references
		 */
		Set GetReferencedProjects (Project project)
		{
			Set set = (Set) projectReferences [project];
			if (set != null) return set;

			set = new Set();

			foreach (ProjectReference reference in project.ProjectReferences) 
			{
				if (reference.ReferenceType == ReferenceType.Project)
					set.Add (reference.Reference);
			}

			projectReferences[project] = set;
			return set;
		}

		// cache references
		Hashtable combineProjects = new Hashtable();
		Hashtable combineReferences = new Hashtable();
		/**
		 * returns a set of projects that a combine contains and a set of projects
		 * that are referenced from combine projects but not part of the combine
		 */
		void GetAllProjects (CombineConfiguration config, out Set projects, out Set references)
		{
			projects = (Set) combineProjects [config];
			if(projects != null) 
			{
				references = (Set) combineReferences [config];
				return;
			}

			projects = new Set();
			references = new Set();
			
			foreach (CombineConfigurationEntry centry in config.Entries) 
			{
				if ( !centry.Build ) continue;
				
				CombineEntry entry = centry.Entry;
				if (entry is Project) 
				{
					Project project = entry as Project;
					projects.Add (project.Name);
					references.Union ( GetReferencedProjects (project) );
				} 
				else if (entry is Combine) 
				{
					Set subProjects;
					Set subReferences;
					
					CombineConfiguration cc = (entry as Combine).Configurations[config.Name] as CombineConfiguration;
					if ( cc == null ) continue;
					GetAllProjects ( cc, out subProjects, out subReferences);

					projects.Union (subProjects);
					references.Union (subReferences);
				}
			}
			
			references.Without (projects);
			combineProjects [config] = projects;
			combineReferences [config] = references;
		}
	}
}