basis.cxx
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.cxx
12  * @brief BASIS utilities of BASIS project of BASIS package.
13  *
14  * @note The basis.cxx module was automatically generated by BASIS from the
15  * template file basis.cxx.in which is part of the BASIS installation.
16  */
17 
18 #include <map>
19 
20 #include <basis//basis.h>
21 
22 
23 // acceptable in .cxx file
24 using namespace std;
25 using namespace basis;
26 
27 
28 namespace basis {
29 
30 
31 // ===========================================================================
32 // constants
33 // ===========================================================================
34 
35 // ---------------------------------------------------------------------------
36 // project attributes
37 const char* PROJECT = "BASIS";
38 const char* VERSION = "3.3.1";
39 const char* RELEASE = "v3.3 (fb18c98)";
40 
41 const unsigned int VERSION_MAJOR = 3;
42 const unsigned int VERSION_MINOR = 3;
43 const unsigned int VERSION_PATCH = 0;
44 
45 const char* COPYRIGHT = "2011-12 University of Pennsylvania, 2013-14 Carnegie Mellon University, 2013-16 Andreas Schuh";
46 const char* LICENSE = "See https://cmake-basis.github.io/download.html#license or COPYING file.";
47 const char* CONTACT = "andreas.schuh.84@gmail.com";
48 
49 // ===========================================================================
50 // local helper functions
51 // ===========================================================================
52 
53 /**
54  * @brief Determine if this is the built or the installed executable.
55  *
56  * @returns Whether the executable was executed from within the build tree.
57  */
58 static inline bool executing_in_build_tree()
59 {
60  // get executable path relative to top directory of build tree
61  string rel_path = os::path::relpath(
62  // directory of this executable
63  os::exedir(),
64  // The following path is the absolute path of the top directory of the build
65  // tree in which the software was built. It can be used to determine whether
66  // an executable is executing from within the build tree rather than an
67  // installation. Given that we tend to build software in a directory with the
68  // prefix "-build", but never choose such directory for the installation prefix,
69  // a check whether or not the location of the executable file is inside this
70  // build tree is enough to know whether or not it is executed from within the
71  // build tree or an installation.
72  os::path::realpath("/Users/as12312/Software/BASIS/Xcode"));
73  // return whether executable directory is inside the build tree or not
74  return !(rel_path == "" || rel_path == "." ||
75  (rel_path.substr(0, 2) == ".." && (rel_path.size() == 2 || rel_path[2] == '/')));
76 }
77 
78 // ===========================================================================
79 // class: ExecutableTargetInfo (declaration)
80 // ===========================================================================
81 
82 // The definition of the class methods can be found at the end of this file.
83 
84 /**
85  * @brief Provides information about executable build targets.
86  *
87  * @sa IExecutableTargetInfo
88  */
89 class ExecutableTargetInfo : public basis::util::IExecutableTargetInfo
90 {
91  // -----------------------------------------------------------------------
92  // typedefs
93 private:
94 
95  typedef std::map <std::string, std::string> MapType;
96  typedef MapType::const_iterator MapIterator;
97 
98  // -----------------------------------------------------------------------
99  // construction / destruction
100 private:
101 
102  /**
103  * @brief Constructor.
104  *
105  * Initializes the data members. The initialization code is in particular
106  * generated by BASIS during the configuration of the build system.
107  */
108  ExecutableTargetInfo();
109 
110  /// @brief Destructor.
111  ~ExecutableTargetInfo() {}
112 
113 public:
114 
115  /**
116  * @brief Get static instance of this module.
117  *
118  * @attention This method is not thread-safe!
119  *
120  * @return Static instance of this class.
121  */
122  static const ExecutableTargetInfo* instance();
123 
124  // -----------------------------------------------------------------------
125  // public interface
126 public:
127 
128  std::string targetuid(const std::string& target) const;
129  bool istarget(const std::string& target) const;
130  std::string basename(const std::string& target) const;
131  std::string dirname(const std::string& target) const;
132 
133  // -----------------------------------------------------------------------
134  // unsupported methods
135 private:
136 
137  /**
138  * @brief Copy constructor.
139  *
140  * @note Intentionally not implemented.
141  */
142  ExecutableTargetInfo(const ExecutableTargetInfo&);
143 
144  /**
145  * @brief Assignment operator.
146  *
147  * @note Intentionally not implemented.
148  */
149  void operator=(const ExecutableTargetInfo&);
150 
151  // -----------------------------------------------------------------------
152  // members
153 private:
154 
155  /// Maps build target names to executable file names.
156  MapType _exec_names;
157  /// Maps build target names to output directories in build tree.
158  MapType _build_dirs;
159  /// Maps build target names to installation directories relative to
160  /// installation prefix as returned by GetInstallationPrefix().
161  MapType _install_dirs;
162 
163 }; // class ExecutableTargetInfo
164 
165 // ===========================================================================
166 // package directories
167 // ===========================================================================
168 
169 /**
170  * @brief Get absolute path of installation directory.
171  *
172  * This function returns the absolute path of the installation prefix path.
173  * If the installation was not moved after a "make install", the returned
174  * directory corresponds to the value of the INSTALL_PREFIX CMake variable
175  * as specified during the build of the executable file. Note, however,
176  * that even when the installation tree was moved after the configuration
177  * and build of the software, the correct path is returned as long as the
178  * relative directory structure of the installation tree is maintained.
179  * This is because the path is determined relative to the directory of the
180  * executable itself, knowning in which path this executable is located
181  * relative to the INSTALL_PREFIX.
182  *
183  * @note If the executable is executed from within the build tree, the
184  * returned path will not be correct. Therefore, only use this
185  * function when executing_in_build_tree() returns false. Otherwise,
186  * the configured absolute paths which are valid for the build tree
187  * have to be used. Note that the build tree is not supposed to be
188  * relocatable in any case as it is only a temporary directory tree
189  * and CMake requires it to be not moved anywhere else.
190  *
191  * @returns Absolute path of top directory of installation tree.
192  */
193 static string install_prefix()
194 {
195 #ifdef LIBEXEC
196  return os::path::join(os::exedir(), "..");
197 #else
198  return os::path::join(os::exedir(), "..");
199 #endif
200 }
201 
202 // ---------------------------------------------------------------------------
203 string bindir()
204 {
205  if (executing_in_build_tree()) {
206  return "/Users/as12312/Software/BASIS/Xcode/bin";
207  } else {
208  return os::path::join(install_prefix(), "bin");
209  }
210 }
211 
212 // ---------------------------------------------------------------------------
213 string libexecdir()
214 {
215  if (executing_in_build_tree()) {
216  return "/Users/as12312/Software/BASIS/Xcode/lib";
217  } else {
218  return os::path::join(install_prefix(), "lib");
219  }
220 }
221 
222 // ---------------------------------------------------------------------------
223 string libdir()
224 {
225  if (executing_in_build_tree()) {
226  return "/Users/as12312/Software/BASIS/Xcode/lib";
227  } else {
228  return os::path::join(install_prefix(), "lib");
229  }
230 }
231 
232 // ---------------------------------------------------------------------------
233 string datadir()
234 {
235  if (executing_in_build_tree()) {
236  return "/Users/as12312/Software/BASIS/Workspace/data";
237  } else {
238  return os::path::join(install_prefix(), "share/data");
239  }
240 }
241 
242 // ===========================================================================
243 // executable information
244 // ===========================================================================
245 
246 // ---------------------------------------------------------------------------
247 void print_contact(const char* contact)
248 {
249  basis::util::print_contact(contact != NULL ? contact : CONTACT);
250 }
251 
252 // ---------------------------------------------------------------------------
253 void print_version(const char* name, const char* version, const char* project, const char* copyright, const char* license)
254 {
256  version != NULL ? version : RELEASE,
257  project != NULL ? project : PROJECT,
258  copyright != NULL ? copyright : COPYRIGHT,
259  license != NULL ? license : LICENSE);
260 }
261 
262 // ---------------------------------------------------------------------------
263 string targetuid(const string& name)
264 {
265  return basis::util::targetuid(name, ExecutableTargetInfo::instance());
266 }
267 
268 // ---------------------------------------------------------------------------
269 bool istarget(const string& name)
270 {
271  return basis::util::istarget(name, ExecutableTargetInfo::instance());
272 }
273 
274 // ---------------------------------------------------------------------------
275 string exepath(const string& name)
276 {
277  return basis::util::exepath(name, ExecutableTargetInfo::instance());
278 }
279 
280 // ---------------------------------------------------------------------------
281 string exename(const std::string& name)
282 {
283  return basis::util::exename(name, ExecutableTargetInfo::instance());
284 }
285 
286 // ---------------------------------------------------------------------------
287 string exedir(const std::string& name)
288 {
289  return basis::util::exedir(name, ExecutableTargetInfo::instance());
290 }
291 
292 // ===========================================================================
293 // command execution
294 // ===========================================================================
295 
296 // ---------------------------------------------------------------------------
297 int execute(const string& cmd, bool quiet, ostream* out,
298  bool allow_fail, int verbose, bool simulate)
299 {
300  return basis::util::execute(cmd, quiet, out, allow_fail, verbose, simulate,
301  ExecutableTargetInfo::instance());
302 }
303 
304 // ---------------------------------------------------------------------------
305 int execute(vector<string> args, bool quiet, ostream* out,
306  bool allow_fail, int verbose, bool simulate)
307 {
308  return basis::util::execute(args, quiet, out, allow_fail, verbose, simulate,
309  ExecutableTargetInfo::instance());
310 }
311 
312 // ===========================================================================
313 // class: ExecutableTargetInfo (definition)
314 // ===========================================================================
315 
316 // ---------------------------------------------------------------------------
317 const ExecutableTargetInfo* ExecutableTargetInfo::instance()
318 {
319  static ExecutableTargetInfo instance;
320  return &instance;
321 }
322 
323 // ---------------------------------------------------------------------------
324 string ExecutableTargetInfo::targetuid(const string& target) const
325 {
326  // empty(, invalid) target name remains unchanged
327  if (target.empty()) return "";
328  // in case of a leading namespace separator, do not modify target name
329  if (target[0] == '.') return target;
330  // project namespace
331  string prefix = string("basis");
332  // try prepending namespace or parts of it until target is known
333  for (;;) {
334  if (_exec_names.find(prefix + "." + target) != _exec_names.end()) {
335  return prefix + "." + target;
336  }
337  string::size_type pos = prefix.rfind('.');
338  if (pos == string::npos) break;
339  prefix = prefix.substr(0, pos);
340  }
341  // otherwise, return target name unchanged
342  return target;
343 }
344 
345 // ---------------------------------------------------------------------------
346 bool ExecutableTargetInfo::istarget(const string& target) const
347 {
348  if (target.empty()) return false;
349  string uid;
350  if (target[0] == '.') uid = target.substr(1);
351  else uid = targetuid(target);
352  return _exec_names.find(uid) != _exec_names.end();
353 }
354 
355 // ---------------------------------------------------------------------------
356 string ExecutableTargetInfo::basename(const string& target) const
357 {
358  if (target.empty()) return "";
359  string uid;
360  if (target[0] == '.') uid = target.substr(1);
361  else uid = targetuid(target);
362  MapIterator it = _exec_names.find(uid);
363  if (it == _exec_names.end ()) return "";
364  return it->second;
365 }
366 
367 // ---------------------------------------------------------------------------
368 string ExecutableTargetInfo::dirname(const string& target) const
369 {
370  if (target.empty()) return "";
371  string uid;
372  if (target[0] == '.') uid = target.substr(1);
373  else uid = targetuid(target);
374  if (executing_in_build_tree()) {
375  MapIterator it = _build_dirs.find(uid);
376  if (it == _build_dirs.end()) return "";
377 #ifdef CMAKE_INTDIR
378  const char * match = "/$<CONFIG>";
379  const size_t n = strlen(match);
380  if (it->second.length() >= n) {
381  const size_t pos = it->second.length() - n;
382  if (it->second.compare(pos, n, match, n) == 0) {
383  return os::path::join(it->second.substr(0, pos), CMAKE_INTDIR);
384  }
385  }
386 #endif
387  return it->second;
388  } else {
389  MapIterator it = _install_dirs.find(uid);
390  if (it == _install_dirs.end()) return "";
391  return os::path::join(install_prefix(), it->second);
392  }
393 }
394 
395 // ---------------------------------------------------------------------------
396 ExecutableTargetInfo::ExecutableTargetInfo()
397 {
398  // the following code was automatically generated by the BASIS
399  // CMake function basis_configure_ExecutableTargetInfo()
400 
401  // basisproject
402  _exec_names ["basis.basisproject"] = "basisproject";
403  _build_dirs ["basis.basisproject"] = "/Users/as12312/Software/BASIS/Xcode/bin/$<CONFIG>";
404  _install_dirs["basis.basisproject"] = "/opt/basis/bin";
405 
406  // testdriver
407  _exec_names ["basis.testdriver"] = "testdriver";
408  _build_dirs ["basis.testdriver"] = "/Users/as12312/Software/BASIS/Xcode/lib/$<CONFIG>";
409  _install_dirs["basis.testdriver"] = "/opt/basis/lib";
410 
411  // basistest
412  _exec_names ["basis.basistest"] = "basistest";
413  _build_dirs ["basis.basistest"] = "/Users/as12312/Software/BASIS/Xcode/bin/$<CONFIG>";
414  _install_dirs["basis.basistest"] = "/opt/basis/bin";
415 
416  // basistest-master
417  _exec_names ["basis.basistest-master"] = "basistest-master";
418  _build_dirs ["basis.basistest-master"] = "/Users/as12312/Software/BASIS/Xcode/lib/$<CONFIG>";
419  _install_dirs["basis.basistest-master"] = "/opt/basis/lib";
420 
421  // basistest-slave
422  _exec_names ["basis.basistest-slave"] = "basistest-slave";
423  _build_dirs ["basis.basistest-slave"] = "/Users/as12312/Software/BASIS/Xcode/lib/$<CONFIG>";
424  _install_dirs["basis.basistest-slave"] = "/opt/basis/lib";
425 
426  // basistest-cron
427  _exec_names ["basis.basistest-cron"] = "basistest-cron";
428  _build_dirs ["basis.basistest-cron"] = "/Users/as12312/Software/BASIS/Xcode/lib/$<CONFIG>";
429  _install_dirs["basis.basistest-cron"] = "/opt/basis/lib";
430 
431  // basistest-svn
432  _exec_names ["basis.basistest-svn"] = "basistest-svn";
433  _build_dirs ["basis.basistest-svn"] = "/Users/as12312/Software/BASIS/Xcode/lib/$<CONFIG>";
434  _install_dirs["basis.basistest-svn"] = "/opt/basis/lib";
435 
436  // doxyfilter
437  _exec_names ["basis.doxyfilter"] = "doxyfilter";
438  _build_dirs ["basis.doxyfilter"] = "/Users/as12312/Software/BASIS/Xcode/lib/$<CONFIG>";
439  _install_dirs["basis.doxyfilter"] = "/opt/basis/lib";
440 
441  // doxyfilter-perl
442  _exec_names ["basis.doxyfilter-perl"] = "doxyfilter-perl";
443  _build_dirs ["basis.doxyfilter-perl"] = "/Users/as12312/Software/BASIS/Xcode/lib/$<CONFIG>";
444  _install_dirs["basis.doxyfilter-perl"] = "/opt/basis/lib";
445 
446  // dummy_command
447  _exec_names ["basis.dummy_command"] = "dummy_command";
448  _build_dirs ["basis.dummy_command"] = "/Users/as12312/Software/BASIS/Xcode/Testing/bin/$<CONFIG>";
449  _install_dirs["basis.dummy_command"] = "/opt/basis/bin";
450 
451  // test_future_statements
452  _exec_names ["basis.test_future_statements"] = "test_future_statements";
453  _build_dirs ["basis.test_future_statements"] = "/Users/as12312/Software/BASIS/Xcode/Testing/bin/$<CONFIG>";
454  _install_dirs["basis.test_future_statements"] = "/opt/basis/bin";
455 
456  // test_matlabtools
457  _exec_names ["basis.test_matlabtools"] = "test_matlabtools";
458  _build_dirs ["basis.test_matlabtools"] = "/Users/as12312/Software/BASIS/Xcode/Testing/bin/$<CONFIG>";
459  _install_dirs["basis.test_matlabtools"] = "/opt/basis/bin";
460 
461  // test_basisproject
462  _exec_names ["basis.test_basisproject"] = "test_basisproject";
463  _build_dirs ["basis.test_basisproject"] = "/Users/as12312/Software/BASIS/Xcode/Testing/bin/$<CONFIG>";
464  _install_dirs["basis.test_basisproject"] = "/opt/basis/bin";
465 
466  // test_os
467  _exec_names ["basis.test_os"] = "test_os";
468  _build_dirs ["basis.test_os"] = "/Users/as12312/Software/BASIS/Xcode/Testing/bin/$<CONFIG>";
469  _install_dirs["basis.test_os"] = "/opt/basis/bin";
470 
471  // test_path
472  _exec_names ["basis.test_path"] = "test_path";
473  _build_dirs ["basis.test_path"] = "/Users/as12312/Software/BASIS/Xcode/Testing/bin/$<CONFIG>";
474  _install_dirs["basis.test_path"] = "/opt/basis/bin";
475 
476  // test_subprocess
477  _exec_names ["basis.test_subprocess"] = "test_subprocess";
478  _build_dirs ["basis.test_subprocess"] = "/Users/as12312/Software/BASIS/Xcode/Testing/bin/$<CONFIG>";
479  _install_dirs["basis.test_subprocess"] = "/opt/basis/bin";
480 
481  // test_core
482  _exec_names ["basis.test_core"] = "test_core";
483  _build_dirs ["basis.test_core"] = "/Users/as12312/Software/BASIS/Xcode/Testing/bin/$<CONFIG>";
484  _install_dirs["basis.test_core"] = "/opt/basis/bin";
485 
486  // test_shutilities
487  _exec_names ["basis.test_shutilities"] = "test_shutilities";
488  _build_dirs ["basis.test_shutilities"] = "/Users/as12312/Software/BASIS/Xcode/Testing/bin/$<CONFIG>";
489  _install_dirs["basis.test_shutilities"] = "/opt/basis/bin";
490 
491  // test_shtap
492  _exec_names ["basis.test_shtap"] = "test_shtap";
493  _build_dirs ["basis.test_shtap"] = "/Users/as12312/Software/BASIS/Xcode/Testing/bin/$<CONFIG>";
494  _install_dirs["basis.test_shtap"] = "/opt/basis/bin";
495 
496  // parseargs
497  _exec_names ["basis.parseargs"] = "parseargs";
498  _build_dirs ["basis.parseargs"] = "/Users/as12312/Software/BASIS/Xcode/Testing/bin/$<CONFIG>";
499  _install_dirs["basis.parseargs"] = "/opt/basis/bin";
500 
501  // test_utilities
502  _exec_names ["basis.test_utilities"] = "test_utilities";
503  _build_dirs ["basis.test_utilities"] = "/Users/as12312/Software/BASIS/Xcode/Testing/bin/$<CONFIG>";
504  _install_dirs["basis.test_utilities"] = "/opt/basis/bin";
505 }
506 
507 
508 } // end of namespaces
std::string bindir()
Get absolute path to directory containing runtime executables.
Definition: basis.cxx:203
const unsigned int VERSION_MAJOR
The major version number.
Definition: basis.cxx:41
string PROJECT
Project name.
Definition: utilities.sh:79
std::string exedir(const std::string &name=std::string(), const IExecutableTargetInfo *targets=NULL)
Get directory of executable file.
Definition: utilities.cxx:115
function targetuid(out uid, in name)
Get UID of build target.
std::string libexecdir()
Get absolute path to directory containing auxiliary executables.
Definition: basis.cxx:213
function realpath(in path)
Get canonical file path.
function istarget(in target)
Determine whether a given build target is known.
void print_contact(const char *contact)
Print contact information.
Definition: utilities.cxx:33
int execute(const std::string &cmd, bool quiet=false, std::ostream *out=NULL, bool allow_fail=false, int verbose=0, bool simulate=false, const IExecutableTargetInfo *targets=NULL)
Execute command as subprocess.
Definition: utilities.cxx:138
std::string join(const std::string &base, const std::string &path)
Join two paths, e.g., base path and relative path.
Definition: path.cxx:432
STL namespace.
Definition: basis.h:34
string COPYRIGHT
Default copyright of executables.
Definition: utilities.sh:91
std::string basename(const std::string &path)
Get file name.
Definition: path.cxx:273
Provides information about executable build targets.
Definition: utilities.h:88
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
BASIS utilities of BASIS project of BASIS package.
std::string relpath(const std::string &path, const std::string &base=std::string())
Make path relative.
Definition: path.cxx:308
string VERSION
Project version.
Definition: utilities.sh:81
std::string exepath(const std::string &name=std::string(), const IExecutableTargetInfo *targets=NULL)
Get absolute path of executable file.
Definition: utilities.cxx:64
function exepath(out path, in target)
Get absolute path of executable file.
string RELEASE
Project release.
Definition: utilities.sh:89
std::string targetuid(const std::string &name, const IExecutableTargetInfo *targets=NULL)
Get UID of build target.
Definition: utilities.cxx:52
std::string dirname(const std::string &path)
Get file directory.
Definition: path.cxx:265
std::string libdir()
Get absolute path to directory containing libraries.
Definition: basis.cxx:223
function match(in value, in pattern)
This function implements a more portable way to do pattern matching.
function exedir(out dir, in name)
Get directory of executable file.
string LICENSE
Default license of executables.
Definition: utilities.sh:93
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
std::string exename(const std::string &name=std::string(), const IExecutableTargetInfo *targets=NULL)
Get name of executable file.
Definition: utilities.cxx:102
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
void print_version(const char *name, const char *version, const char *project=NULL, const char *copyright=NULL, const char *license=NULL)
Print version information including copyright and license notices.
Definition: utilities.cxx:39
void print_contact(const char *contact=NULL)
Print contact information.
Definition: basis.cxx:247
function exename(out file, in name)
Get name of executable file.
bool istarget(const std::string &name, const IExecutableTargetInfo *targets=NULL)
Determine whether a given build target is known.
Definition: utilities.cxx:58
string CONTACT
Default contact to use for help output of executables.
Definition: utilities.sh:95
function execute(in options, in cmd, in args)
Execute command as subprocess.