1 <?xml version=
"1.0" encoding=
"utf-8"?>
2 <Project DefaultTargets=
"Build" ToolsVersion=
"4.0" xmlns=
"http://schemas.microsoft.com/developer/msbuild/2003">
4 <UsingTask TaskName=
"WinConfigSetup" TaskFactory=
"CodeTaskFactory" AssemblyFile=
"$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
6 <ConfigFileRoot ParameterType=
"System.String" Required=
"true" />
7 <DisableDefines ParameterType=
"System.String" Required=
"false" />
8 <EnableDefines ParameterType=
"System.String" Required=
"false" />
9 <HaveDefines ParameterType=
"System.String" Required=
"false" />
12 <Code Type=
"Class" Language=
"cs">
18 using Microsoft.Build.Framework;
19 using Microsoft.Build.Utilities;
20 using System.Collections.Generic;
21 using System.Text.RegularExpressions;
23 public class _WinConfigSetup : Task
25 void GetVersionsFromConfigureAC (string configureACFile, out string monoVersion, out string monoCorlibVersion)
28 monoCorlibVersion = null;
29 if (File.Exists (configureACFile))
31 var monoVersionRegEx = new Regex (@
"(.*AC_INIT\(mono, \[)(\d+\.\d+\.\d+)");
32 var monoCorlibVersionRegEx = new Regex (@
"(MONO_CORLIB_VERSION=)([A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12})");
33 using (StreamReader reader = new StreamReader (configureACFile))
36 while ((line = reader.ReadLine ()) != null && (monoVersion == null || monoCorlibVersion == null))
38 var monoVersionMatch = monoVersionRegEx.Match (line);
39 if (monoVersionMatch != null && monoVersionMatch.Success && monoVersionMatch.Groups.Count ==
3)
41 monoVersion = monoVersionMatch.Groups[
2].Value;
45 var monoCorlibVersionMatch = monoCorlibVersionRegEx.Match (line);
46 if (monoCorlibVersionMatch != null && monoCorlibVersionMatch.Success && monoCorlibVersionMatch.Groups.Count ==
3)
48 monoCorlibVersion = monoCorlibVersionMatch.Groups[
2].Value;
56 string [] GetDisabledConfigFeatures (string path)
58 var disabledFeatures = new List
<string> ();
59 if (File.Exists (path))
61 var regex = new Regex (
".*#define.*DISABLE_.*1");
62 using (StreamReader reader = new StreamReader (path))
65 while ((line = reader.ReadLine ()) != null)
67 var match = regex.Match (line);
68 if (match != null && match.Success)
70 if (match.Groups != null && match.Groups.Count ==
1)
72 var configLine = match.Groups[
0].ToString ();
73 if (!String.IsNullOrEmpty (configLine))
75 var configItems = configLine.Trim ().Split (' ');
76 if (configItems != null && configItems.Length ==
3)
78 // Second item should be the define.
79 disabledFeatures.Add (configItems[
1]);
87 return disabledFeatures.ToArray ();
90 void CreateConfigUsingTemplate (string templatePath, string targetPath, string [] disabledDefines, string [] enabledDefines, string [] haveDefines, string monoVersion, string monoCorlibVersion)
92 string tempFilePath =
"";
96 if (File.Exists (templatePath))
98 var disabledDefinesRegex = new Regex (
".*@DISABLE_DEFINES@.*");
99 var enabledDefinesRegex = new Regex (
".*@ENABLE_DEFINES@.*");
100 var haveDefinesRegex = new Regex (
".*@HAVE_DEFINES.*");
102 var definesList = new List
<Tuple<Regex, string []
>>
104 new Tuple
<Regex, string []
>(disabledDefinesRegex, disabledDefines),
105 new Tuple
<Regex, string []
>(enabledDefinesRegex, enabledDefines),
106 new Tuple
<Regex, string []
>(haveDefinesRegex, haveDefines)
109 var monoCorlibVersions = new string [] {
110 ".*#MONO_CORLIB_VERSION#.*",
111 "#MONO_CORLIB_VERSION#",
114 var monoCorlibVersionRegex = new Regex (monoCorlibVersions[
0]);
116 var monoVersions = new string [] {
117 ".*#MONO_VERSION#.*",
121 var monoVersionRegex = new Regex (monoVersions[
0]);
123 var versionList = new List
<Tuple<Regex, string []
>>
125 new Tuple
<Regex, string []
>(monoCorlibVersionRegex, monoCorlibVersions),
126 new Tuple
<Regex, string []
>(monoVersionRegex, monoVersions)
129 tempFilePath = Path.GetTempFileName ();
131 using (StreamReader reader = new StreamReader (templatePath))
132 using (StreamWriter writer = new StreamWriter (tempFilePath))
136 while ((line = reader.ReadLine ()) != null)
138 var replaced = false;
139 foreach (var defines in definesList)
141 match = defines.Item1.Match (line);
142 if (match != null && match.Success && defines.Item2 != null)
144 // Replace with disabled defines.
145 foreach (var define in defines.Item2)
147 writer.WriteLine (
"#ifndef {0}", define);
148 writer.WriteLine (
"#define {0} 1", define);
149 writer.WriteLine (
"#endif");
158 foreach (var version in versionList)
160 match = version.Item1.Match (line);
161 if (match != null && match.Success)
163 writer.WriteLine (line.Replace(version.Item2[
1], version.Item2[
2]));
171 writer.WriteLine (line);
175 bool overwrite = true;
176 if (File.Exists (targetPath))
178 if (File.ReadAllBytes (tempFilePath).SequenceEqual (File.ReadAllBytes (targetPath)))
183 File.Copy (tempFilePath, targetPath, true);
188 if (!String.IsNullOrEmpty (tempFilePath))
189 File.Delete (tempFilePath);
193 void CreateVersionFile (string targetFile)
195 string tempFile =
"";
199 tempFile = Path.GetTempFileName ();
201 using (StreamWriter writer = new StreamWriter (tempFile))
203 writer.WriteLine (
"#define FULL_VERSION \"Visual Studio built mono\
"");
206 bool overwrite = true;
207 if (File.Exists (targetFile))
209 if (File.ReadAllBytes (tempFile).SequenceEqual (File.ReadAllBytes (targetFile)))
214 File.Copy (tempFile, targetFile, true);
218 if (!String.IsNullOrEmpty (tempFile))
219 File.Delete (tempFile);
223 bool BackupConfigFile (string sourceConfigFile, string targetConfigFile)
226 if (File.Exists (sourceConfigFile))
228 var includesCygconfig = new Regex (
".*#include \"cygconfig.h\
".*");
230 var allText = File.ReadAllText (sourceConfigFile);
231 var match = includesCygconfig.Match (allText);
232 if (match == null || !match.Success)
234 File.Copy (sourceConfigFile, targetConfigFile, true);
242 public string ConfigFileRoot { get; set; }
243 public string DisableDefines { get; set; }
244 public string EnableDefines { get; set; }
245 public string HaveDefines { get; set; }
247 public override bool Execute ()
249 string configureACFile = Path.Combine (ConfigFileRoot, @
"configure.ac");
250 string configFile = Path.Combine (ConfigFileRoot, @
"config.h");
251 string cygConfigFile = Path.Combine (ConfigFileRoot, @
"cygconfig.h");
252 string winConfigFile = Path.Combine (ConfigFileRoot, @
"winconfig.h");
253 string versionFile = Path.Combine (ConfigFileRoot, @
"mono\mini\version.h");
255 Log.LogMessage (MessageImportance.High,
"Setting up Mono configuration headers...");
257 BackupConfigFile (configFile, cygConfigFile);
259 string monoVersion = null;
260 string monoCorlibVersion = null;
261 GetVersionsFromConfigureAC (configureACFile, out monoVersion, out monoCorlibVersion);
263 var disableDefines = GetDisabledConfigFeatures (cygConfigFile);
264 var enableDefines = (EnableDefines != null) ? EnableDefines.Split (';') : null;
265 var haveDefines = (HaveDefines != null) ? HaveDefines.Split (';') : null;
266 CreateConfigUsingTemplate (winConfigFile, configFile, disableDefines, enableDefines, haveDefines, monoVersion, monoCorlibVersion);
268 CreateVersionFile (versionFile);
270 Log.LogMessage (MessageImportance.High,
"Successfully setup Mono configuration headers.");
280 <Target Name=
"RunWinConfigSetup">
281 <WinConfigSetup ConfigFileRoot=
"$(MSBuildThisFileDirectory)..\"