-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgfortran_utils.sh
167 lines (153 loc) · 5.76 KB
/
gfortran_utils.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Bash utilities for use with gfortran
ARCHIVE_SDIR="${ARCHIVE_SDIR:-archives}"
GF_UTIL_DIR=$(dirname "${BASH_SOURCE[0]}")
function get_distutils_platform {
# Report platform as in form of distutils get_platform.
# This is like the platform tag that pip will use.
# Modify fat architecture tags on macOS to reflect compiled architecture
# Deprecate this function once get_distutils_platform_ex is used in all
# downstream projects
local plat=$1
case $plat in
i686|x86_64|arm64|universal2|intel|aarch64|s390x|ppc64le) ;;
*) echo Did not recognize plat $plat; return 1 ;;
esac
local uname=${2:-$(uname)}
if [ "$uname" != "Darwin" ]; then
if [ "$plat" == "intel" ]; then
echo plat=intel not allowed for Manylinux
return 1
fi
echo "manylinux1_$plat"
return
fi
# macOS 32-bit arch is i386
[ "$plat" == "i686" ] && plat="i386"
local target=$(echo $MACOSX_DEPLOYMENT_TARGET | tr .- _)
echo "macosx_${target}_${plat}"
}
function get_distutils_platform_ex {
# Report platform as in form of distutils get_platform.
# This is like the platform tag that pip will use.
# Modify fat architecture tags on macOS to reflect compiled architecture
# For non-darwin, report manylinux version
local plat=$1
local mb_ml_ver=${MB_ML_VER:-1}
case $plat in
i686|x86_64|arm64|universal2|intel|aarch64|s390x|ppc64le) ;;
*) echo Did not recognize plat $plat; return 1 ;;
esac
local uname=${2:-$(uname)}
if [ "$uname" != "Darwin" ]; then
if [ "$plat" == "intel" ]; then
echo plat=intel not allowed for Manylinux
return 1
fi
echo "manylinux${mb_ml_ver}_${plat}"
return
fi
# macOS 32-bit arch is i386
[ "$plat" == "i686" ] && plat="i386"
local target=$(echo $MACOSX_DEPLOYMENT_TARGET | tr .- _)
echo "macosx_${target}_${plat}"
}
function get_macosx_target {
# Report MACOSX_DEPLOYMENT_TARGET as given by distutils get_platform.
python3 -c "import sysconfig as s; print(s.get_config_vars()['MACOSX_DEPLOYMENT_TARGET'])"
}
function check_gfortran {
# Check that gfortran exists on the path
if [ -z "$(which gfortran)" ]; then
echo Missing gfortran
exit 1
fi
}
function get_gf_lib_for_suf {
local suffix=$1
local prefix=$2
local plat=${3:-$PLAT}
local uname=${4:-$(uname)}
if [ -z "$prefix" ]; then echo Prefix not defined; exit 1; fi
local plat_tag=$(get_distutils_platform_ex $plat $uname)
if [ -n "$suffix" ]; then suffix="-$suffix"; fi
local fname="$prefix-${plat_tag}${suffix}.tar.gz"
local out_fname="${ARCHIVE_SDIR}/$fname"
[ -s $out_fname ] || (echo "$out_fname is empty"; exit 24)
echo "$out_fname"
}
if [ "$(uname)" == "Darwin" ]; then
mac_target=${MACOSX_DEPLOYMENT_TARGET:-$(get_macosx_target)}
export MACOSX_DEPLOYMENT_TARGET=$mac_target
# Keep this for now as some builds might depend on this being
# available before install_gfortran is called
export GFORTRAN_SHA=c469a420d2d003112749dcdcbe3c684eef42127e
# Set SDKROOT env variable if not set
export SDKROOT=${SDKROOT:-$(xcrun --show-sdk-path)}
function download_and_unpack_gfortran {
local arch=$1
local type=$2
curl -L -O https://github.com/isuruf/gcc/releases/download/gcc-11.3.0-2/gfortran-darwin-${arch}-${type}.tar.gz
case ${arch}-${type} in
arm64-native)
export GFORTRAN_SHA=0d5c118e5966d0fb9e7ddb49321f63cac1397ce8
;;
arm64-cross)
export GFORTRAN_SHA=527232845abc5af21f21ceacc46fb19c190fe804
;;
x86_64-native)
export GFORTRAN_SHA=c469a420d2d003112749dcdcbe3c684eef42127e
;;
x86_64-cross)
export GFORTRAN_SHA=107604e57db97a0ae3e7ca7f5dd722959752f0b3
;;
esac
if [[ "$(shasum gfortran-darwin-${arch}-${type}.tar.gz)" != "${GFORTRAN_SHA} gfortran-darwin-${arch}-${type}.tar.gz" ]]; then
echo "shasum mismatch for gfortran-darwin-${arch}-${type}"
exit 1
fi
sudo mkdir -p /opt/
sudo cp "gfortran-darwin-${arch}-${type}.tar.gz" /opt/gfortran-darwin-${arch}-${type}.tar.gz
pushd /opt
sudo tar -xvf gfortran-darwin-${arch}-${type}.tar.gz
sudo rm gfortran-darwin-${arch}-${type}.tar.gz
popd
if [[ "${type}" == "native" ]]; then
# Link these into /usr/local so that there's no need to add rpath or -L
for f in libgfortran.dylib libgfortran.5.dylib libgcc_s.1.dylib libgcc_s.1.1.dylib libquadmath.dylib libquadmath.0.dylib; do
ln -sf /opt/gfortran-darwin-${arch}-${type}/lib/$f /usr/local/lib/$f
done
# Add it to PATH
ln -sf /opt/gfortran-darwin-${arch}-${type}/bin/gfortran /usr/local/bin/gfortran
fi
}
function install_arm64_cross_gfortran {
download_and_unpack_gfortran arm64 cross
export FC_ARM64="$(find /opt/gfortran-darwin-arm64-cross/bin -name "*-gfortran")"
local libgfortran="$(find /opt/gfortran-darwin-arm64-cross/lib -name libgfortran.dylib)"
local libdir=$(dirname $libgfortran)
export FC_ARM64_LDFLAGS="-L$libdir -Wl,-rpath,$libdir"
if [[ "${PLAT:-}" == "arm64" ]]; then
export FC=$FC_ARM64
fi
}
function install_gfortran {
download_and_unpack_gfortran $(uname -m) native
check_gfortran
if [[ "${PLAT:-}" == "universal2" || "${PLAT:-}" == "arm64" ]]; then
install_arm64_cross_gfortran
fi
}
function get_gf_lib {
# Get lib with gfortran suffix
get_gf_lib_for_suf "gf_${GFORTRAN_SHA:0:7}" $@
}
else
function install_gfortran {
# No-op - already installed on manylinux image
check_gfortran
}
function get_gf_lib {
# Get library with no suffix
get_gf_lib_for_suf "" $@
}
fi