OpenMS
Loading...
Searching...
No Matches
BuildInfo.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Julianus Pfeuffer $
6// $Authors: Julianus Pfeuffer $
7// --------------------------------------------------------------------------
8
9#pragma once
10
11#include <OpenMS/build_config.h>
13
14#include <cstddef>
15#include <string>
16#include <fstream>
17#include <cstring>
18
19#ifdef _WIN32
20 #include <windows.h>
21 #include <winternl.h>
22#elif defined(__APPLE__)
23 #include <sys/types.h>
24 #include <sys/sysctl.h>
25#endif
26
27#ifdef _OPENMP
28 #include "omp.h"
29#endif
30
31namespace OpenMS
32{
33 namespace Internal
34 {
35
37 inline const std::string OpenMS_OSNames[] = {"unknown", "MacOS", "Windows", "Linux"};
39 inline const std::string OpenMS_ArchNames[] = {"unknown", "32 bit", "64 bit"};
40
42 inline std::string getOSVersionString_()
43 {
44#if defined(_WIN32)
45 // Use RtlGetVersion to get the real version (not compatibility-shimmed)
46 typedef NTSTATUS (WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
47 HMODULE hMod = GetModuleHandleW(L"ntdll.dll");
48 if (hMod)
49 {
50 auto fxPtr = reinterpret_cast<RtlGetVersionPtr>(GetProcAddress(hMod, "RtlGetVersion"));
51 if (fxPtr)
52 {
53 RTL_OSVERSIONINFOW rovi{};
54 rovi.dwOSVersionInfoSize = sizeof(rovi);
55 if (fxPtr(&rovi) == 0)
56 {
57 return std::to_string(rovi.dwMajorVersion) + "." + std::to_string(rovi.dwMinorVersion)
58 + "." + std::to_string(rovi.dwBuildNumber);
59 }
60 }
61 }
62 return "unknown";
63#elif defined(__APPLE__)
64 char version[64] = {};
65 size_t len = sizeof(version);
66 if (sysctlbyname("kern.osproductversion", version, &len, nullptr, 0) == 0)
67 {
68 return std::string(version);
69 }
70 return "unknown";
71#elif defined(__unix__)
72 // Read VERSION_ID from /etc/os-release
73 std::ifstream ifs("/etc/os-release");
74 std::string line;
75 while (std::getline(ifs, line))
76 {
77 if (line.compare(0, 11, "VERSION_ID=") == 0)
78 {
79 std::string val = line.substr(11);
80 // Strip surrounding quotes if present
81 if (val.size() >= 2 && val.front() == '"' && val.back() == '"')
82 {
83 val = val.substr(1, val.size() - 2);
84 }
85 return val;
86 }
87 }
88 return "unknown";
89#else
90 return "unknown";
91#endif
92 }
93
94 class OPENMS_DLLAPI OpenMSOSInfo
95 {
99
100 public:
102 os_(OpenMS_OS::OS_UNKNOWN),
103 os_version_("unknown"),
105 {}
106
109 {
110 return OpenMS_OSNames[static_cast<size_t>(os_)];
111 }
112
115 {
116 return OpenMS_ArchNames[static_cast<size_t>(arch_)];
117 }
118
121 {
122 return os_version_;
123 }
124
127 {
128 size_t bytes = sizeof(size_t);
129 switch (bytes)
130 {
131 case 4:
132 return OpenMS_ArchNames[static_cast<size_t>(OpenMS_Architecture::ARCH_32BIT)];
133 case 8:
134 return OpenMS_ArchNames[static_cast<size_t>(OpenMS_Architecture::ARCH_64BIT)];
135 default:
136 return OpenMS_ArchNames[static_cast<size_t>(OpenMS_Architecture::ARCH_UNKNOWN)];
137 }
138 }
139
142
145 {
146 OpenMSOSInfo info;
147 #if defined(WIN32) // Windows
148 info.os_ = OpenMS_OS::OS_WINDOWS;
149 #elif (defined(__MACH__) && defined(__APPLE__)) // MacOS
150 info.os_ = OpenMS_OS::OS_MACOS;
151 #elif (defined(__unix__)) //Linux/FreeBSD TODO make a difference?
152 info.os_ = OpenMS_OS::OS_LINUX;
153 #endif // else stays unknown
154
155 // Get OS version using platform APIs
157
158 // identify architecture by pointer size
159 if (sizeof(void*) == 4)
160 {
161 info.arch_ = OpenMS_Architecture::ARCH_32BIT;
162 }
163 else
164 {
165 info.arch_ = OpenMS_Architecture::ARCH_64BIT;
166 }
167
168 return info;
169 }
170 };
171
174
175 {
176 public:
177
179 static bool isOpenMPEnabled()
180 {
181 #ifdef _OPENMP
182 return true;
183 #else
184 return false;
185 #endif
186 }
187
190 {
191 return OPENMS_BUILD_TYPE;
192 }
193
198 {
199 #ifdef _OPENMP
200 return omp_get_max_threads();
201 #else
202 return 1;
203 #endif
204 }
207 static void setOpenMPNumThreads(Int num_threads)
208 {
209 #ifdef _OPENMP
210 omp_set_num_threads(num_threads);
211 #endif
212 (void)num_threads; // avoid 'unreferenced formal parameter' C4100 on Windows
213 }
214 };
215
216 } // NS Internal
217} // NS OpenMS
Definition BuildInfo.h:95
static String getActiveSIMDExtensions()
Obtain a list of SIMD extensions which are currently in use (i.e. used by the compiler during optimiz...
static String getBinaryArchitecture()
Get Architecture of this binary (simply by looking at size of a pointer, i.e. size_t).
Definition BuildInfo.h:126
String os_version_
Definition BuildInfo.h:97
OpenMS_Architecture arch_
Definition BuildInfo.h:98
OpenMSOSInfo()
Definition BuildInfo.h:101
String getOSVersionAsString() const
Get the OS version (e.g. 10.15 for macOS or 10 for Windows)
Definition BuildInfo.h:120
static OpenMSOSInfo getOSInfo()
Constructs and returns an OpenMSOSInfo object.
Definition BuildInfo.h:144
OpenMS_OS os_
Definition BuildInfo.h:96
String getArchAsString() const
Get the current architecture (32-bit or 64-bit)
Definition BuildInfo.h:114
String getOSAsString() const
Get the current operating system (Windows, MacOS, Linux)
Definition BuildInfo.h:108
A more convenient string class.
Definition String.h:34
int Int
Signed integer type.
Definition Types.h:72
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
std::string getOSVersionString_()
Helper: get OS version string using platform APIs.
Definition BuildInfo.h:42
const std::string OpenMS_ArchNames[]
Definition BuildInfo.h:39
OpenMS_Architecture
Definition BuildInfo.h:38
OpenMS_OS
Definition BuildInfo.h:36
const std::string OpenMS_OSNames[]
Definition BuildInfo.h:37
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Struct with some static methods to get informations on the build configuration.
Definition BuildInfo.h:175
static String getBuildType()
Get the build type used during building the OpenMS library.
Definition BuildInfo.h:189
static Size getOpenMPMaxNumThreads()
Get the maximum number of threads that OpenMP will use (including hyperthreads) Note: This could also...
Definition BuildInfo.h:197
static void setOpenMPNumThreads(Int num_threads)
Set the number of threads that OpenMP will use (including hyperthreads) Note: Can be initialized by t...
Definition BuildInfo.h:207
static bool isOpenMPEnabled()
Checks if OpenMP was enabled during build, based on the _OPENMP macro.
Definition BuildInfo.h:179