| 1 |
#! /bin/bash |
| 2 |
|
| 3 |
# Copyright (c) 2019 Giovanni Bussi |
| 4 |
|
| 5 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
# of this software and associated documentation files (the "Software"), to deal |
| 7 |
# in the Software without restriction, including without limitation the rights |
| 8 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
# copies of the Software, and to permit persons to whom the Software is |
| 10 |
# furnished to do so, subject to the following conditions: |
| 11 |
|
| 12 |
# The above copyright notice and this permission notice shall be included in all |
| 13 |
# copies or substantial portions of the Software. |
| 14 |
|
| 15 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
# SOFTWARE. |
| 22 |
|
| 23 |
export COLUMNS=80 |
| 24 |
|
| 25 |
if [ "$GITHUB_ACTIONS" = true ] ; then |
| 26 |
echo "COLUMNS=$COLUMNS" >> "$GITHUB_ENV" |
| 27 |
fi |
| 28 |
|
| 29 |
# file to be source at the end of subshell: |
| 30 |
export MACPORTS_CI_SOURCEME="$(mktemp)" |
| 31 |
|
| 32 |
( |
| 33 |
# start subshell |
| 34 |
# this allows to use the script in two ways: |
| 35 |
# 1. as ./macports-ci |
| 36 |
# 2. as source ./macports-ci |
| 37 |
# as of now, choice 2 only changes the env var COLUMNS. |
| 38 |
|
| 39 |
MACPORTS_VERSION=2.6.4 |
| 40 |
MACPORTS_PREFIX=/opt/local |
| 41 |
MACPORTS_SYNC=tarball |
| 42 |
|
| 43 |
action=$1 |
| 44 |
shift |
| 45 |
|
| 46 |
case "$action" in |
| 47 |
(install) |
| 48 |
|
| 49 |
echo "macports-ci: install" |
| 50 |
|
| 51 |
KEEP_BREW=yes |
| 52 |
|
| 53 |
for opt |
| 54 |
do |
| 55 |
case "$opt" in |
| 56 |
(--source) SOURCE=yes ;; |
| 57 |
(--binary) SOURCE=no ;; |
| 58 |
(--keep-brew) KEEP_BREW=yes ;; |
| 59 |
(--remove-brew) KEEP_BREW=no ;; |
| 60 |
(--version=*) MACPORTS_VERSION="${opt#--version=}" ;; |
| 61 |
(--prefix=*) MACPORTS_PREFIX="${opt#--prefix=}" ;; |
| 62 |
(--sync=*) MACPORTS_SYNC="${opt#--sync=}" ;; |
| 63 |
(*) echo "macports-ci: unknown option $opt" |
| 64 |
exit 1 ;; |
| 65 |
esac |
| 66 |
done |
| 67 |
|
| 68 |
if test "$KEEP_BREW" = no ; then |
| 69 |
echo "macports-ci: removing homebrew" |
| 70 |
pushd "$(mktemp -d)" |
| 71 |
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall > uninstall |
| 72 |
chmod +x uninstall |
| 73 |
./uninstall --force |
| 74 |
popd |
| 75 |
else |
| 76 |
echo "macports-ci: keeping HomeBrew" |
| 77 |
fi |
| 78 |
|
| 79 |
echo "macports-ci: prefix=$MACPORTS_PREFIX" |
| 80 |
|
| 81 |
if test "$MACPORTS_PREFIX" != /opt/local ; then |
| 82 |
echo "macports-ci: Installing on non standard prefix $MACPORTS_PREFIX can be only made from sources" |
| 83 |
SOURCE=yes |
| 84 |
fi |
| 85 |
|
| 86 |
if test "$SOURCE" = yes ; then |
| 87 |
echo "macports-ci: Installing from source" |
| 88 |
else |
| 89 |
echo "macports-ci: Installing from binary" |
| 90 |
fi |
| 91 |
|
| 92 |
echo "macports-ci: Sync mode=$MACPORTS_SYNC" |
| 93 |
|
| 94 |
pushd "$(mktemp -d)" |
| 95 |
|
| 96 |
OSX_VERSION="$(sw_vers -productVersion | grep -o '^[0-9][0-9]*\.[0-9][0-9]*')" |
| 97 |
|
| 98 |
if test "$OSX_VERSION" == 10.10 ; then |
| 99 |
OSX_NAME=Yosemite |
| 100 |
elif test "$OSX_VERSION" == 10.11 ; then |
| 101 |
OSX_NAME=ElCapitan |
| 102 |
elif test "$OSX_VERSION" == 10.12 ; then |
| 103 |
OSX_NAME=Sierra |
| 104 |
elif test "$OSX_VERSION" == 10.13 ; then |
| 105 |
OSX_NAME=HighSierra |
| 106 |
elif test "$OSX_VERSION" == 10.14 ; then |
| 107 |
OSX_NAME=Mojave |
| 108 |
elif test "$OSX_VERSION" == 10.15 ; then |
| 109 |
OSX_NAME=Catalina |
| 110 |
else |
| 111 |
echo "macports-ci: Unknown OSX version $OSX_VERSION" |
| 112 |
exit 1 |
| 113 |
fi |
| 114 |
|
| 115 |
echo "macports-ci: OSX version $OSX_VERSION $OSX_NAME" |
| 116 |
|
| 117 |
MACPORTS_PKG=MacPorts-${MACPORTS_VERSION}-${OSX_VERSION}-${OSX_NAME}.pkg |
| 118 |
|
| 119 |
# this is a workaround needed because binary installer MacPorts-2.6.3-10.12-Sierra.pkg is broken |
| 120 |
if [ "$SOURCE" != yes ] && [ "$MACPORTS_PKG" = "MacPorts-2.6.3-10.12-Sierra.pkg" ] ; then |
| 121 |
echo "macports-ci: WARNING $MACPORTS_PKG installer is broken" |
| 122 |
echo "macports-ci: reverting to 2.6.2 installer followed by selfupdate" |
| 123 |
MACPORTS_VERSION=2.6.2 |
| 124 |
MACPORTS_PKG=MacPorts-${MACPORTS_VERSION}-${OSX_VERSION}-${OSX_NAME}.pkg |
| 125 |
fi |
| 126 |
|
| 127 |
URL="https://distfiles.macports.org/MacPorts" |
| 128 |
URL="https://github.com/macports/macports-base/releases/download/v$MACPORTS_VERSION/" |
| 129 |
|
| 130 |
echo "macports-ci: Base URL is $URL" |
| 131 |
|
| 132 |
if test "$SOURCE" = yes ; then |
| 133 |
# download source: |
| 134 |
curl -LO $URL/MacPorts-${MACPORTS_VERSION}.tar.bz2 |
| 135 |
tar xjf MacPorts-${MACPORTS_VERSION}.tar.bz2 |
| 136 |
cd MacPorts-${MACPORTS_VERSION} |
| 137 |
# install |
| 138 |
./configure --prefix="$MACPORTS_PREFIX" --with-applications-dir="$MACPORTS_PREFIX/Applications" >/dev/null && |
| 139 |
sudo make install >/dev/null |
| 140 |
else |
| 141 |
|
| 142 |
# download installer: |
| 143 |
curl -LO $URL/$MACPORTS_PKG |
| 144 |
# install: |
| 145 |
sudo installer -verbose -pkg $MACPORTS_PKG -target / |
| 146 |
fi |
| 147 |
|
| 148 |
# update: |
| 149 |
export PATH="$MACPORTS_PREFIX/bin:$PATH" |
| 150 |
|
| 151 |
echo "PATH=\"$MACPORTS_PREFIX/bin:\$PATH\"" > "$MACPORTS_CI_SOURCEME" |
| 152 |
|
| 153 |
if [ "$GITHUB_ACTIONS" = true ] ; then |
| 154 |
echo "$MACPORTS_PREFIX/bin" >> "$GITHUB_PATH" |
| 155 |
fi |
| 156 |
|
| 157 |
|
| 158 |
SOURCES="${MACPORTS_PREFIX}"/etc/macports/sources.conf |
| 159 |
|
| 160 |
case "$MACPORTS_SYNC" in |
| 161 |
(rsync) |
| 162 |
echo "macports-ci: Using rsync" |
| 163 |
;; |
| 164 |
(github) |
| 165 |
echo "macports-ci: Using github" |
| 166 |
pushd "$MACPORTS_PREFIX"/var/macports/sources |
| 167 |
sudo mkdir -p github.com/macports/macports-ports/ |
| 168 |
sudo chown -R $USER:admin github.com |
| 169 |
git clone https://github.com/macports/macports-ports.git github.com/macports/macports-ports/ |
| 170 |
awk '{if($NF=="[default]") print "file:///opt/local/var/macports/sources/github.com/macports/macports-ports/"; else print}' "$SOURCES" > $HOME/$$.tmp |
| 171 |
sudo mv -f $HOME/$$.tmp "$SOURCES" |
| 172 |
popd |
| 173 |
;; |
| 174 |
(tarball) |
| 175 |
echo "macports-ci: Using tarball" |
| 176 |
awk '{if($NF=="[default]") print "https://distfiles.macports.org/ports.tar.gz [default]"; else print}' "$SOURCES" > $$.tmp |
| 177 |
sudo mv -f $$.tmp "$SOURCES" |
| 178 |
;; |
| 179 |
(*) |
| 180 |
echo "macports-ci: Unknown sync mode $MACPORTS_SYNC" |
| 181 |
;; |
| 182 |
esac |
| 183 |
|
| 184 |
i=1 |
| 185 |
# run through a while to retry upon failure |
| 186 |
while true |
| 187 |
do |
| 188 |
echo "macports-ci: Trying to selfupdate (iteration $i)" |
| 189 |
# here I test for the presence of a known portfile |
| 190 |
# this check confirms that ports were installed |
| 191 |
# notice that port -N selfupdate && break is not sufficient as a test |
| 192 |
# (sometime it returns a success even though ports have not been installed) |
| 193 |
# for some misterious reasons, running without "-d" does not work in some case |
| 194 |
sudo port -d -N selfupdate 2>&1 | grep -v DEBUG | awk '{if($1!="x")print}' |
| 195 |
port info xdrfile > /dev/null && break || true |
| 196 |
sleep 5 |
| 197 |
i=$((i+1)) |
| 198 |
if ((i>20)) ; then |
| 199 |
echo "macports-ci: Failed after $i iterations" |
| 200 |
exit 1 |
| 201 |
fi |
| 202 |
done |
| 203 |
|
| 204 |
echo "macports-ci: Selfupdate successful after $i iterations" |
| 205 |
|
| 206 |
dir="$PWD" |
| 207 |
popd |
| 208 |
sudo rm -fr $dir |
| 209 |
|
| 210 |
;; |
| 211 |
|
| 212 |
(localports) |
| 213 |
|
| 214 |
echo "macports-ci: localports" |
| 215 |
|
| 216 |
for opt |
| 217 |
do |
| 218 |
case "$opt" in |
| 219 |
(*) ports="$opt" ;; |
| 220 |
esac |
| 221 |
done |
| 222 |
|
| 223 |
if ! test -d "$ports" ; then |
| 224 |
echo "macports-ci: Please provide a port directory" |
| 225 |
exit 1 |
| 226 |
fi |
| 227 |
|
| 228 |
w=$(which port) |
| 229 |
|
| 230 |
MACPORTS_PREFIX="${w%/bin/port}" |
| 231 |
|
| 232 |
cd "$ports" |
| 233 |
|
| 234 |
ports="$(pwd)" |
| 235 |
|
| 236 |
echo "macports-ci: Portdir fullpath: $ports" |
| 237 |
SOURCES="${MACPORTS_PREFIX}"/etc/macports/sources.conf |
| 238 |
|
| 239 |
awk -v repo="file://$ports" '{if($NF=="[default]") print repo; print}' "$SOURCES" > $$.tmp |
| 240 |
sudo mv -f $$.tmp "$SOURCES" |
| 241 |
|
| 242 |
portindex |
| 243 |
|
| 244 |
;; |
| 245 |
|
| 246 |
(ccache) |
| 247 |
w=$(which port) |
| 248 |
MACPORTS_PREFIX="${w%/bin/port}" |
| 249 |
|
| 250 |
echo "macports-ci: ccache" |
| 251 |
|
| 252 |
ccache_do=install |
| 253 |
|
| 254 |
for opt |
| 255 |
do |
| 256 |
case "$opt" in |
| 257 |
(--save) ccache_do=save ;; |
| 258 |
(--install) ccache_do=install ;; |
| 259 |
(*) echo "macports-ci: ccache: unknown option $opt" |
| 260 |
exit 1 ;; |
| 261 |
esac |
| 262 |
done |
| 263 |
|
| 264 |
|
| 265 |
case "$ccache_do" in |
| 266 |
(install) |
| 267 |
# first install ccache |
| 268 |
sudo port -N install ccache |
| 269 |
# then tell macports to use it |
| 270 |
CONF="${MACPORTS_PREFIX}"/etc/macports/macports.conf |
| 271 |
awk '{if(match($0,"configureccache")) print "configureccache yes" ; else print }' "$CONF" > $$.tmp |
| 272 |
sudo mv -f $$.tmp "$CONF" |
| 273 |
|
| 274 |
# notice that cache size is set to 512Mb, same as it is set by Travis-CI on linux |
| 275 |
# might be changed in the future |
| 276 |
test -f "$HOME"/.macports-ci-ccache/ccache.conf && |
| 277 |
sudo rm -fr "$MACPORTS_PREFIX"/var/macports/build/.ccache && |
| 278 |
sudo mkdir -p "$MACPORTS_PREFIX"/var/macports/build/.ccache && |
| 279 |
sudo cp -a "$HOME"/.macports-ci-ccache/* "$MACPORTS_PREFIX"/var/macports/build/.ccache/ && |
| 280 |
sudo echo "max_size = 512M" > "$MACPORTS_PREFIX"/var/macports/build/.ccache/ccache.conf && |
| 281 |
sudo chown -R macports:admin "$MACPORTS_PREFIX"/var/macports/build/.ccache |
| 282 |
|
| 283 |
;; |
| 284 |
(save) |
| 285 |
|
| 286 |
sudo rm -fr "$HOME"/.macports-ci-ccache |
| 287 |
sudo mkdir -p "$HOME"/.macports-ci-ccache |
| 288 |
sudo cp -a "$MACPORTS_PREFIX"/var/macports/build/.ccache/* "$HOME"/.macports-ci-ccache/ |
| 289 |
|
| 290 |
esac |
| 291 |
|
| 292 |
CCACHE_DIR="$MACPORTS_PREFIX"/var/macports/build/.ccache/ ccache -s |
| 293 |
|
| 294 |
;; |
| 295 |
|
| 296 |
(*) |
| 297 |
echo "macports-ci: unknown action $action" |
| 298 |
|
| 299 |
esac |
| 300 |
|
| 301 |
) |
| 302 |
|
| 303 |
# allows setting env var if necessary: |
| 304 |
source "$MACPORTS_CI_SOURCEME" |