NEURON
config.h
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #pragma once
9 
10 /**
11  * \dir
12  * \brief Global project configurations
13  *
14  * \file
15  * \brief Version information and units file path
16  */
17 
18 #include <cstdlib>
19 #include <filesystem>
20 #include <fstream>
21 #include <sstream>
22 #include <string>
23 #include <vector>
24 
25 #include "utils/common_utils.hpp"
26 
27 namespace nmodl {
28 namespace fs = std::filesystem;
29 
30 /**
31  * \brief Project version information
32  */
33 struct Version {
34  /// git revision id
35  static const std::string GIT_REVISION;
36 
37  /// project tagged version in the cmake
38  static const std::string NMODL_VERSION;
39 
40  /// return version string (version + git id) as a string
41  static std::string to_string() {
42  return NMODL_VERSION + " " + GIT_REVISION;
43  }
44 };
45 
46 /**
47  * \brief Information of units database i.e. `nrnunits.lib`
48  */
49 struct NrnUnitsLib {
50  /// paths where nrnunits.lib can be found
51  static std::vector<std::string> NRNUNITSLIB_PATH;
52 
53  static const std::string_view embedded_nrnunits;
54 
55  /**
56  * Return content of units database file
57  */
58  static std::string get_content(const std::string& path) {
59  if (!path.size()) {
60  return std::string(embedded_nrnunits);
61  }
62  auto f = std::ifstream(path);
63  auto size = std::filesystem::file_size(path);
64  auto content = std::string(size, '\0');
65  f.read(content.data(), size);
66  return content;
67  }
68 
69  /**
70  * Return path of units database file
71  */
72  static std::string get_path() {
73  // first look for NMODLHOME env variable
74  if (const char* nmodl_home = std::getenv("NMODLHOME")) {
75  auto path = std::string(nmodl_home) + "/share/nmodl/nrnunits.lib";
76  NRNUNITSLIB_PATH.emplace(NRNUNITSLIB_PATH.begin(), path);
77  }
78 
79  // check paths in order and return if found
80  for (const auto& path: NRNUNITSLIB_PATH) {
81  std::ifstream f(path.c_str());
82  if (f.good()) {
83  return path;
84  }
85  }
86  return "";
87  }
88 };
89 
90 struct CMakeInfo {
91  static const std::string SHARED_LIBRARY_SUFFIX;
92 };
93 
94 } // namespace nmodl
Common utility functions for file/dir manipulation.
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
static const std::string SHARED_LIBRARY_SUFFIX
Definition: config.h:91
Information of units database i.e.
Definition: config.h:49
static std::string get_path()
Return path of units database file.
Definition: config.h:72
static std::vector< std::string > NRNUNITSLIB_PATH
paths where nrnunits.lib can be found
Definition: config.h:51
static const std::string_view embedded_nrnunits
Definition: config.h:53
static std::string get_content(const std::string &path)
Return content of units database file.
Definition: config.h:58
Project version information.
Definition: config.h:33
static const std::string NMODL_VERSION
project tagged version in the cmake
Definition: config.h:38
static std::string to_string()
return version string (version + git id) as a string
Definition: config.h:41
static const std::string GIT_REVISION
git revision id
Definition: config.h:35