libpmodules.bash 19.9 KB
Newer Older
1
#!/bin/bash
gsell's avatar
gsell committed
2

3
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
gsell's avatar
gsell committed
4

5 6 7
# disable auto-echo feature of 'cd'
unset CDPATH

8
shopt -s expand_aliases
gsell's avatar
gsell committed
9 10
declare -rx ARGS="$@"

11 12


13 14
declare -r  BUILDSCRIPT_DIR=$( cd $(dirname "$0") && pwd )
declare -r  BUILDSCRIPT="${BUILDSCRIPT_DIR}"/$(basename "$0")
15
declare -rx SHLIBDIR=$( cd $(dirname "$BASH_SOURCE") && pwd )
gsell's avatar
gsell committed
16 17 18 19 20 21 22 23
source "${SHLIBDIR}/lib.bash"

declare -rx BUILD_BASEDIR=$(abspath $SHLIBDIR/..)





24
declare -r OS=$(uname -s)
gsell's avatar
gsell committed
25

26
# number of parallel make jobs
27
declare -i  JOBS=3
gsell's avatar
gsell committed
28

29 30 31 32 33 34 35 36
source "$(readlink ${BUILD_BASEDIR}/config/environment.bash)"

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"

gsell's avatar
gsell committed
37
source "${BUILD_CONFIGDIR}/Pmodules.conf"
38 39 40

declare -x  PREFIX=''
declare -x  DOCDIR=''
gsell's avatar
gsell committed
41
declare -x  MODULE_GROUP=''
42
declare	-x  MODULE_RELEASE=''
43 44 45
declare     cur_module_release=''

declare     DEPEND_RELEASE=''
46 47 48 49 50 51 52 53 54
declare -x  MODULE_NAME=''

# these directories are module dependend
declare -x  MODULE_SRCDIR=''
declare -x  MODULE_BUILDDIR=''

declare -x  MODULE_BUILD_DEPENDENCIES
declare -x  MODULE_DEPENDENCIES

55 56 57 58 59 60
declare -x  C_INCLUDE_PATH
declare -x  CPLUS_INCLUDE_PATH
declare -x  CPP_INCLUDE_PATH
declare -x  LIBRARY_PATH
declare -x  LD_LIBRARY_PATH
declare -x  DYLD_LIBRARY_PATH
61

62 63
##############################################################################
#
gsell's avatar
gsell committed
64
usage() {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	error "
Usage: $0 [OPTIONS..] [VERSION] [ENV=VALUE...]

VERSION
        Version of module to compile.

ENV=VALUE
        Set environment variable ENV to VALUE. This can be used to
        overwrite default versions.

-? | -h | --help
        Print usage

-v | --verbose )
        Verbose output

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

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

87 88 89
-b | --bootstrap 
	Bootstrap Pmodules

90 91 92
--with=P/V
        Preload module P with version V. To preload multiple modules,
        use this option per module. Nete that order may matter.
93

94
--release=stable|unstable|deprecated
95 96 97 98 99

"
	exit 1
}

100 101 102 103 104 105
##############################################################################
#
# test whether a the string passed in $1 is a valid release name
#
# $1: string to be tested
#
106 107
is_release () {
	[[ ${releases} =~ :$1: ]]
108
}
109

110 111
##############################################################################
#
gsell's avatar
gsell committed
112
# set supported OS
113
#
gsell's avatar
gsell committed
114
# $1: OS (as printed by 'uname -s')
115
#
gsell's avatar
gsell committed
116
pmodules.supported_os() {
117 118
	for os in "$@"; do
		[[ ${os} == ${OS} ]] && return 0
gsell's avatar
gsell committed
119
	done
120
	die 0 "${P}: Not available for ${OS}."
gsell's avatar
gsell committed
121 122
}

gsell's avatar
gsell committed
123 124 125 126 127 128 129
##############################################################################
#
# install module in given group
#
# $1: group
#
pmodules.add_to_group() {
gsell's avatar
gsell committed
130
	if [[ -z ${1} ]]; then
gsell's avatar
gsell committed
131
		die 42 "${FUNCNAME}: Missing group argument."
gsell's avatar
gsell committed
132
	fi
gsell's avatar
gsell committed
133 134
	if [[ ! -d ${PMODULES_ROOT}/${PMODULES_TEMPLATES_DIR}/${1} ]]; then
		die 43 "${1}: group does not exist."
gsell's avatar
gsell committed
135
	fi
gsell's avatar
gsell committed
136 137 138
	MODULE_GROUP=$1
}

