modbuild 7.46 KB
Newer Older
gsell's avatar
gsell committed
1 2
#!/bin/bash

gsell's avatar
gsell committed
3 4
#set -x

gsell's avatar
gsell committed
5 6
# The directory where the programm is installed will be added to PATH and
# to the search path of BASH libraries.
gsell's avatar
gsell committed
7 8 9 10 11 12 13 14
declare -r  mydir=$(dirname "$0")


declare -r  libpbuild='libpbuild.bash'
declare -r  libstd='libstd.bash'
declare -r  pmodule_environment='environment.bash'
declare -ra bash_libpath=("${mydir}" "${mydir}/../lib")

gsell's avatar
gsell committed
15

gsell's avatar
gsell committed
16 17 18 19 20 21 22 23 24 25 26 27 28
##############################################################################
#
# set an error handler. If a function _exit() exists, it will be called
# with the passed exit code.
#
# $1	exit code
#
set -o errexit

_exit() {
	:
}

gsell's avatar
gsell committed
29
error_handler() {
gsell's avatar
gsell committed
30 31 32 33 34 35
	local -i ec=$?

	_exit ${ec}
	exit ${ec}
}

gsell's avatar
gsell committed
36 37
trap "error_handler" ERR

gsell's avatar
gsell committed
38 39
# disable auto-echo feature of 'cd'
unset CDPATH
gsell's avatar
gsell committed
40

gsell's avatar
gsell committed
41 42 43
##############################################################################
#
usage() {
gsell's avatar
gsell committed
44
	std::error "
gsell's avatar
gsell committed
45
Usage: $0 [OPTIONS..] [BUILD_BLOCK] [VERSION]
gsell's avatar
gsell committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

VERSION
        Version of module to compile.

-? | -h | --help
        Print usage

-v | --verbose )
        Verbose output

-j N | --jobs=N
        Run N parallel make jobs

-f | --force-rebuild
        Force rebuild of module.

gsell's avatar
gsell committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
--dry-run
	Dry run.

--enable-cleanup-build
	Cleanup files in the build directory (default).

--enable-cleanup-src
	Cleanup files in the source directory.

--disable-cleanup
	Keep files in build and source directory

--disable-cleanup-build
	Keep files in build directory.

--disable-cleanup-src
	Keep files in source directory (default).

gsell's avatar
gsell committed
80 81 82 83 84 85
--with=P/V
        Preload module P with version V. To preload multiple modules,
        use this option per module. Nete that order may matter.

--release=stable|unstable|deprecated

gsell's avatar
gsell committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
--prep
	Unpack sources only.

--configure
	Unpack sources and configure build.

--compile
	Unpack sources, configure build and compile software

--install
	Unpack sources, configure build, compile software and install.

--all
	Do everything, this step includes cleanup files and setting 
	the symbolic link to the modulefile.
gsell's avatar
gsell committed
101 102 103 104
"
	exit 1
}

gsell's avatar
gsell committed
105
# setup PATH
gsell's avatar
gsell committed
106 107 108 109 110 111 112 113 114 115 116
PATH='/usr/bin:/bin:/usr/sbin:/sbin'

declare -r OS=$(uname -s)
if [[ "${OS}" == "Darwin" ]]; then
	# :FIXME: why do we need this?
	[[ -d "/opt/X11/bin" ]] && PATH+=':/opt/X11/bin'
fi

PATH+="${mydir}"


gsell's avatar
gsell committed
117 118 119 120 121 122 123 124
#
# We need GNU versions of the following utilities. This code works
# well on Linux and Mac OS X with MacPorts.
# :FIXME: implement a smarter, portable solution.
#
shopt -s expand_aliases
unalias -a

gsell's avatar
gsell committed
125
__path=$(which gdate 2>/dev/null || : )
gsell's avatar
gsell committed
126 127 128 129 130 131
if [[ $__path ]]; then
	alias date=$__path
else
	alias date=$(which date 2>/dev/null)
fi

gsell's avatar
gsell committed
132
__path=$(which ginstall 2>/dev/null || : )
gsell's avatar
gsell committed
133 134 135 136 137 138
if [[ $__path ]]; then
	alias install=$__path
else
	alias install=$(which install 2>/dev/null)
fi

