00001 ##############################################################################
00002 # @file ExecutableTargetInfo.pm
00003 # @brief Provides information about executables built by BASIS.
00004 #
00005 # @note The ExecutableTargetInfo.pm module is automatically created by
00006 # BASIS from the template file ExecutableTargetInfo.pm.in which is
00007 # part of the BASIS installation.
00008 #
00009 # Copyright (c) 2011 University of Pennsylvania. All rights reserved.<br />
00010 # See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.
00011 #
00012 # Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00013 #
00014 # @ingroup BasisPerlUtilities
00015 ##############################################################################
00016
00017 package SBIA::BASIS::ExecutableTargetInfo;
00018
00019 use strict;
00020 use warnings;
00021
00022 # ============================================================================
00023 # exports
00024 # ============================================================================
00025
00026 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
00027
00028 BEGIN {
00029 use Exporter ();
00030
00031 $VERSION = 1.02_03;
00032 @ISA = qw (Exporter);
00033
00034 %EXPORT_TAGS = (
00035 default => [qw (
00036 get_executable_name
00037 get_executable_directory
00038 get_executable_path
00039 )],
00040
00041 everything => [qw (
00042 get_executable_name
00043 get_executable_directory
00044 get_executable_path
00045 get_target_uid
00046 is_known_target
00047 )]
00048 );
00049
00050 Exporter::export_ok_tags ('everything');
00051 }
00052
00053 # ============================================================================
00054 # modules
00055 # ============================================================================
00056
00057 use Cwd qw(realpath);
00058 use File::Basename qw(dirname basename);
00059 use File::Spec qw(rel2abs);
00060
00061 use SBIA::BASIS::File::Which;
00062
00063 # ============================================================================
00064 # private data
00065 # ============================================================================
00066
00067 ## @brief Maps build target names to executable file paths relative to module.
00068 my $_location = undef;
00069
00070 # ============================================================================
00071 # public functions
00072 # ============================================================================
00073
00074 ## @addtogroup BasisPerlUtilities
00075 # @{
00076
00077
00078 # ----------------------------------------------------------------------------
00079 ## @brief Get UID of build target.
00080 #
00081 # This function prepends the default namespace of used for targets build as
00082 # part of the project this module belongs to if the given target name is yet
00083 # neither known nor fully-qualified, i.e., in the form <namespace>::<target>.
00084 #
00085 # @note This function does not check whether the returned fully-qualified
00086 # target name is known to this class.
00087 #
00088 # @param [in] targe Name of build target.
00089 #
00090 # @returns Fully-qualified build target name.
00091 sub get_target_uid
00092 {
00093 my $target = $_[0];
00094 # initialize module if not done yet - this is only done here because
00095 # whenever information is looked up about an executable target, this
00096 # function is invoked first
00097 defined $_location or initialize ();
00098 # handle invalid arguments
00099 defined $target or return undef;
00100 length ($target) > 0 or return undef;
00101 # in case of a leading namespace separator, do not modify target name
00102 $target !~ /^\./ or return $target;
00103 # project namespace
00104 my $prefix='sbia.basis.DUMMY';
00105 # try prepending namespace or parts of it until target is known
00106 while ($prefix =~ s/(.*)\.[^.]*/$1/) {
00107 if (exists $_location->{$prefix . '.' . $target}) {
00108 return $prefix . '.' . $target;
00109 }
00110 }
00111 # otherwise, return target name unchanged
00112 return $target;
00113 }
00114
00115 # ----------------------------------------------------------------------------
00116 ## @brief Determine whether a given target is known.
00117 #
00118 # @param [in] target Name/UID of build target.
00119 #
00120 # @returns Whether the given build target is known by this module.
00121 sub is_known_target
00122 {
00123 my $uid = get_target_uid ($_[0]);
00124 defined $uid or return 0;
00125 $uid =~ s/^[.]?(.*)/$1/;
00126 exists $_location->{$uid};
00127 }
00128
00129 # ----------------------------------------------------------------------------
00130 ## @brief Get name part of executable file path.
00131 #
00132 # @param [in] target Name/UID of build target.
00133 #
00134 # @returns Name part of executable file path.
00135 sub get_executable_name
00136 {
00137 my $path = get_executable_path (@_);
00138 defined $path or return undef;
00139 return basename ($path);
00140 }
00141
00142 # ----------------------------------------------------------------------------
00143 ## @brief Get directory part of executable file path.
00144 #
00145 # @param [in] target Name/UID of build target.
00146 #
00147 # @returns Directory part of executable file path.
00148 sub get_executable_directory
00149 {
00150 my $path = get_executable_path (@_);
00151 defined $path or return undef;
00152 return dirname ($path);
00153 }
00154
00155 # ----------------------------------------------------------------------------
00156 ## @brief Get absolute path of executable file.
00157 #
00158 # This function determines the absolute file path of an executable. If no
00159 # arguments are given, the absolute path of this executable is returned.
00160 # If the given argument is a known build target name, the absolute path
00161 # of the executable built by this target is returned. Otherwise, the named
00162 # command is searched in the system PATH and it's absolute path returned
00163 # if found. If the given argument is neither the name of a known build target
00164 # nor an executable found on the PATH, undef is returned.
00165 #
00166 # @param [in] target Name/UID of build target.
00167 #
00168 # @returns Absolute file path of executable.
00169 sub get_executable_path
00170 {
00171 my $path = undef;
00172 if (@_ == 0) {
00173 $path = realpath ($0);
00174 } else {
00175 my $uid = get_target_uid ($_[0]);
00176 defined $uid and $uid =~ s/^[.]?(.*)/$1/;
00177 if (defined $uid and exists $_location->{$uid}) {
00178 $path = $_location->{$uid};
00179 if ($path =~ m/\$\(IntDir\)/) {
00180 my $tmppath = '';
00181 my $intdir = '';
00182 foreach $intdir ('Release', 'Debug', 'RelWithDebInfo', 'MinSizeRel') {
00183 $tmppath = $path;
00184 $tmppath =~ s/\$\(IntDir\)/$intdir/g;
00185 if (-e $tmppath) {
00186 $path = $tmppath;
00187 last;
00188 }
00189 }
00190 $path =~ s/\$\(IntDir\)
00191 }
00192 $path = File::Spec->rel2abs ($path, dirname (__FILE__));
00193 # the realpath() function only works for existing paths
00194 $path = realpath ($path) if -e $path;
00195 } else {
00196 $path = which ($_[0])
00197 }
00198 }
00199 return $path;
00200 }
00201
00202
00203 ## @}
00204 # end of Doxygen group
00205
00206 # ============================================================================
00207 # protected/private functions
00208 # ============================================================================
00209
00210 # ----------------------------------------------------------------------------
00211 ## @brief Initialize executable target information.
00212 #
00213 # This function initializes the structures of information about the executable
00214 # build targets. In order to reduce the start-up time of applications that do
00215 # not make use of this module, the initialization is only performed on demand.
00216 #
00217 # The initialization is done in get_target_uid() as this function is always
00218 # called first before any lookup of information.
00219 #
00220 # @returns Nothing.
00221 sub initialize
00222 {
00223 $_location = {
00224 'sbia.basis.basisproject' => '../../../../bin/basisproject',
00225 'sbia.basis.which' => '../../../../bin/which',
00226 'sbia.basis.doxyfilter' => '../../../doxyfilter',
00227 'sbia.basis.doxyfilter-matlab' => '../../../doxyfilter-matlab',
00228 'sbia.basis.doxyfilter-bash' => '../../../doxyfilter-bash',
00229 'sbia.basis.doxyfilter-cmake' => '../../../doxyfilter-cmake',
00230 'sbia.basis.doxyfilter-python' => '../../../doxyfilter-python',
00231 'sbia.basis.testdriver' => '../../../../bin/basistest-driver',
00232 'sbia.basis.basistest-svn' => '../../../../bin/basistest-svn',
00233 'sbia.basis.basistest-slave' => '../../../../bin/basistest-slave',
00234 'sbia.basis.basistest-master' => '../../../../bin/basistest-master',
00235 'sbia.basis.basistest-cron' => '../../../../bin/basistest-cron',
00236 'sbia.basis.basistest' => '../../../../bin/basistest',
00237 'sbia.basis.make_html_verbatim' => '../../../../bin/make_html_verbatim',
00238 'sbia.basis.dummy_command' => '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/dummy_command',
00239 'sbia.basis.test_matlabtools' => '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_matlabtools',
00240 'sbia.basis.test_basisproject' => '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_basisproject',
00241 'sbia.basis.test_path' => '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_path',
00242 'sbia.basis.test_subprocess' => '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_subprocess',
00243 'sbia.basis.test_core' => '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_core',
00244 'sbia.basis.test_shtap' => '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_shtap',
00245 'sbia.basis.parseargs' => '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/parseargs',
00246 'sbia.basis.test_utilities' => '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_utilities'
00247 };
00248 }
00249
00250
00251 1; # indicate success of module loading