gsell's avatar
gsell committed
139 140 141 142 143 144 145
##############################################################################
#
# install module in given group
#
# $1: group
#
pmodules.set_build_dependencies() {
146
	MODULE_BUILD_DEPENDENCIES=("$@")
147 148
}

gsell's avatar
gsell committed
149 150
pmodules.set_docfiles() {
	MODULE_DOCFILES=("$@")
gsell's avatar
gsell committed
151 152
}

gsell's avatar
gsell committed
153 154
module_is_available() {
	[[ -n $("${MODULECMD}" bash avail "$1" 2>&1 1>/dev/null) ]]
gsell's avatar
gsell committed
155 156
}

gsell's avatar
gsell committed
157 158 159 160 161 162 163 164
##############################################################################
#
# test whether a module is loaded or not
#
# $1: module name
#
is_loaded() {
        [[ :${LOADEDMODULES}: =~ :$1: ]]
gsell's avatar
gsell committed
165 166
}

167

gsell's avatar
gsell committed
168
load_build_dependencies() {
169
	for m in "${MODULE_BUILD_DEPENDENCIES[@]}"; do
170 171
		[[ -z $m ]] && continue
		if [[ ! $m =~ "*/*" ]]; then
gsell's avatar
gsell committed
172 173 174 175
			local _V=$(echo -n $m | tr [:lower:] [:upper:] )_VERSION
			if [[ -n ${!_V} ]]; then
		    		m=$m/${!_V}
			else
gsell's avatar
gsell committed
176
				echo "$m: warning: No version set, loading default ..."
gsell's avatar
gsell committed
177
			fi
178
		fi
gsell's avatar
gsell committed
179
		is_loaded "$m" && continue
180 181
		if ! module_is_available "$m"; then
		        debug "${m}: module not available"
gsell's avatar
gsell committed
182
			local rels=( ${releases//:/ } )
183 184 185 186 187
			for rel in "${rels[@]}"; do
				debug "${m}: check release \"${rel}\""
				eval $("${MODULECMD}" bash use ${rel})
				if module_is_available "${m}"; then
					die 1 "${m}: module available with release \"${rel}\", add this release with \"module use ${rel}\" and re-run build script."
188 189
				fi
			done
190 191 192
			[[ ${dry_run} == yes ]] && {
				die 1 "${m}: module does not exist, cannot continue with dry run..."
				}
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

			echo "$m: module does not exist, trying to build it..."
			local args=( '' )
			set -- ${ARGS[@]}
			while (( $# > 0 )); do
				case $1 in
				-j )
					args+=( "-j $2" )
					shift
					;;
				--jobs=[0-9]* )
					args+=( $1 )
					;;
				-v | --verbose)
					args+=( $1 )
					;;
				--release=* )
					args+=( $1 )
					;;
				--with=*/* )
					args+=( $1 )
					;;
				*=* )
					args+=( $1 )
					;;
				esac
				shift
			done
221
			"${BUILD_SCRIPTSDIR}"/*/"${m/\/*}/build" ${args[@]}
222
			if [[ -z $(module avail "$m" 2>&1) ]]; then
gsell's avatar
gsell committed
223
				die 1 "$m: oops: build failed..."
224 225
			fi
		fi
gsell's avatar
gsell committed
226 227
		# :FIXME: this doesn't work any more!
		local modulepath_root="${PMODULES_ROOT}/${PMODULES_MODULEFILES_DIR}"
228 229
		local tmp=$( module display "${m}" 2>&1 | grep -m1 -- "${modulepath_root}" )
		tmp=${tmp/${modulepath_root}\/}
230 231 232
		tmp=${tmp%%/*}
		local _family=( ${tmp//./ } )
		if [[ ${_family[1]} == deprecated ]]; then
233 234
			# set module release to 'deprecated' if a build dependency
			# is deprecated
235
			DEPEND_RELEASE='deprecated'
236
		elif [[ ${_family[1]} == unstable ]] && [[ -z ${DEPEND_RELEASE} ]]; then
237 238
			# set module release to 'unstable' if a build dependency is
			# unstable and release not yet set
239
			DEPEND_RELEASE='unstable'
240
		fi
gsell's avatar
gsell committed
241 242

		
243 244
		echo "Loading module: ${m}"
		module load "${m}"
245 246 247
	done
}

248
if [[ ${bootstrap} == yes ]]; then
gsell's avatar
gsell committed
249
    load_build_dependencies() {
250 251 252 253
	    :
    }
fi

gsell's avatar
gsell committed
254
write_runtime_dependencies() {
gsell's avatar
gsell committed
255
	local -r fname="${PREFIX}/.dependencies"
256
	info "Writing run-time dependencies to ${fname}"
gsell's avatar
gsell committed
257 258
	local dep
	echo -n "" > "${fname}"
259
	for dep in "${MODULE_DEPENDENCIES[@]}"; do
gsell's avatar
gsell committed
260
		[[ -z $dep ]] && continue
261
		if [[ ! $dep =~ .*/.* ]]; then
