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

MakefileData.cs « MonoDevelop.Autotools « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 01a6b1cb51d4bb3841456ef191c5466cab5c2a7c (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
//
// MakefileData.cs
//
// Author:
//   Ankit Jain <jankit@novell.com>
//
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

using MonoDevelop.Core;
using MonoDevelop.Core.Serialization;
using MonoDevelop.Projects;
using MonoDevelop.Core.Assemblies;
using MonoDevelop.Ide;
using System.Xml;

namespace MonoDevelop.Autotools
{
	[DataItem ("MakefileInfo")]
	public class MakefileData : ICloneable
	{
		bool integrationEnabled;
		string relativeMakefileName;
		string buildTargetName;
		string cleanTargetName;
		string executeTargetName;
		Project ownerProject;
		CustomMakefile makefile;
		
		[ItemProperty ("ExcludedFiles")]
		[ProjectPathItemProperty("File", Scope="*")]
		List<string> excludedFiles = new List<string> ();
		
		IAssemblyContext assemblyContext;

		public MakefileData ()
		{
			relativeMakefileName = String.Empty;
			buildTargetName = "all";
			cleanTargetName = "clean";
			executeTargetName = String.Empty;
			ParallelProcesses = 1;
			
			assemblyContext = GetMonoRuntimeContext ();
			if (assemblyContext == null)
				integrationEnabled = false;
		}

		public static MakefileData Read (XmlElement ext)
		{
			XmlDataSerializer ser = new XmlDataSerializer (new DataContext ());
			return (MakefileData) ser.Deserialize (new XmlNodeReader (ext), typeof(MakefileData));
		}

		public XmlElement Write ()
		{
			XmlDataSerializer ser = new XmlDataSerializer (new DataContext ());
			var sw = new StringWriter ();
			ser.Serialize (new XmlTextWriter (sw), this);
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml (sw.ToString ());
			var elem = doc.DocumentElement;
			doc.RemoveChild (elem);
			return elem;
		}
		
		internal static IAssemblyContext GetMonoRuntimeContext ()
		{
			if (Runtime.SystemAssemblyService.CurrentRuntime.RuntimeId == "Mono")
				return Runtime.SystemAssemblyService.CurrentRuntime.AssemblyContext;
			else {
				foreach (TargetRuntime r in Runtime.SystemAssemblyService.GetTargetRuntimes ("Mono"))
					return r.AssemblyContext;
			}
			return null;
		}

		public Project OwnerProject {
			get { return ownerProject; }
			set {
				if (!Object.ReferenceEquals (ownerProject, value) && value != null) {
					//Setup handlers
					ProjectReferenceEventHandler refhandler = delegate {
						if (SyncReferences)
							dirty = true;
					};
					
					if (value is DotNetProject) {
						((DotNetProject)value).ReferenceRemovedFromProject += refhandler;
						((DotNetProject)value).ReferenceAddedToProject += refhandler;
					}

					ProjectFileEventHandler filehandler = delegate (object sender, ProjectFileEventArgs args) {
						MakefileVar var = null;
						foreach (ProjectFileEventInfo e in args) {
							switch (e.ProjectFile.BuildAction) {
							case "Compile":
								var = BuildFilesVar;
								break;
							case "Content":
								var = DeployFilesVar;
								break;
							case "EmbeddedResource":
								var = ResourcesVar;
								break;
							case "None":
								var = OthersVar;
								break;
							}
							if (var != null && var.Sync)
								dirty = true;
						}
					};

					value.FileRemovedFromProject += filehandler;
					value.FileAddedToProject += filehandler;

					value.FilePropertyChangedInProject += delegate {
						dirty = true;
					};

					value.FileRenamedInProject += delegate {
						dirty = true;
					};
				}
				ownerProject = value;
				InitBuildVars ();
			}
		}

		[ItemProperty (DefaultValue = false)]
		public bool IntegrationEnabled {
			get { return integrationEnabled; }
			set { integrationEnabled = value;}
		}

		public bool SupportsIntegration {
			get { return IntegrationEnabled && !Platform.IsWindows; }
		}
		
		[ItemProperty (DefaultValue = "")]
		public string RelativeMakefileName {
			get {
				if (ownerProject == null)
					return relativeMakefileName;
				return ownerProject.GetRelativeChildPath (relativeMakefileName);
			}
			set {
				if (String.Compare (relativeMakefileName, value) == 0)
					return;

				relativeMakefileName = value;
				makefile = null;
				InitBuildVars ();
			}
		}

		public string AbsoluteMakefileName {
			get {
				if (ownerProject == null)
					return relativeMakefileName;
				return ownerProject.GetAbsoluteChildPath (relativeMakefileName);
			}
		}

		public string BaseDirectory {
			get {
				//FIXME: Check for AbsoluteMakefileName == null or empty
				return Path.GetDirectoryName (AbsoluteMakefileName);
			}
		}

		[ItemProperty (DefaultValue = "all")]
		public string BuildTargetName {
			get { return buildTargetName; }
			set { buildTargetName = value;}
		}

		[ItemProperty (DefaultValue = "clean")]
		public string CleanTargetName {
			get { return cleanTargetName; }
			set { cleanTargetName = value;}
		}

		[ItemProperty (DefaultValue = "")]
		public string ExecuteTargetName {
			get { return executeTargetName; }
			set { executeTargetName = value;}
		}
		
		/// <summary>
		/// The number of parallel build processes to use
		/// </summary>
		[ItemProperty (DefaultValue = 1)]
		public int ParallelProcesses { get; set; }

		//Makefile variables
		MakefileVar buildFilesVar;
		[ItemProperty]
		public MakefileVar BuildFilesVar {
			get {
				if (buildFilesVar == null)
					buildFilesVar = new MakefileVar ();
				return buildFilesVar;
			}
			set { buildFilesVar = value; }
		}

		MakefileVar deployFilesVar;
		[ItemProperty]
		public MakefileVar DeployFilesVar {
			get {
				if (deployFilesVar == null)
					deployFilesVar = new MakefileVar ();
				return deployFilesVar;
			}
			set { deployFilesVar = value; }
		}

		MakefileVar resourcesVar;
		[ItemProperty]
		public MakefileVar ResourcesVar {
			get {
				if (resourcesVar == null)
					resourcesVar = new MakefileVar ();
				return resourcesVar;
			}
			set { resourcesVar = value; }
		}

		MakefileVar othersVar;
		[ItemProperty]
		public MakefileVar OthersVar {
			get {
				if (othersVar == null)
					othersVar = new MakefileVar ();
				return othersVar;
			}
			set { othersVar = value; }
    		}

		//Individual Sync of *RefVar is ignored, instead
		//SyncReferences is used
		bool syncReferences = false;
		[ItemProperty (DefaultValue = false)]
		public bool SyncReferences {
			get { return syncReferences; }
			set {
				PackageRefVar.Sync = value;
				AsmRefVar.Sync = value;
				ProjectRefVar.Sync =value;
				syncReferences = value;
			}
		}

		bool SaveReferences = true;

		bool isAutotoolsProject;
		[ItemProperty (DefaultValue = false)]
		public bool IsAutotoolsProject {
			get { return isAutotoolsProject; }
			set {
				if (isAutotoolsProject == value)
					return;
				isAutotoolsProject = value;
				configuredPackages = null;
			}
		}

		public bool UseAutotools {
			get { return ConfiguredPackages != null; }
		}

		MakefileVar packageRefVar;
		
		//FIXME: name is quick hack to avoid changing format
		[ItemProperty ("GacRefVar")]
		public MakefileVar PackageRefVar {
			get {
				if (packageRefVar == null)
					packageRefVar = new MakefileVar ();
				return packageRefVar;
			}
			set { packageRefVar = value; }
		}

		MakefileVar asmRefVar;
		[ItemProperty]
		public MakefileVar AsmRefVar {
			get {
				if (asmRefVar == null)
					asmRefVar = new MakefileVar ();
				return asmRefVar;
			}
			set { asmRefVar = value; }
		}

		MakefileVar projectRefVar;
		[ItemProperty]
		public MakefileVar ProjectRefVar {
			get {
				if (projectRefVar == null)
					projectRefVar = new MakefileVar ();
				return projectRefVar;
			}
			set { projectRefVar = value; }
		}
		
		string errorRegex = String.Empty;
		[ItemProperty (Name = "MessageRegex/Error", DefaultValue = "")]
		public string CustomErrorRegex {
			get {
				if (MessageRegexName != "Custom")
					return String.Empty;
				return errorRegex;
			}
			set { errorRegex = value; }
		}
		
		string warningRegex = String.Empty;
		[ItemProperty (Name = "MessageRegex/Warning", DefaultValue = "")]
		public string CustomWarningRegex {
			get {
				if (MessageRegexName != "Custom")
					return String.Empty;
				return warningRegex;
			}
			set { warningRegex = value; }			
		}
		
		string messageRegexName = "C# (mcs)";
		[ItemProperty (Name = "MessageRegex/Name", DefaultValue = "C# (mcs)")]
		public string MessageRegexName {
			get { return messageRegexName; }
			set {
				if (value != "Custom") {
					// !Custom
					if (CompilerMessageRegex.ContainsKey (value)) {
						errorRegex = CompilerMessageRegex [value][0];
						warningRegex = CompilerMessageRegex [value][1];
					} else {
						LoggingService.LogError ("Invalid value for MessageRegexName : {0}", value);
						//FIXME: If !valid then throw
					}
				}
				messageRegexName = value;
			}
		}
		
		// Custom is not stored in this list!
		static Dictionary<string, string []> compilerMessageRegex;
		static public Dictionary<string, string[]> CompilerMessageRegex {
			get {
				if (compilerMessageRegex == null)
					InitCompilerMessageRegex ();
				return compilerMessageRegex;
			}
		}

		static Regex [] customRegex = null;

		// Regex(string) to Regex object
		static Dictionary<string, Regex> regexTable;
		static Dictionary<string, Regex> RegexTable {
			get {
				if (regexTable == null)
					regexTable = new Dictionary<string, Regex> ();
				return regexTable;
			}
		}

		string relativeConfigureInPath = String.Empty;
		[ItemProperty (DefaultValue = "")]
		//FIXME: Sanitize usage .. relative required or absolute ??
		public string RelativeConfigureInPath {
			get {
				return relativeConfigureInPath;
			}
			set {
				if (String.Compare (relativeConfigureInPath, value) == 0)
					return;

				relativeConfigureInPath = value;
				configuredPackages = null;

				if (String.IsNullOrEmpty (relativeConfigureInPath))
					return;

				relativeConfigureInPath = GetRelativePath (relativeConfigureInPath);
				InitBuildVars ();
			}
		}

		public string AbsoluteConfigureInPath {
			get { return GetAbsolutePath (RelativeConfigureInPath); }
		}

		ConfiguredPackagesManager configuredPackages = null;
		public ConfiguredPackagesManager ConfiguredPackages {
			get { return configuredPackages; }
		}

		string outputDirVar;
		[ItemProperty (DefaultValue = "")]
		public string OutputDirVar {
			get { return outputDirVar; }
			set { outputDirVar = value;}
    		}

		string assemblyNameVar;
		[ItemProperty (DefaultValue = "")]
		public string AssemblyNameVar {
			get { return assemblyNameVar; }
			set { assemblyNameVar = value;}
    		}

		public CustomMakefile Makefile {
			get {
				if (makefile == null)
					makefile = new CustomMakefile (AbsoluteMakefileName);
				return makefile;
			}
		}

		Dictionary <string, string> unresolvedReferences;
		Dictionary <string, string> UnresolvedReferences {
			get { 
				if (unresolvedReferences == null)
					unresolvedReferences = new Dictionary <string, string> ();
				return unresolvedReferences;
			}
 		}

		Dictionary<string, string> buildVariables ;
		public Dictionary<string, string> BuildVariables  {
			get {
				if (buildVariables == null)
					buildVariables = new Dictionary<string, string> ();
				return buildVariables;
			}
		}
		
		public bool IsFileIntegrationEnabled (string buildAction)
		{
			if (IntegrationEnabled) {
				if (buildAction == BuildAction.Compile)
					return BuildFilesVar.Sync;
				else if (buildAction == BuildAction.EmbeddedResource)
					return ResourcesVar.Sync;
				else if (buildAction == BuildAction.Content)
					return DeployFilesVar.Sync;
				else
					return OthersVar.Sync;
			}
			return false;
		}
		
		public bool IsFileExcluded (string fileName)
		{
			return excludedFiles.Contains (fileName);
		}
		
		public void SetFileExcluded (string fileName, bool exclude)
		{
			if (exclude)
				excludedFiles.Add (fileName);
			else
				excludedFiles.Remove (fileName);
			dirty = true;
		}

		bool dirty = false;

		public object Clone ()
		{
			MakefileData data = new MakefileData ();
			data.OwnerProject = this.OwnerProject;
			data.IntegrationEnabled = this.IntegrationEnabled;
			data.RelativeMakefileName = this.RelativeMakefileName;
			data.BuildTargetName = this.BuildTargetName;
			data.CleanTargetName = this.CleanTargetName;
			data.ExecuteTargetName = this.ExecuteTargetName;
			data.ParallelProcesses = this.ParallelProcesses;
			
			data.BuildFilesVar = new MakefileVar (this.BuildFilesVar);
			data.DeployFilesVar = new MakefileVar (this.DeployFilesVar);
			data.ResourcesVar = new MakefileVar (this.ResourcesVar);
			data.OthersVar = new MakefileVar (this.OthersVar);
			data.PackageRefVar = new MakefileVar (this.PackageRefVar);
			data.AsmRefVar = new MakefileVar (this.AsmRefVar);
			data.ProjectRefVar = new MakefileVar (this.ProjectRefVar);

			data.SyncReferences = this.SyncReferences;
			data.IsAutotoolsProject = this.IsAutotoolsProject;
			data.RelativeConfigureInPath = this.RelativeConfigureInPath;
			data.OutputDirVar = this.OutputDirVar;
			data.AssemblyNameVar = this.AssemblyNameVar;

			data.CustomErrorRegex = this.CustomErrorRegex;
			data.CustomWarningRegex = this.CustomWarningRegex;
			data.MessageRegexName = this.MessageRegexName;
			// This shouldn't be required
			//data.unresolvedReferences = new List<string> (this.UnresolvedReferences);

			return data;
		}

		static void InitCompilerMessageRegex ()
		{
			compilerMessageRegex = new Dictionary<string, string[]> ();
			compilerMessageRegex ["C# (mcs)"] = new string [] {
				@"(^\s*(?<file>.*)\((?<line>\d*){1}(,(?<column>\d*[\+]*))?\)(:|)\s+)*error\s*(?<number>.*):\s(?<message>.*)",
				@"(^\s*(?<file>.*)\((?<line>\d*){1}(,(?<column>\d*[\+]*))?\)(:|)\s+)*warning\s*(?<number>.*):\s(?<message>.*)"
			};
			
			compilerMessageRegex ["gcc"] = new string [] {
				@"^\s*(?<file>[^:]*):(?<line>\d*){1}(:(?<column>\d*))?\s*:\s*error\s*:\s(?<message>.*)",
				@"^\s*(?<file>[^:]*):(?<line>\d*){1}(:(?<column>\d*))?\s*:\s*warning\s*:\s(?<message>.*)"
			};
			
			compilerMessageRegex ["Boo"] = new string [] {
				@"(^\s*(?<file>.*)\((?<line>\d*){1}(,(?<column>\d*[\+]*))?\)(:|)\s+)*(?=BCE)(?<number>.*?):\s(?<message>.*)",
				@"(^\s*(?<file>.*)\((?<line>\d*){1}(,(?<column>\d*[\+]*))?\)(:|)\s+)*(?=BCW)(?<number>.*?):\sWARNING:\s(?<message>.*)"
			};

			compilerMessageRegex ["Vala"] = new string [] {
				@"((^\s*(?<file>.*):(?<line>\d*)\.(?<column>\d*)-\d*\.\d*: error: (?<message>.*))|(^\s*(?<file>[^:]*):(?<line>\d*){1}(:(?<column>\d*))?\s*:\s*error\s*:\s(?<message>.*)))",
				@"((^\s*(?<file>.*):(?<line>\d*)\.(?<column>\d*)-\d*\.\d*: warning: (?<message>.*))|(^\s*(?<file>[^:]*):(?<line>\d*){1}(:(?<column>\d*))?\s*:\s*warning\s*:\s(?<message>.*)))"
			};
		}

		void InitBuildVars ()
		{
			if (!String.IsNullOrEmpty (RelativeConfigureInPath)) {
				BuildVariables ["top_srcdir"] = RelativeConfigureInPath;
				BuildVariables ["top_builddir"] = RelativeConfigureInPath;
			}

			if (!String.IsNullOrEmpty (AbsoluteMakefileName))
				BuildVariables ["srcdir"] = BaseDirectory;
		}

		public string GetRelativePath (string path)
		{
			if (Path.IsPathRooted (path))
				return FileService.AbsoluteToRelativePath (BaseDirectory, path);
			else
				return path;
		}

		public string GetAbsolutePath (string path)
		{
			if (Path.IsPathRooted (path))
				return path;
			else
				return FileService.RelativeToAbsolutePath (BaseDirectory, path);
		}

		public void Save ()
		{
			Makefile.Save ();
		}

		public Regex GetErrorRegex (bool throwOnError)
		{
			return GetCompilerMessageRegex (0, throwOnError);
		}

		public Regex GetWarningRegex (bool throwOnError)
		{
			return GetCompilerMessageRegex (1, throwOnError);
		}

		Regex GetCompilerMessageRegex (int index, bool throwOnError)
		{
			if (index < 0 || index > 1)
				throw new ArgumentException ("index");

			string str = null;
			if (MessageRegexName != "Custom") {
				str = CompilerMessageRegex [MessageRegexName][index];
				if (!RegexTable.ContainsKey (str))
					RegexTable [str] = new Regex (str, RegexOptions.Compiled | RegexOptions.ExplicitCapture);

				return RegexTable [str];
			}

			// Custom
			if (customRegex == null)
				customRegex = new Regex [2];

			str = index == 0 ? CustomErrorRegex : CustomWarningRegex;
			if (customRegex [index] == null || customRegex [index].ToString () != str) {
				try {
					customRegex [index] = new Regex (str, RegexOptions.Compiled | RegexOptions.ExplicitCapture);
				} catch (ArgumentException) {
					// Invalid regex string
					LoggingService.LogError ("Invalid {0}Regex '{1}' specified for project '{2}'.",
						(index == 0 ? "Error" : "Warning"), str, OwnerProject.Name);
					customRegex [index] = null;
					if (throwOnError)
						throw;
				}
			}

			return customRegex [index];
		}

		ProgressMonitor monitor = null;

		// VarName -> Encode filenames Eg. $(srcdir)
		Dictionary<string, bool> encodeValues;
		Dictionary<string, bool> EncodeValues {
			get {
				if (encodeValues == null) {
					encodeValues = new Dictionary<string, bool> ();

					// Default is false!
					encodeValues [BuildFilesVar.Name] = false;
					encodeValues [DeployFilesVar.Name] = false;
					encodeValues [OthersVar.Name] = false;
					encodeValues [ResourcesVar.Name] = false;

					encodeValues [PackageRefVar.Name] = false;
					encodeValues [AsmRefVar.Name] = false;
					encodeValues [ProjectRefVar.Name] = false;
				}

				return encodeValues;
			}
		}

		// Global table for keeping weak references to ConfiguredPackagesManager objects,
		// one per configure.in file. A strong reference to the object is kept in
		// every project's ExtendedProperties, this ensures that it won't get collected
		// too early.
		static Dictionary<string, WeakReference> pkgManagerTable = null;
		static Dictionary<string, WeakReference> PkgManagerTable {
			get {
				if (pkgManagerTable == null)
					pkgManagerTable = new Dictionary<string, WeakReference> ();
				return pkgManagerTable;
			}
		}

		//use events.. 
		public void UpdateProject (ProgressMonitor monitor, bool promptForRemoval)
		{
			if (!IntegrationEnabled)
				return;

			if (ownerProject == null)
				throw new InvalidOperationException ("Internal Error: ownerProject not set");

			this.monitor = monitor;

			try {
				Makefile.GetVariables ();
			} catch (Exception e) {
				monitor.ReportError (GettextCatalog.GetString (
					"Invalid Makefile '{0}'. Disabling Makefile integration.", AbsoluteMakefileName), e);
				IntegrationEnabled = false;

				return;
			}

			//FIXME: Improve the message
			if (promptForRemoval) {
				AlertButton projectButton = new AlertButton ("_Project");
				AlertButton makefileButton = new AlertButton ("_Makefile");
				AlertButton choice = MessageService.AskQuestion (GettextCatalog.GetString ("Enabling Makefile integration. You can choose to have either the Project or the Makefile be used as the master copy. This is done only when enabling this feature. After this, the Makefile will be taken as the master copy."),
				                                                 projectButton, makefileButton);
				if (choice == projectButton) {
					//Sync Project --> Makefile
					dirty = true;
					return;
				}
			}

			dirty = true;
			encodeValues = null;

			try {
				if (IsAutotoolsProject) {
					string path = Path.Combine (AbsoluteConfigureInPath, "configure.in");
					if (!File.Exists (path))
						path = Path.Combine (AbsoluteConfigureInPath, "configure.ac");
					
					configuredPackages = null;
					WeakReference weakref;
					if (PkgManagerTable.TryGetValue (path, out weakref)) {
						if (weakref.IsAlive) {
							configuredPackages = (ConfiguredPackagesManager) weakref.Target;
							FileInfo finfo = new FileInfo (path);
							if (finfo.LastWriteTime > configuredPackages.LastWriteTime)
								// file has changed since last time we parsed it!
								configuredPackages = null;
						}
					}

					// no entry in table or it got collected or file has changed
					if (configuredPackages == null) {
						configuredPackages = new ConfiguredPackagesManager (path);
						PkgManagerTable [path] = new WeakReference (configuredPackages);
						ownerProject.ExtendedProperties ["MonoDevelop.Autotools.ConfiguredPackagesManager"] = configuredPackages;
					}
				}
			} catch (Exception e) {
				LoggingService.LogWarning (
					"Error trying to read configure.in ('{0}') for project '{1}':\n{2}",
					AbsoluteConfigureInPath, OwnerProject.Name, e.ToString ());

				monitor.ReportWarning (GettextCatalog.GetString (
					"Error trying to read configure.in ('{0}') for project '{1}':\n{2}",
					AbsoluteConfigureInPath, OwnerProject.Name, e.Message));
			}

			ReadFiles (BuildFilesVar, BuildAction.Compile, "Build", promptForRemoval);
			ReadFiles (DeployFilesVar, BuildAction.Content, "Deploy", promptForRemoval);
			ReadFiles (OthersVar, BuildAction.None, "Others", promptForRemoval);
			ReadFiles (ResourcesVar, BuildAction.EmbeddedResource, "Resources", promptForRemoval);

			if (!SyncReferences)
				return;

			try {
				SaveReferences = true;

				//Do these for DotNetProject only
				DotNetProject dotnetProject = ownerProject as DotNetProject;
				if (dotnetProject != null) {
					PackageRefVar.Extra.Clear ();
					AsmRefVar.Extra.Clear ();
					ProjectRefVar.Extra.Clear ();

					existingPackageRefs = new Dictionary<string, ProjectReference> ();
					requiredPackageVersions = new Dictionary<string,string> ();
					newPackageRefs = new Dictionary<string, ProjectReference> ();

					List<ProjectReference> toRemove = new List<ProjectReference> ();
					foreach (ProjectReference pref in dotnetProject.References) {
						if (pref.ReferenceType == ReferenceType.Package) {
							string [] files = pref.GetReferencedFileNames (ConfigurationSelector.Default);
							if (files == null)
								continue;

							if (pref.ReferenceType == ReferenceType.Package) {
								// Store the package version required by this reference. We'll use
								// the same version when trying to match references coming from the makefile
								SystemAssembly asm = assemblyContext.GetAssemblyFromFullName (pref.StoredReference, pref.Package != null ? pref.Package.Name : null, dotnetProject.TargetFramework);
								if (asm != null && asm.Package != null)
									requiredPackageVersions [asm.Package.Name] = asm.Package.Version;
							}
							// this should help normalize paths like /foo//bar/../
							string fullpath = Path.GetFullPath (files [0]);
							if (existingPackageRefs.ContainsKey (fullpath))
								toRemove.Add (pref);
							else
								existingPackageRefs [fullpath] = pref;
						}
					}

					// Remove the repeats
					foreach (ProjectReference pref in toRemove)
						dotnetProject.References.Remove (pref);

					ReadReferences (PackageRefVar, ReferenceType.Package, "Package References", dotnetProject);

					// !SaveReferences indicates that previous ref reading failed
					if (SaveReferences && String.Compare (AsmRefVar.Name, PackageRefVar.Name) != 0)
						ReadReferences (AsmRefVar, ReferenceType.Assembly, "Asm References", dotnetProject);
					if (SaveReferences && (String.Compare (ProjectRefVar.Name, PackageRefVar.Name) != 0) && 
						(String.Compare (ProjectRefVar.Name, AsmRefVar.Name) != 0))
						ReadReferences (ProjectRefVar, ReferenceType.Project, "Project References", dotnetProject);
					
					//Resolve References
					//Required when UpdateProject gets called by ui
					if (ownerProject.ParentSolution != null)
						ResolveProjectReferences (ownerProject.ParentSolution.RootFolder, monitor);
					
					//only remove unmatched existing refs if everything resolved without errors
					if (SaveReferences) {
						foreach (ProjectReference pr in existingPackageRefs.Values)
							dotnetProject.References.Remove (pr);
					}

					existingPackageRefs.Clear ();
					newPackageRefs.Clear ();
				}
			} catch (Exception e) {
				string msg = GettextCatalog.GetString (
					"Error in loading references: {0}. Skipping syncing of references", e.Message);
				LoggingService.LogWarning (msg);
				monitor.ReportWarning (msg);

				SaveReferences = false;
			}

			this.monitor = null;
		}

		void ReadFiles (MakefileVar fileVar, string buildAction, string id, bool promptForRemoval)
		{
			try { 
				fileVar.SaveEnabled = true;
				ReadFilesActual (fileVar, buildAction, id, promptForRemoval);
			} catch (Exception e) {
				string msg = GettextCatalog.GetString ("Error in loading files for '{0}'. Skipping.", id);
				LoggingService.LogError (msg, e);
				monitor.ReportWarning (msg);
				fileVar.SaveEnabled = false;
			}
		}

		void ReadFilesActual (MakefileVar fileVar, string buildAction, string id, bool promptForRemoval)
		{
			fileVar.Extra.Clear ();
			if (!fileVar.Sync || String.IsNullOrEmpty (fileVar.Name))
				return;

			//All filenames are treated as relative to the Makefile path
			List<string> files = Makefile.GetListVariable (fileVar.Name);
			if (files == null) {
				//FIXME: Move this to the caller, try-catch there
				string msg = GettextCatalog.GetString (
					"Makefile variable '{0}' not found. Skipping syncing of '{1}' file list for project '{2}'.",
					fileVar.Name, id, ownerProject.Name);
				LoggingService.LogWarning (msg);
				monitor.ReportWarning (msg);

				fileVar.SaveEnabled = false;
				return;
			}

			//FIXME: Trim?
			bool usePrefix = !String.IsNullOrEmpty (fileVar.Prefix);
			int len = 0;
			if (fileVar.Prefix != null)
				len = fileVar.Prefix.Length;

			Dictionary<string, ProjectFile> existingFiles = new Dictionary<string, ProjectFile> ();
			foreach (ProjectFile pf in ownerProject.Files) {
				if (pf.BuildAction == buildAction)
					existingFiles [ownerProject.GetAbsoluteChildPath (pf.FilePath)] = pf;
			}

			// True if the user has been warned that filenames contain Variables
			// but no configure.in path is set
			bool autotoolsWarned = false;
			bool varFound = false;

			foreach (string f in files) { 
				string fname = f.Trim ();
				
				try { 
					if (usePrefix && String.Compare (fileVar.Prefix, 0, fname, 0, len) == 0)
						//FIXME: If it doesn't match, then?
						fname = fname.Substring (len);

					fname = FromMakefilePath (fname);

					string resourceId = null;
					if (buildAction == BuildAction.EmbeddedResource && fname.IndexOf (',') >= 0) {
						string [] tmp = fname.Split (new char [] {','}, 2);
						fname = tmp [0];
						if (tmp.Length > 1)
							resourceId = tmp [1];
					}

					if ((fname.Length > 2 && fname [0] == '$' && fname [1] == '(') && !UseAutotools) {
						fileVar.Extra.Add (f);
						if (!autotoolsWarned) {
							string msg = GettextCatalog.GetString (
								"Files in variable '{0}' contains variables which cannot be parsed without the path " +
								"to configure.in being set. Ignoring such files.", fileVar.Name);
							LoggingService.LogWarning (msg);
							monitor.ReportWarning (msg);
							autotoolsWarned = true;
						}
						continue;
					}

					varFound = false;
					fname = ResolveBuildVars (fname, ref varFound);
					EncodeValues [fileVar.Name] |= varFound;

					//File path in the makefile are relative to the makefile,
					//but have to be added to the project as relative to project.BaseDirectory
					string absPath = Path.GetFullPath (Path.Combine (BaseDirectory, fname));

					if (existingFiles.ContainsKey (absPath)) {
						existingFiles.Remove (absPath);
						continue;
					}

					if (!File.Exists (absPath)) {
						//Invalid file, maybe we couldn't parse it correctly!
						string msg = GettextCatalog.GetString (
							"Ignoring invalid file '{0}' found in '{1}' for project '{2}'.", f,
							relativeMakefileName, OwnerProject.Name);
						LoggingService.LogWarning (msg);
						monitor.ReportWarning (msg);
						fileVar.Extra.Add (f);
						continue;
					}

					ProjectFile pf = ownerProject.AddFile (absPath, buildAction);
					if (buildAction == BuildAction.EmbeddedResource && resourceId != null)
						pf.ResourceId = resourceId;
				} catch (Exception e) {
					LoggingService.LogError (e.ToString ());
					fileVar.Extra.Add (f);
				}
			}

			if (existingFiles.Count > 0) {
				foreach (ProjectFile file in existingFiles.Values) {
					if (!IsFileExcluded (file.FilePath))
						ownerProject.Files.Remove (file);
				}
			}
		}

		Dictionary<string, ProjectReference> existingPackageRefs = null;
		Dictionary<string, ProjectReference> newPackageRefs = null;
		Dictionary<string, string> requiredPackageVersions = null;

		void ReadReferences (MakefileVar refVar, ReferenceType refType, string id, DotNetProject project)
		{
			if (String.IsNullOrEmpty (refVar.Name) || project == null)
				return;

			//All filenames are treated as relative to the Makefile path
			List<string> references = Makefile.GetListVariable (refVar.Name);
			if (references == null) {
				string msg = GettextCatalog.GetString (
					"Makefile variable '{0}' not found. Skipping syncing of all '{1}' references for project {2}.",
					refVar.Name, id, project.Name);
				LoggingService.LogWarning (msg);
				monitor.ReportWarning (msg);
				SaveReferences = false;
				return;
			}
			//FIXME: Trim?
			bool usePrefix = !String.IsNullOrEmpty (refVar.Prefix);
			int len = 0;
			if (refVar.Prefix != null)
				len = refVar.Prefix.Length;

			ReferencedPackages.Clear ();
			foreach (string r in references) {
				//Handle -r:System,System.Data also
				try { 
					ParseReference (r, usePrefix, refVar, len, refType, project);
				} catch (Exception e) {
					string msg = GettextCatalog.GetString ("Unable to parse reference '{0}' for project '{1}'. Ignoring.", r, project.Name);
					LoggingService.LogWarning (msg, e);
					monitor.ReportWarning (msg);
					refVar.Extra.Add (r);
				}
			}

			referencedPackages = null;
		}

		List<string> referencedPackages;
		List<string> ReferencedPackages  {
			get {
				if (referencedPackages == null)
					referencedPackages = new List<string> ();
				return referencedPackages;
			}
    		}

		//FIXME: change return type to bool, on false, extra.Add (reference)
		void ParseReference (string reference, bool usePrefix, MakefileVar refVar, int len, ReferenceType refType, 
				DotNetProject project)
		{
			string rname = reference;
			// .StartsWith "$("
			if (rname.Length > 3 && rname [0] == '$' && rname [1] == '(' && rname [rname.Length - 1] == ')') {
				if (!UseAutotools) {
					refVar.Extra.Add (reference);
					return;
				}

				// Autotools based project

				if (!rname.EndsWith ("_LIBS)")) {
					//Not a pkgconfig *_LIBS var
					refVar.Extra.Add (reference);
					return;
				}

				string pkgVarName = rname.Substring (2, rname.Length - 3).Replace ("_LIBS", String.Empty);
				List<PackageContent> pkgNames = ConfiguredPackages.GetPackageContentFromVarName (pkgVarName);
				if (pkgNames == null) {
					 LoggingService.LogWarning  ("Package named '{0}' not found in configure.in. Ignoring reference to '{1}'.",
						pkgVarName, rname);
					refVar.Extra.Add (reference);
					SaveReferences = false;
					return;
				}

				bool added = false;
				foreach (PackageContent packageContent in pkgNames) {
					if (ReferencedPackages.Contains (packageContent.Name)) {
						added = true;
						continue;
					}

					// Add all successfully added packages to ReferencedPackages
					foreach (string referencedName in packageContent.AllReferencedNames) {
						if (LoadPackageReference (referencedName, project, refVar.Prefix)) {
							ReferencedPackages.Add (referencedName);
							added = true;
						}
					}
				}

				// none of the packages could be added
				if (!added)
					refVar.Extra.Add (reference);

				return;
			}

			// .StartsWith "-pkg:"
			if (rname.Length >= 5 && rname [0] == '-' && rname [1] == 'p' && rname [2] == 'k' && rname [3] == 'g' && rname [4] == ':') {
				string pkgName = rname.Substring (5);
				//-pkg:foo,bar
				foreach (string s in pkgName.Split (new char [] {','}, StringSplitOptions.RemoveEmptyEntries)) {
					if (ReferencedPackages.Contains (s))
						continue;

					if (LoadPackageReference (s, project, refVar.Prefix))
						ReferencedPackages.Add (s);
					else
						refVar.Extra.Add ("-pkg:" + s);
				}

				return;
			}

			if (usePrefix && String.Compare (refVar.Prefix, 0, rname, 0, len) == 0)
				rname = rname.Substring (len);

			//FIXME: try/catch around the split refs ?
			bool varFound = false;
			foreach (string r in rname.Split (new char [] {','}, StringSplitOptions.RemoveEmptyEntries)) {
				string refname = r;
				if (refname.Length >= 2 && refname [0] == '$' && refname [1] == '(' && !UseAutotools) {
					//Eg. -r:$(top_builddir)/foo.dll
					refVar.Extra.Add (reference);
					continue;
				}

				varFound = false;
				refname = ResolveBuildVars (refname, ref varFound);
				EncodeValues [refVar.Name] |= varFound;

				string fullpath = Path.GetFullPath (Path.Combine (BaseDirectory, refname));
				
				// if refname is part of a package then add as package
				// but don't do it if the refname exactly matches a file name in the project dir
				if (refname.IndexOf (Path.DirectorySeparatorChar) < 0 && !File.Exists (fullpath) &&
					ParseReferenceAsPackage (refname, project) != null)
					continue;
				
				if (TryGetExistingPackageRef (fullpath) != null)
					continue;

				if (refname.IndexOf (Path.DirectorySeparatorChar) < 0) {
					// Check that its a valid assembly
					string fullname = null;
					try {
						fullname = AssemblyName.GetAssemblyName (fullpath).FullName;
					} catch (FileNotFoundException) {
					} catch (BadImageFormatException) {
					}

					// Valid assembly, From a package, add as Package
					SystemPackage pkg = assemblyContext.GetPackageFromPath (fullpath);
					if (fullname != null && pkg != null) {
						SystemAssembly sa = assemblyContext.GetAssemblyFromFullName (fullname, pkg.Name, project.TargetFramework);
						if (sa != null) {
							AddNewPackageReference (project, sa);
							continue;
						}
					}
				}

				//Else add to unresolved project refs, avoid repeats
				if (!UnresolvedReferences.ContainsKey (fullpath))
					UnresolvedReferences [fullpath] = fullpath;
			}
		}

		bool LoadPackageReference (string pkgName, DotNetProject project, string prefix)
		{
			SystemPackage pkg = null;
			string packageVersion;
			if (requiredPackageVersions.TryGetValue (pkgName, out packageVersion))
				pkg = assemblyContext.GetPackage (pkgName, packageVersion);
			if (pkg == null)
				pkg = assemblyContext.GetPackage (pkgName);
			
			if (pkg == null) {
				LoggingService.LogWarning ("No package named '{0}' found. Ignoring.", pkgName);
				//don't sync the unresolved refs from the project back to the makefile or things go horribly wrong
				SaveReferences = false;
				return false;
			}

			foreach (SystemAssembly sa in pkg.Assemblies) {
				if (TryGetExistingPackageRef (sa.Location) != null)
					continue;

				//Get fullname of the assembly
				AddNewPackageReference (project, sa);
			}

			return true;
		}

		ProjectReference ParseReferenceAsPackage (string rname, DotNetProject project)
		{
			string aname = rname;
			if (rname.EndsWith (".dll", StringComparison.InvariantCultureIgnoreCase))
				//-r:Mono.Posix.dll
				aname = rname.Substring (0, rname.Length - 4);

			string fullname = assemblyContext.GetAssemblyFullName (aname, project.TargetFramework);
			if (fullname == null)
				return null;

			SystemAssembly asm = assemblyContext.GetAssemblyForVersion (fullname, null, project.TargetFramework);
			if (asm == null)
				return null;

			ProjectReference pref = TryGetExistingPackageRef (asm.Location);
			if (pref != null)
				return pref;

			return AddNewPackageReference (project, asm);
		}

		ProjectReference TryGetExistingPackageRef (string fullpath)
		{
			if (existingPackageRefs.ContainsKey (fullpath)) {
				ProjectReference ret = existingPackageRefs [fullpath];
				existingPackageRefs.Remove (fullpath);
				newPackageRefs [fullpath] = ret;

				return ret;
			}

			if (newPackageRefs.ContainsKey (fullpath))
				return newPackageRefs [fullpath];

			return null;
		}

		ProjectReference AddNewPackageReference (DotNetProject project, SystemAssembly sa)
		{
			ProjectReference pref = new ProjectReference (sa);
			project.References.Add (pref);
			newPackageRefs [sa.Location] = pref;

			return pref;
		}

		public static void ResolveProjectReferences (SolutionFolder folder, ProgressMonitor monitor)
		{
			Dictionary<string, DotNetProject> projects = new Dictionary<string, DotNetProject> ();
			foreach (DotNetProject p in folder.GetAllItems<DotNetProject> ()) {
				string filename = p.GetOutputFileName (ConfigurationSelector.Default);
				// Can be null for Generic projects
				if (!String.IsNullOrEmpty (filename))
					projects [filename] = p;
			}

			foreach (DotNetProject sproj in projects.Values) {
				MakefileData mdata = sproj.ExtendedProperties ["MonoDevelop.Autotools.MakefileInfo"] as MakefileData;
				if (mdata == null)
					continue;

				if (mdata.UnresolvedReferences.Count != 0) {
				
					Dictionary<string, ProjectReference> asmProjectRefs =
						new Dictionary<string, ProjectReference> ();

					foreach (ProjectReference pr in sproj.References) {
						if (pr.ReferenceType != ReferenceType.Assembly &&
							pr.ReferenceType != ReferenceType.Project)
							continue;
						
						string [] files = pr.GetReferencedFileNames (ConfigurationSelector.Default);
						if (files.Length > 0)
							asmProjectRefs [files [0]] = pr;
					}

					List<string> toRemove = new List<string> ();
					foreach (string refstr in mdata.UnresolvedReferences.Keys) {
						if (asmProjectRefs.ContainsKey (refstr)) {
							// ref already exists in the project
							toRemove.Add (refstr);
							asmProjectRefs.Remove (refstr);
						} else {
							// Try as a project ref
							if (projects.ContainsKey (refstr)) {
								sproj.References.Add (new ProjectReference (projects [refstr]));
								toRemove.Add (refstr);
							}
						}
					}

					foreach (string s in toRemove)
						mdata.UnresolvedReferences.Remove (s);

					// Add all remaining unresolved refs as Assembly refs
					foreach (string s in mdata.UnresolvedReferences.Keys)
						sproj.References.Add (new ProjectReference (ReferenceType.Assembly, s));
						
					// Remove asm/project refs not found in UnresolvedReferences
					foreach (ProjectReference pr in asmProjectRefs.Values)
						sproj.References.Remove (pr);

					mdata.UnresolvedReferences.Clear ();
				}

				mdata.UpdateMakefile (monitor);
			}
		}

		string ResolveBuildVars (string filename, ref bool varFound)
		{   
			varFound = false;
			if (filename.IndexOf ('$') < 0)
				return filename;

			StringBuilder sb = new StringBuilder ();
			char c;
			int len = filename.Length;
			for (int i = 0; i < len; i ++) {
				c = filename [i];
				if (c != '$' || (i + 3 >= len)) {
					sb.Append (c);
					continue;
				}

				if (filename [i + 1] == '(') {
					int j = i + 2;
					while (j < len && filename [j] != ')')
						j ++;

					if (j >= len) {
						sb.Append (filename.Substring (i));
						break;
					}

					string varname = filename.Substring (i + 2, j - (i + 2));

					if (BuildVariables.ContainsKey (varname)) {
						sb.Append (BuildVariables [varname]);
						varFound = true;
					}
					else
						sb.Append ("$(" + varname + ")");
					i = j;
				}
			}

			return sb.ToString ();
		}

		//Converts a absolute filename to use the specified buildvar like top_builddir,
		//if applicable
		string EncodeFileName (string filename, string varname, bool isAbsolute)
		{
			if (!UseAutotools)
				return filename;

			string varpath = null;
			if (isAbsolute)
				varpath = GetAbsolutePath (BuildVariables [varname]);
			else
				varpath = GetRelativePath (BuildVariables [varname]);

			if (filename.StartsWith (varpath)) {
				string suffix = filename.Substring (varpath.Length);
				if (suffix [0] == Path.DirectorySeparatorChar)
					return String.Format ("$({0}){1}", varname, suffix);
				else
					return String.Format ("$({0}){1}{2}", varname, Path.DirectorySeparatorChar, suffix);
			}

			return filename;
		}
		

		string NormalizeFileName (string fileName)
		{
			// Cosmetic fix: remove the "./" prefix
			
			if (! (fileName [0] == '.' && fileName [1] == '/'))
				return fileName;
				
			do {
				if (fileName [0] == '.' && fileName [1] == '/')
					fileName = fileName.Substring (2);
				else if (fileName [0] == '/')
					fileName = fileName.Substring (1);
				else
					return fileName;
			} while (true);
		}

		//Writing methods

		public void UpdateMakefile (ProgressMonitor monitor)
		{
			//FIXME: AssemblyName & OutputDir

			if (!dirty || !IntegrationEnabled)
				return;

			this.monitor = monitor;
			bool makeRelative = (OwnerProject.BaseDirectory != BaseDirectory);

			//FIXME: If anything fails while writing, skip completely
			//FIXME: All the file vars must be distinct
			WriteFiles (BuildFilesVar, BuildAction.Compile, makeRelative, "Build");
			WriteFiles (DeployFilesVar, BuildAction.Content, makeRelative, "Deploy");
			WriteFiles (OthersVar, BuildAction.None, makeRelative, "Others");
			WriteFiles (ResourcesVar, BuildAction.EmbeddedResource, makeRelative, "Resources");

			if (SyncReferences && SaveReferences) {
				Makefile.ClearVariableValue (PackageRefVar.Name);
				Makefile.ClearVariableValue (AsmRefVar.Name);
				Makefile.ClearVariableValue (ProjectRefVar.Name);

				WriteReferences (PackageRefVar, ReferenceType.Package, makeRelative, "Package");
				WriteReferences (AsmRefVar, ReferenceType.Assembly, makeRelative, "Assembly");
				WriteReferences (ProjectRefVar, ReferenceType.Project, makeRelative, "Project");
			
				// Sort list of references in the makefile,
				// but sort only once per distinct var
				// (Required as we are comparing the full makefile as a string,
				//  to detect changes!)
				List<string> list = Makefile.GetListVariable (PackageRefVar.Name);
				if (list != null)
					list.Sort (System.StringComparer.InvariantCulture);

				if (String.Compare (AsmRefVar.Name, PackageRefVar.Name) != 0) {
					list = Makefile.GetListVariable (AsmRefVar.Name);
					if (list != null)
						list.Sort (System.StringComparer.InvariantCulture);
				}

				if ((String.Compare (ProjectRefVar.Name, PackageRefVar.Name) != 0) && 
					(String.Compare (ProjectRefVar.Name, AsmRefVar.Name) != 0)) {
					list = Makefile.GetListVariable (ProjectRefVar.Name);
					if (list != null)
						list.Sort (System.StringComparer.InvariantCulture);
				}
			}

			Save ();
			dirty = false;

			this.monitor = null;
		}

		bool WriteFiles (MakefileVar fileVar, string buildAction, bool makeRelative, string id)
		{
			if (!fileVar.Sync || !fileVar.SaveEnabled)
				return false;

			if (Makefile.GetListVariable (fileVar.Name) == null) {
				//Var not found, skip
				string msg = GettextCatalog.GetString (
                                        "Makefile variable '{0}' not found. Skipping writing of '{1}' files to the Makefile.", fileVar.Name, id);
                                LoggingService.LogWarning (msg);
                                monitor.ReportWarning (msg);
				return false;
			}

			List<string> files = new List<string> ();
			foreach (ProjectFile pf in OwnerProject.Files) {
				if (pf.Subtype != Subtype.Code)
					continue;
				if (IsFileExcluded (pf.FilePath))
					continue;
				if (pf.BuildAction == buildAction) {
					string str = null;
					if (makeRelative)
						//Files are relative to the Makefile
						str = GetRelativePath (pf.FilePath);
					else
						str = pf.FilePath.ToRelative (pf.Project.BaseDirectory);

					string unescapedFileName = Path.GetFileName (str);

					if (EncodeValues [fileVar.Name]) {
						if (pf.IsExternalToProject)
							str = EncodeFileName (str, "top_srcdir", false);
						else
							str = EncodeFileName (str, "srcdir", false);
					}
					str = ToMakefilePath (str);

					// Emit the resource ID only when it is different from the file name
					if (pf.BuildAction == BuildAction.EmbeddedResource && pf.ResourceId != null && pf.ResourceId.Length > 0 && pf.ResourceId != unescapedFileName)
						str = String.Format ("{0}{1},{2}", fileVar.Prefix, str, EscapeString (pf.ResourceId));
					else
						str = String.Format ("{0}{1}", fileVar.Prefix, str);

					str = NormalizeFileName (str);
					files.Add (str);
				}
			}

			foreach (string s in fileVar.Extra)
				files.Add (s);

			// Keep the file list sorted in the makefile
			files.Sort (System.StringComparer.InvariantCulture);
			
			Makefile.SetListVariable (fileVar.Name, files);
			return true;
		}

		bool WriteReferences (MakefileVar refVar, ReferenceType refType, bool makeRelative, string id)
		{
			//Reference vars can be shared too, so use existing list
			//Eg. REF for both Package and Asm ref
			List<string> references = Makefile.GetListVariable (refVar.Name);
			if (references == null) {
				//Var not found, skip
				string msg = GettextCatalog.GetString (
                                        "Makefile variable '{0}' not found. Skipping syncing of '{1}' references.", refVar.Name, id);
				LoggingService.LogWarning (msg);
				monitor.ReportWarning (msg);
				return false;
			}

			//if .Value is true
			//	key -> Varname, Emit as $key_LIBS
			//if .Value is false
			//	key -> pkgname, emit as -pkg:$key
			Dictionary<string, bool> hasAcSubstPackages = new Dictionary<string, bool> ();

			foreach (ProjectReference pr in ((DotNetProject)OwnerProject).References) {
				if (pr.ReferenceType != refType)
					continue;

				string refstr = String.Empty;
				switch (refType) {
				case ReferenceType.Package:
					//Assemblies coming from packages are always added as Package
					refstr = PackageRefToString (pr, hasAcSubstPackages, refVar);
					if (refstr == null)
						continue;
					break;
				case ReferenceType.Assembly:
					refstr = AsmRefToString (pr.Reference, refVar, false);
					break;
				case ReferenceType.Project:
					refstr = ProjectRefToString (pr, refVar);
					if (refstr == null)
						continue;
					break;
				default:
					//not supported
					continue;
				}

				references.Add (String.Format ("{0}{1}", refVar.Prefix, refstr));
			}

			//Add packages
			foreach (KeyValuePair<string, bool> pair in hasAcSubstPackages) {
				if (pair.Value)
					references.Add (String.Format ("$({0}_LIBS)", pair.Key));
				else
					references.Add (String.Format ("-pkg:{0}", pair.Key));
			}

			foreach (string s in refVar.Extra)
				references.Add (s);

			Makefile.SetListVariable (refVar.Name, references);
			return true;
		}

		string PackageRefToString (ProjectReference pr, Dictionary<string, bool> hasAcSubstPackages, MakefileVar refVar)
		{
			//Package ref can be a full name OR a path!
			//FIXME: Use GetReferencedFileName and GetPackageFromPath ?
			string fullname = pr.Reference;
			SystemPackage pkg = pr.Package;
			if (pkg == null) {
				//reference could be a path
				pkg = assemblyContext.GetPackageFromPath (Path.GetFullPath (pr.Reference));
				if (pkg != null) {
					//Path
					try {
						fullname = AssemblyName.GetAssemblyName (pr.Reference).FullName;
						//If this throws : Invalid assembly!
						//let it fall through and be emitted as a asm ref
					} catch (FileNotFoundException) {
						pkg = null;
					} catch (BadImageFormatException) {
						pkg = null;
					}
				}
			}

			if (pkg == null)
				return AsmRefToString (pr.GetReferencedFileNames (ConfigurationSelector.Default) [0], refVar, false);

			// Reference is from a package

			if (pkg.IsCorePackage)
				//pkg:mono, Eg. System, System.Data etc
				return fullname.Split (new char [] {','}, 2) [0];

			//Ref is from a non-core package
			string varname = null;
			if (UseAutotools)
				//Check whether ref'ed in configure.in
				varname = ConfiguredPackages.GetVarNameFromName (pkg.Name);
			
			if (varname == null) {
				//Package not referenced in configure.in
				//Or not a autotools based project,
				//so emit -pkg:

				if (!hasAcSubstPackages.ContainsKey (pkg.Name)) {
					if (UseAutotools) {
						//Warn only if UseAutotools
						string msg = GettextCatalog.GetString (
							"A reference to the pkg-config package '{0}' is being emitted to the Makefile, " +
							"because at least one assembly from the package is used in the project '{1}'. However, " +
							"this dependency is not specified in the configure.in file, so you might need to " +
							"add it to ensure that the project builds successfully on other systems.", pkg.Name, pr.OwnerProject.Name);
						LoggingService.LogWarning (msg);
						monitor.ReportWarning (msg);
					}

					hasAcSubstPackages [pkg.Name] = false;
				}
			} else {
				// If the package as AC_SUBST(FOO_LIBS) defined, then
				// emit FOO_LIBS, else emit -pkg:foo
				if (ConfiguredPackages.HasAcSubst (varname + "_LIBS")) {
					hasAcSubstPackages [varname] = true;
				} else {
					hasAcSubstPackages [pkg.Name] = false;
				}
			}

			return null;
		}

		string AsmRefToString (string reference, MakefileVar refVar, bool isBuiltAssembly)
		{
			if (EncodeValues [refVar.Name]) {
				if (!isBuiltAssembly)
					return ToMakefilePath (EncodeFileName (reference, "top_srcdir", true));
				else if (!reference.StartsWith (BaseDirectory))
					//Reference is external to this project
					return ToMakefilePath (EncodeFileName (reference, "top_builddir", true));
			}

			// !external and !encode
			return ToMakefilePath (GetRelativePath (reference));
		}

		string ProjectRefToString (ProjectReference pr, MakefileVar refVar)
		{
			string [] tmp = pr.GetReferencedFileNames (ConfigurationSelector.Default);
			if (tmp == null || tmp.Length == 0)
				//Reference not found, ignoring
				return null;

			return AsmRefToString (tmp [0], refVar, true);
		}

		public static string GetUnixPath (string path)
		{
			if (Path.DirectorySeparatorChar != '/')
				return path.Replace (Path.DirectorySeparatorChar, '/');
			else
				return path;
		}

		public static string FromUnixPath (string path)
		{
			if (Path.DirectorySeparatorChar != '/')
				return path.Replace (Path.DirectorySeparatorChar, '/');
			else
				return path;
		}

		public static string ToMakefilePath (string str)
		{
			return EscapeString (GetUnixPath (str));
		}

		public static string FromMakefilePath (string str)
		{
			return FromUnixPath (UnescapeString (str));
		}

		public static string EscapeString (string str)
		{
			StringBuilder sb = new StringBuilder ();
			int len = str.Length;
			for (int i = 0; i < len; i ++) {
				char c = str [i];
				if (c == '\\' || c == '#' || c == ' ')
					sb.Append ("\\");
				
				sb.Append (c);
			}
			
			return sb.ToString ();
		}
		
		static string UnescapeString (string str)
		{
			if (str.IndexOf ('\\') < 0)
				return str;
			
			StringBuilder sb = new StringBuilder ();
			int len = str.Length;
			for (int i = 0; i < len; i ++) {
				char c = str [i];
				if (c != '\\' || i == len - 1) {
					sb.Append (c);
					continue;
				}
				char next = str [i + 1];
				if (next != '\\' && next != '#' && next != ' ')
					sb.Append ("\\");
				
				sb.Append (next);
				i ++;
			}
			
			return sb.ToString ();
		}

	}

	public class ConfiguredPackagesManager
	{
		Dictionary<string, List<PackageContent>> pkgVarNameToPkgName;
		Dictionary<string, string> pkgNameToPkgVarName;

		//This dict has entries for all vars that have a
		//AC_SUBST(VARNAME_LIBS) defined
		Dictionary<string, string> varNameAcSubst;

		string fullpath;
		DateTime lastWriteTime;

		public ConfiguredPackagesManager (string fullpath)
		{
			this.fullpath = fullpath;
			
			if (!File.Exists (fullpath) || (Path.GetFileName (fullpath) != "configure.in" && Path.GetFileName (fullpath) != "configure.ac"))
				//FIXME: Exception type?
				throw new ArgumentException (GettextCatalog.GetString ("Unable to find configure.in at '{0}'.", fullpath));

			FileInfo finfo = new FileInfo (fullpath);
			lastWriteTime = finfo.LastWriteTime;
			ReadPackagesList ();
		}
		
		//Gets the pkg-config PackageContent from the makefile (or autoconf?) var name
		public List<PackageContent> GetPackageContentFromVarName (string varname)
		{
			if (!pkgVarNameToPkgName.ContainsKey (varname)) {
				LoggingService.LogDebug ("pkg-config variable {0} not found in pkgVarNameToPkgName.", varname);
				return null;
			}
			
			return pkgVarNameToPkgName [varname];
		}

		//Gets the pkg-config varname from a package name (info comes from configure.in)
		public string GetVarNameFromName (string name)
		{
			if (!pkgNameToPkgVarName.ContainsKey (name)) {
				LoggingService.LogDebug ("Package named '{0}' not specified in configure.in", name);
				return null;
			}

			return pkgNameToPkgVarName [name];
		}

		public bool HasAcSubst (string varname)
		{
			return varNameAcSubst.ContainsKey (varname);
		}
		
		
		void ReadPackagesList ()
		{
			pkgVarNameToPkgName = new Dictionary<string, List<PackageContent>> ();
			pkgNameToPkgVarName = new Dictionary<string, string> ();
			varNameAcSubst = new Dictionary<string, string> ();

			string content = null;
			using (StreamReader sr = new StreamReader (fullpath))
				content = sr.ReadToEnd ();
			var assemblyContext = MakefileData.GetMonoRuntimeContext ();

			foreach (Match match in PkgCheckModulesRegex.Matches (content)) {
				if (!match.Success)
					continue;

				List<PackageContent> pkgs = new List<PackageContent> ();
				string pkgId = match.Groups ["pkgId"].Value;
				if (pkgId.Length > 2 && pkgId [0] == '[' && pkgId [pkgId.Length - 1] == ']')
					// Remove [] used for quoting
					pkgId = pkgId.Substring (1, pkgId.Length - 2);

				foreach (Capture c in match.Groups ["content"].Captures) {
					string s = c.Value.Trim ();
					if (s.Length == 0)
						continue;

					if (s.Length > 2 && s [0] == '[' && s [s.Length - 1] == ']')
						// Remove [] used for quoting
						s = s.Substring (1, s.Length - 2);
					PackageContent packageContent = new PackageContent (s);
					pkgs.Add (packageContent);
					pkgNameToPkgVarName [s] = pkgId;
					AddRequiredPackages (assemblyContext, pkgId, s, packageContent);
				}
				pkgVarNameToPkgName [pkgId] = pkgs;
			}
			
			foreach (Match match in AcSubstRegex.Matches (content)) {
				if (!match.Success)
					continue;

				string s = match.Groups [1].Value;
				if (!s.EndsWith ("_LIBS"))
					continue;

				string pkgVarName = s.Replace ("_LIBS", String.Empty);
				List<PackageContent> l = GetPackageContentFromVarName (pkgVarName);
				
				IAssemblyContext r = MakefileData.GetMonoRuntimeContext ();
				if (l != null && l.Count == 1 && r.GetPackage (l [0].Name) != null) {
					//PKG_CHECK_MODULES for pkgVarName was found
					//and it references only a single managed package
					//
					//This ensures that we don't emit $(FOO_LIBS)
					//for pkgVarName's that reference > 1 package or
					//for unmanaged packages
					varNameAcSubst [s] = s;
				}
			}
		}

		void AddRequiredPackages (IAssemblyContext assemblyContext, string variableName, string packageName, PackageContent packageContent)
		{
			SystemPackage package = assemblyContext.GetPackage (packageName);
			if (package != null && !string.IsNullOrEmpty (package.Requires)) {
				foreach (string requiredPackageName in package.Requires.Split (' ')) {
					if (!pkgNameToPkgVarName.ContainsKey (requiredPackageName)) {
						pkgNameToPkgVarName[requiredPackageName] = variableName;
						packageContent.RequiredPackages.Add (requiredPackageName);
					}
					AddRequiredPackages (assemblyContext, variableName, requiredPackageName, packageContent);
				}
			}
		}
		
		public DateTime LastWriteTime {
			get { return lastWriteTime; }
		}

		static Regex pkgCheckModulesRegex = null;
		static Regex PkgCheckModulesRegex {
			get {
				if (pkgCheckModulesRegex == null)
					pkgCheckModulesRegex = new Regex (
						@".*PKG_CHECK_MODULES\(\s*(?<pkgId>[^,\) \t]*)\s*,(\s*(?<content>[^,\) \t]*)\s*([><=]+\s*[^,\) \t]+|))*");
				return pkgCheckModulesRegex;
			}
		}

		static Regex acsubstRegex = null;
		static Regex AcSubstRegex {
			get {
				if (acsubstRegex == null)
					acsubstRegex = new Regex (@"AC_SUBST\(\s*([^\)\s]+)\s*\)");
				return acsubstRegex;
			}
		}
	}
	
	public class PackageContent
	{
		public string Name { get; set; }
		
		System.Collections.Generic.List<string> requiredPackages = new System.Collections.Generic.List<string> ();
		public List<string> RequiredPackages { 
			get {
				return requiredPackages; 
			}
		}
		
		public IEnumerable<string> AllReferencedNames {
			get {
				yield return Name;
				foreach (string name in requiredPackages) {
					yield return name;
				}
			}
		}
		
		public PackageContent (string name)
		{
			this.Name = name;
		}
	}
}