DDS  ver. 3.6
Environment.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 // This file contains a number of helpers to calculate execution time of a function.
4 //
5 #ifndef _DDS_ENVIRONMENT_H_
6 #define _DDS_ENVIRONMENT_H_
7 
8 // DDS
9 #include "BuildConstants.h"
10 #include "Logger.h"
11 // BOOST
12 #include <boost/algorithm/string.hpp>
13 #include <boost/filesystem.hpp>
14 
15 namespace dds::misc
16 {
17  namespace impl
18  {
19  inline std::string getEnv(const std::string& _key)
20  {
21  auto value{ std::getenv(_key.c_str()) };
22  return (value != nullptr) ? std::string(value) : std::string();
23  }
24 
25  inline void setupEnvVar(const std::string& _location,
26  const std::string& _varName,
27  const std::string& _dirName,
28  const std::string& _defaultDir)
29  {
30  namespace fs = boost::filesystem;
31  const fs::path locationPath(_location);
32  const std::string install{ (locationPath / fs::path(_dirName)).string() };
33  std::string path{ impl::getEnv(_varName) };
34  // Don't prepend the same path multiple times
35  if (path == install || boost::starts_with(path, install + std::string(":")))
36  {
37  LOG(info) << "DDS " << std::quoted(_dirName) << " directory (" << std::quoted(install)
38  << ") already prepends $" << _varName << " (" << std::quoted(path) << ")";
39  }
40  else
41  {
42  const std::string ddsInstall{ _location == kDDSInstallPrefix ? _defaultDir : install };
43  path = (path.empty()) ? ddsInstall : ddsInstall + std::string(":") + path;
44  setenv(_varName.c_str(), path.c_str(), 1);
45  LOG(info) << "Set $" << _varName << " to " << std::quoted(path);
46  }
47  }
48 
49  inline void setupLocale()
50  {
51  // Setup locale.
52  // Some Boost libraries throw an exception if the locale is not set.
53  // export LC_ALL=C; unset LANGUAGE
54  setenv("LC_ALL", "C", 1);
55  unsetenv("LANGUAGE");
56  }
57  } // namespace impl
58 
59  inline void setupEnv()
60  {
62 
63  std::string location{ impl::getEnv("DDS_LOCATION") };
64 
65  // Setup $DDS_LOCATION
66  if (location != kDDSInstallPrefix)
67  {
68  if (location.empty())
69  {
70  location = kDDSInstallPrefix;
71  setenv("DDS_LOCATION", location.c_str(), 1);
72  LOG(info) << "Set $DDS_LOCATION to " << std::quoted(location);
73  }
74  else
75  {
76  LOG(info) << "$DDS_LOCATION (" << std::quoted(location) << ") differs from the linked one ("
77  << std::quoted(kDDSInstallPrefix) << ")";
78  }
79  }
80 
81  // Setup $PATH
82  impl::setupEnvVar(location, "PATH", "bin", kDDSBinaryDir);
83 
84 #ifdef __APPLE__
85  const std::string ldVar = "DYLD_LIBRARY_PATH";
86 #else
87  const std::string ldVar = "LD_LIBRARY_PATH";
88 #endif
89  // Setup $(DY)LD_LIBRARY_PATH
90  impl::setupEnvVar(location, ldVar, "lib", kDDSLibraryDir);
91  }
92 } // namespace dds::misc
93 
94 #endif /*_DDS_ENVIRONMENT_H_*/
#define LOG(severity)
Definition: Logger.h:34
void setupEnv()
Definition: Environment.h:59
std::string getEnv(const std::string &_key)
Definition: Environment.h:19
void setupEnvVar(const std::string &_location, const std::string &_varName, const std::string &_dirName, const std::string &_defaultDir)
Definition: Environment.h:25
Definition: def.h:147
void setupLocale()
Definition: Environment.h:49
Definition: BoostHelper.h:14