262
		    local _V=$(echo -n $dep | tr [:lower:] [:upper:] )_VERSION
gsell's avatar
gsell committed
263 264 265 266 267 268
		    dep=$dep/${!_V}
		fi
		echo "${dep}" >> "${fname}"
	done
}

gsell's avatar
gsell committed
269
write_build_dependencies() {
gsell's avatar
gsell committed
270
	local -r fname="${PREFIX}/.build_dependencies"
271
	info "Writing build dependencies to ${fname}"
gsell's avatar
gsell committed
272 273
	local dep
	echo -n "" > "${fname}"
274
	for dep in "${MODULE_BUILD_DEPENDENCIES[@]}"; do
gsell's avatar
gsell committed
275 276
		[[ -z $dep ]] && continue
		if [[ ! $dep =~ "*/*" ]]; then
277
		    local _V=$(echo -n $dep | tr [:lower:] [:upper:] )_VERSION
gsell's avatar
gsell committed
278 279 280 281 282 283
		    dep=$dep/${!_V}
		fi
		echo "${dep}" >> "${fname}"
	done
}

284 285 286 287
##############################################################################
# 
# cleanup environment
#
gsell's avatar
gsell committed
288
pmodules.cleanup_env() {
289

gsell's avatar
gsell committed
290 291 292 293 294 295 296
	C_INCLUDE_PATH=''
	CPLUS_INCLUDE_PATH=''
	CPP_INCLUDE_PATH=''
	LIBRARY_PATH=''
	LD_LIBRARY_PATH=''
	DYLD_LIBRARY_PATH=''

297 298 299 300 301
	CFLAGS=''
	CPPFLAGS=''
	CXXFLAGS=''
	LIBS=''
	LDFLAGS=''
302 303 304 305 306 307 308 309
}

##############################################################################
#
# load default versions
#
 _setup_env1() {
	local varname=''
310 311 312 313
	while read _name _version; do
		[[ -z ${_name} ]] && continue
		[[ -z ${_version} ]] && continue
		[[ "${_name:0:1}" == '#' ]] && continue
314 315 316 317 318
		var_name=$(echo ${_name} | tr [:lower:] [:upper:])_VERSION
		# don't set version, if already set
		if [[ -z ${!var_name} ]]; then
			eval ${var_name}="${_version}"
		fi
319 320 321 322
	done < "${BUILD_VERSIONSFILE}"

}

gsell's avatar
gsell committed
323 324 325 326 327 328
#
# find tarball 
# $1: download directory
# $2: name without version
# $3: version
#
gsell's avatar
gsell committed
329
find_tarball() {
gsell's avatar
gsell committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	local -r dir=$1
	local -r name=$2
	local -r version=$3

	TARBALL=""
	local ext
	for ext in tar tar.gz tgz tar.bz2 tar.xz; do
		local fname="${dir}/${name}-${OS}-${version}.${ext}"
		if [[ -r "${fname}" ]]; then
			TARBALL="${fname}"
			break
		fi
		local fname="${dir}/${name}-${version}.${ext}"
		if [[ -r "${fname}" ]]; then
			TARBALL="${fname}"
			break
		fi
	done
	if [[ -z ${TARBALL} ]]; then
		error "tar-ball for $P/$V not found."
		exit 43
	fi
}