gsell's avatar
gsell committed
139
__path=$(which greadlink 2>/dev/null || : )
gsell's avatar
gsell committed
140 141 142 143 144 145
if [[ $__path ]]; then
	alias readlink=$__path
else
	alias readlink=$(which readlink 2>/dev/null)
fi

gsell's avatar
gsell committed
146
__path=$(which gsed 2>/dev/null || : )
gsell's avatar
gsell committed
147 148 149 150 151 152
if [[ $__path ]]; then
	alias sed=$__path
else
	alias sed=$(which sed 2>/dev/null)
fi

gsell's avatar
gsell committed
153

gsell's avatar
gsell committed
154 155 156
declare	    ok=1

##############################################################################
gsell's avatar
gsell committed
157 158
# source BASH library with standard functions
for dir in "${bash_libpath[@]}"; do
gsell's avatar
gsell committed
159 160
        if [[ -r ${dir}/${libstd} ]]; then
		source "${dir}/${libstd}"
gsell's avatar
gsell committed
161 162 163 164
		ok=0
		break
	fi
done
gsell's avatar
gsell committed
165 166 167 168
if (( ok != 0 )); then
	echo "Oops: required BASH library '${libstd}' not found" 1>&2
	exit 1
fi
gsell's avatar
gsell committed
169 170

##############################################################################
gsell's avatar
gsell committed
171
#
gsell's avatar
gsell committed
172 173
# parse arguments
#
gsell's avatar
gsell committed
174 175 176 177 178
debug_on='no'
force_rebuild='no'
dry_run='no'
enable_cleanup_build='yes'
enable_cleanup_src='no'
gsell's avatar
gsell committed
179

gsell's avatar
gsell committed
180
target='all'
gsell's avatar
gsell committed
181

gsell's avatar
gsell committed
182 183
# array collecting all modules specified on the command line via '--with=module'
with_modules=()
gsell's avatar
gsell committed
184

gsell's avatar
gsell committed
185 186 187 188
# save arguments for building dependencies
declare -rx ARGS="$@"

# 
gsell's avatar
gsell committed
189 190 191 192 193 194 195 196 197
while (( $# > 0 )); do
	case $1 in
	-j )
		JOBS=$2
		shift
		;;
	--jobs=[0-9]* )
		JOBS=${1/--jobs=}
		;;
gsell's avatar
gsell committed
198
	-v | --verbose | --debug )
gsell's avatar
gsell committed
199 200 201 202 203
		debug_on='yes'
		;;
	-f | --force-rebuild )
		force_rebuild='yes'
		;;
gsell's avatar
gsell committed
204
	-\? | -h | --help )
