[interp] Add a few starting stats (#13347)
[mono-project.git] / msvc / mono.winconfig.targets
blobcc0b51a7b11faf29b415e11a7295e32fd25083c0
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">
5 <ParameterGroup>
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" />
10 </ParameterGroup>
11 <Task>
12 <Code Type="Class" Language="cs">
13 <![CDATA[
14 using System;
15 using System.IO;
16 using System.Text;
17 using System.Linq;
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)
27 monoVersion = null;
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))
35 string line;
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;
42 continue;
45 var monoCorlibVersionMatch = monoCorlibVersionRegEx.Match (line);
46 if (monoCorlibVersionMatch != null && monoCorlibVersionMatch.Success && monoCorlibVersionMatch.Groups.Count == 3)
48 monoCorlibVersion = monoCorlibVersionMatch.Groups[2].Value;
49 continue;
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))
64 string line;
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 = "";
94 try
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#",
112 monoCorlibVersion
114 var monoCorlibVersionRegex = new Regex (monoCorlibVersions[0]);
116 var monoVersions = new string [] {
117 ".*#MONO_VERSION#.*",
118 "#MONO_VERSION#",
119 monoVersion
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))
134 string line;
135 Match match;
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");
151 replaced = true;
152 break;
156 if (!replaced)
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]));
164 replaced = true;
165 break;
170 if (!replaced)
171 writer.WriteLine (line);
175 bool overwrite = true;
176 if (File.Exists (targetPath))
178 if (File.ReadAllBytes (tempFilePath).SequenceEqual (File.ReadAllBytes (targetPath)))
179 overwrite = false;
182 if (overwrite)
183 File.Copy (tempFilePath, targetPath, true);
186 finally
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)))
210 overwrite = false;
213 if (overwrite)
214 File.Copy (tempFile, targetFile, true);
216 finally
218 if (!String.IsNullOrEmpty (tempFile))
219 File.Delete (tempFile);
223 bool BackupConfigFile (string sourceConfigFile, string targetConfigFile)
225 bool result = false;
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);
235 result = true;
239 return result;
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.");
272 return true;
276 </Code>
277 </Task>
278 </UsingTask>
280 <Target Name="RunWinConfigSetup">
281 <WinConfigSetup ConfigFileRoot="$(MSBuildThisFileDirectory)..\"
282 DisableDefines=""
283 EnableDefines=""
284 HaveDefines=""/>
285 </Target>
287 </Project>