354
#setup module specific environment
gsell's avatar
gsell committed
355
setup_env2() {
gsell's avatar
gsell committed
356 357
	if [[ -z ${MODULE_GROUP} ]]; then
		die 1 "$P: group not set."
gsell's avatar
gsell committed
358 359 360
	fi

	# overwrite environment variables with values we got on the cmd line
gsell's avatar
gsell committed
361
	eval "${ENVIRONMENT_ARGS}"
gsell's avatar
gsell committed
362

363 364
	# this allows us to specify the version as PKGNAME_VERSION=1.2.3 on
	# the cmd-line
gsell's avatar
gsell committed
365 366 367 368 369 370 371 372
        if [[ -z $V ]]; then
		V=$(eval echo \$${_P}_VERSION)
	fi

	# oops, we need a version
	if [[ -z $V ]]; then
		die 1 "$P: Missing version."
	fi
373
	MODULE_SRCDIR="${BUILD_TMPDIR}/src/${P/_serial}-$V"
374
	MODULE_BUILDDIR="${BUILD_TMPDIR}/build/$P-$V"
gsell's avatar
gsell committed
375 376

	# build module name
377 378
	# :FIXME: the MODULE_PREFIX should be derived from MODULE_NAME
	# :FIXME: this should be read from a configuration file
gsell's avatar
gsell committed
379
	case ${MODULE_GROUP} in
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
		Tools )
			MODULE_RPREFIX="${P}/${V}"
			MODULE_NAME="${P}/${V}"
			;;
		Programming )
			MODULE_RPREFIX="${P}/${V}"
			MODULE_NAME="${P}/${V}"
			;;
		Libraries )
			MODULE_RPREFIX="${P}/${V}"
			MODULE_NAME="${P}/${V}"
			;;
		System )
			MODULE_RPREFIX="${P}/${V}"
			MODULE_NAME="${P}/${V}"
			;;
		Compiler )
			MODULE_RPREFIX="${P}/${V}"
			MODULE_RPREFIX+="/${COMPILER}/${COMPILER_VERSION}"
			
			MODULE_NAME="${COMPILER}/${COMPILER_VERSION}/"
			MODULE_NAME+="${P}/${V}"
			;;
		MPI )
			MODULE_RPREFIX="${P}/${V}/"
			MODULE_RPREFIX+="${MPI}/${MPI_VERSION}/"
			MODULE_RPREFIX+="${COMPILER}/${COMPILER_VERSION}"
			
			MODULE_NAME="${COMPILER}/${COMPILER_VERSION}/"
			MODULE_NAME+="${MPI}/${MPI_VERSION}/"
			MODULE_NAME+="${P}/${V}"
			;;
		HDF5 )
			MODULE_RPREFIX="${P}/${V}"
			MODULE_RPREFIX+="/${HDF5}/${HDF5_VERSION}"
			MODULE_RPREFIX+="/${MPI}/${MPI_VERSION}"
			MODULE_RPREFIX+="/${COMPILER}/${COMPILER_VERSION}"
			
			MODULE_NAME="${COMPILER}/${COMPILER_VERSION}/"
			MODULE_NAME+="${MPI}/${MPI_VERSION}/"
			MODULE_NAME+="${HDF5}/${HDF5_VERSION}/"
			MODULE_NAME+="${P}/${V}"
			;;
423 424 425 426 427 428 429 430 431 432 433
		OPAL )
			MODULE_RPREFIX="${P}/${V}"
			MODULE_RPREFIX+="/${OPAL}/${OPAL_VERSION}"
			MODULE_RPREFIX+="/${MPI}/${MPI_VERSION}"
			MODULE_RPREFIX+="/${COMPILER}/${COMPILER_VERSION}"
			
			MODULE_NAME="${COMPILER}/${COMPILER_VERSION}/"
			MODULE_NAME+="${MPI}/${MPI_VERSION}/"
			MODULE_NAME+="${OPAL}/${OPAL_VERSION}/"
			MODULE_NAME+="${P}/${V}"
			;;
434 435 436 437 438 439
		HDF5_serial )
			MODULE_RPREFIX="${P}/${V}"
			MODULE_RPREFIX+="/hdf5_serial/${HDF5_SERIAL_VERSION}"
			MODULE_RPREFIX+="/${COMPILER}/${COMPILER_VERSION}"
			
			MODULE_NAME="${COMPILER}/${COMPILER_VERSION}/"
gsell's avatar
gsell committed
440
			MODULE_NAME+="hdf5_serial/${HDF5_SERIAL_VERSION}/"
