2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Declares gmx::ProgramInfo.
39 * This header is installed to support init.h because some compilers don't
40 * allow returning a reference to an incomplete type from a function.
41 * It should not be necessary to use gmx::ProgramInfo outside the Gromacs
44 * \author Teemu Murtola <teemu.murtola@gmail.com>
46 * \ingroup module_utility
48 #ifndef GMX_UTILITY_PROGRAMINFO_H
49 #define GMX_UTILITY_PROGRAMINFO_H
55 #include "uniqueptr.h"
60 //! \addtogroup module_utility
63 /*! \libinternal \brief
64 * Allows customization of the way various directories are found by
67 * For the ProgramInfo constructors that do not take this interface as a
68 * parameter, a default implementation is used that forwards the calls to the
69 * corresponding methods in gmx::Path.
73 class ExecutableEnvironmentInterface
76 virtual ~ExecutableEnvironmentInterface() {}
79 * Returns the working directory when the program was launched.
81 virtual std::string getWorkingDirectory() const = 0;
83 * Returns list of paths where executables are searched for.
85 * The returned list should be in priority order. An empty string in
86 * the returned list corresponds to getWorkindDirectory().
88 virtual std::vector<std::string> getExecutablePaths() const = 0;
91 //! Shorthand for a smart pointer to ExecutableEnvironmentInterface.
92 typedef gmx_unique_ptr<ExecutableEnvironmentInterface>::type
93 ExecutableEnvironmentPointer;
95 /*! \libinternal \brief
96 * Helper class for managing information about the running binary.
98 * This class provides access to the name of the binary currently running, as
99 * well as other information derived from it.
101 * ProgramInfo::init() should be called before any other (C++) Gromacs calls in
102 * a command-line program, as the information is used for printing error
105 * Constructors are provided mostly for unit testing purposes; in normal usage,
106 * a single ProgramInfo object is constructed with init() in the beginning of
107 * the program. The returned object can be explicitly passed to other methods,
108 * or accessed through getInstance().
110 * Unless explicitly noted otherwise, methods in this class may throw
111 * std::bad_alloc on out-of-memory conditions, but do not throw other
120 * Returns the singleton ProgramInfo object.
122 * \returns The same object as initialized with the last call to init().
123 * \throws std::bad_alloc if out of memory (only if this is the first
124 * call and init() has not been called either).
125 * \throws tMPI::system_error on thread synchronization errors.
127 static const ProgramInfo &getInstance();
129 * Initializes global program information.
131 * \param[in] argc argc value passed to main().
132 * \param[in] argv argv array passed to main().
133 * \returns Reference to initialized program information object.
135 * The return value of realBinaryName() is the same as
136 * invariantProgramName().
138 * Does not throw. Terminates the program on out-of-memory error.
140 static ProgramInfo &init(int argc, const char *const argv[]);
142 * Initializes global program information with explicit binary name.
144 * \param[in] realBinaryName Name of the binary
145 * (without Gromacs binary suffix or .exe on Windows).
146 * \param[in] argc argc value passed to main().
147 * \param[in] argv argv array passed to main().
148 * \returns Reference to initialized program information object.
150 * This overload is provided for cases where the program may be invoked
151 * through a symlink, and it is necessary to know the real name of the
154 * Does not throw. Terminates the program on out-of-memory error.
156 static ProgramInfo &init(const char *realBinaryName,
157 int argc, const char *const argv[]);
160 * Constructs an empty program info objects.
162 * All methods in the constructed object return dummy values.
166 * Initializes a program information object with binary name only.
168 * \param[in] realBinaryName Name of the binary
169 * (without Gromacs binary suffix or .exe on Windows).
171 * This is needed for unit testing purposes.
172 * The constructed object works as if the command line consisted of
173 * only of the binary name.
175 explicit ProgramInfo(const char *realBinaryName);
177 * Initializes a program information object based on command line.
179 * \param[in] argc argc value passed to main().
180 * \param[in] argv argv array passed to main().
182 ProgramInfo(int argc, const char *const argv[]);
184 * Initializes a program information object based on binary name and
187 * \param[in] realBinaryName Name of the binary
188 * (without Gromacs binary suffix or .exe on Windows).
189 * \param[in] argc argc value passed to main().
190 * \param[in] argv argv array passed to main().
192 ProgramInfo(const char *realBinaryName,
193 int argc, const char *const argv[]);
195 * Initializes a program information object based on binary name and
198 * \param[in] realBinaryName Name of the binary
199 * (without Gromacs binary suffix or .exe on Windows).
200 * \param[in] argc argc value passed to main().
201 * \param[in] argv argv array passed to main().
202 * \param[in] env Customizes the way the binary name is handled.
204 * This overload allows one to customize the way the binary is located
205 * by providing a custom ExecutableEnvironmentInterface implementation.
206 * This is mainly useful for testing purposes to make it possible to
207 * test different paths without setting environment variables, changing
208 * the working directory or doing other process-wide operations.
209 * It may also be useful for making Gromacs behave better when linked
210 * into a non-Gromacs executable (with possible extensions in
211 * ExecutableEnvironmentInterface).
213 ProgramInfo(const char *realBinaryName,
214 int argc, const char *const argv[],
215 ExecutableEnvironmentPointer env);
219 * Sets a display name for the binary.
221 * \throws std::bad_alloc if out of memory.
222 * \throws tMPI::system_error on thread synchronization errors.
224 * This is used with the wrapper binary to add the name of the invoked
225 * module to the name of the binary shown.
227 void setDisplayName(const std::string &name);
230 * Returns the real name of the binary.
232 * The returned value is comparable to invariantProgramName(), i.e., it
233 * has suffixes and OS-specific extensions removed.
237 const std::string &realBinaryName() const;
239 * Returns the name of the binary as it was invoked without any path.
243 const std::string &programName() const;
245 * Returns an invariant name of the binary.
247 * The returned value has OS-specific extensions (.exe on Windows)
248 * removed, as well as any binary suffix that was configured.
252 const std::string &invariantProgramName() const;
254 * Returns a display name of the current module.
256 * \throws tMPI::system_error on thread synchronization errors.
258 * The returned value equals programName(), unless a separate display
259 * name has been set with setDisplayName().
261 const std::string &displayName() const;
263 * Returns the full command line used to invoke the binary.
267 const std::string &commandLine() const;
270 * Returns the full path of the invoked binary.
272 * Returns argv[0] if there was an error in finding the absolute path.
276 const std::string &fullBinaryPath() const;
281 PrivateImplPointer<Impl> impl_;