Clean up cmake build host detection
[gromacs.git] / src / gromacs / utility / programcontext.h
blob9ac17299364133d5bf51d7af91460b89f1eb88c3
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source 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.
35 /*! \file
36 * \brief
37 * Declares gmx::IProgramContext and related methods.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \inpublicapi
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_PROGRAMCONTEXT_H
44 #define GMX_UTILITY_PROGRAMCONTEXT_H
46 namespace gmx
49 //! \addtogroup module_utility
50 //! \{
52 /*! \brief
53 * Provides information about installation prefix (see
54 * IProgramContext::installationPrefix()).
56 * \inpublicapi
58 struct InstallationPrefixInfo
60 //! Initializes the structure with given values.
61 InstallationPrefixInfo(const char *path, bool bSource)
62 : path(path), bSourceLayout(bSource)
66 /*! \brief
67 * Path to the installation prefix of the current \Gromacs instance.
69 * If this is `NULL` or empty, data files cannot be looked up from the
70 * install tree and \Gromacs functions that access such files may fail.
71 * This can also contain a path to the source tree (see \a bSourceLayout).
73 const char *const path;
74 /*! \brief
75 * Whether \a path points to a source tree -like layout.
77 * For testing, it is useful to read data files from the source tree.
78 * For such cases, the program context can return the source tree root path
79 * in \a path, and set this to `true` to indicate that the data files
80 * should be searched using the layout of the source tree instead of the
81 * installation.
83 const bool bSourceLayout;
87 /*! \brief
88 * Provides context information about the program that is calling the library.
90 * This class provides access to information about the program that is
91 * currently running. This information is used to provide better information
92 * to the user, and to locate the \Gromacs data files.
94 * setProgramContext() should be called before any other \Gromacs calls in
95 * a program (except for gmx::init()). This avoids thread safety issues, and
96 * allows nicely formatted error messages.
98 * For thread safety, the implementations of the interface should ensure that
99 * the returned strings remain valid until the end of the program (see
100 * getProgramContext() for discussion on deinitialization). Callers of the
101 * interface within \Gromacs are prepared for exceptions, but generally
102 * terminate the program on any exception. Implementations should avoid
103 * exception except for truly fatal conditions.
105 * The destructor is protected to signify that the context is never destroyed
106 * through the interface.
108 * \see getProgramContext()
109 * \see setProgramContext()
110 * \inpublicapi
112 class IProgramContext
114 public:
115 /*! \brief
116 * Returns the name of the binary as it was invoked without any path.
118 * This is typically `argv[0]` with any leading directory stripped.
119 * Currently, this should be a valid file name.
121 virtual const char *programName() const = 0;
122 /*! \brief
123 * Returns a display name for the program.
125 * For simple programs, this can equal programName(). For the \Gromacs
126 * `gmx` wrapper binary, this includes the name of the module (e.g.,
127 * `gmx angle`). This is used only for informational purposes, and
128 * there are no constraints on contents, except that it should not be
129 * `NULL`.
131 virtual const char *displayName() const = 0;
132 /*! \brief
133 * Returns the full path of the running binary.
135 * This is mainly used for informational purposes. There are no
136 * constraints on contents, except that it should not be `NULL`.
137 * Currently, this is also used for sanity checks in checkpointing.
139 * The implementation can provide an empty string if the path to the
140 * binary is not available. In such a case, the information is not
141 * shown.
143 virtual const char *fullBinaryPath() const = 0;
144 /*! \brief
145 * Returns the installation prefix for \Gromacs.
147 * This path is used to locate the data files that are in `share/top/`
148 * in the source directory.
149 * The implementation can provide an empty string if the path is not
150 * available; in such a case, functions that require data files may
151 * fail.
153 * The returned structure also contains a flag to indicate whether the
154 * prefix actually points to the source tree. This is used for tests
155 * and to support running binaries directly from the build tree.
157 virtual InstallationPrefixInfo installationPrefix() const = 0;
158 /*! \brief
159 * Returns the full command line used to invoke the binary.
161 * The implementation can provide an empty string if no command line is
162 * available.
164 virtual const char *commandLine() const = 0;
166 protected:
167 virtual ~IProgramContext() {}
170 /*! \brief
171 * Returns the global IProgramContext instance.
173 * \returns The context set with setProgramContext().
175 * If nothing has been set with setProgramContext(), returns a default
176 * implementation that returns `"GROMACS"` for the program and display names,
177 * and empty strings for other values.
178 * The default implementation never throws.
180 * Does not throw.
182 * See setProgramContext() for thread safety notes. You should not call this
183 * method in global deinitialization methods (e.g., destructors of global
184 * variables), since it is very difficult to clean up the state correctly in
185 * the presence of such calls. For example, initForCommandLine() assumes that
186 * such calls do not exist to be able to free the context before exiting.
188 * \see IProgramContext
190 const IProgramContext &getProgramContext();
191 /*! \brief
192 * Sets the global IProgramContext instance.
194 * \param[in] context Program context to set
195 * (can be NULL to restore the default context).
197 * The library does not take ownership of \p context.
198 * The provided object must remain valid until the global instance is changed
199 * by another call to setProgramContext().
201 * This method is not thread-safe. It must be the first call to the library
202 * after gmx::init(), and multi-threaded access is only supported after the
203 * call completes. If \Gromacs is getting called from multiple threads, or
204 * uses multiple threads simultaneously, changing the program context is not
205 * supported once it is set.
206 * If the context is cleared at the end of the program, the caller must ensure
207 * that all other threads have been terminated at this point.
208 * These constraints simplify the implementation significantly.
210 * Does not throw.
212 * \see IProgramContext
214 void setProgramContext(const IProgramContext *context);
216 //! \}
218 } // namespace gmx
220 #endif