BASIS  r3148
basis.cxx
Go to the documentation of this file.
00001 /**
00002  * @file  basis.cxx
00003  * @brief BASIS utilities of BASIS project of BASIS package.
00004  *
00005  * @note The basis.cxx module was automatically generated by BASIS from the
00006  *       template file basis.cxx.in which is part of the BASIS installation.
00007  *
00008  * Copyright (c) 2011, 2012 University of Pennsylvania. All rights reserved.<br />
00009  * See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.
00010  *
00011  * Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00012  */
00013 
00014 #include <map>
00015 
00016 #include <basis//basis.h>
00017 
00018 
00019 // acceptable in .cxx file
00020 using namespace std;
00021 using namespace basis;
00022 
00023 
00024 namespace basis {
00025 
00026 
00027 // ===========================================================================
00028 // constants
00029 // ===========================================================================
00030 
00031 // ---------------------------------------------------------------------------
00032 // project attributes
00033 const char* PROJECT = "BASIS";
00034 const char* VERSION = "0.0.0";
00035 const char* RELEASE = "r3148";
00036 
00037 const unsigned int VERSION_MAJOR = 0;
00038 const unsigned int VERSION_MINOR = 0;
00039 const unsigned int VERSION_PATCH = 0;
00040 
00041 const char* COPYRIGHT = "2011, 2012, 2013 University of Pennsylvania";
00042 const char* LICENSE   = "See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.";
00043 const char* CONTACT   = "SBIA Group <sbia-software at uphs.upenn.edu>";
00044 
00045 // ===========================================================================
00046 // local helper functions
00047 // ===========================================================================
00048 
00049 /**
00050  * @brief Determine if this is the built or the installed executable.
00051  *
00052  * @returns Whether the executable was executed from within the build tree.
00053  */
00054 static inline bool executing_in_build_tree()
00055 {
00056     // get executable path relative to top directory of build tree
00057     string rel_path = os::path::relpath(
00058             // directory of this executable
00059             os::exedir(),
00060             // The following path is the absolute path of the top directory of the build
00061             // tree in which the software was built. It can be used to determine whether
00062             // an executable is executing from within the build tree rather than an
00063             // installation. Given that we tend to build software in a directory with the
00064             // prefix "-build", but never choose such directory for the installation prefix,
00065             // a check whether or not the location of the executable file is inside this
00066             // build tree is enough to know whether or not it is executed from within the
00067             // build tree or an installation.
00068             os::path::realpath("/sbia/home/schuha/sandbox/build/basis"));
00069     // return whether executable directory is inside the build tree or not
00070     return !(rel_path == "" || rel_path == "." ||
00071             (rel_path.substr(0, 2) == ".." && (rel_path.size() == 2 || rel_path[2] == '/')));
00072 }
00073 
00074 // ===========================================================================
00075 // class: ExecutableTargetInfo (declaration)
00076 // ===========================================================================
00077 
00078 // The definition of the class methods can be found at the end of this file.
00079 
00080 /**
00081  * @brief Provides information about executable build targets.
00082  *
00083  * @sa IExecutableTargetInfo
00084  */
00085 class ExecutableTargetInfo : public basis::util::IExecutableTargetInfo
00086 {
00087     // -----------------------------------------------------------------------
00088     // typedefs
00089 private:
00090 
00091     typedef std::map <std::string, std::string> MapType;
00092     typedef MapType::const_iterator             MapIterator;
00093 
00094     // -----------------------------------------------------------------------
00095     // construction / destruction
00096 private:
00097 
00098     /**
00099      * @brief Constructor.
00100      *
00101      * Initializes the data members. The initialization code is in particular
00102      * generated by BASIS during the configuration of the build system.
00103      */
00104     ExecutableTargetInfo();
00105 
00106     /// @brief Destructor.
00107     ~ExecutableTargetInfo() {}
00108 
00109 public:
00110 
00111     /**
00112      * @brief Get static instance of this module.
00113      *
00114      * @attention This method is not thread-safe!
00115      *
00116      * @return Static instance of this class.
00117      */
00118     static const ExecutableTargetInfo* instance();
00119 
00120     // -----------------------------------------------------------------------
00121     // public interface
00122 public:
00123 
00124     std::string targetuid(const std::string& target) const;
00125     bool istarget(const std::string& target) const;
00126     std::string basename(const std::string& target) const;
00127     std::string dirname(const std::string& target) const;
00128 
00129     // -----------------------------------------------------------------------
00130     // unsupported methods
00131 private:
00132 
00133     /**
00134      * @brief Copy constructor.
00135      *
00136      * @note Intentionally not implemented.
00137      */
00138     ExecutableTargetInfo(const ExecutableTargetInfo&);
00139 
00140     /**
00141      * @brief Assignment operator.
00142      *
00143      * @note Intentionally not implemented.
00144      */
00145     void operator=(const ExecutableTargetInfo&);
00146 
00147     // -----------------------------------------------------------------------
00148     // members
00149 private:
00150 
00151     /// Maps build target names to executable file names.
00152     MapType _exec_names;
00153     /// Maps build target names to output directories in build tree.
00154     MapType _build_dirs;
00155     /// Maps build target names to installation directories relative to
00156     /// installation prefix as returned by GetInstallationPrefix().
00157     MapType _install_dirs;
00158 
00159 }; // class ExecutableTargetInfo
00160 
00161 // ===========================================================================
00162 // package directories
00163 // ===========================================================================
00164 
00165 /**
00166  * @brief Get absolute path of installation directory.
00167  *
00168  * This function returns the absolute path of the installation prefix path.
00169  * If the installation was not moved after a "make install", the returned
00170  * directory corresponds to the value of the INSTALL_PREFIX CMake variable
00171  * as specified during the build of the executable file. Note, however,
00172  * that even when the installation tree was moved after the configuration
00173  * and build of the software, the correct path is returned as long as the
00174  * relative directory structure of the installation tree is maintained.
00175  * This is because the path is determined relative to the directory of the
00176  * executable itself, knowning in which path this executable is located
00177  * relative to the INSTALL_PREFIX.
00178  *
00179  * @note If the executable is executed from within the build tree, the
00180  *       returned path will not be correct. Therefore, only use this
00181  *       function when executing_in_build_tree() returns false. Otherwise,
00182  *       the configured absolute paths which are valid for the build tree
00183  *       have to be used. Note that the build tree is not supposed to be
00184  *       relocatable in any case as it is only a temporary directory tree
00185  *       and CMake requires it to be not moved anywhere else.
00186  *
00187  * @returns Absolute path of top directory of installation tree.
00188  */
00189 static string install_prefix()
00190 {
00191 #ifdef LIBEXEC
00192     return os::path::join(os::exedir(), "..");
00193 #else
00194     return os::path::join(os::exedir(), "..");
00195 #endif
00196 }
00197 
00198 // ---------------------------------------------------------------------------
00199 string bindir()
00200 {
00201     if (executing_in_build_tree()) {
00202         return "/sbia/home/schuha/sandbox/build/basis/bin";
00203     } else {
00204         return os::path::join(install_prefix(), "bin");
00205     }
00206 }
00207 
00208 // ---------------------------------------------------------------------------
00209 string libexecdir()
00210 {
00211     if (executing_in_build_tree()) {
00212         return "/sbia/home/schuha/sandbox/build/basis/lib";
00213     } else {
00214         return os::path::join(install_prefix(), "lib");
00215     }
00216 }
00217 
00218 // ---------------------------------------------------------------------------
00219 string libdir()
00220 {
00221     if (executing_in_build_tree()) {
00222         return "/sbia/home/schuha/sandbox/build/basis/lib";
00223     } else {
00224         return os::path::join(install_prefix(), "lib");
00225     }
00226 }
00227 
00228 // ---------------------------------------------------------------------------
00229 string datadir()
00230 {
00231     if (executing_in_build_tree()) {
00232         return "/sbia/home/schuha/projects/basis/data";
00233     } else {
00234         return os::path::join(install_prefix(), "share/data");
00235     }
00236 }
00237 
00238 // ===========================================================================
00239 // executable information
00240 // ===========================================================================
00241 
00242 // ---------------------------------------------------------------------------
00243 void print_contact(const char* contact)
00244 {
00245     basis::util::print_contact(contact != NULL ? contact : CONTACT);
00246 }
00247 
00248 // ---------------------------------------------------------------------------
00249 void print_version(const char* name, const char* version, const char* project, const char* copyright, const char* license)
00250 {
00251     basis::util::print_version(name,
00252                                version   != NULL ? version   : RELEASE,
00253                                project   != NULL ? project   : PROJECT,
00254                                copyright != NULL ? copyright : COPYRIGHT,
00255                                license   != NULL ? license   : LICENSE);
00256 }
00257 
00258 // ---------------------------------------------------------------------------
00259 string targetuid(const string& name)
00260 {
00261     return basis::util::targetuid(name, ExecutableTargetInfo::instance());
00262 }
00263 
00264 // ---------------------------------------------------------------------------
00265 bool istarget(const string& name)
00266 {
00267     return basis::util::istarget(name, ExecutableTargetInfo::instance());
00268 }
00269 
00270 // ---------------------------------------------------------------------------
00271 string exepath(const string& name)
00272 {
00273     return basis::util::exepath(name, ExecutableTargetInfo::instance());
00274 }
00275 
00276 // ---------------------------------------------------------------------------
00277 string exename(const std::string& name)
00278 {
00279     return basis::util::exename(name, ExecutableTargetInfo::instance());
00280 }
00281 
00282 // ---------------------------------------------------------------------------
00283 string exedir(const std::string& name)
00284 {
00285     return basis::util::exedir(name, ExecutableTargetInfo::instance());
00286 }
00287 
00288 // ===========================================================================
00289 // command execution
00290 // ===========================================================================
00291 
00292 // ---------------------------------------------------------------------------
00293 int execute(const string& cmd, bool quiet, ostream* out,
00294             bool allow_fail, int verbose, bool simulate)
00295 {
00296     return basis::util::execute(cmd, quiet, out, allow_fail, verbose, simulate,
00297                                 ExecutableTargetInfo::instance());
00298 }
00299 
00300 // ---------------------------------------------------------------------------
00301 int execute(vector<string> args, bool quiet, ostream* out,
00302             bool allow_fail, int verbose, bool simulate)
00303 {
00304     return basis::util::execute(args, quiet, out, allow_fail, verbose, simulate,
00305                                 ExecutableTargetInfo::instance());
00306 }
00307 
00308 // ===========================================================================
00309 // class: ExecutableTargetInfo (definition)
00310 // ===========================================================================
00311 
00312 // ---------------------------------------------------------------------------
00313 const ExecutableTargetInfo* ExecutableTargetInfo::instance()
00314 {
00315     static ExecutableTargetInfo instance;
00316     return &instance;
00317 }
00318 
00319 // ---------------------------------------------------------------------------
00320 string ExecutableTargetInfo::targetuid(const string& target) const
00321 {
00322     // empty(, invalid) target name remains unchanged
00323     if (target.empty()) return "";
00324     // in case of a leading namespace separator, do not modify target name
00325     if (target[0] == '.') return target;
00326     // project namespace
00327     string prefix = string("basis");
00328     // try prepending namespace or parts of it until target is known
00329     for (;;) {
00330         if (_exec_names.find(prefix + "." + target) != _exec_names.end()) {
00331             return prefix + "." + target;
00332         }
00333         string::size_type pos = prefix.rfind('.');
00334         if (pos == string::npos) break;
00335         prefix = prefix.substr(0, pos);
00336     }
00337     // otherwise, return target name unchanged
00338     return target;
00339 }
00340 
00341 // ---------------------------------------------------------------------------
00342 bool ExecutableTargetInfo::istarget(const string& target) const
00343 {
00344     if (target.empty()) return false;
00345     string uid;
00346     if (target[0] == '.') uid = target.substr(1);
00347     else uid = targetuid(target);
00348     return _exec_names.find(uid) != _exec_names.end();
00349 }
00350 
00351 // ---------------------------------------------------------------------------
00352 string ExecutableTargetInfo::basename(const string& target) const
00353 {
00354     if (target.empty()) return "";
00355     string uid;
00356     if (target[0] == '.') uid = target.substr(1);
00357     else uid = targetuid(target);
00358     MapIterator it = _exec_names.find(uid);
00359     if (it == _exec_names.end ()) return "";
00360     return it->second;
00361 }
00362 
00363 // ---------------------------------------------------------------------------
00364 string ExecutableTargetInfo::dirname(const string& target) const
00365 {
00366     if (target.empty()) return "";
00367     string uid;
00368     if (target[0] == '.') uid = target.substr(1);
00369     else uid = targetuid(target);
00370     if (executing_in_build_tree()) {
00371         MapIterator it = _build_dirs.find(uid);
00372         if (it == _build_dirs.end()) return "";
00373         #if defined CMAKE_INTDIR
00374             // this subdirectory is used by CMake generators for IDEs such as Visual Studio
00375             return os::path::join(it->second, CMAKE_INTDIR);
00376         #else
00377             return it->second;
00378         #endif
00379     } else {
00380         MapIterator it = _install_dirs.find(uid);
00381         if (it == _install_dirs.end()) return "";
00382         return os::path::join(install_prefix(), it->second);
00383     }
00384 }
00385 
00386 // ---------------------------------------------------------------------------
00387 ExecutableTargetInfo::ExecutableTargetInfo()
00388 {
00389     // the following code was automatically generated by the BASIS
00390     // CMake function basis_configure_ExecutableTargetInfo()
00391 
00392     // basisproject
00393     _exec_names  ["basis.basisproject"]      = "basisproject";
00394     _build_dirs  ["basis.basisproject"]      = "/sbia/home/schuha/sandbox/build/basis/bin";
00395     _install_dirs["basis.basisproject"]      = "/sbia/home/schuha/sandbox/installs/basis/bin";
00396 
00397     // doxyfilter
00398     _exec_names  ["basis.doxyfilter"]        = "doxyfilter";
00399     _build_dirs  ["basis.doxyfilter"]        = "/sbia/home/schuha/sandbox/build/basis/lib";
00400     _install_dirs["basis.doxyfilter"]        = "/sbia/home/schuha/sandbox/installs/basis/lib";
00401 
00402     // doxyfilter-perl
00403     _exec_names  ["basis.doxyfilter-perl"]   = "doxyfilter-perl";
00404     _build_dirs  ["basis.doxyfilter-perl"]   = "/sbia/home/schuha/sandbox/build/basis/lib";
00405     _install_dirs["basis.doxyfilter-perl"]   = "/sbia/home/schuha/sandbox/installs/basis/lib";
00406 
00407     // testdriver
00408     _exec_names  ["basis.testdriver"]        = "testdriver";
00409     _build_dirs  ["basis.testdriver"]        = "/sbia/home/schuha/sandbox/build/basis/lib";
00410     _install_dirs["basis.testdriver"]        = "/sbia/home/schuha/sandbox/installs/basis/lib";
00411 
00412     // basistest-svn
00413     _exec_names  ["basis.basistest-svn"]     = "basistest-svn";
00414     _build_dirs  ["basis.basistest-svn"]     = "/sbia/home/schuha/sandbox/build/basis/lib";
00415     _install_dirs["basis.basistest-svn"]     = "/sbia/home/schuha/sandbox/installs/basis/lib";
00416 
00417     // basistest-slave
00418     _exec_names  ["basis.basistest-slave"]   = "basistest-slave";
00419     _build_dirs  ["basis.basistest-slave"]   = "/sbia/home/schuha/sandbox/build/basis/lib";
00420     _install_dirs["basis.basistest-slave"]   = "/sbia/home/schuha/sandbox/installs/basis/lib";
00421 
00422     // basistest-master
00423     _exec_names  ["basis.basistest-master"]  = "basistest-master";
00424     _build_dirs  ["basis.basistest-master"]  = "/sbia/home/schuha/sandbox/build/basis/lib";
00425     _install_dirs["basis.basistest-master"]  = "/sbia/home/schuha/sandbox/installs/basis/lib";
00426 
00427     // basistest-cron
00428     _exec_names  ["basis.basistest-cron"]    = "basistest-cron";
00429     _build_dirs  ["basis.basistest-cron"]    = "/sbia/home/schuha/sandbox/build/basis/lib";
00430     _install_dirs["basis.basistest-cron"]    = "/sbia/home/schuha/sandbox/installs/basis/lib";
00431 
00432     // basistest
00433     _exec_names  ["basis.basistest"]         = "basistest";
00434     _build_dirs  ["basis.basistest"]         = "/sbia/home/schuha/sandbox/build/basis/bin";
00435     _install_dirs["basis.basistest"]         = "/sbia/home/schuha/sandbox/installs/basis/bin";
00436 
00437     // dummy_command
00438     _exec_names  ["basis.dummy_command"]     = "dummy_command";
00439     _build_dirs  ["basis.dummy_command"]     = "/sbia/home/schuha/sandbox/build/basis/Testing/bin";
00440     _install_dirs["basis.dummy_command"]     = "/sbia/home/schuha/sandbox/installs/basis/bin";
00441 
00442     // test_matlabtools
00443     _exec_names  ["basis.test_matlabtools"]  = "test_matlabtools";
00444     _build_dirs  ["basis.test_matlabtools"]  = "/sbia/home/schuha/sandbox/build/basis/Testing/bin";
00445     _install_dirs["basis.test_matlabtools"]  = "/sbia/home/schuha/sandbox/installs/basis/bin";
00446 
00447     // test_basisproject
00448     _exec_names  ["basis.test_basisproject"] = "test_basisproject";
00449     _build_dirs  ["basis.test_basisproject"] = "/sbia/home/schuha/sandbox/build/basis/Testing/bin";
00450     _install_dirs["basis.test_basisproject"] = "/sbia/home/schuha/sandbox/installs/basis/bin";
00451 
00452     // test_os
00453     _exec_names  ["basis.test_os"]           = "test_os";
00454     _build_dirs  ["basis.test_os"]           = "/sbia/home/schuha/sandbox/build/basis/Testing/bin";
00455     _install_dirs["basis.test_os"]           = "/sbia/home/schuha/sandbox/installs/basis/bin";
00456 
00457     // test_path
00458     _exec_names  ["basis.test_path"]         = "test_path";
00459     _build_dirs  ["basis.test_path"]         = "/sbia/home/schuha/sandbox/build/basis/Testing/bin";
00460     _install_dirs["basis.test_path"]         = "/sbia/home/schuha/sandbox/installs/basis/bin";
00461 
00462     // test_subprocess
00463     _exec_names  ["basis.test_subprocess"]   = "test_subprocess";
00464     _build_dirs  ["basis.test_subprocess"]   = "/sbia/home/schuha/sandbox/build/basis/Testing/bin";
00465     _install_dirs["basis.test_subprocess"]   = "/sbia/home/schuha/sandbox/installs/basis/bin";
00466 
00467     // test_core
00468     _exec_names  ["basis.test_core"]         = "test_core";
00469     _build_dirs  ["basis.test_core"]         = "/sbia/home/schuha/sandbox/build/basis/Testing/bin";
00470     _install_dirs["basis.test_core"]         = "/sbia/home/schuha/sandbox/installs/basis/bin";
00471 
00472     // test_shutilities
00473     _exec_names  ["basis.test_shutilities"]  = "test_shutilities";
00474     _build_dirs  ["basis.test_shutilities"]  = "/sbia/home/schuha/sandbox/build/basis/Testing/bin";
00475     _install_dirs["basis.test_shutilities"]  = "/sbia/home/schuha/sandbox/installs/basis/bin";
00476 
00477     // test_shtap
00478     _exec_names  ["basis.test_shtap"]        = "test_shtap";
00479     _build_dirs  ["basis.test_shtap"]        = "/sbia/home/schuha/sandbox/build/basis/Testing/bin";
00480     _install_dirs["basis.test_shtap"]        = "/sbia/home/schuha/sandbox/installs/basis/bin";
00481 
00482     // parseargs
00483     _exec_names  ["basis.parseargs"]         = "parseargs";
00484     _build_dirs  ["basis.parseargs"]         = "/sbia/home/schuha/sandbox/build/basis/Testing/bin";
00485     _install_dirs["basis.parseargs"]         = "/sbia/home/schuha/sandbox/installs/basis/bin";
00486 
00487     // test_utilities
00488     _exec_names  ["basis.test_utilities"]    = "test_utilities";
00489     _build_dirs  ["basis.test_utilities"]    = "/sbia/home/schuha/sandbox/build/basis/Testing/bin";
00490     _install_dirs["basis.test_utilities"]    = "/sbia/home/schuha/sandbox/installs/basis/bin";
00491 }
00492 
00493 
00494 } // end of namespaces