basis.h
Go to the documentation of this file.
1 // ============================================================================
2 // Copyright (c) 2011-2012 University of Pennsylvania
3 // Copyright (c) 2013-2016 Andreas Schuh
4 // All rights reserved.
5 //
6 // See COPYING file for license information or visit
7 // https://cmake-basis.github.io/download.html#license
8 // ============================================================================
9 
10 /**
11  * @file basis.h
12  * @brief BASIS utilities of BASIS project of BASIS package.
13  *
14  * @note The basis.h module was automatically generated by BASIS from the
15  * template file basis.h.in which is part of the BASIS installation.
16  *
17  * This header file includes the header files of the remaining BASIS utilities.
18  * Hence, it is sufficient to only include this header file.
19  *
20  * @ingroup BasisCxxUtilities
21  */
22 
23 #pragma once
24 #ifndef _BASIS_BASIS_BASIS_H
25 #define _BASIS_BASIS_BASIS_H
26 
27 #include <basis/utilities.h> // project-independent utilities
28 
29 
30 /// @addtogroup BasisCxxUtilities
31 /// @{
32 
33 
34 namespace basis {
35 
36 
37 // ===========================================================================
38 // constants
39 // ===========================================================================
40 
41 /// @brief The project name.
42 extern const char* PROJECT;
43 /// @brief The version string given as "<major>.<minor>.<patch>".
44 extern const char* VERSION;
45 /// @brief The major version number.
46 extern const unsigned int VERSION_MAJOR;
47 /// @brief The minor version number.
48 extern const unsigned int VERSION_MINOR;
49 /// @brief The patch number.
50 extern const unsigned int VERSION_PATCH;
51 /// @brief Complete version information as output by --version option.
52 extern const char* RELEASE;
53 /// @brief Default copyright of executables.
54 extern const char* COPYRIGHT;
55 /// @brief Default license of executables.
56 extern const char* LICENSE;
57 /// @brief Default contact to use for help output of executables.
58 extern const char* CONTACT;
59 
60 // ===========================================================================
61 // package directories
62 // ===========================================================================
63 
64 /**
65  * @brief Get absolute path to directory containing runtime executables.
66  *
67  * @returns Absolute path to directory containing runtime executables.
68  */
69 std::string bindir();
70 
71 /**
72  * @brief Get absolute path to directory containing auxiliary executables.
73  *
74  * @returns Absolute path to directory containing auxiliary executables.
75  */
76 std::string libexecdir();
77 
78 /**
79  * @brief Get absolute path to directory containing libraries.
80  *
81  * @returns Absolute path to directory containing libraries.
82  */
83 std::string libdir();
84 
85 /**
86  * @brief Get absolute path to directory containing auxiliary data.
87  *
88  * @returns Absolute path to directory containing auxiliary data.
89  */
90 std::string datadir();
91 
92 // ===========================================================================
93 // executable information
94 // ===========================================================================
95 
96 /**
97  * @brief Print contact information.
98  *
99  * @param [in] contact Name of contact. If @c NULL, CONTACT is used.
100  */
101 void print_contact(const char* contact = NULL);
102 
103 /**
104  * @brief Print version information including copyright and license notices.
105  *
106  * @param [in] name Name of executable. Should not be set programmatically
107  * to the first argument of the @c main() function, but
108  * a string literal instead.
109  * @param [in] version Version of executable, e.g., release of project
110  * this executable belongs to. Defaults to RELEASE.
111  * @param [in] project Name of project this executable belongs to.
112  * If @c NULL, defaults to PROJECT. If an empty string,
113  * no project information is printed.
114  * @param [in] copyright The copyright notice, excluding the common prefix
115  * "Copyright (c) " and suffix ". All rights reserved.".
116  * If @c NULL, COPYRIGHT is used. If an empty string,
117  * no copyright notice is printed.
118  * @param [in] license Information regarding licensing. If @c NULL, LICENSE
119  * is used. If an empty string, no license information
120  * is printed.
121  */
122 void print_version(const char* name,
123  const char* version = NULL,
124  const char* project = NULL,
125  const char* copyright = NULL,
126  const char* license = NULL);
127 
128 /**
129  * @brief Get UID of build target.
130  *
131  * The UID of a build target is its name prepended by a namespace identifier
132  * which should be unique for each project.
133  *
134  * @param [in] name Name of build target.
135  *
136  * @returns UID of named build target.
137  */
138 std::string targetuid(const std::string& name);
139 
140 /**
141  * @brief Determine whether a given build target is known.
142  *
143  * @param [in] name Name of build target.
144  *
145  * @returns Whether the named target is a known executable target.
146  */
147 bool istarget(const std::string& name);
148 
149 /**
150  * @brief Get absolute path of executable file.
151  *
152  * This function determines the absolute file path of an executable. If no
153  * arguments are given, the absolute path of this executable is returned.
154  * If the command names a known executable build target, the absolute path to
155  * the corresonding built (and installed) executable file is returned.
156  * Otherwise, the named command is searched in the system @c PATH and its
157  * absolute path returned if found. If the executable is not found, an
158  * empty string is returned.
159  *
160  * @todo This function currently makes use of the which command implemented
161  * in Python and called as subprocess in order to search a command
162  * in the system @c PATH. This which command is part of BASIS and
163  * can also be used on Windows. However, a native C++ implementation
164  * would be desireable.
165  *
166  * @param [in] name Name of command or @c NULL.
167  *
168  * @returns Absolute path of executable or an empty string if not found.
169  * If @p name is @c NULL, the path of this executable is returned.
170  *
171  * @sa exename()
172  * @sa exedir()
173  */
174 std::string exepath(const std::string& name = std::string());
175 
176 /**
177  * @brief Get name of executable file.
178  *
179  * @note The name of the executable may or may not include the file name
180  * extension depending on the executable type and operating system.
181  * Hence, this function is neither an equivalent to
182  * os::path::basename(exepath()) nor os::path::filename(exepath()).
183  * In particular, on Windows, the .exe and .com extension is not
184  * included in the returned executable name.
185  *
186  * @param [in] name Name of command or @c NULL.
187  *
188  * @returns Name of executable file or an empty string if not found.
189  * If @p name is @c NULL, the name of this executable is returned.
190  *
191  * @sa exepath()
192  */
193 std::string exename(const std::string& name = std::string());
194 
195 /**
196  * @brief Get directory of executable file.
197  *
198  * @param [in] name Name of command or @c NULL.
199  *
200  * @returns Absolute path to directory containing executable or an empty string if not found.
201  * If @p name is @c NULL, the directory of this executable is returned.
202  *
203  * @sa exepath()
204  */
205 std::string exedir(const std::string& name = std::string());
206 
207 // ===========================================================================
208 // command execution
209 // ===========================================================================
210 
211 /**
212  * @brief Execute command as subprocess.
213  *
214  * This function is a replacement for system() on Unix and is furthermore
215  * less platform dependent. The first argument of the given command-line string
216  * is mapped to an absolute executable file using exepath() if the given first
217  * argument is a know build target name. Otherwise, the command-line is used
218  * unmodified.
219  *
220  * @param [in] cmd Command-line given as double quoted string. Arguments
221  * containing whitespaces have to be quoted using double
222  * quotes. Use a backslash (\\) to escape double quotes
223  * inside an argument as well as to escape a backslash
224  * itself (required if backslash at end of double quoted
225  * argument, e.g., "this argument \\").
226  * @param [in] quiet Turns off output of stdout of child process to stdout
227  * of parent process.
228  * @param [out] out Output stream where command output is written to.
229  * @param [in] allow_fail If true, no exception is thrown if the exit code
230  * of the child process is non-zero. Otherwise,
231  * a SubprocessException object is thrown in that case.
232  * @param [in] verbose Verbosity of output messages. Does not affect
233  * verbosity of executed command.
234  * @param [in] simulate Whether to simulate command execution only.
235  *
236  * @returns Exit code of command or -1 if subprocess creation failed.
237  *
238  * @throws SubprocessError If subprocess creation failed or command returned
239  * a non-zero exit code while @p allow_fail is false.
240  */
241 int execute(const std::string& cmd,
242  bool quiet = false,
243  // attention: stdout is a macro defined by windows.h
244  std::ostream* out = NULL,
245  bool allow_fail = false,
246  int verbose = 0,
247  bool simulate = false);
248 
249 /**
250  * @brief Execute command as subprocess.
251  *
252  * This function is a replacement for system() on Unix and is furthermore
253  * less platform dependent. The first argument of the given command-line string
254  * is mapped to an absolute executable file using exepath() if the given first
255  * argument is a know build target name. Otherwise, the command-line is used
256  * unmodified.
257  *
258  * @param [in,out] args Command-line given as argument vector. The first
259  * argument has to be either a build target name or the
260  * name/path of the command to execute.
261  * @param [in] quiet Turns off output of stdout of child process to
262  * stdout of parent process.
263  * @param [out] out Output stream where command output is written to.
264  * @param [in] allow_fail If true, no exception is thrown if the exit code
265  * of the child process is non-zero. Otherwise,
266  * a SubprocessException object is thrown in that case.
267  * @param [in] verbose Verbosity of output messages. Does not affect
268  * verbosity of executed command.
269  * @param [in] simulate Whether to simulate command execution only.
270  *
271  * @returns Exit code of command or -1 if subprocess creation failed.
272  *
273  * @throws SubprocessError If subprocess creation failed or command returned
274  * a non-zero exit code while @p allow_fail is false.
275  */
276 int execute(std::vector<std::string> args,
277  bool quiet = false,
278  // attention: stdout is a macro defined by windows.h
279  std::ostream* out = NULL,
280  bool allow_fail = false,
281  int verbose = 0,
282  bool simulate = false);
283 
284 
285 } // end of namespaces
286 
287 
288 /// @}
289 // end of Doxygen group
290 
291 
292 #endif // _BASIS_BASIS_BASIS_H
std::string bindir()
Get absolute path to directory containing runtime executables.
Definition: basis.cxx:203
std::string exename(const std::string &name=std::string())
Get name of executable file.
Definition: basis.cxx:281
const unsigned int VERSION_MAJOR
The major version number.
Definition: basis.cxx:41
std::string libexecdir()
Get absolute path to directory containing auxiliary executables.
Definition: basis.cxx:213
Main module of project-independent BASIS utilities.
const char * LICENSE
Default license of executables.
Definition: basis.cxx:46
std::string exedir(const std::string &name=std::string())
Get directory of executable file.
Definition: basis.cxx:287
std::string targetuid(const std::string &name)
Get UID of build target.
Definition: basis.cxx:263
Definition: basis.h:34
int execute(const std::string &cmd, bool quiet=false, std::ostream *out=NULL, bool allow_fail=false, int verbose=0, bool simulate=false)
Execute command as subprocess.
Definition: basis.cxx:297
bool istarget(const std::string &name)
Determine whether a given build target is known.
Definition: basis.cxx:269
void print_version(const char *name, const char *version=NULL, const char *project=NULL, const char *copyright=NULL, const char *license=NULL)
Print version information including copyright and license notices.
Definition: basis.cxx:253
const char * RELEASE
Complete version information as output by –version option.
Definition: basis.cxx:39
std::string exepath(const std::string &name=std::string())
Get absolute path of executable file.
Definition: basis.cxx:275
std::string libdir()
Get absolute path to directory containing libraries.
Definition: basis.cxx:223
const char * CONTACT
Default contact to use for help output of executables.
Definition: basis.cxx:47
MultiSwitchArg verbose("v", "verbose", "Increase verbosity of output messages.", false)
std::string datadir()
Get absolute path to directory containing auxiliary data.
Definition: basis.cxx:233
const unsigned int VERSION_MINOR
The minor version number.
Definition: basis.cxx:42
const unsigned int VERSION_PATCH
The patch number.
Definition: basis.cxx:43
const char * COPYRIGHT
Default copyright of executables.
Definition: basis.cxx:45
void print_contact(const char *contact=NULL)
Print contact information.
Definition: basis.cxx:247
const char * VERSION
The version string given as "<major>.<minor>.<patch>".
Definition: basis.cxx:38
const char * PROJECT
The project name.
Definition: basis.cxx:37