|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# build |
| 4 | +# |
| 5 | +# -- This script installs OpenCoarrays and its prerequisites. |
| 6 | +# |
| 7 | +# OpenCoarrays is distributed under the OSI-approved BSD 3-clause License: |
| 8 | +# Copyright (c) 2015, Sourcery, Inc. |
| 9 | +# Copyright (c) 2015, Sourcery Institute |
| 10 | +# All rights reserved. |
| 11 | +# |
| 12 | +# Redistribution and use in source and binary forms, with or without modification, |
| 13 | +# are permitted provided that the following conditions are met: |
| 14 | +# |
| 15 | +# 1. Redistributions of source code must retain the above copyright notice, this |
| 16 | +# list of conditions and the following disclaimer. |
| 17 | +# 2. Redistributions in binary form must reproduce the above copyright notice, this |
| 18 | +# list of conditions and the following disclaimer in the documentation and/or |
| 19 | +# other materials provided with the distribution. |
| 20 | +# 3. Neither the names of the copyright holders nor the names of their contributors |
| 21 | +# may be used to endorse or promote products derived from this software without |
| 22 | +# specific prior written permission. |
| 23 | +# |
| 24 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 25 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 26 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 27 | +# IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 28 | +# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 29 | +# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 30 | +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 31 | +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 32 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 33 | +# POSSIBILITY OF SUCH DAMAGE. |
| 34 | + |
| 35 | +this_script=`basename $0` |
| 36 | + |
| 37 | +usage() |
| 38 | +{ |
| 39 | + echo "" |
| 40 | + echo " $this_script - Bash script for installing OpenCoarrays and its prerequisites" |
| 41 | + echo "" |
| 42 | + echo " Usage (optional arguments in square brackets): " |
| 43 | + echo " $this_script [<options>] or [<installation-path> <number-of-threads>]" |
| 44 | + echo "" |
| 45 | + echo " Options:" |
| 46 | + echo " --help, -h Show this help message" |
| 47 | + echo "" |
| 48 | + echo " Examples:" |
| 49 | + echo "" |
| 50 | + echo " $this_script" |
| 51 | + echo " $this_script ${PWD}" |
| 52 | + echo " $this_script /opt/opencoarrays 4" |
| 53 | + echo " $this_script --help" |
| 54 | + echo "" |
| 55 | + exit 1 |
| 56 | +} |
| 57 | + |
| 58 | +# Interpret the first command-line argument, if present, as the installation path. |
| 59 | +# Otherwise, install in a subdirectory of the present working directory. |
| 60 | +default_install_path=${PWD}/opencoarrays-installation |
| 61 | +if [[ -z $1 ]]; then |
| 62 | + install_path=$default_install_path |
| 63 | +else |
| 64 | + install_path=$1 |
| 65 | +fi |
| 66 | + |
| 67 | +build_path=${PWD}/opencorrays-build |
| 68 | + |
| 69 | +#Interpret the second command-line argument, if present, as the number of threads for 'make'. |
| 70 | +#Otherwise, default to single-threaded 'make'. |
| 71 | +if [[ -z $2 ]]; then |
| 72 | + num_threads=1 |
| 73 | +else |
| 74 | + num_threads=$2 |
| 75 | +fi |
| 76 | + |
| 77 | +find_or_install() |
| 78 | +{ |
| 79 | + package=$1 |
| 80 | + # This is a bash 3 hack standing in for a bash 4 hash (bash 3 is the lowest common |
| 81 | + # denominator because, for licensing reasons, OS X only has bash 3 by default.) |
| 82 | + # See http://stackoverflow.com/questions/1494178/how-to-define-hash-tables-in-bash |
| 83 | + package_executable_array=( |
| 84 | + "gcc:gfortran" |
| 85 | + "cmake:cmake" |
| 86 | + "mpich:mpif90" |
| 87 | + "_unknown:0" |
| 88 | + ) |
| 89 | + for element in "${package_executable_array[@]}" ; do |
| 90 | + KEY="${element%%:*}" |
| 91 | + VALUE="${element##*:}" |
| 92 | + if [[ "$KEY" == "_unknown" ]]; then |
| 93 | + # No recognizeable argument passed so print usage information and exit: |
| 94 | + printf "$this_script: Package name not recognized in function find_or_install.\n" |
| 95 | + exit 1 |
| 96 | + elif [[ $package == "$KEY" ]]; then |
| 97 | + executable=$VALUE |
| 98 | + break |
| 99 | + fi |
| 100 | + done |
| 101 | + printf "Checking whether $executable is in the PATH... " |
| 102 | + if type $executable > /dev/null; then |
| 103 | + printf "yes.\n" |
| 104 | + |
| 105 | + if [[ $package == "mpich" ]]; then |
| 106 | + echo "Using the pre-installed mpicc and mpif90" |
| 107 | + MPICC=mpicc |
| 108 | + MPIFC=mpif90 |
| 109 | + fi |
| 110 | + |
| 111 | + never=false |
| 112 | + until [ $never == true ]; do |
| 113 | + # To avoid an infinite loop every branch must end with return, exit, or break |
| 114 | + |
| 115 | + if [[ $package != "gcc" ]]; then |
| 116 | + |
| 117 | + return # return |
| 118 | + |
| 119 | + else #package is gcc |
| 120 | + |
| 121 | + printf "Checking whether gfortran is version 5.1.0 or later..." |
| 122 | + gfortran -o acceptable_compiler ./install_prerequisites/acceptable_compiler.f90 |
| 123 | + gfortran -o print_true ./install_prerequisites/print_true.f90 |
| 124 | + is_true=`./print_true` |
| 125 | + acceptable=`./acceptable_compiler` |
| 126 | + rm acceptable_compiler print_true |
| 127 | + |
| 128 | + if [[ $acceptable == $is_true ]]; then |
| 129 | + |
| 130 | + printf "yes\n" |
| 131 | + FC=gfortran |
| 132 | + CC=gcc |
| 133 | + CXX=g++ |
| 134 | + |
| 135 | + return # found an acceptable gfortran in PATH |
| 136 | + |
| 137 | + else |
| 138 | + printf "no\n" |
| 139 | + |
| 140 | + break # need to install gfortran below |
| 141 | + fi |
| 142 | + printf "$this_script: an infinite until loop is missing a break, return, or exit.\n" |
| 143 | + exit 1 # This should never be reached |
| 144 | + fi |
| 145 | + printf "$this_script: an infinite until loop is missing a break, return, or exit.\n" |
| 146 | + exit 1 # This should never be reached |
| 147 | + done |
| 148 | + # The $executable is not an acceptable version |
| 149 | + fi |
| 150 | + |
| 151 | + # Now we know the $executable is not in the PATH or is not an acceptable version |
| 152 | + printf "Ok to downloand, build, and install $package from source?\n" |
| 153 | + if [[ $proceed == "y" ]]; then |
| 154 | + printf "Downloading, building, and installing $package \n" |
| 155 | + cd install_prerequisites && |
| 156 | + FC=gfortran CC=gcc CXX=g++ ./build $package && |
| 157 | + package_install_path=`./build $package --default --query` && |
| 158 | + if [[ -f $package_install_path/bin/$executable ]]; then |
| 159 | + printf "Installation successful." |
| 160 | + printf "$package is in $package_install_path/bin \n" |
| 161 | + if [[ $package == "gcc" ]]; then |
| 162 | + FC=$package_install_path/bin/gfortran |
| 163 | + CC=$package_install_path/bin/gcc |
| 164 | + CXX=$package_install_path/bin/g++ |
| 165 | + elif [[ $package == "mpich" ]]; then |
| 166 | + MPICC=$package_install_path/bin/mpicc |
| 167 | + MPIFC=$package_install_path/bin/mpif90 |
| 168 | + else |
| 169 | + PATH=$package_install_path/bin:$PATH |
| 170 | + fi |
| 171 | + return |
| 172 | + else |
| 173 | + printf "Installation unsuccessful." |
| 174 | + printf "$package is not in the expected path: $package_install_path/bin \n" |
| 175 | + exit 1 |
| 176 | + fi |
| 177 | + else |
| 178 | + printf "Aborting. OpenCoarrays installation requires $package" |
| 179 | + exit 1 |
| 180 | + fi |
| 181 | + printf "$this_script: the dependency installation logic is missing a return or exit.\n" |
| 182 | + exit 1 |
| 183 | +} |
| 184 | + |
| 185 | +###### Main Body ########## |
| 186 | + |
| 187 | +if [[ $1 == '--help' || $1 == '-h' ]]; then |
| 188 | + # Print usage information if script is invoked with --help or -h argument |
| 189 | + usage | less |
| 190 | +elif [[ $1 == '-v' || $1 == '-V' || $1 == '--version' ]]; then |
| 191 | + # Print script copyright if invoked with -v, -V, or --version argument |
| 192 | + echo "" |
| 193 | + echo "OpenCoarrays installer" |
| 194 | + echo "Copyright (C) 2015 Sourcery, Inc." |
| 195 | + echo "Copyright (C) 2015 Sourcery Institute" |
| 196 | + echo "" |
| 197 | + echo "$this_script comes with NO WARRANTY, to the extent permitted by law." |
| 198 | + echo "You may redistribute copies of $this_script under the terms of the" |
| 199 | + echo "BSD 3-Clause License. For more information about these matters, see" |
| 200 | + echo "http://www.sourceryinstitute.org/license.html" |
| 201 | + echo "" |
| 202 | +else |
| 203 | + |
| 204 | + # Find or install prerequisites and install OpenCoarrays |
| 205 | + installation_record=install-opencoarrays.log |
| 206 | + time \ |
| 207 | + { |
| 208 | + # Find or install GCC first to ensure we build MPICH with an acceptable version of GCC |
| 209 | + find_or_install gcc && |
| 210 | + find_or_install mpich && |
| 211 | + find_or_install cmake && |
| 212 | + mkdir -p opencoarrays-build && |
| 213 | + cd opencoarrays-build && |
| 214 | + if [[ -z $MPICC || -z $MPIFC ]]; then |
| 215 | + echo "Empty MPICC=$MPICC or MPIFC=$MPIFC" |
| 216 | + exit 1 |
| 217 | + else |
| 218 | + CC=$MPICC FC=$MPIFC cmake .. -DCMAKE_INSTALL_PREFIX=$install_path && |
| 219 | + make && |
| 220 | + make install && |
| 221 | + make clean |
| 222 | + fi |
| 223 | + } >&1 | tee $installation_record |
| 224 | + |
| 225 | + # Report installation success or failure |
| 226 | + printf "\n" |
| 227 | + if [[ -f $install_path/bin/caf && -f $install_path/bin/cafrun && -f $install_path/bin/build ]]; then |
| 228 | + printf "$this_script: Done.\n\n" |
| 229 | + printf "The OpenCoarrays compiler wrapper (caf), program launcher (cafrun), and\n" |
| 230 | + printf "prerequisite package installer (build) are in the following directory:\n" |
| 231 | + printf "$install_path/bin.\n\n" |
| 232 | + else |
| 233 | + printf "$this_script: Done. \n\n" |
| 234 | + printf "Something went wrong. The OpenCoarrays compiler wrapper (caf),\n" |
| 235 | + printf "program launcher (cafrun), and prerequisite package installer (build) \n" |
| 236 | + printf "are not in the expected location:\n" |
| 237 | + printf "$install_path/bin.\n" |
| 238 | + printf "Please review the '$installation_record' file for more information.\n\n" |
| 239 | + fi |
| 240 | +fi |
0 commit comments