| 1 | # This file is vendored from github.com/MacPython/gfortran-install It is |
| 2 | # licensed under BSD-2 which is copied as a comment below |
| 3 | |
| 4 | # Copyright 2016-2021 Matthew Brett, Isuru Fernando, Matti Picus |
| 5 | |
| 6 | # Redistribution and use in source and binary forms, with or without |
| 7 | # modification, are permitted provided that the following conditions are met: |
| 8 | |
| 9 | # Redistributions of source code must retain the above copyright notice, this |
| 10 | # list of conditions and the following disclaimer. |
| 11 | |
| 12 | # Redistributions in binary form must reproduce the above copyright notice, this |
| 13 | # list of conditions and the following disclaimer in the documentation and/or |
| 14 | # other materials provided with the distribution. |
| 15 | |
| 16 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 20 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | |
| 27 | # Bash utilities for use with gfortran |
| 28 | |
| 29 | GF_LIB_URL="https://3f23b170c54c2533c070-1c8a9b3114517dc5fe17b7c3f8c63a43.ssl.cf2.rackcdn.com" |
| 30 | ARCHIVE_SDIR="${ARCHIVE_SDIR:-archives}" |
| 31 | |
| 32 | GF_UTIL_DIR=$(dirname "${BASH_SOURCE[0]}") |
| 33 | |
| 34 | function get_distutils_platform { |
| 35 | # Report platform as in form of distutils get_platform. |
| 36 | # This is like the platform tag that pip will use. |
| 37 | # Modify fat architecture tags on macOS to reflect compiled architecture |
| 38 | |
| 39 | # Deprecate this function once get_distutils_platform_ex is used in all |
| 40 | # downstream projects |
| 41 | local plat=$1 |
| 42 | case $plat in |
| 43 | i686|x86_64|arm64|universal2|intel|aarch64|s390x|ppc64le) ;; |
| 44 | *) echo Did not recognize plat $plat; return 1 ;; |
| 45 | esac |
| 46 | local uname=${2:-$(uname)} |
| 47 | if [ "$uname" != "Darwin" ]; then |
| 48 | if [ "$plat" == "intel" ]; then |
| 49 | echo plat=intel not allowed for Manylinux |
| 50 | return 1 |
| 51 | fi |
| 52 | echo "manylinux1_$plat" |
| 53 | return |
| 54 | fi |
| 55 | # macOS 32-bit arch is i386 |
| 56 | [ "$plat" == "i686" ] && plat="i386" |
| 57 | local target=$(echo $MACOSX_DEPLOYMENT_TARGET | tr .- _) |
| 58 | echo "macosx_${target}_${plat}" |
| 59 | } |
| 60 | |
| 61 | function get_distutils_platform_ex { |
| 62 | # Report platform as in form of distutils get_platform. |
| 63 | # This is like the platform tag that pip will use. |
| 64 | # Modify fat architecture tags on macOS to reflect compiled architecture |
| 65 | # For non-darwin, report manylinux version |
| 66 | local plat=$1 |
| 67 | local mb_ml_ver=${MB_ML_VER:-1} |
| 68 | case $plat in |
| 69 | i686|x86_64|arm64|universal2|intel|aarch64|s390x|ppc64le) ;; |
| 70 | *) echo Did not recognize plat $plat; return 1 ;; |
| 71 | esac |
| 72 | local uname=${2:-$(uname)} |
| 73 | if [ "$uname" != "Darwin" ]; then |
| 74 | if [ "$plat" == "intel" ]; then |
| 75 | echo plat=intel not allowed for Manylinux |
| 76 | return 1 |
| 77 | fi |
| 78 | echo "manylinux${mb_ml_ver}_${plat}" |
| 79 | return |
| 80 | fi |
| 81 | # macOS 32-bit arch is i386 |
| 82 | [ "$plat" == "i686" ] && plat="i386" |
| 83 | local target=$(echo $MACOSX_DEPLOYMENT_TARGET | tr .- _) |
| 84 | echo "macosx_${target}_${plat}" |
| 85 | } |
| 86 | |
| 87 | function get_macosx_target { |
| 88 | # Report MACOSX_DEPLOYMENT_TARGET as given by distutils get_platform. |
| 89 | python -c "import sysconfig as s; print(s.get_config_vars()['MACOSX_DEPLOYMENT_TARGET'])" |
| 90 | } |
| 91 | |
| 92 | function check_gfortran { |
| 93 | # Check that gfortran exists on the path |
| 94 | if [ -z "$(which gfortran)" ]; then |
| 95 | echo Missing gfortran |
| 96 | exit 1 |
| 97 | fi |
| 98 | } |
| 99 | |
| 100 | function get_gf_lib_for_suf { |
| 101 | local suffix=$1 |
| 102 | local prefix=$2 |
| 103 | local plat=${3:-$PLAT} |
| 104 | local uname=${4:-$(uname)} |
| 105 | if [ -z "$prefix" ]; then echo Prefix not defined; exit 1; fi |
| 106 | local plat_tag=$(get_distutils_platform_ex $plat $uname) |
| 107 | if [ -n "$suffix" ]; then suffix="-$suffix"; fi |
| 108 | local fname="$prefix-${plat_tag}${suffix}.tar.gz" |
| 109 | local out_fname="${ARCHIVE_SDIR}/$fname" |
| 110 | if [ ! -e "$out_fname" ]; then |
| 111 | curl -L "${GF_LIB_URL}/$fname" > $out_fname || (echo "Fetch of $out_fname failed"; exit 1) |
| 112 | fi |
| 113 | [ -s $out_fname ] || (echo "$out_fname is empty"; exit 24) |
| 114 | echo "$out_fname" |
| 115 | } |
| 116 | |
| 117 | if [ "$(uname)" == "Darwin" ]; then |
| 118 | mac_target=${MACOSX_DEPLOYMENT_TARGET:-$(get_macosx_target)} |
| 119 | export MACOSX_DEPLOYMENT_TARGET=$mac_target |
| 120 | GFORTRAN_DMG="${GF_UTIL_DIR}/archives/gfortran-4.9.0-Mavericks.dmg" |
| 121 | export GFORTRAN_SHA="$(shasum $GFORTRAN_DMG)" |
| 122 | |
| 123 | function install_arm64_cross_gfortran { |
| 124 | curl -L -O https://github.com/isuruf/gcc/releases/download/gcc-10-arm-20210728/gfortran-darwin-arm64.tar.gz |
| 125 | export GFORTRAN_SHA=4a1354e61294d5163609e83b6b2b082bd9a9bbdf |
| 126 | if [[ "$(shasum gfortran-darwin-arm64.tar.gz)" != "${GFORTRAN_SHA} gfortran-darwin-arm64.tar.gz" ]]; then |
| 127 | echo "shasum mismatch for gfortran-darwin-arm64" |
| 128 | exit 1 |
| 129 | fi |
| 130 | sudo mkdir -p /opt/ |
| 131 | sudo cp "gfortran-darwin-arm64.tar.gz" /opt/gfortran-darwin-arm64.tar.gz |
| 132 | pushd /opt |
| 133 | sudo tar -xvf gfortran-darwin-arm64.tar.gz |
| 134 | sudo rm gfortran-darwin-arm64.tar.gz |
| 135 | popd |
| 136 | export FC_ARM64="$(find /opt/gfortran-darwin-arm64/bin -name "*-gfortran")" |
| 137 | export FC_LOC=/opt/gfortran-darwin-arm64/bin |
| 138 | local libgfortran="$(find /opt/gfortran-darwin-arm64/lib -name libgfortran.dylib)" |
| 139 | local libdir=$(dirname $libgfortran) |
| 140 | |
| 141 | export FC_LIBDIR=$libdir |
| 142 | export FC_ARM64_LDFLAGS="-L$libdir -Wl,-rpath,$libdir" |
| 143 | echo $FC_ARM64_LDFLAGS |
| 144 | if [[ "${PLAT:-}" == "arm64" ]]; then |
| 145 | export FC=$FC_ARM64 |
| 146 | fi |
| 147 | } |
| 148 | function install_gfortran { |
| 149 | hdiutil attach -mountpoint /Volumes/gfortran $GFORTRAN_DMG |
| 150 | sudo installer -pkg /Volumes/gfortran/gfortran.pkg -target / |
| 151 | check_gfortran |
| 152 | if [[ "${PLAT:-}" == "universal2" || "${PLAT:-}" == "arm64" ]]; then |
| 153 | install_arm64_cross_gfortran |
| 154 | fi |
| 155 | } |
| 156 | |
| 157 | function get_gf_lib { |
| 158 | # Get lib with gfortran suffix |
| 159 | get_gf_lib_for_suf "gf_${GFORTRAN_SHA:0:7}" $@ |
| 160 | } |
| 161 | else |
| 162 | function install_gfortran { |
| 163 | # No-op - already installed on manylinux image |
| 164 | check_gfortran |
| 165 | } |
| 166 | |
| 167 | function get_gf_lib { |
| 168 | # Get library with no suffix |
| 169 | get_gf_lib_for_suf "" $@ |
| 170 | } |
| 171 | fi |