Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
FoundationUtils.cxx
Go to the documentation of this file.
1/// \file FoundationUtils.cxx
2///
3/// \brief The file contains utilities which are foundational and could be used
4/// across the core component of ROOT.
5///
6///
7/// \author Vassil Vassilev <vvasilev@cern.ch>
8///
9/// \date June, 2019
10///
11/*************************************************************************
12 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
13 * All rights reserved. *
14 * *
15 * For the licensing terms see $ROOTSYS/LICENSE. *
16 * For the list of contributors see $ROOTSYS/README/CREDITS. *
17 *************************************************************************/
18
20
21#include <RConfigure.h>
22
23#include <algorithm>
24#include <cassert>
25
26#include <errno.h>
27#include <string.h>
28
29#ifdef _WIN32
30#include <direct.h>
31#include <Windows4Root.h>
32#else
33#include <unistd.h>
34#endif // _WIN32
35
36#ifdef __FreeBSD__
37#include <sys/syslimits.h>
38#include <sys/param.h>
39#include <sys/user.h>
40#include <sys/types.h>
41#include <libutil.h>
42#include <libprocstat.h>
43#endif // __FreeBSD__
44
45namespace ROOT {
46namespace FoundationUtils {
47std::string GetCurrentDir()
48{
49 char fixedLength[1024];
50 char *currWorkDir = fixedLength;
51 size_t len = 1024;
52 char *result = currWorkDir;
53
54 do {
55 if (!result) {
56 len = 2 * len;
57 if (fixedLength != currWorkDir) {
58 delete[] currWorkDir;
59 }
60 currWorkDir = new char[len];
61 }
62#ifdef WIN32
63 result = ::_getcwd(currWorkDir, len);
64#else
65 result = getcwd(currWorkDir, len);
66#endif
67 } while (!result && errno == ERANGE);
68
69 std::string output = currWorkDir;
70 output += '/';
71#ifdef WIN32
72 // convert backslashes into forward slashes
73 std::replace(output.begin(), output.end(), '\\', '/');
74#endif
75
76 if (fixedLength != currWorkDir) {
77 delete[] currWorkDir;
78 }
79 return output;
80}
81
82std::string MakePathRelative(const std::string &path, const std::string &base, bool isBuildingROOT /* = false*/)
83{
84 std::string result(path);
85
86 const char *currWorkDir = base.c_str();
87 size_t lenCurrWorkDir = strlen(currWorkDir);
88 if (result.substr(0, lenCurrWorkDir) == currWorkDir) {
89 // Convert to path relative to $PWD.
90 // If that's not what the caller wants, she should pass -I to rootcling and a
91 // different relative path to the header files.
92 result.erase(0, lenCurrWorkDir);
93 }
94 // FIXME: This is not a generic approach for an interface. We should rework
95 // this part.
96 if (isBuildingROOT) {
97 // For ROOT, convert module directories like core/base/inc/ to include/
98 int posInc = result.find("/inc/");
99 if (posInc != -1) {
100 result = /*std::string("include") +*/ result.substr(posInc + 5);
101 }
102 }
103 return result;
104}
105
106/// Transforms a file path by replacing its backslashes with slashes.
107void ConvertToUnixPath(std::string& Path) {
108 std::replace(Path.begin(), Path.end(), '\\', '/');
109}
110
111const std::string& GetFallbackRootSys() {
112 static std::string fallback;
113 if (!fallback.empty())
114 return fallback;
115
116#if defined(WIN32) || defined(__FreeBSD__)
117 auto parent_path = [](std::string path) {
118 return path.substr(0, path.find_last_of("/\\"));
119 };
120#endif
121
122#ifdef WIN32
123 static char lpFilename[_MAX_PATH];
124 if (::GetModuleFileNameA(
125 NULL, // handle to module to find filename for
126 lpFilename, // pointer to buffer to receive module path
127 sizeof(lpFilename))) { // size of buffer, in characters
128 fallback = parent_path(parent_path(lpFilename));
129 }
130#elif defined __FreeBSD__
131 procstat* ps = procstat_open_sysctl(); //
132 kinfo_proc* kp = kinfo_getproc(getpid());
133
134 char lpFilename[PATH_MAX] = "";
135 if (kp!=NULL) {
136 procstat_getpathname(ps, kp, lpFilename, sizeof(lpFilename));
137 fallback = parent_path(parent_path({lpFilename}));
138 }
139
140 free(kp);
141 procstat_close(ps);
142#else
143 // FIXME: We should not hardcode this path. We can use a similar to the
144 // windows technique to get the path to the executable. The easiest way
145 // to do this is to depend on LLVMSupport and use getMainExecutable.
146 fallback = "/usr/local/root";
147#endif
148 return fallback;
149}
150
151#ifdef ROOTPREFIX
152static bool IgnorePrefix() {
153 static bool ignorePrefix = ::getenv("ROOTIGNOREPREFIX");
154 return ignorePrefix;
155}
156#endif
157
158const std::string& GetRootSys() {
159#ifdef ROOTPREFIX
160 if (!IgnorePrefix()) {
161 const static std::string rootsys = ROOTPREFIX;
162 return rootsys;
163 }
164#endif
165 static std::string rootsys;
166 if (rootsys.empty()) {
167 if (const char* envValue = ::getenv("ROOTSYS")) {
168 rootsys = envValue;
169 // We cannot use gSystem->UnixPathName.
170 ConvertToUnixPath(rootsys);
171 }
172 }
173 // FIXME: Should this also call UnixPathName for consistency?
174 if (rootsys.empty())
175 rootsys = GetFallbackRootSys();
176 return rootsys;
177}
178
179
180const std::string& GetIncludeDir() {
181#ifdef ROOTINCDIR
182 if (!IgnorePrefix()) {
183 const static std::string rootincdir = ROOTINCDIR;
184 return rootincdir;
185 }
186#endif
187 static std::string rootincdir;
188 if (rootincdir.empty()) {
189 const std::string& sep = GetPathSeparator();
190 rootincdir = GetRootSys() + sep + "include" + sep;
191 }
192 return rootincdir;
193}
194
195const std::string& GetEtcDir() {
196#ifdef ROOTETCDIR
197 if (!IgnorePrefix()) {
198 const static std::string rootetcdir = ROOTETCDIR;
199 return rootetcdir;
200 }
201#endif
202
203 const static std::string rootetcdir =
205 return rootetcdir;
206}
207
208static std::string str_tolower(std::string s) {
209 std::transform(s.begin(), s.end(), s.begin(),
210 [](unsigned char c){ return std::tolower(c); });
211 return s;
212}
213
214bool CanConvertEnvValueToBool(const std::string& value) {
215 std::string lowercase = str_tolower(value);
216 if (lowercase == "1" || lowercase == "on" || lowercase == "true")
217 return true;
218 if (lowercase == "0" || lowercase == "off" || lowercase == "false")
219 return true;
220
221 return false;
222}
223
224bool ConvertEnvValueToBool(const std::string& value) {
226 std::string lowercase = str_tolower(value);
227 if (lowercase == "1" || lowercase == "on" || lowercase == "true")
228 return true;
229 if (lowercase == "0" || lowercase == "off" || lowercase == "false")
230 return false;
231 // FIXME: Implement a wrapper around __builtin_unreachable() and use it here
232 return false;
233}
234
235} // namespace FoundationUtils
236} // namespace ROOT
The file contains utilities which are foundational and could be used across the core component of ROO...
#define c(i)
Definition RSha256.hxx:101
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
#define free
Definition civetweb.c:1539
static int lowercase(const char *s)
Definition civetweb.c:2977
const std::string & GetPathSeparator()
const std::string & GetIncludeDir()
\ returns the include directory in the installation.
static std::string str_tolower(std::string s)
void ConvertToUnixPath(std::string &Path)
Transforms a file path by replacing its backslashes with slashes.
bool CanConvertEnvValueToBool(const std::string &value)
const std::string & GetRootSys()
std::string MakePathRelative(const std::string &path, const std::string &base, bool isBuildingROOT=false)
bool ConvertEnvValueToBool(const std::string &value)
const std::string & GetFallbackRootSys()
const std::string & GetEtcDir()
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
static void output()