Go to the documentation of this file.00001 ##############################################################################
00002 # @file executabletargetinfo.sh
00003 # @brief Provides information about the executables built by BASIS.
00004 #
00005 # @note The executabletargetinfo.sh module is automatically created by
00006 # BASIS from the template file executabletargetinfo.sh.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
00015 # return if already loaded
00016 [ "${_SBIA_BASIS_EXECUTABLETARGETINFO_INCLUDED:-0}" -eq 1 ] && return 0
00017 _SBIA_BASIS_EXECUTABLETARGETINFO_INCLUDED=1
00018
00019
00020 # ============================================================================
00021 # constants
00022 # ============================================================================
00023
00024 ## @brief Absolute directory path of ExecutableTargetInfo.sh module.
00025 readonly _EXECUTABLETARGETINFO_DIR=$(cd -P -- "$(dirname -- "${BASH_SOURCE}")" && pwd -P)
00026
00027 # ============================================================================
00028 # modules
00029 # ============================================================================
00030
00031 source "${_EXECUTABLETARGETINFO_DIR}/./core.sh" # upvar()
00032
00033 # ============================================================================
00034 # public functions
00035 # ============================================================================
00036
00037 ## @addtogroup BasisBashUtilities
00038 # @{
00039
00040
00041 # ----------------------------------------------------------------------------
00042 ## @brief Get UID of build target.
00043 #
00044 # @param [out] uid UID of build target.
00045 # @param [in] target Name/UID of build target.
00046 #
00047 # @returns Nothing.
00048 #
00049 # @retval 0 On success.
00050 # @retval 1 On failure.
00051 function get_target_uid
00052 {
00053 [ -n "$1" ] && [ $# -eq 2 ] || return 1
00054 local target="$2"
00055 # initialize module if not done yet - this is only done here because
00056 # whenever information is looked up about an executable target, this
00057 # function is invoked first
00058 if [ -z "${_EXECUTABLETARGETINFO_INITIALIZED}" ]; then
00059 _executabletargetinfo_initialize || return 1
00060 fi
00061 # empty string as input remains unchanged
00062 [ -z "${target}" ] && local "$1" && upvar $1 '' && return 0
00063 # in case of a leading namespace separator, do not modify target name
00064 [ "${target:0:1}" == '.' ] && local "$1" && upvar $1 "${target}" && return 0
00065 # project namespace
00066 local prefix="sbia.basis.DUMMY"
00067 # try prepending namespace or parts of it until target is known
00068 local path=''
00069 while [ "${prefix/\.*/}" != "${prefix}" ]; do
00070 prefix="${prefix%\.*}"
00071 _executabletargetinfo_get path "${prefix}.${target}" LOCATION
00072 if [ -n "${path}" ]; then
00073 local "$1" && upvar $1 "${prefix}.${target}"
00074 return 0
00075 fi
00076 done
00077 # otherwise, return target name unchanged
00078 local "$1" && upvar $1 "${target}"
00079 }
00080
00081 # ----------------------------------------------------------------------------
00082 ## @brief Determine whether a given target is known.
00083 #
00084 # @param [in] target Name/UID of build target.
00085 #
00086 # @returns Whether the given build target is known by this module.
00087 function is_known_target
00088 {
00089 get_target_uid uid "$1"
00090 [ -n "${uid}" ] || return 1
00091 local path && _executabletargetinfo_get path "${uid}" LOCATION
00092 [ -n "${path}" ]
00093 }
00094
00095 # ----------------------------------------------------------------------------
00096 ## @brief Get name of executable file.
00097 #
00098 # @param [out] name Name of the executable or an empty string if unknown.
00099 # @param [in] target Name/UID of build target. If no argument is given,
00100 # the name of the calling executable is returned instead.
00101 #
00102 # @returns Nothing.
00103 #
00104 # @retval 0 On success.
00105 # @retval 1 On failure.
00106 function get_executable_name
00107 {
00108 [ -n "$1" ] && [ $# -eq 1 -o $# -eq 2 ] || return 1
00109
00110 local name
00111
00112 # if no target name given, get name of this executable
00113 if [ $# -lt 2 ]; then
00114 name="${0##*/}"
00115 # otherwise, get name of executable built by named target
00116 else
00117 local path && get_executable_path path "$2"
00118 [ $? -eq 0 ] || return 1
00119 name="`basename "${path}"`"
00120 [ $? -eq 0 ] || return 1
00121 fi
00122
00123 local "$1" && upvar $1 "${name}"
00124 }
00125
00126 # ----------------------------------------------------------------------------
00127 ## @brief Get directory of executable file.
00128 #
00129 # @param [out] dir Absolute directory of executable file.
00130 # @param [in] target Name/UID of build target. If no argument is given,
00131 # the directory where the file of the calling executable
00132 # is located is returned.
00133 #
00134 # @returns Nothing.
00135 #
00136 # @retval 0 On success.
00137 # @retval 1 On failure.
00138 function get_executable_directory
00139 {
00140 [ -n "$1" ] && [ $# -eq 1 -o $# -eq 2 ] || return 1
00141
00142 local path
00143
00144 # if no target name given, get path of this executable
00145 if [ $# -lt 2 ]; then
00146 get_executable_path path
00147 # otherwise, get path of executable of named target
00148 else
00149 get_executable_path path "$2"
00150 fi
00151 [ $? -eq 0 ] || return 1
00152 # get directory component of path
00153 local dir="`dirname "${path}"`"
00154
00155 local "$1" && upvar $1 "${dir}"
00156 }
00157
00158 # ----------------------------------------------------------------------------
00159 ## @brief Get absolute path of executable file.
00160 #
00161 # This function determines the absolute file path of an executable. If no
00162 # arguments are given, the absolute path of this executable is returned.
00163 # If the given argument is a known build target name, the absolute path
00164 # of the executable built by this target is returned. Otherwise, the named
00165 # command is searched in the system PATH and it's absolute path returned
00166 # if found. If the given argument is neither the name of a known build target
00167 # nor an executable found on the PATH, an empty string is returned and
00168 # the return value is 1.
00169 #
00170 # @param [out] path Absolute path of executable file.
00171 # @param [in] target Name/UID of build target. If no argument is given,
00172 # the file path of the calling executable is returned.
00173 #
00174 # @returns Nothing.
00175 #
00176 # @retval 0 On success.
00177 # @retval 1 On failure.
00178 function get_executable_path
00179 {
00180 [ -n "$1" ] && [ $# -eq 1 -o $# -eq 2 ] || return 1
00181
00182 local path
00183
00184 # if no target name given, get path of this executable
00185 if [ $# -lt 2 ]; then
00186 path="`realpath "$0"`"
00187 # otherwise, get path of executable built by named target
00188 else
00189 # get UID of target
00190 local uid && get_target_uid uid "$2"
00191 [ "${uid:0:1}" == '.' ] && uid=${uid:1}
00192 if [ -n "${uid}" ]; then
00193 # get path relative to this module
00194 _executabletargetinfo_get path "${uid}" LOCATION
00195 if [ -n "${path}" ]; then
00196 # make path absolute
00197 _executabletargetinfo_to_absolute_path path "${path}" "${_EXECUTABLETARGETINFO_DIR}"
00198 [ $? -eq 0 ] || return 1
00199 else
00200 path=`/usr/bin/which "$2" 2> /dev/null`
00201 fi
00202 else
00203 path=`/usr/bin/which "$2" 2> /dev/null`
00204 fi
00205 fi
00206
00207 local "$1" && upvar $1 "${path}"
00208 [ $? -eq 0 ] && [ -n "${path}" ]
00209 }
00210
00211
00212 ## @}
00213 # end of Doxygen group
00214
00215 # ============================================================================
00216 # private helpers
00217 # ============================================================================
00218
00219 # ----------------------------------------------------------------------------
00220 ## @brief Sanitize string for use in variable name.
00221 #
00222 # @param [out] out Sanitized string.
00223 # @param [in] str String to be sanitized.
00224 #
00225 # @returns Nothing.
00226 #
00227 # @retval 0 On success.
00228 # @retval 1 On failure.
00229 function _executabletargetinfo_sanitize
00230 {
00231 [ $# -eq 2 ] || return 1
00232 [ -n "$2" ] || {
00233 upvar $1 ''
00234 return 0
00235 }
00236 local sane="`echo -n "$2" | tr [:space:] '_' | tr -c [:alpha:] '_'`"
00237 [ -n "${sane}" ] || {
00238 echo "_executabletargetinfo_sanitize(): Failed to sanitize string '$2'" 1>&2
00239 exit 1
00240 }
00241 local "$1" && upvar $1 "${sane}"
00242 }
00243
00244 # ----------------------------------------------------------------------------
00245 ## @brief Add (key, value) pair to executable target info "hash".
00246 #
00247 # @sa _executabletargetinfo_get()
00248 #
00249 # @param [in] key Hash key.
00250 # @param [in] name Name of the hash table.
00251 # @param [in] value Value associated with the given hash key.
00252 #
00253 # @returns Sets a readonly variable that represents the (key, value) entry.
00254 function _executabletargetinfo_add
00255 {
00256 [ $# -eq 3 ] || return 1
00257
00258 local key && _executabletargetinfo_sanitize key "$1"
00259 local name && _executabletargetinfo_sanitize name "$2"
00260 [ -n "${key}" ] && [ -n "${name}" ] || {
00261 if [ -z "${key}" ] && [ -z "${name}" ]; then
00262 echo "_executabletargetinfo_add(): Neither lookup table nor key specified" 1>&2
00263 elif [ -z "${key}" ]; then
00264 echo "_executabletargetinfo_add(): No key specified for addition to hash table '${name}'" 1>&2
00265 else
00266 echo "_executabletargetinfo_add(): No lookup table given for addition of key '${key}'" 1>&2
00267 fi
00268 exit 1
00269 }
00270 eval "readonly __EXECUTABLETARGETINFO_${name}_${key}='$3'"
00271 }
00272
00273 # ----------------------------------------------------------------------------
00274 ## @brief Get value from executable target info "hash".
00275 #
00276 # @sa _executabletargetinfo_add()
00277 #
00278 # @param [out] value Value corresponding to given @p key
00279 # or an empty string if key is unknown.
00280 # @param [in] key Hash key.
00281 # @param [in] name Name of the hash table.
00282 #
00283 # @returns Nothing.
00284 #
00285 # @retval 0 On success.
00286 # @retval 1 On failure.
00287 function _executabletargetinfo_get
00288 {
00289 [ $# -eq 3 ] || return 1
00290
00291 local key && _executabletargetinfo_sanitize key "$2"
00292 local name && _executabletargetinfo_sanitize name "$3"
00293 [ -n "${key}" ] && [ -n "${name}" ] || {
00294 if [ -z "${key}" ] && [ -z "${name}" ]; then
00295 echo "_executabletargetinfo_get(): Neither lookup table nor key specified" 1>&2
00296 elif [ -z "${key}" ]; then
00297 echo "_executabletargetinfo_get(): No key specified for lookup in hash table '${name}'" 1>&2
00298 else
00299 echo "_executabletargetinfo_get(): No lookup table given for lookup of key '${key}'" 1>&2
00300 fi
00301 exit 1
00302 }
00303 eval "local value=\${__EXECUTABLETARGETINFO_${name}_${key}}"
00304
00305 local "$1" && upvar $1 "${value}"
00306 }
00307
00308 # ----------------------------------------------------------------------------
00309 ## @brief Make relative path absolute.
00310 #
00311 # @attention The directory component of the path must be an existent directory.
00312 #
00313 # @todo Implement this function without the use of cd such that non-existent
00314 # paths can be simplified as well.
00315 #
00316 # @param [out] simplified Simplified @p path.
00317 # @param [in] path Absolute or relative path.
00318 # @param [in] base Base path used for relative paths.
00319 #
00320 # @returns Nothing.
00321 #
00322 # @retval 0 On success.
00323 # @retval 1 On failure.
00324 function _executabletargetinfo_to_absolute_path
00325 {
00326 [ $# -eq 2 ] || [ $# -eq 3 ] || return 1
00327
00328 local path="$2"
00329 local base="$3"
00330
00331 [ -n "${path}" ] || return 1
00332
00333 local dir=`dirname ${path}`
00334
00335 if [ -d "${base}" -a -d "${base}/${dir}" ]; then
00336 dir=`cd ${base}; cd ${dir}; pwd`
00337 [ $? -eq 0 ] || return 1
00338 path=${dir}/`basename ${path}`
00339 [ $? -eq 0 ] || return 1
00340 fi
00341
00342 local "$1" && upvar $1 "${path}"
00343 }
00344
00345 # ----------------------------------------------------------------------------
00346 ## @brief Initialize executable target information.
00347 #
00348 # This function initializes the structures of information about the executable
00349 # build targets. If this function is executed another time, it does nothing
00350 # because the structures are initialized already. In order to reduce the start
00351 # time of applications that do not make use of this module, the initialization
00352 # is only performed on demand.
00353 #
00354 # The initialization is done in get_target_uid() as this function is always
00355 # called first before any lookup of information.
00356 #
00357 # @returns Nothing.
00358 #
00359 # @retval 0 On success.
00360 # @retval 1 On failure.
00361 function _executabletargetinfo_initialize
00362 {
00363 [ $# -eq 0 ] || return 1
00364
00365 _executabletargetinfo_add 'sbia.basis.basisproject' LOCATION '../bin/basisproject'
00366 _executabletargetinfo_add 'sbia.basis.which' LOCATION '../bin/which'
00367 _executabletargetinfo_add 'sbia.basis.doxyfilter' LOCATION 'doxyfilter'
00368 _executabletargetinfo_add 'sbia.basis.doxyfilter-matlab' LOCATION 'doxyfilter-matlab'
00369 _executabletargetinfo_add 'sbia.basis.doxyfilter-bash' LOCATION 'doxyfilter-bash'
00370 _executabletargetinfo_add 'sbia.basis.doxyfilter-cmake' LOCATION 'doxyfilter-cmake'
00371 _executabletargetinfo_add 'sbia.basis.doxyfilter-python' LOCATION 'doxyfilter-python'
00372 _executabletargetinfo_add 'sbia.basis.testdriver' LOCATION '../bin/basistest-driver'
00373 _executabletargetinfo_add 'sbia.basis.basistest-svn' LOCATION '../bin/basistest-svn'
00374 _executabletargetinfo_add 'sbia.basis.basistest-slave' LOCATION '../bin/basistest-slave'
00375 _executabletargetinfo_add 'sbia.basis.basistest-master' LOCATION '../bin/basistest-master'
00376 _executabletargetinfo_add 'sbia.basis.basistest-cron' LOCATION '../bin/basistest-cron'
00377 _executabletargetinfo_add 'sbia.basis.basistest' LOCATION '../bin/basistest'
00378 _executabletargetinfo_add 'sbia.basis.make_html_verbatim' LOCATION '../bin/make_html_verbatim'
00379 _executabletargetinfo_add 'sbia.basis.dummy_command' LOCATION '../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/dummy_command'
00380 _executabletargetinfo_add 'sbia.basis.test_matlabtools' LOCATION '../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_matlabtools'
00381 _executabletargetinfo_add 'sbia.basis.test_basisproject' LOCATION '../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_basisproject'
00382 _executabletargetinfo_add 'sbia.basis.test_path' LOCATION '../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_path'
00383 _executabletargetinfo_add 'sbia.basis.test_subprocess' LOCATION '../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_subprocess'
00384 _executabletargetinfo_add 'sbia.basis.test_core' LOCATION '../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_core'
00385 _executabletargetinfo_add 'sbia.basis.test_shtap' LOCATION '../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_shtap'
00386 _executabletargetinfo_add 'sbia.basis.parseargs' LOCATION '../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/parseargs'
00387 _executabletargetinfo_add 'sbia.basis.test_utilities' LOCATION '../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_utilities'
00388
00389 _EXECUTABLETARGETINFO_INITIALIZED=1
00390 return 0
00391 }
00392
00393 # ============================================================================
00394 # aliases (optional)
00395 # ============================================================================
00396
00397 if [ -z "${use_executable_target_aliases}" ] || [ ${use_executable_target_aliases} -eq 0 ]; then
00398 return
00399 fi
00400
00401 # define aliases of build target names to executable file paths
00402 alias 'sbia.basis.basisproject'=`get_executable_path 'sbia.basis.basisproject'`
00403 alias 'sbia.basis.which'=`get_executable_path 'sbia.basis.which'`
00404 alias 'sbia.basis.doxyfilter'=`get_executable_path 'sbia.basis.doxyfilter'`
00405 alias 'sbia.basis.doxyfilter-matlab'=`get_executable_path 'sbia.basis.doxyfilter-matlab'`
00406 alias 'sbia.basis.doxyfilter-bash'=`get_executable_path 'sbia.basis.doxyfilter-bash'`
00407 alias 'sbia.basis.doxyfilter-cmake'=`get_executable_path 'sbia.basis.doxyfilter-cmake'`
00408 alias 'sbia.basis.doxyfilter-python'=`get_executable_path 'sbia.basis.doxyfilter-python'`
00409 alias 'sbia.basis.testdriver'=`get_executable_path 'sbia.basis.testdriver'`
00410 alias 'sbia.basis.basistest-svn'=`get_executable_path 'sbia.basis.basistest-svn'`
00411 alias 'sbia.basis.basistest-slave'=`get_executable_path 'sbia.basis.basistest-slave'`
00412 alias 'sbia.basis.basistest-master'=`get_executable_path 'sbia.basis.basistest-master'`
00413 alias 'sbia.basis.basistest-cron'=`get_executable_path 'sbia.basis.basistest-cron'`
00414 alias 'sbia.basis.basistest'=`get_executable_path 'sbia.basis.basistest'`
00415 alias 'sbia.basis.make_html_verbatim'=`get_executable_path 'sbia.basis.make_html_verbatim'`
00416 alias 'sbia.basis.dummy_command'=`get_executable_path 'sbia.basis.dummy_command'`
00417 alias 'sbia.basis.test_matlabtools'=`get_executable_path 'sbia.basis.test_matlabtools'`
00418 alias 'sbia.basis.test_basisproject'=`get_executable_path 'sbia.basis.test_basisproject'`
00419 alias 'sbia.basis.test_path'=`get_executable_path 'sbia.basis.test_path'`
00420 alias 'sbia.basis.test_subprocess'=`get_executable_path 'sbia.basis.test_subprocess'`
00421 alias 'sbia.basis.test_core'=`get_executable_path 'sbia.basis.test_core'`
00422 alias 'sbia.basis.test_shtap'=`get_executable_path 'sbia.basis.test_shtap'`
00423 alias 'sbia.basis.parseargs'=`get_executable_path 'sbia.basis.parseargs'`
00424 alias 'sbia.basis.test_utilities'=`get_executable_path 'sbia.basis.test_utilities'`
00425
00426 # define short aliases for this project's targets
00427
00428
00429 # enable expansion of aliases also in non-interactive shells
00430 shopt -s expand_aliases