1 /**
2 Copyright 2018 Mark Fisher
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in 
6 the Software without restriction, including without limitation the rights to 
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
8 of the Software, and to permit persons to whom the Software is furnished to do 
9 so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all 
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
20 SOFTWARE.
21 **/
22 module dxx.sys.constants;
23 
24 private import std.compiler;
25 private import std.file;
26 private import std.path;
27 private import std..string : split;
28 
29 private import core.runtime;
30 private import core.cpuid;
31 private import std.array : appender,join;
32 
33 private import semver;
34 
35 private import dxx.packageVersion;
36 
37 class Constants {
38     //enum {
39     //    COMPILER_VERSION = std.compiler.name,
40     //    PALTFORM = _PLATFORM,
41     //    //CPU = core.cpuid.getCpuFeatures.vendorID
42     //}
43     enum compilerName =  __VENDOR__;
44     enum compilerVersionMajor = version_major;
45     enum compilerVersionMinor = version_minor;
46 
47     enum compileTimestamp = __TIMESTAMP__;
48 
49     enum libVersion = packageVersion;
50     enum libVersionRange = "~>"~(packageVersion[1..$].split("-")[0]);
51     enum libTimestamp = packageTimestamp;
52     enum libTimestampISO = packageTimestampISO;
53 
54     //enum cpuID = core.cpuid.getCpuFeatures.vendorID;
55     alias cpuCores = core.cpuid.coresPerCPU;
56     alias cpuThreads = core.cpuid.threadsPerCPU;
57 
58     version (OSX) { enum hostOperatingSystem = "OSX"; }
59     else version(MacOS) { enum hostOperatingSystem = "MacOS"; }
60     else version (linux) { enum hostOperatingSystem = "linux"; }
61     else version(Windows) { enum hostOperatingSystem = "Windows"; }
62     else version (FreeBSD) { enum hostOperatingSystem = "FreeBSD"; }
63     else version (NetBSD) { enum hostOperatingSystem = "NetBSD"; }
64     else version (DragonFlyBSD) { enum hostOperatingSystem = "DragonFlyBSD"; }
65     else version (Solaris) { enum hostOperatingSystem = "Solaris"; }
66     else version(Posix) { enum hostOperatingSystem = "Posix"; }
67     else { enum hostOperatingSystem = "<unknown-OS>"; }
68 
69     version(Windows) {
70         enum Windows = true;
71         enum Posix = false;
72     } else version(Posix) {
73         enum Windows = false;
74         enum Posix = true;
75     } else {
76         enum Windows = false;
77         enum Posix = false;
78     }
79 
80     version(unittest) { enum unitTest = true; }
81     else { enum unitTest = false; }
82 
83     debug {
84         enum buildType = "Debug";
85     } else version(Release) {
86         enum buildType = "Release";
87     } else {
88         enum buildType = "<unknown-buildtype>";
89     }
90 
91     version(DXX_Module) {
92         enum dxxModule = true;
93     } else {
94         enum dxxModule = false;
95     }
96 
97     //template libSemVer() {
98     //    auto libSemVer = SemVer(packageVersion);
99     //}
100 };
101 
102 struct RTConstants {
103     const(string) libVersion = Constants.libVersion;
104     const(string) libTimestamp = Constants.libTimestamp;
105     const(string) libTimestampISO = Constants.libTimestampISO;
106     const(string) libVersionRange = Constants.libVersionRange;
107     
108     const(string) compilerName = Constants.compilerName;
109     const(uint) compilerVersionMajor = Constants.compilerVersionMajor;
110     const(uint) compilerVersionMinor = Constants.compilerVersionMinor;
111 
112     const(string) compileTimestamp = Constants.compileTimestamp;
113 
114     const(string) hostOperatingSystem = Constants.hostOperatingSystem;
115     
116     const(bool) unitTest = Constants.unitTest;
117     const(string) buildType = Constants.buildType;
118 
119     const(bool) dxxModule = Constants.dxxModule;
120 
121     const(string) appFileName;
122     const(string) appDir;
123     const(string) curDir;
124     const(string) appBaseName;
125     const(string) argString;
126 
127     shared static this() {
128         runtimeConstants.appFileName = thisExePath;
129         runtimeConstants.appDir = dirName(runtimeConstants.appFileName);
130         runtimeConstants.curDir = getcwd;
131         runtimeConstants.argString = Runtime.args.join(" ");
132          
133         version(Windows) {
134             version(DXX_Module) {
135                 runtimeConstants.appBaseName = baseName(runtimeConstants.appFileName,".dll");
136             } else {
137                 runtimeConstants.appBaseName = baseName(runtimeConstants.appFileName,".exe");
138             }
139         } else {
140             runtimeConstants.appBaseName = baseName(runtimeConstants.appFileName);
141         }    
142     }
143 
144 
145     // the following variables may be filled in by the application...
146     
147     string userAppVersion; // user-defined app version string.
148     string appName; // user-defined app name.
149     string orgName; // user-defined org name.
150     
151     shared void registerAppVersion(vers)() {
152         userAppVersion = vers;
153     }
154 
155     shared void registerAppVars(T)() {
156         orgName = T.organizationName;
157         appName = T.applicationName;
158     }
159 
160 
161     static __gshared shared(RTConstants) runtimeConstants;
162 
163     alias constants = runtimeConstants;
164     
165     unittest {
166         assert(runtimeConstants.unitTest);
167         debug {
168             assert(runtimeConstants.buildType == "Debug");
169         } else version(Release) {
170             assert(runtimeConstants.buildType == "Release");
171         } else {
172             assert(runtimeConstants.buildType == "<unknown-buildtype>");
173         }
174         version(DXX_Module) {
175             assert(runtimeConstants.dxxModule);
176         } else {
177             assert(runtimeConstants.dxxModule == false);
178         }
179     }
180 
181     const shared inout ref
182     auto libVersions() {
183         auto r = SemVerRange(libVersionRange);
184         assert(r.isValid);
185         return r;
186     }
187     const shared inout ref 
188     auto semVer() {
189         return SemVer(libVersion);
190     }
191     const shared inout 
192     bool checkVersion(SemVer v) {
193         assert(v.isValid);
194         return v.satisfies(libVersions);
195     }
196 };
197 
198