-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathVersionIdEvaluation.cs
193 lines (163 loc) · 4.55 KB
/
VersionIdEvaluation.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace D_Parser.Misc
{
/// <summary>
/// Helper class for retrieving all predefined version identifiers depending on e.g.
/// the currently used OS, CPU-specific properties and further flags.
/// For details, see http://dlang.org/version.html, "Predefined Versions"
/// </summary>
public static class VersionIdEvaluation
{
static string[] minimalConfiguration;
// Stolen from http://stackoverflow.com/questions/10138040/how-to-detect-properly-windows-linux-mac-operating-systems
public enum Platform
{
None,
Unkown,
Windows,
Linux,
Mac
}
static Platform _os = Platform.None;
public static Platform OS
{
get
{
if (_os != Platform.None)
return _os;
switch (Environment.OSVersion.Platform)
{
case PlatformID.Unix:
// Well, there are chances MacOSX is reported as Unix instead of MacOSX.
// Instead of platform check, we'll do a feature checks (Mac specific root folders)
if (Directory.Exists("/Applications")
& Directory.Exists("/System")
& Directory.Exists("/Users")
& Directory.Exists("/Volumes"))
_os = Platform.Mac;
else
_os = Platform.Linux;
break;
case PlatformID.MacOSX:
_os = Platform.Mac;
break;
default:
_os = Platform.Windows;
break;
}
return _os;
}
}
public static string[] GetOSAndCPUVersions()
{
if(minimalConfiguration != null)
return minimalConfiguration;
var l = new List<string>();
l.Add("all");
l.Add ("assert");
// OS
bool is64BitOS = Environment.Is64BitOperatingSystem;
switch (OS)
{
case Platform.Windows:
l.Add("Windows");
l.Add(is64BitOS ? "Win64" : "Win32");
break;
case Platform.Linux:
l.Add ("Posix");
l.Add("linux");
break;
case Platform.Mac:
l.Add("OSX");
l.Add("darwin");
l.Add ("Posix");
break;
}
//TODO: Execute uname to retrieve further info of the Posix-OS
// http://www.computerhope.com/unix/uuname.htm
// CPU information
var cpuArch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
switch (cpuArch)
{
case "X86":
x86:
l.Add("D_InlineAsm_X86");
l.Add("X86");
break;
case "AMD64":
x64:
l.Add("D_InlineAsm_X86_64");
l.Add("X86_64");
break;
case "IA64":
l.Add("IA64");
break;
default:
if (string.IsNullOrWhiteSpace (cpuArch)) {
if (is64BitOS)
goto x64;
else
goto x86;
}
break;
}
//TODO: Other architectures...
if(BitConverter.IsLittleEndian)
l.Add("LittleEndian");
else
l.Add("BigEndian");
return minimalConfiguration = l.ToArray();
}
static readonly Regex versionRegex = new Regex ("version=(?<n>\\w+)", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
/// <summary>
/// See class description.
/// </summary>
/// <returns>
/// The version identifiers.
/// </returns>
/// <param name="compilerId">The compiler-specific version identifier which is e.g. DigitalMars for dmd1/dmd2</param>
/// <param name="finalCompilerCommandLine">
/// Used for extracting additional information like "-cov" that implies D_Coverage or "-m64" that
/// implies D
/// </param>
/// <param name="isD1">If false, D_Version2 will be defined</param>
public static string[] GetVersionIds(string compilerId,string finalCompilerCommandLine, bool unittests, bool isD1 = false)
{
var l = new List<string>();
l.AddRange(GetOSAndCPUVersions());
// Compiler id
if(!string.IsNullOrEmpty(compilerId))
l.Add(compilerId);
// D specific info
if(finalCompilerCommandLine.Contains("-cov"))
l.Add("D_Coverage");
if(finalCompilerCommandLine.Contains("-D"))
l.Add("D_Ddoc");
if (finalCompilerCommandLine.Contains("-m64"))
l.Add("D_LP64");
else
l.Add("D_X32");
// D_HardFloat, D_SoftFloat -- how to determine this?
l.Add("D_HardFloat");
//l.Add("D_SoftFloat");
if(finalCompilerCommandLine.Contains("-fPIC"))
l.Add("D_PIC");
l.Add("D_SIMD");
if(!isD1)
l.Add("D_Version2");
if(finalCompilerCommandLine.Contains("-noboundscheck"))
l.Add("D_NoBOundsChecks");
if(finalCompilerCommandLine.Contains("-unittest") || unittests)
l.Add("unittest");
foreach (Match m in versionRegex.Matches(finalCompilerCommandLine)) {
var ver = m.Groups ["n"].Value;
if (!string.IsNullOrEmpty (ver) && !l.Contains (ver))
l.Add (ver);
}
return l.ToArray();
}
}
}