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

mono.winconfig.targets « msvc - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d8e39ca7ad2dc73355fd6fe7d1e13c24ef76167b (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
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <UsingTask TaskName="GetVersionsFromConfigureAC" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
      <ParameterGroup>
        <ConfigFileRoot ParameterType="System.String" Required="true" />
        <MonoVersion ParameterType="System.String" Output="true" />
        <MonoCorlibVersion ParameterType="System.String" Output="true" />
      </ParameterGroup>
        <Task>
            <Using Namespace="System" />
            <Using Namespace="System.Text.RegularExpressions" />
            <Using Namespace="System.IO" />
            <Code Type="Fragment" Language="cs">
            <![CDATA[
                MonoVersion = null;
                MonoCorlibVersion = null;

                string configureACFile = Path.Combine (ConfigFileRoot, @"configure.ac");

                if (File.Exists (configureACFile))
                {
                    // These are both fairly arbitrary strings.
                    var monoVersionRegEx = new Regex (@"^AC_INIT\s*\(\s*mono,\s*\[?\s*([^\],\s]+)");
                    var monoCorlibVersionRegEx = new Regex (@"^MONO_CORLIB_VERSION\s*=\s*(\S+)");
                    using (StreamReader reader = new StreamReader (configureACFile))
                    {
                        string line;
                        while ((line = reader.ReadLine ()) != null && (MonoVersion == null || MonoCorlibVersion == null))
                        {
                            var monoVersionMatch = monoVersionRegEx.Match (line);
                            if (monoVersionMatch.Success)
                            {
                                if (MonoVersion != null)
                                {
                                    Log.LogError("duplicate MONO_INIT in {0}", configureACFile);
                                }
                                MonoVersion = monoVersionMatch.Groups[1].Value;
                                Log.LogMessage ("{0}:AC_INIT:MONO_VERSION=\"{1}\"", configureACFile, MonoVersion);
                                continue;
                            }

                            var monoCorlibVersionMatch = monoCorlibVersionRegEx.Match (line);
                            if (monoCorlibVersionMatch.Success)
                            {
                                if (MonoCorlibVersion != null)
                                {
                                    // This is misleading. The loop stops when both found, so duplicates usually not noticed.
                                    Log.LogError("duplicate MONO_CORLIB_VERSION in {0}", configureACFile);
                                }
                                MonoCorlibVersion = monoCorlibVersionMatch.Groups[1].Value;
                                Log.LogMessage ("{0}:MONO_CORLIB_VERSION=\"{1}\"", configureACFile, MonoCorlibVersion);
                                continue;
                            }
                        }
                    }
                }
            ]]>
            </Code>
        </Task>
    </UsingTask>

    <UsingTask TaskName="WinConfigSetup" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
      <ParameterGroup>
        <ConfigFileRoot ParameterType="System.String" Required="true" />
        <MonoVersion ParameterType="System.String" Required="true" />
        <MonoCorlibVersion ParameterType="System.String" Required="true" />
        <DisableDefines ParameterType="System.String" Required="false" />
        <EnableDefines ParameterType="System.String" Required="false" />
        <HaveDefines ParameterType="System.String" Required="false" />
      </ParameterGroup>
        <Task>
            <Code Type="Class" Language="cs">
            <![CDATA[
                using System;
                using System.IO;
                using System.Text;
                using System.Linq;
                using Microsoft.Build.Framework;
                using Microsoft.Build.Utilities;
                using System.Collections.Generic;
                using System.Text.RegularExpressions;

                public class _WinConfigSetup : Task
                {
                  List<string> GetConfigFeatures (string path, string featuresRegex)
                  {
                      var features = new List<string> ();
                      if (File.Exists (path))
                      {
                          var regex = new Regex (featuresRegex);
                          using (StreamReader reader = new StreamReader (path))
                          {
                              string line;
                              while ((line = reader.ReadLine ()) != null)
                              {
                                  var match = regex.Match (line);
                                  if (match != null && match.Success)
                                  {
                                      if (match.Groups != null && match.Groups.Count == 1)
                                      {
                                          var configLine = match.Groups[0].ToString ();
                                          if (!String.IsNullOrEmpty (configLine))
                                          {
                                              var configItems = configLine.Trim ().Split (' ');
                                              if (configItems != null && configItems.Length == 3)
                                              {
                                                  // Second item should be the define.
                                                  features.Add (configItems[1]);
                                              }
                                          }
                                      }
                                  }
                              }
                          }
                      }
                      return features;
                  }

                  string [] GetEnabledConfigFeatures (string path, string enableDefines)
                  {
                      string [] supportedFeatures = { "ENABLE_ICALL_SYMBOL_MAP",
                                                      "ENABLE_LLVM",
                                                      "ENABLE_LLVM_RUNTIME",
                                                      "ENABLE_HYBRID_SUSPEND",
                                                      "ENABLE_COOP_SUSPEND",
                                                      "ENABLE_NETCORE" };

                      var enableFeatures = GetConfigFeatures(path, ".*#define.*ENABLE_.*1");
                      if (enableDefines != null)
                          enableFeatures.AddRange (enableDefines.Split (';'));

                      // Only keep supported features on Windows platforms using MSVC.
                      var features = new List<string> ();
                      foreach (var feature in enableFeatures)
                          if (Array.Exists(supportedFeatures, s => String.CompareOrdinal (s,feature) == 0))
                              features.Add(feature);
                      return features.ToArray ();
                  }

                  string [] GetDisabledConfigFeatures (string path)
                  {
                      return GetConfigFeatures(path, ".*#define.*DISABLE_.*1").ToArray ();
                  }

                  void CreateConfigUsingTemplate (string templatePath, string targetPath, string [] disabledDefines, string [] enabledDefines, string [] haveDefines, string monoVersion, string monoCorlibVersion)
                  {
                      string tempFilePath = "";

                      try
                      {
                          if (File.Exists (templatePath))
                          {
                              var disabledDefinesRegex = new Regex (".*@DISABLE_DEFINES@.*");
                              var enabledDefinesRegex = new Regex (".*@ENABLE_DEFINES@.*");
                              var haveDefinesRegex = new Regex (".*@HAVE_DEFINES.*");

                              var definesList = new List<Tuple<Regex, string []>>
                              {
                                  new Tuple<Regex, string []>(disabledDefinesRegex, disabledDefines),
                                  new Tuple<Regex, string []>(enabledDefinesRegex, enabledDefines),
                                  new Tuple<Regex, string []>(haveDefinesRegex, haveDefines)
                              };

                              var monoCorlibVersions = new string [] {
                                  ".*#MONO_CORLIB_VERSION#.*",
                                  "#MONO_CORLIB_VERSION#",
                                  monoCorlibVersion
                              };
                              var monoCorlibVersionRegex = new Regex (monoCorlibVersions[0]);

                              var monoVersions = new string [] {
                                  ".*#MONO_VERSION#.*",
                                  "#MONO_VERSION#",
                                  monoVersion
                              };
                              var monoVersionRegex = new Regex (monoVersions[0]);

                              var versionList = new List<Tuple<Regex, string []>>
                              {
                                  new Tuple<Regex, string []>(monoCorlibVersionRegex, monoCorlibVersions),
                                  new Tuple<Regex, string []>(monoVersionRegex, monoVersions)
                              };

                              tempFilePath = Path.GetTempFileName ();

                              using (StreamReader reader = new StreamReader (templatePath))
                              using (StreamWriter writer = new StreamWriter (tempFilePath))
                              {
                                  string line;
                                  Match match;
                                  while ((line = reader.ReadLine ()) != null)
                                  {
                                      var replaced = false;
                                      foreach (var defines in definesList)
                                      {
                                          match = defines.Item1.Match (line);
                                          if (match != null && match.Success && defines.Item2 != null)
                                          {
                                              // Replace with disabled defines.
                                              foreach (var define in defines.Item2)
                                              {
                                                  writer.WriteLine ("#ifndef {0}", define);
                                                  writer.WriteLine ("#define {0} 1", define);
                                                  writer.WriteLine ("#endif");
                                              }
                                              replaced = true;
                                              break;
                                          }
                                      }

                                      if (!replaced)
                                      {
                                          foreach (var version in versionList)
                                          {
                                              match = version.Item1.Match (line);
                                              if (match != null && match.Success)
                                              {
                                                  writer.WriteLine (line.Replace(version.Item2[1], version.Item2[2]));
                                                  replaced = true;
                                                  break;
                                              }
                                          }
                                      }

                                      if (!replaced)
                                          writer.WriteLine (line);
                                  }
                              }

                              bool overwrite = true;
                              if (File.Exists (targetPath))
                              {
                                  if (File.ReadAllBytes (tempFilePath).SequenceEqual (File.ReadAllBytes (targetPath)))
                                      overwrite = false;
                              }

                              if (overwrite)
                                  File.Copy (tempFilePath, targetPath, true);
                          }
                      }
                      finally
                      {
                          if (!String.IsNullOrEmpty (tempFilePath))
                              File.Delete (tempFilePath);
                      }
                  }

                  void CreateVersionFile (string targetFile)
                  {
                      string tempFile = "";

                      try
                      {
                          tempFile = Path.GetTempFileName ();

                          using (StreamWriter writer = new StreamWriter (tempFile))
                          {
                              writer.WriteLine ("#define FULL_VERSION \"Visual Studio built mono\"");
                          }

                          bool overwrite = true;
                          if (File.Exists (targetFile))
                          {
                              if (File.ReadAllBytes (tempFile).SequenceEqual (File.ReadAllBytes (targetFile)))
                                  overwrite = false;
                          }

                          if (overwrite)
                              File.Copy (tempFile, targetFile, true);
                      }
                      finally
                      {
                          if (!String.IsNullOrEmpty (tempFile))
                              File.Delete (tempFile);
                      }
                  }

                  bool BackupConfigFile (string sourceConfigFile, string targetConfigFile)
                  {
                      bool result = false;
                      if (File.Exists (sourceConfigFile))
                      {
                          var includesCygconfig = new Regex (".*#include \"cygconfig.h\".*");

                          var allText = File.ReadAllText (sourceConfigFile);
                          var match = includesCygconfig.Match (allText);
                          if (match == null || !match.Success)
                          {
                              File.Copy (sourceConfigFile, targetConfigFile, true);
                              result = true;
                          }
                      }

                      return result;
                  }

                  public string ConfigFileRoot { get; set; }
                  public string MonoVersion { get; set; }
                  public string MonoCorlibVersion { get; set; }
                  public string DisableDefines { get; set; }
                  public string EnableDefines { get; set; }
                  public string HaveDefines { get; set; }

                  public override bool Execute ()
                  {
                    if (String.IsNullOrEmpty (ConfigFileRoot))
                    {
                        Log.LogError("Missing ConfigFileRoot");
                        return false;
                    }

                    if (String.IsNullOrEmpty (MonoVersion))
                    {
                        Log.LogError("Missing MonoVersion");
                        return false;
                    }

                    if (String.IsNullOrEmpty (MonoCorlibVersion))
                    {
                        Log.LogError("Missing MonoCorlibVersion");
                        return false;
                    }

                    string configFile = Path.Combine (ConfigFileRoot, @"config.h");
                    string cygConfigFile = Path.Combine (ConfigFileRoot, @"cygconfig.h");
                    string winConfigFile = Path.Combine (ConfigFileRoot, @"winconfig.h");
                    string versionFile = Path.Combine (ConfigFileRoot, @"mono\mini\version.h");

                    Log.LogMessage (MessageImportance.High, "Setting up Mono configuration headers...");

                    BackupConfigFile (configFile, cygConfigFile);

                    var disableDefines = GetDisabledConfigFeatures (cygConfigFile);
                    var enableDefines = GetEnabledConfigFeatures (cygConfigFile, EnableDefines);
                    var haveDefines = (HaveDefines != null) ? HaveDefines.Split (';') : null;
                    CreateConfigUsingTemplate (winConfigFile, configFile, disableDefines, enableDefines, haveDefines, MonoVersion, MonoCorlibVersion);

                    CreateVersionFile (versionFile);

                    Log.LogMessage (MessageImportance.High, "Successfully setup Mono configuration headers {0} and {1} from {2}.", configFile, versionFile, winConfigFile);

                    return true;
                  }
                }
            ]]>
            </Code>
        </Task>
    </UsingTask>

    <Target Name="RunWinConfigSetup">
        <GetVersionsFromConfigureAC ConfigFileRoot="$(MSBuildThisFileDirectory)..\">
            <Output TaskParameter="MonoVersion" PropertyName="_MonoVersion" />
            <Output TaskParameter="MonoCorlibVersion" PropertyName="_MonoCorlibVersion" />
        </GetVersionsFromConfigureAC>

        <WinConfigSetup ConfigFileRoot="$(MSBuildThisFileDirectory)..\"
                        MonoVersion="$(_MonoVersion)"
                        MonoCorlibVersion="$(_MonoCorlibVersion)"
                        DisableDefines=""
                        EnableDefines=""
                        HaveDefines=""/>
    </Target>

</Project>