Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4FindDataDir.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26
27#include <mutex>
28
29#ifndef G4GMAKE
30#include "G4FindDataDir.hh"
31#include "G4Filesystem.hh"
32
33#include <cstdio>
34#include <cstdlib>
35#include <cstring>
36
37#if defined(_MSC_VER)
38#define setenv(name, value, overwrite) _putenv_s(name, value)
39#endif
40
41using namespace G4fs;
42
43static const char * const system_paths[] = {
44 GEANT4_INSTALL_FULL_DATADIR,
45 CMAKE_INSTALL_PREFIX,
46#if defined(_MSC_VER)
47 "C:\\Program Files",
48 "C:\\Geant4"
49#else
50 "/usr/local",
51 "/usr",
52 "/cvmfs/geant4.cern.ch"
53#endif
54};
55
56static const char * const data_paths[] = {
57 ".",
58 GEANT4_INSTALL_DATADIR,
59 CMAKE_INSTALL_DATADIR,
60 "share/Geant4/data",
61 "share/geant4/data",
62 "share/data",
63 "data"
64};
65
66static const char* G4GetDataDir(const char* name)
67{
68 for (const auto& d : dataset_definitions)
69 if (strcmp(name, d.env) == 0)
70 return d.dir;
71
72 return nullptr;
73}
74
75static const char* G4FindDataDir(const char* name, const path& prefix, const path& dataset)
76{
77 if (!is_directory(prefix))
78 return nullptr;
79
80 for (const auto data_path : data_paths) {
81 path datadir = prefix;
82 if (strcmp(data_path,".") == 0)
83 datadir /= dataset;
84 else
85 datadir /= path(data_path) / dataset;
86 if (is_directory(absolute(datadir)))
87 return setenv(name, absolute(datadir).string().c_str(), 0) == 0 ? std::getenv(name) : nullptr;
88 }
89
90 return nullptr;
91}
92
93const char* G4FindDataDir(const char* name)
94{
95#if defined(G4MULTITHREADED)
96 static std::mutex mutex;
97 std::lock_guard<std::mutex> lock(mutex);
98#endif
99
100 /* If environment variable is set for this dataset, use it */
101 if (const char *datadir = std::getenv(name))
102 return datadir;
103
104 /* If we know which directory/version to search for, try to find it */
105 if (const char *dataset = G4GetDataDir(name)) {
106 /* If GEANT4_DATA_DIR environment variable is set, use it and don't search further */
107 if (const char *basedir = std::getenv("GEANT4_DATA_DIR"))
108 return G4FindDataDir(name, basedir, dataset);
109
110 /* If GEANT4_DATA_DIR environment variable is not set, search in default system paths */
111 for (const auto prefix : system_paths)
112 if (const auto datadir = G4FindDataDir(name, prefix, dataset))
113 return datadir;
114 }
115
116 return nullptr;
117}
118
119#else
120// Support GNUmake builds
121#include <cstdlib>
122
123const char* G4FindDataDir(const char* name)
124{
125#if defined(G4MULTITHREADED)
126 static std::mutex mutex;
127 std::lock_guard<std::mutex> lock(mutex);
128#endif
129 /* If environment variable is set for this dataset, use it */
130 if (const char *datadir = std::getenv(name))
131 return datadir;
132
133 return nullptr;
134}
135#endif
const char * G4FindDataDir(const char *)