gsell's avatar
gsell committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		usage
		;;
	--disable-cleanup )
		enable_cleanup_build='no'
		enable_cleanup_src='no'
		;;
	--enable-cleanup-build )
		enable_cleanup_build='yes'
		;;
	--disable-cleanup-build )
		enable_cleanup_build='no'
		;;
	--enable-cleanup-src )
		enable_cleanup_src='yes'
		;;
	--disable-cleanup-src )
		enable_cleanup_src='no'
		;;
	--dry-run )
		dry_run='yes'
		;;
	--release=* )
		MODULE_RELEASE=${1/--release=}
		;;
	--with=*/* )
		with_modules+=( ${1/--with=} )
		;;
gsell's avatar
gsell committed
232
	--prep | --configure | --compile | --install | --all )
gsell's avatar
gsell committed
233 234 235 236 237 238
		target=$1
		;;
	[0-9]* )
		V=$1
		;;
	* )
gsell's avatar
gsell committed
239 240
	        declare -rx BUILD_BLOCK=$(std::get_abspath "$1")
		declare -rx BUILD_BLOCK_DIR=$(dirname "${BUILD_BLOCK}")
gsell's avatar
gsell committed
241 242 243 244 245 246 247 248
		;;
	esac
	shift
done

if [[ ${debug_on} == yes ]]; then
	trap 'echo "$BASH_COMMAND"' DEBUG
fi
gsell's avatar
gsell committed
249

gsell's avatar
gsell committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
[[ -n ${BUILD_BLOCK} ]] || std::die 1 "No build-block specified!"
[[ -r ${BUILD_BLOCK} ]] || std::die 1 "${BUILD_BLOCK}: no such file!"



# source Pmodule environment configuration
if [[ -r ${BUILD_BLOCK_DIR}/../config/${pmodule_environment} ]]; then
	# we are bootstrapping
        source "${BUILD_BLOCK_DIR}/../config/${pmodule_environment}"
	declare -rx BUILD_BASEDIR=$(std::get_abspath "${BUILD_BLOCK_DIR}/..")
	bootstrap='yes'
	
elif [[ -n ${PMODULES_ROOT} ]] && [[ -n ${PMODULES_CONFIG_DIR} ]] && \
     [[ -r ${PMODULES_ROOT}/${PMODULES_CONFIG_DIR}/${pmodule_environment} ]]; then
	source ${PMODULES_ROOT}/${PMODULES_CONFIG_DIR}/${pmodule_environment}
	declare -rx BUILD_BASEDIR=$(std::get_abspath "${BUILD_BLOCK_DIR}/../../..")
	bootstrap='no'

else
	std::die 3 "Build environment not setup properbly!"
fi

declare -xr BUILD_CONFIGDIR="${BUILD_BASEDIR}/config"
declare -xr BUILD_SCRIPTSDIR="${BUILD_BASEDIR}/scripts"
declare -xr BUILD_TMPDIR="${BUILD_BASEDIR}/tmp"
declare -xr BUILD_DOWNLOADSDIR="${BUILD_BASEDIR}/Downloads"
declare -xr BUILD_VERSIONSFILE="${BUILD_CONFIGDIR}/versions.conf"


# source BASH library with standard functions
((ok=1))
for dir in "${bash_libpath[@]}"; do
        if [[ -r ${dir}/${libpbuild} ]]; then
		source "${dir}/${libpbuild}"
		ok=0
		break
	fi
done
(( ok == 0 )) || std::die 3 "Oops: required BASH library '${libpbuild}' not found"

# Unset all PATH's and FLAGS's which might be used by a compiler.
# This includes C_INCLUDE_PATH, CFLAGS etc.
pbuild::cleanup_env

gsell's avatar
gsell committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
# load all modules passed with the '--with' argument
if [[ ${bootstrap} == no ]]; then
	# we aren't bootstraping
	if [[ -r "${BUILD_BLOCK_DIR}/with_modules-$V" ]]; then
		with_modules+=( $(cat "${BUILD_BLOCK_DIR}/with_modules-$V") )
	elif [[ -r "${BUILD_BLOCK_DIR}/with_modules" ]]; then
		with_modules+=( $(cat "${BUILD_BLOCK_DIR}/with_modules") )
	fi

        source	"${PMODULES_ROOT}/${PMODULES_CONFIG_DIR}/profile.bash"
	MODULECMD="${PMODULES_HOME}/bin/modulecmd"
	[[ -x ${MODULECMD} ]] || std::die 1 "${MODULECMD}: no such executable"
	module purge
	module use unstable
	# :FIXME: this is a hack!!!
	module use Libraries
	for m in "${with_modules[@]}"; do
		if pbuild::module_is_available "$m"; then
			echo "Loading module: ${m}"
			module load "${m}"
		else
			std::die 44 "$m: module not available!"
		fi
	done
gsell's avatar
gsell committed
318
else
gsell's avatar
gsell committed
319 320 321 322
	#  the module command is not yet available...
	unset PMODULES_HOME
	unset PMODULES_VERSION
	std::read_versions "${BUILD_BASEDIR}/config/versions.conf"
gsell's avatar
gsell committed
323 324
fi

gsell's avatar
gsell committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
P=$(basename $(dirname "${BUILD_BLOCK}"))
P=${P%.*}
_P=$(echo $P | tr [:lower:] [:upper:])
_P=${_P//-/_}
_V=${_P}_VERSION

if [[ -n ${PMODULES_DEFINED_RELEASES} ]]; then
        declare -r releases="${PMODULES_DEFINED_RELEASES}"
else
	# set defaults, if file doesn't exist or isn't readable
	declare -r releases=":unstable:stable:deprecated:"
fi


#
# run build
#
gsell's avatar
gsell committed
342
source "${BUILD_BLOCK}"
gsell's avatar
gsell committed
343 344 345 346 347 348

# Local Variables:
# mode: sh
# sh-basic-offset: 8
# tab-width: 8
# End: