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
39 inline const std::string OpenMS_OSNames[] = {"unknown", "MacOS", "Windows", "Linux"};
43 inline const std::string OpenMS_ArchNames[] = {"unknown", "32 bit", "64 bit"};
44
56 inline std::string getOSVersionString_()
57 {
58#if defined(_WIN32)
59 // Use RtlGetVersion to get the real version (not compatibility-shimmed)
60 typedef NTSTATUS (WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
61 HMODULE hMod = GetModuleHandleW(L"ntdll.dll");
62 if (hMod)
63 {
64 auto fxPtr = reinterpret_cast<RtlGetVersionPtr>(GetProcAddress(hMod, "RtlGetVersion"));
65 if (fxPtr)
66 {
67 RTL_OSVERSIONINFOW rovi{};
68 rovi.dwOSVersionInfoSize = sizeof(rovi);
69 if (fxPtr(&rovi) == 0)
70 {
71 return std::to_string(rovi.dwMajorVersion) + "." + std::to_string(rovi.dwMinorVersion)
72 + "." + std::to_string(rovi.dwBuildNumber);
73 }
74 }
75 }
76 return "unknown";
77#elif defined(__APPLE__)
78 char version[64] = {};
79 size_t len = sizeof(version);
80 if (sysctlbyname("kern.osproductversion", version, &len, nullptr, 0) == 0)
81 {
82 return std::string(version);
83 }
84 return "unknown";
85#elif defined(__unix__)
86 // Read VERSION_ID from /etc/os-release
87 std::ifstream ifs("/etc/os-release");
88 std::string line;
89 while (std::getline(ifs, line))
90 {
91 if (line.compare(0, 11, "VERSION_ID=") == 0)
92 {
93 std::string val = StringUtils::substr(line, 11);
94 // Strip surrounding quotes if present
95 if (val.size() >= 2 && val.front() == '"' && val.back() == '"')
96 {
97 val = StringUtils::substr(val, 1, val.size() - 2);
98 }
99 return val;
100 }
101 }
102 return "unknown";
103#else
104 return "unknown";
105#endif
106 }
107
122 class OPENMS_DLLAPI OpenMSOSInfo
123 {
125 std::string os_version_;
127
128 public:
131 os_(OpenMS_OS::OS_UNKNOWN),
132 os_version_("unknown"),
134 {}
135
137 std::string getOSAsString() const
138 {
139 return OpenMS_OSNames[static_cast<size_t>(os_)];
140 }
141
143 std::string getArchAsString() const
144 {
145 return OpenMS_ArchNames[static_cast<size_t>(arch_)];
146 }
147
149 std::string getOSVersionAsString() const
150 {
151 return os_version_;
152 }
153
160 static std::string getBinaryArchitecture()
161 {
162 size_t bytes = sizeof(size_t);
163 switch (bytes)
164 {
165 case 4:
166 return OpenMS_ArchNames[static_cast<size_t>(OpenMS_Architecture::ARCH_32BIT)];
167 case 8:
168 return OpenMS_ArchNames[static_cast<size_t>(OpenMS_Architecture::ARCH_64BIT)];
169 default:
170 return OpenMS_ArchNames[static_cast<size_t>(OpenMS_Architecture::ARCH_UNKNOWN)];
171 }
172 }
173
181 static std::string getActiveSIMDExtensions();
182
192 {
193 OpenMSOSInfo info;
194 #if defined(WIN32) // Windows
195 info.os_ = OpenMS_OS::OS_WINDOWS;
196 #elif (defined(__MACH__) && defined(__APPLE__)) // MacOS
197 info.os_ = OpenMS_OS::OS_MACOS;
198 #elif (defined(__unix__)) //Linux/FreeBSD TODO make a difference?
199 info.os_ = OpenMS_OS::OS_LINUX;
200 #endif // else stays unknown
201
202 // Get OS version using platform APIs
204
205 // identify architecture by pointer size
206 if (sizeof(void*) == 4)
207 {
208 info.arch_ = OpenMS_Architecture::ARCH_32BIT;
209 }
210 else
211 {
212 info.arch_ = OpenMS_Architecture::ARCH_64BIT;
213 }
214
215 return info;
216 }
217 };
218
229
230 {
231 public:
232
234 static bool isOpenMPEnabled()
235 {
236 #ifdef _OPENMP
237 return true;
238 #else
239 return false;
240 #endif
241 }
242
244 static std::string getBuildType()
245 {
246 return OPENMS_BUILD_TYPE;
247 }
248
257 {
258 #ifdef _OPENMP
259 return omp_get_max_threads();
260 #else
261 return 1;
262 #endif
263 }
270 static void setOpenMPNumThreads(Int num_threads)
271 {
272 #ifdef _OPENMP
273 omp_set_num_threads(num_threads);
274 #endif
275 (void)num_threads; // avoid 'unreferenced formal parameter' C4100 on Windows
276 }
277 };
278
279 } // NS Internal
280} // NS OpenMS
Snapshot of the runtime operating system: name, version, and pointer-width architecture.
Definition BuildInfo.h:123
std::string getArchAsString() const
Return the running architecture ("32 bit", "64 bit", or "unknown") as detected by getOSInfo from the ...
Definition BuildInfo.h:143
static std::string getActiveSIMDExtensions()
Return a comma-separated list of SIMD instruction sets compiled into this binary.
std::string getOSVersionAsString() const
Return the OS version captured by getOSInfo (e.g. "10.15" on macOS or "10.0.19045" on Windows); "unkn...
Definition BuildInfo.h:149
OpenMS_Architecture arch_
Definition BuildInfo.h:126
OpenMSOSInfo()
Construct an empty instance with all fields set to "unknown". Use getOSInfo to probe the runtime plat...
Definition BuildInfo.h:130
static std::string getBinaryArchitecture()
Return the pointer width of this binary, derived from sizeof(size_t).
Definition BuildInfo.h:160
static OpenMSOSInfo getOSInfo()
Probe the running platform and return a populated OpenMSOSInfo.
Definition BuildInfo.h:191
std::string os_version_
Definition BuildInfo.h:125
OpenMS_OS os_
Definition BuildInfo.h:124
std::string getOSAsString() const
Return the running operating system name ("Windows", "MacOS", "Linux", or "unknown").
Definition BuildInfo.h:137
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_()
Return the running OS version using a platform-specific probe.
Definition BuildInfo.h:56
const std::string OpenMS_ArchNames[]
String labels paired with OpenMS_Architecture; index by static_cast<size_t>(enum_value).
Definition BuildInfo.h:43
OpenMS_Architecture
Pointer-width architectures OpenMSOSInfo can report. SIZE_OF_OPENMS_ARCHITECTURE is a sentinel for ar...
Definition BuildInfo.h:41
OpenMS_OS
Operating systems OpenMSOSInfo can report. SIZE_OF_OPENMS_OS is a sentinel for array sizing.
Definition BuildInfo.h:37
const std::string OpenMS_OSNames[]
String labels paired with OpenMS_OS; index by static_cast<size_t>(enum_value).
Definition BuildInfo.h:39
std::string substr(const std::string &s, size_t pos=0, size_t n=std::string::npos)
Wrapper around std::string::substr; clamps pos to [0, size].
Definition StringUtils.h:525
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Build-configuration accessors: build type, OpenMP availability, and OpenMP thread count.
Definition BuildInfo.h:230
static Size getOpenMPMaxNumThreads()
Return the maximum number of OpenMP threads (including hyperthreads).
Definition BuildInfo.h:256
static void setOpenMPNumThreads(Int num_threads)
Override the OpenMP thread count for subsequent parallel regions.
Definition BuildInfo.h:270
static bool isOpenMPEnabled()
Return true when the library was compiled with OpenMP support (_OPENMP defined), false otherwise.
Definition BuildInfo.h:234
static std::string getBuildType()
Return the CMake build type baked into the library (e.g. "Release", "Debug"), captured from the OPENM...
Definition BuildInfo.h:244