441 442 443
			MODULE_NAME+="${P}/${V}"
			;;
		* )
gsell's avatar
gsell committed
444
			die 1 "$P: oops: unknown group: ${MODULE_GROUP}"
445
			;;
gsell's avatar
gsell committed
446 447
	esac

gsell's avatar
gsell committed
448
	# set PREFIX of module
gsell's avatar
gsell committed
449
	PREFIX="${PMODULES_ROOT}/${MODULE_GROUP}/${MODULE_RPREFIX}"
gsell's avatar
gsell committed
450

451 452
	# get module release if already installed
	local saved_modulepath=${MODULEPATH}
453 454 455
	rels=( ${releases//:/ } )
	for rel in "${rels[@]}"; do
		eval $("${MODULECMD}" bash unuse ${rel})
456
	done
457 458 459
	for rel in "${rels[@]}"; do
		eval $("${MODULECMD}" bash use ${rel})
		if module_is_available "${P}/${V}"; then
460
			cur_module_release=${rel}
461
			info "${P}/${V}: already available and released as \"${rel}\""
462 463 464 465 466 467 468 469 470 471
			break
		fi
	done
	MODULEPATH=${saved_modulepath}

	# set release of module
	# release is deprecated
	#   - if a build-dependency is deprecated or 
	#   - the module already exists and is deprecated or
	#   - is forced to be deprecated by setting this on the command line
472 473 474 475
	if [[ "${depend_release}" == 'deprecated' ]] || \
		[[ "${cur_module_release}" == 'deprecated' ]] \
		|| [[ "${MODULE_RELEASE}" == 'deprecated' ]]; then
		MODULE_RELEASE='deprecated'
476 477 478 479 480
		info "${P}/${V}: will be released as \"deprecated\""
	#
	# release is stable
	#   - if all build-dependency are stable or
	#   - the module already exists and is stable
481 482
	#   - an unstable release of the module exists and the release is
	#     changed to stable on the command line
483 484 485 486
	elif [[ "${depend_release}" == 'stable' ]] \
		|| [[ "${cur_module_release}" == 'stable' ]] \
		|| [[ "${MODULE_RELEASE}" == 'stable' ]]; then
		MODULE_RELEASE='stable'
487 488 489 490
		info "${P}/${V}: will be released as \"stable\""
	#
	# release is unstable
	#   - if a build-dependency is unstable or 
491 492
	#   - if the module does not exists and no other release-type is
	#     given on the command line
493 494
	#   - and all the cases I didn't think of
	else
495
		MODULE_RELEASE='unstable'
496 497
		info "${P}/${V}: will be released as \"unstable\""
	fi
498 499

	# directory for README's, license files etc
gsell's avatar
gsell committed
500 501
	DOCDIR="${PREFIX}/share/doc/$P"

502
	# set tar-ball and flags for tar
gsell's avatar
gsell committed
503
	find_tarball "${BUILD_DOWNLOADSDIR}" "${P/_serial}" "${V}"
504

gsell's avatar
gsell committed
505 506
}

507
# redefine function for bootstrapping
gsell's avatar
gsell committed
508
setup_env2_bootstrap() {
gsell's avatar
gsell committed
509 510
	if [[ -z ${MODULE_GROUP} ]]; then
		die 1 "$P: group not set."
511
	fi
512

513
        if [[ -z $V ]]; then
514
		V=$(eval echo \$${_P}_VERSION)
515
	fi
516

517 518
	# oops, we need a version
	if [[ -z $V ]]; then
519
		die 1 "$P: Missing version."
520 521 522
	fi
	MODULE_SRCDIR="${BUILD_TMPDIR}/src/${P/_serial}-$V"
	MODULE_BUILDDIR="${BUILD_TMPDIR}/build/$P-$V"
gsell's avatar
gsell committed
523
	MODULE_GROUP='Tools'
524 525
	MODULE_NAME="Pmodules/${PMODULES_VERSION}"
	# set PREFIX of module
gsell's avatar
gsell committed
526
	PREFIX="${PMODULES_ROOT}/${MODULE_GROUP}/${MODULE_NAME}"
527 528 529 530 531 532 533 534
	
	MODULE_RELEASE='unstable'
	info "${MODULE_NAME}: will be released as \"${MODULE_RELEASE}\""

	# directory for README's, license files etc
	DOCDIR="${PREFIX}/share/doc/$P"

	# set tar-ball and flags for tar
gsell's avatar
gsell committed
535 536
	find_tarball "${BUILD_DOWNLOADSDIR}" "${P/_serial}" "${V}"

537 538 539 540 541 542 543 544 545
	C_INCLUDE_PATH="${PREFIX}/include"
	CPLUS_INCLUDE_PATH="${PREFIX}/include"
	CPP_INCLUDE_PATH="${PREFIX}/include"
	LIBRARY_PATH="${PREFIX}/lib"
	LD_LIBRARY_PATH="${PREFIX}/lib"
	DYLD_LIBRARY_PATH="${PREFIX}/lib"

	PATH+=":${PREFIX}/bin"
}
546

gsell's avatar
gsell committed
547
prep() {
gsell's avatar
gsell committed
548 549

	# untar sources
550 551
	if [[ ! -d ${MODULE_SRCDIR} ]]; then
		mkdir -p "${BUILD_TMPDIR}/src"
552
		(cd "${BUILD_TMPDIR}/src" && tar xvf "${TARBALL}")
gsell's avatar
gsell committed
553 554 555
	fi

	# create build directory
556
	mkdir -p "${MODULE_BUILDDIR}"
gsell's avatar
gsell committed
557 558 559

}

gsell's avatar
gsell committed
560
pmodules.pre_configure() {
gsell's avatar
gsell committed
561 562 563
	:
}

gsell's avatar
gsell committed
564
pmodules.configure() {
565
	${MODULE_SRCDIR}/configure \
gsell's avatar
gsell committed
566 567 568
		--prefix="${PREFIX}"
}

gsell's avatar
gsell committed
569
pmodules.build() {
gsell's avatar
gsell committed
570 571 572
	make -j${JOBS}
}

gsell's avatar
gsell committed
573
pmodules.install() {
gsell's avatar
gsell committed
574 575 576
	make install
}

gsell's avatar
gsell committed
577
pmodules.post_install() {
gsell's avatar
gsell committed
578 579 580
	:
}

gsell's avatar
gsell committed
581
pmodules.install_doc() {
582
	info "Installing documentation to ${DOCDIR}"
583
	install -m 0755 -d "${DOCDIR}"
584
	install -m0444 "${MODULE_DOCFILES[@]/#/${MODULE_SRCDIR}/}" "${BUILDSCRIPT}" "${DOCDIR}"
gsell's avatar
gsell committed
585 586
}

gsell's avatar
gsell committed
587
set_legacy_link() {
gsell's avatar
gsell committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
	local -r link_name="${PMODULES_ROOT}/${PMODULES_MODULEFILES_DIR}/${MODULE_GROUP}/${MODULE_NAME}"
	local -r dir_name=${link_name%/*}
	local -r release_file="${dir_name}/.release-${MODULE_NAME##*/}"
	if [[ ! -e "${_path}" ]]; then
	    (
		    info "Setting new sym-link \"${link_name}\" ..."
		    mkdir -p "${dir_name}"
		    cd "${dir_name}"
		    local x
		    IFS='/' x=( ${dir_name/${PMODULES_ROOT}\/${PMODULES_MODULEFILES_DIR}\/} )
		    local n=${#x[@]}
		    local -r _target="../"$(eval printf "../%.s" {1..${n}})${PMODULES_TEMPLATES_DIR##*/}/"${MODULE_GROUP}/${P}/modulefile"
		    ln -fs "${_target}" "${MODULE_NAME##*/}"
	    )
	fi
	info "${MODULE_NAME}: set release to '${MODULE_RELEASE}'"
	echo "${MODULE_RELEASE}" > "${release_file}"
}

gsell's avatar
gsell committed
607
set_link() {
gsell's avatar
gsell committed
608
	local -r link_name="${PMODULES_ROOT}/${MODULE_GROUP}/${PMODULES_MODULEFILES_DIR}/${MODULE_NAME}"
609 610 611 612 613 614 615 616
	local -r dir_name=${link_name%/*}
	local -r release_file="${dir_name}/.release-${MODULE_NAME##*/}"
	if [[ ! -e "${_path}" ]]; then
	    (
		    info "Setting new sym-link \"${link_name}\" ..."
		    mkdir -p "${dir_name}"
		    cd "${dir_name}"
		    local x
gsell's avatar
gsell committed
617
		    IFS='/' x=( ${dir_name/${PMODULES_ROOT}\/${MODULE_GROUP}\/} )
618
		    local n=${#x[@]}
gsell's avatar
gsell committed
619
		    local -r _target="../"$(eval printf "../%.s" {1..${n}})${PMODULES_TEMPLATES_DIR##*/}/"${MODULE_GROUP}/${P}/modulefile"
620 621
		    ln -fs "${_target}" "${MODULE_NAME##*/}"
	    )
622
	fi
623 624
	info "${MODULE_NAME}: set release to '${MODULE_RELEASE}'"
	echo "${MODULE_RELEASE}" > "${release_file}"
gsell's avatar
gsell committed
625 626
}

gsell's avatar
gsell committed
627
pmodules.cleanup_build() {
628 629 630 631 632 633 634 635
	[[ -n "${MODULE_BUILDDIR}" ]]     \
		|| die 1 "Oops: internal error: MODULE_BUILDDIR is set to empty string..."
	[[ "${MODULE_BUILDDIR}" == "/" ]] \
		&& die 1 "Oops: internal error: MODULE_BUILDDIR is set to '/'..."
	[[ -d "/${MODULE_BUILDDIR}" ]]    \
		|| die 1 "Oops: internal error: MODULE_BUILDDIR=${MODULE_BUILDDIR} is not a directory..."
	echo "Cleaning up '/${MODULE_BUILDDIR}'..."
	rm -rf  "/${MODULE_BUILDDIR}"
gsell's avatar
gsell committed
636 637
}

gsell's avatar
gsell committed
638
pmodules.cleanup_src() {
639
    (
640 641
	[[ -d /${MODULE_SRCDIR} ]] || return 0
	cd "/${MODULE_SRCDIR}/..";
642 643
	if [[ $(pwd) != / ]]; then
		echo "Cleaning up $(pwd)"
644
		rm -rf ${MODULE_SRCDIR##*/}
645 646
	fi
    );
647
	return 0
648 649
}

gsell's avatar
gsell committed
650
check_compiler() {
651 652
	test -z ${MODULE_SUPPORTED_COMPILERS} && return 0
	for cc in ${MODULE_SUPPORTED_COMPILERS[@]}; do
gsell's avatar
gsell committed
653 654 655 656 657 658
		if [[ ${COMPILER}/${COMPILER_VERSION} =~ ${cc} ]]; then
			return 0
		fi
	done
	die 0 "Package cannot be build with ${COMPILER}/${COMPILER_VERSION}."
}
659

660
# unfortunatelly we need sometime an OS depended post-install
gsell's avatar
gsell committed
661
post_install_linux() {
662 663 664
	cd "${PREFIX}"
	# solve multilib problem with LIBRARY_PATH on 64bit Linux
	[[ -d "lib" ]] && [[ ! -d "lib64" ]] && ln -s lib lib64
665
	return 0
666 667
}

gsell's avatar
gsell committed
668
post_install() {
669
	info "Run post-installation for ${OS} ..."
gsell's avatar
gsell committed
670
	[[ "${OS}" == "Linux" ]] && post_install_linux
671
	info "Post-installation done ..."
gsell's avatar
gsell committed
672
	return 0
673
}
674

gsell's avatar
gsell committed
675
pmodules.make_all() {
gsell's avatar
gsell committed
676
	local building='no'
677
	echo "${P}:"
678
	_setup_env1
gsell's avatar
gsell committed
679
	load_build_dependencies
680
	# setup module specific environment
681
	if [[ ${bootstrap} == no ]]; then
gsell's avatar
gsell committed
682
		setup_env2
683
	else
gsell's avatar
gsell committed
684
		setup_env2_bootstrap
685
	fi
686

687
	if [[ ! -d "${PREFIX}" ]] || [[ ${force_rebuild} == 'yes' ]]; then
gsell's avatar
gsell committed
688
		building='yes'
689
 		echo "Building $P/$V ..."
690
		[[ ${dry_run} == yes ]] && die 0 ""
gsell's avatar
gsell committed
691 692
		check_compiler
		prep
693
		cd "${MODULE_SRCDIR}"
gsell's avatar
gsell committed
694
		pmodules.pre_configure
695
		cd "${MODULE_BUILDDIR}"
gsell's avatar
gsell committed
696 697 698 699 700 701
		pmodules.configure
		pmodules.build
		pmodules.install
		pmodules.post_install
		pmodules.install_doc
		post_install
702
		if [[ ${bootstrap} == 'no' ]]; then
gsell's avatar
gsell committed
703 704
			write_runtime_dependencies
			write_build_dependencies
705
		fi
gsell's avatar
gsell committed
706 707
		[[ ${enable_cleanup_build} == yes ]] && pmodules.cleanup_build
		[[ ${enable_cleanup_src} == yes ]] && pmodules.cleanup_src
708
		
gsell's avatar
gsell committed
709
	else
710
 		echo "Not rebuilding $P/$V ..."
gsell's avatar
gsell committed
711
	fi
712
	if [[ ${bootstrap} == 'no' ]]; then
gsell's avatar
gsell committed
713
		if [[ -d "${PMODULES_ROOT}/${PMODULES_MODULEFILES_DIR}" ]]; then
gsell's avatar
gsell committed
714
			set_legacy_link
gsell's avatar
gsell committed
715
		fi
gsell's avatar
gsell committed
716
		set_link
717
	fi
gsell's avatar
gsell committed
718
	return 0
gsell's avatar
gsell committed
719 720
}

721 722 723 724 725 726 727
##############################################################################
#
debug_on='no'
force_rebuild='no'
ENVIRONMENT_ARGS=''
dry_run='no'
bootstrap='no'
gsell's avatar
gsell committed
728 729
enable_cleanup_build='yes'
enable_cleanup_src='no'
730

gsell's avatar
gsell committed
731
pmodules.cleanup_env
732

733 734 735
# array collecting all modules specified on the command line via '--with=module'
with_modules=()

736 737 738 739
if [[ -r "${BUILDSCRIPT_DIR}/with_modules" ]]; then
	with_modules+=( $(cat "${BUILDSCRIPT_DIR}/with_modules") )
fi

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
while (( $# > 0 )); do
	case $1 in
	-j )
		JOBS=$2
		shift
		;;
	--jobs=[0-9]* )
		JOBS=${1/--jobs=}
		;;
	-v | --verbose)
		debug_on='yes'
		;;
	-f | --force-rebuild )
		force_rebuild='yes'
		;;
	-b | --bootstrap )
		bootstrap='yes'
		force_rebuild='yes'
		;;
	-? | -h | --help )
		usage
		;;
gsell's avatar
gsell committed
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
	--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'
		;;
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
	--dry-run )
		dry_run='yes'
		;;
	--release=* )
		MODULE_RELEASE=${1/--release=}
		;;
	--with=*/* )
		with_modules+=( ${1/--with=} )
		;;
	*=* )
		eval $1
		ENVIRONMENT_ARGS="${ENVIRONMENT_ARGS} $1"
		;;
	* )
		V=$1
		;;
	esac
	shift
done

if [[ ${debug_on} == yes ]]; then
	trap 'echo "$BASH_COMMAND"' DEBUG
fi

# while bootstraping the module command is not yet available
if [[ ${bootstrap} == no ]]; then
gsell's avatar
gsell committed
804
        source	"${PMODULES_ROOT}/${PMODULES_CONFIG_DIR}/profile.bash"
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
	MODULECMD="${PMODULES_HOME}/bin/modulecmd"
	[[ -x ${MODULECMD} ]] || die 1 "${MODULECMD}: no such executable"
	module use unstable
	module purge
	for m in "${with_modules[@]}"; do
		if module_is_available "$m"; then
			echo "Loading module: ${m}"
			module load "${m}"
		else
			die 44 "$m: module not available!"
		fi
	done
fi

P=$(basename $(dirname "${BUILDSCRIPT}"))
gsell's avatar
gsell committed
820
P=${P%.*}
821 822 823 824 825 826
_P=$(echo $P | tr [:lower:] [:upper:])
_P=${_P//-/_}
_V=${_P}_VERSION

eval "${ENVIRONMENT_ARGS}"

gsell's avatar
gsell committed
827 828
if [[ -n ${PMODULES_DEFINED_RELEASES} ]]; then
        declare -r releases="${PMODULES_DEFINED_RELEASES}"
829 830 831 832 833
else
	# set defaults, if file doesn't exist or isn't readable
	declare -r releases=":unstable:stable:deprecated:"
fi

834 835 836 837 838
# Local Variables:
# mode: sh
# sh-basic-offset: 8
# tab-width: 8
# End: