osx_utils.sh 18.3 KB
Newer Older
Matthew Brett's avatar
Matthew Brett committed
1
2
3
4
#!/bin/bash
# Use with ``source osx_utils.sh``
set -e

5
6
7
8
# Get our own location on this filesystem, load common utils
MULTIBUILD_DIR=$(dirname "${BASH_SOURCE[0]}")
source $MULTIBUILD_DIR/common_utils.sh

Matthew Brett's avatar
Matthew Brett committed
9
10
11
12
MACPYTHON_URL=https://www.python.org/ftp/python
MACPYTHON_PY_PREFIX=/Library/Frameworks/Python.framework/Versions
WORKING_SDIR=working

Andrew Murray's avatar
Andrew Murray committed
13
# As of 28 Mar 2023 - latest Python of each version with binary download
Matthew Brett's avatar
Matthew Brett committed
14
# available.
Andrew Murray's avatar
Andrew Murray committed
15
# See: https://www.python.org/downloads/macos/
robbuckley's avatar
robbuckley committed
16
LATEST_2p7=2.7.18
Andrew Murray's avatar
Andrew Murray committed
17
LATEST_3p5=3.5.4
Andrew Murray's avatar
Andrew Murray committed
18
LATEST_3p6=3.6.8
Andrew Murray's avatar
Andrew Murray committed
19
LATEST_3p7=3.7.9
Andrew Murray's avatar
Andrew Murray committed
20
LATEST_3p8=3.8.10
Andrew Murray's avatar
Andrew Murray committed
21
LATEST_3p9=3.9.13
Andrew Murray's avatar
Andrew Murray committed
22
23
LATEST_3p10=3.10.10
LATEST_3p11=3.11.2
Matthew Brett's avatar
Matthew Brett committed
24

Matthew Brett's avatar
Matthew Brett committed
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

function check_python {
    if [ -z "$PYTHON_EXE" ]; then
        echo "PYTHON_EXE variable not defined"
        exit 1
    fi
}

function check_pip {
    if [ -z "$PIP_CMD" ]; then
        echo "PIP_CMD variable not defined"
        exit 1
    fi
}

function check_var {
    if [ -z "$1" ]; then
        echo "required variable not defined"
        exit 1
    fi
}

function get_py_digit {
    check_python
    $PYTHON_EXE -c "import sys; print(sys.version_info[0])"
}

function get_py_mm {
    check_python
    $PYTHON_EXE -c "import sys; print('{0}.{1}'.format(*sys.version_info[0:2]))"
}

function get_py_mm_nodot {
    check_python
    $PYTHON_EXE -c "import sys; print('{0}{1}'.format(*sys.version_info[0:2]))"
}

function get_py_prefix {
    check_python
    $PYTHON_EXE -c "import sys; print(sys.prefix)"
}

function fill_pyver {
    # Convert major or major.minor format to major.minor.micro
    #
    # Hence:
    # 2 -> 2.7.11  (depending on LATEST_2p7 value)
    # 2.7 -> 2.7.11  (depending on LATEST_2p7 value)
    local ver=$1
    check_var $ver
    if [[ $ver =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
        # Major.minor.micro format already
        echo $ver
    elif [ $ver == 2 ] || [ $ver == "2.7" ]; then
        echo $LATEST_2p7
Andrew Murray's avatar
Andrew Murray committed
80
    elif [ $ver == 3 ] || [ $ver == "3.11" ]; then
Andrew Murray's avatar
Andrew Murray committed
81
        echo $LATEST_3p11
Andrew Murray's avatar
Andrew Murray committed
82
83
    elif [ $ver == "3.10" ]; then
        echo $LATEST_3p10
Andrew Murray's avatar
Andrew Murray committed
84
    elif [ $ver == "3.9" ]; then
Andrew Murray's avatar
Andrew Murray committed
85
86
        echo $LATEST_3p9
    elif [ $ver == "3.8" ]; then
87
88
        echo $LATEST_3p8
    elif [ $ver == "3.7" ]; then
Andrew Murray's avatar
Andrew Murray committed
89
90
        echo $LATEST_3p7
    elif [ $ver == "3.6" ]; then
Matthew Brett's avatar
Matthew Brett committed
91
92
        echo $LATEST_3p6
    elif [ $ver == "3.5" ]; then
Matthew Brett's avatar
Matthew Brett committed
93
94
        echo $LATEST_3p5
    else
95
        echo "Can't fill version $ver" 1>&2
Matthew Brett's avatar
Matthew Brett committed
96
97
98
99
        exit 1
    fi
}

100
101
102
103
104
105
106
107
108
109
110
function macpython_sdk_list_for_version {
    # return a list of SDK targets supported for a given CPython version
    # Parameters
    #   $py_version (python version in major.minor.extra format)
    # eg
    #  macpython_sdks_for_version 2.7.15
    #  >> 10.6 10.9
    local _ver=$(fill_pyver $1)
    local _major=${_ver%%.*}
    local _return

111
    if [ "$(uname -m)" = "arm64" ]; then
112
113
        _return="11.0"
    elif [ "$_major" -eq "2" ]; then
robbuckley's avatar
robbuckley committed
114
        [ $(lex_ver $_ver) -lt $(lex_ver 2.7.18) ] && _return="10.6"
115
116
117
118
119
120
121
122
123
124
125
126
127
        [ $(lex_ver $_ver) -ge $(lex_ver 2.7.15) ] && _return="$_return 10.9"
    elif [ "$_major" -eq "3" ]; then
        [ $(lex_ver $_ver) -lt $(lex_ver 3.8)    ] && _return="10.6"
        [ $(lex_ver $_ver) -ge $(lex_ver 3.6.5)  ] && _return="$_return 10.9"
    else
        echo "Error version=${_ver}, expecting 2.x or 3.x" 1>&2
        exit 1
    fi
    echo $_return
}

function macpython_sdk_for_version {
    # assumes the output of macpython_sdk_list_for_version is a list
Andrew Murray's avatar
Andrew Murray committed
128
    # of SDK versions XX.Y in sorted order, eg "10.6 10.9" or "10.9"
129
130
131
    echo $(macpython_sdk_list_for_version $1) | awk -F' ' '{print $NF}'
}

Matthew Brett's avatar
Matthew Brett committed
132
133
134
135
136
137
138
139
function pyinst_ext_for_version {
    # echo "pkg" or "dmg" depending on the passed Python version
    # Parameters
    #   $py_version (python version in major.minor.extra format)
    #
    # Earlier Python installers are .dmg, later are .pkg.
    local py_version=$1
    check_var $py_version
140
    py_version=$(fill_pyver $py_version)
Matthew Brett's avatar
Matthew Brett committed
141
142
    local py_0=${py_version:0:1}
    if [ $py_0 -eq 2 ]; then
143
        if [ "$(lex_ver $py_version)" -ge "$(lex_ver 2.7.9)" ]; then
Matthew Brett's avatar
Matthew Brett committed
144
145
146
147
148
            echo "pkg"
        else
            echo "dmg"
        fi
    elif [ $py_0 -ge 3 ]; then
Andrew Murray's avatar
Andrew Murray committed
149
		echo "pkg"
Matthew Brett's avatar
Matthew Brett committed
150
151
152
    fi
}

153
function pyinst_fname_for_version {
154
    # echo filename for OSX installer file given Python and minimum
155
    # macOS versions
156
    # Parameters
157
    #   $py_version (Python version in major.minor.extra format)
158
    #   $py_osx_ver: {major.minor | not defined}
159
160
161
    #       if defined, the minimum macOS SDK version that Python is
    #       built for, eg: "10.6" or "10.9", if not defined, infers
    #       this from $py_version using macpython_sdk_for_version
162
163
    local py_version=$1
    local inst_ext=$(pyinst_ext_for_version $py_version)
Isuru Fernando's avatar
Isuru Fernando committed
164
165
166
    # Use the universal2 installer if we are on arm64
    # universal2 installer for python 3.8 needs macos 11.0 to run on
    # and therefore x86_64 builds use the intel only installer.
167
168
169
170
    # Note that intel only installer can create universal2 wheels, but
    # creates intel only wheels by default. When PLAT=universal2
    # we set the env variable _PYTHON_HOST_PLATFORM to change this
    # default.
171
    if [ "$(uname -m)" == "arm64" ] || [ $(lex_ver $py_version) -ge $(lex_ver 3.10.0) ]; then
Isuru Fernando's avatar
Isuru Fernando committed
172
173
174
175
176
      if [ "$py_version" == "3.9.1" ]; then
        echo "python-${py_version}-macos11.0.${inst_ext}"
      else
        echo "python-${py_version}-macos11.${inst_ext}"
      fi
177
    else
178
      local py_osx_ver=${2:-$(macpython_sdk_for_version $py_version)}
179
180
      echo "python-${py_version}-macosx${py_osx_ver}.${inst_ext}"
    fi
181
182
}

183
function get_macpython_arch {
robbuckley's avatar
robbuckley committed
184
185
186
187
188
189
    # echo arch (e.g. intel or x86_64), extracted from the distutils platform tag
    # Parameters
    #   $distutils_plat   PEP425 style platform tag, or if not provided, calls
    #                       the function get_distutils_platform, provided by
    #                       common_utils.sh. Fails if this is not a mac platform
    #
Andrew Murray's avatar
Andrew Murray committed
190
    # Note: MUST only be called after the version of Python used to build the
robbuckley's avatar
robbuckley committed
191
192
    # target wheel has been installed and is on the path
    local distutils_plat=${1:-$(get_distutils_platform)}
Isuru Fernando's avatar
Isuru Fernando committed
193
    if [[ $distutils_plat =~ macosx-(1[0-9]\.[0-9]+)-(.*) ]]; then
194
195
        echo ${BASH_REMATCH[2]}
    else
robbuckley's avatar
robbuckley committed
196
        echo "Error parsing macOS distutils platform '$distutils_plat'"
197
198
199
200
201
        exit 1
    fi
}

function get_macpython_osx_ver {
robbuckley's avatar
robbuckley committed
202
203
204
205
206
207
    # echo minimum macOS version (e.g. 10.9) from the distutils platform tag
    # Parameters
    #   $distutils_plat   PEP425 style platform tag, or if not provided, calls
    #                       the function get_distutils_platform, provided by
    #                       common_utils.sh. Fails if this is not a mac platform
    #
Andrew Murray's avatar
Andrew Murray committed
208
    # Note: MUST only be called after the version of Python used to build the
robbuckley's avatar
robbuckley committed
209
210
    # target wheel has been installed and is on the path
    local distutils_plat=${1:-$(get_distutils_platform)}
Isuru Fernando's avatar
Isuru Fernando committed
211
    if [[ $distutils_plat =~ macosx-(1[0-9]\.[0-9]+)-(.*) ]]; then
212
213
        echo ${BASH_REMATCH[1]}
    else
robbuckley's avatar
robbuckley committed
214
        echo "Error parsing macOS distutils platform '$distutils_plat'"
215
216
217
        exit 1
    fi
}
218

219
function macpython_arch_for_version {
220
221
    # echo arch (intel or x86_64) that a version of Python is expected
    # to be built for
222
    # Parameters
223
224
    #   $py_ver     Python version, in the format (major.minor.patch) for
    #               CPython, or pypy-(major.minor) for PyPy
225
    #   $py_osx_ver minimum macOS version the target Python is built for
226
    #               (major.minor)
227
228
229
230
231
232
233
234
235
    local py_ver=$1
    local py_osx_ver=${2:-$MB_PYTHON_OSX_VER}
    check_var $1
    if [[ $(macpython_impl_for_version $py_ver) == "cp" ]]; then
        if [[ "$py_osx_ver" == "10.6" ]]; then
            echo "intel"
        elif [[ "$py_osx_ver" == "10.9" ]]; then
            echo "x86_64"
        else
236
            echo "Unexpected CPython macOS version: ${py_osx_ver}, supported values: 10.6 and 10.9"
237
238
239
            exit 1
        fi
    else
Rob Buckley's avatar
Rob Buckley committed
240
        echo "x86_64"
241
242
243
244
245
246
    fi
}

function macpython_impl_for_version {
    # echo Python implementation (cp for CPython, pp for PyPy) given a
    # suitably formatted version string
247
248
    # Parameters:
    #     $version : [implementation-]major[.minor[.patch]]
249
250
    #         Python implementation, e.g. "3.6" for CPython or
    #         "pypy-5.4" for PyPy
251
    local version=$1
252
    check_var $1
mattip's avatar
mattip committed
253
    if [[ "$version" =~ ^pypy ]]; then
254
        echo pp
255
    elif [[ "$version" =~ ([0-9\.]+) ]]; then
256
        echo cp
Kyle Stewart's avatar
Kyle Stewart committed
257
    else
Matt Bachmann's avatar
Matt Bachmann committed
258
        echo "config error: Issue parsing this implementation in install_python:"
259
        echo "    version=$version"
Kyle Stewart's avatar
Kyle Stewart committed
260
261
262
263
        exit 1
    fi
}

264
function strip_macpython_ver_prefix {
265
    # strip any implementation prefix from a Python version string
266
267
268
269
270
271
272
273
274
275
276
    # Parameters:
    #     $version : [implementation-]major[.minor[.patch]]
    #         Python implementation, e.g. "3.6" for CPython or
    #         "pypy-5.4" for PyPy
    local version=$1
    check_var $1
    if [[ "$version" =~ (pypy-)?([0-9\.]+) ]]; then
        echo ${BASH_REMATCH[2]}
    fi
}

277
278
279
280
function install_macpython {
    # Install Python and set $PYTHON_EXE to the installed executable
    # Parameters:
    #     $version : [implementation-]major[.minor[.patch]]
mattip's avatar
mattip committed
281
    #         The Python implementation to install, e.g. "3.6", "pypy-5.4" or "pypy3.6-7.2"
282
    #     $py_osx_ver: {major.minor | not defined}
283
284
    #       if defined, the macOS version that CPython is built for, e.g.
    #       "10.6" or "10.9". Ignored for PyPy
285
    local version=$1
286
    local py_osx_ver=$2
287
288
    local impl=$(macpython_impl_for_version $version)
    if [[ "$impl" == "pp" ]]; then
mattip's avatar
mattip committed
289
        install_pypy $version
290
    elif [[ "$impl" == "cp" ]]; then
291
        local stripped_ver=$(strip_macpython_ver_prefix $version)
292
        install_mac_cpython $stripped_ver $py_osx_ver
Kyle Stewart's avatar
Kyle Stewart committed
293
    else
294
        echo "Unexpected Python impl: ${impl}"
Kyle Stewart's avatar
Kyle Stewart committed
295
296
297
298
        exit 1
    fi
}

299
function install_mac_cpython {
Matthew Brett's avatar
Matthew Brett committed
300
    # Installs Python.org Python
301
302
303
    # Parameters
    #   $py_version
    #       Version given in major or major.minor or major.minor.micro e.g
Andrew Murray's avatar
Andrew Murray committed
304
    #       "3" or "3.7" or "3.7.1".
305
    #   $py_osx_ver
306
    #       {major.minor | not defined}
307
    #       if defined, the macOS version that Python is built for, e.g.
308
    #        "10.6" or "10.9"
309
    # sets $PYTHON_EXE variable to Python executable
Matthew Brett's avatar
Matthew Brett committed
310
    local py_version=$(fill_pyver $1)
Rob Buckley's avatar
Rob Buckley committed
311
    local py_osx_ver=$2
312
    local py_stripped=$(strip_ver_suffix $py_version)
313
    local py_inst=$(pyinst_fname_for_version $py_version $py_osx_ver)
Matthew Brett's avatar
Matthew Brett committed
314
    local inst_path=$DOWNLOADS_SDIR/$py_inst
robbuckley's avatar
robbuckley committed
315
    local retval=""
Matthew Brett's avatar
Matthew Brett committed
316
    mkdir -p $DOWNLOADS_SDIR
robbuckley's avatar
robbuckley committed
317
    # exit early on curl errors, but don't let it exit the shell
robbuckley's avatar
robbuckley committed
318
319
320
321
322
323
    cmd_notexit curl -f $MACPYTHON_URL/$py_stripped/${py_inst} > $inst_path || retval=$?
    if [ ${retval:-0} -ne 0 ]; then
      echo "Python download failed! Check ${py_inst} exists on the server."
      exit $retval
    fi

324
    if [ "${py_inst: -3}" == "dmg" ]; then
Matthew Brett's avatar
Matthew Brett committed
325
326
327
328
        hdiutil attach $inst_path -mountpoint /Volumes/Python
        inst_path=/Volumes/Python/Python.mpkg
    fi
    sudo installer -pkg $inst_path -target /
329
    local py_mm=${py_version%.*}
Matthew Brett's avatar
Matthew Brett committed
330
    PYTHON_EXE=$MACPYTHON_PY_PREFIX/$py_mm/bin/python$py_mm
331
332
333
334
335
    # Install certificates for Python 3.6
    local inst_cmd="/Applications/Python ${py_mm}/Install Certificates.command"
    if [ -e "$inst_cmd" ]; then
        sh "$inst_cmd"
    fi
Matthew Brett's avatar
Matthew Brett committed
336
337
338
339
340
341
342
343
344
345
346
}

function install_virtualenv {
    # Generic install of virtualenv
    # Installs virtualenv into python given by $PYTHON_EXE
    # Assumes virtualenv will be installed into same directory as $PYTHON_EXE
    check_pip
    # Travis VMS install virtualenv for system python by default - force
    # install even if installed already
    $PIP_CMD install virtualenv --ignore-installed
    check_python
347
    VIRTUALENV_CMD="$(dirname $PYTHON_EXE)/virtualenv"
Matthew Brett's avatar
Matthew Brett committed
348
349
350
351
352
353
354
355
356
357
358
359
360
}

function make_workon_venv {
    # Make a virtualenv in given directory ('venv' default)
    # Set $PYTHON_EXE, $PIP_CMD to virtualenv versions
    # Parameter $venv_dir
    #    directory for virtualenv
    local venv_dir=$1
    if [ -z "$venv_dir" ]; then
        venv_dir="venv"
    fi
    venv_dir=`abspath $venv_dir`
    check_python
xoviat's avatar
xoviat committed
361
    $PYTHON_EXE -m virtualenv $venv_dir
Matthew Brett's avatar
Matthew Brett committed
362
363
364
365
366
367
    PYTHON_EXE=$venv_dir/bin/python
    PIP_CMD=$venv_dir/bin/pip
}

function remove_travis_ve_pip {
    # Remove travis installs of virtualenv and pip
xoviat's avatar
xoviat committed
368
369
    # FIXME: What if virtualenv is installed but pip is not?
    if [ "$(sudo which virtualenv)" == /usr/local/bin/virtualenv ] && [ "$(sudo which pip)" == /usr/local/bin/pip ]; then
Matthew Brett's avatar
Matthew Brett committed
370
371
372
373
374
375
376
377
        sudo pip uninstall -y virtualenv;
    fi
    if [ "$(sudo which pip)" == /usr/local/bin/pip ]; then
        sudo pip uninstall -y pip;
    fi
}

function set_py_vars {
378
    # Used by terryfy project; left here for back-compatibility
Matthew Brett's avatar
Matthew Brett committed
379
380
381
382
    export PATH="`dirname $PYTHON_EXE`:$PATH"
    export PYTHON_EXE PIP_CMD
}

383
function get_macpython_environment {
Matthew Brett's avatar
Matthew Brett committed
384
385
    # Set up MacPython environment
    # Parameters:
386
387
    #     $version : [implementation-]major[.minor[.patch]]
    #         The Python implementation to install, e.g. "3.6" or "pypy-5.4"
Matthew Brett's avatar
Matthew Brett committed
388
389
390
    #     $venv_dir : {directory_name|not defined}
    #         If defined - make virtualenv in this directory, set python / pip
    #         commands accordingly
391
    #     $py_osx_ver: {major.minor | not defined}
392
393
    #         if defined, the macOS version that Python is built for, e.g.
    #         "10.6" or "10.9", if not defined, use the version from MB_PYTHON_OSX_VER
Matthew Brett's avatar
Matthew Brett committed
394
395
396
397
398
399
    #
    # Installs Python
    # Sets $PYTHON_EXE to path to Python executable
    # Sets $PIP_CMD to full command for pip (including sudo if necessary)
    # If $venv_dir defined, Sets $VIRTUALENV_CMD to virtualenv executable
    # Puts directory of $PYTHON_EXE on $PATH
400
401
    local version=$1
    local venv_dir=$2
402
    local py_osx_ver=${3:-$MB_PYTHON_OSX_VER}
xoviat's avatar
xoviat committed
403
404
405
406

    if [ "$USE_CCACHE" == "1" ]; then
        activate_ccache
    fi
407

Matthew Brett's avatar
Matthew Brett committed
408
    remove_travis_ve_pip
409
    install_macpython $version $py_osx_ver
Matthew Brett's avatar
Matthew Brett committed
410
    PIP_CMD="$PYTHON_EXE -m pip"
Matthew Brett's avatar
Matthew Brett committed
411
412
413
    # Python 3.5 no longer compatible with latest pip
    if [ "$(get_py_mm)" == "3.5" ]; then
        # https://stackoverflow.com/a/29751768/1939576
414
        curl -LO https://bootstrap.pypa.io/pip/3.5/get-pip.py
Matthew Brett's avatar
Matthew Brett committed
415
416
417
418
419
420
        $PYTHON_EXE get-pip.py
        rm get-pip.py
    else
        $PYTHON_EXE -m ensurepip
        $PIP_CMD install --upgrade pip
    fi
xoviat's avatar
xoviat committed
421

Matthew Brett's avatar
Matthew Brett committed
422
423
424
    if [ -n "$venv_dir" ]; then
        install_virtualenv
        make_workon_venv $venv_dir
425
426
427
        source $venv_dir/bin/activate
    else
        export PATH="`dirname $PYTHON_EXE`:$PATH"
Matthew Brett's avatar
Matthew Brett committed
428
    fi
429
    export PYTHON_EXE PIP_CMD
Matthew Brett's avatar
Matthew Brett committed
430
}
431

432
function install_delocate {
433
434
    check_pip
    $PIP_CMD install delocate
435
436
437
438
439
}

function repair_wheelhouse {
    local wheelhouse=$1
    install_delocate
440
441
    delocate-wheel $wheelhouse/*.whl # copies library dependencies into wheel
}
442
443
444
445

function install_pkg_config {
    # Install pkg-config avoiding error from homebrew
    # See :
Hugo van Kemenade's avatar
Hugo van Kemenade committed
446
    # https://github.com/multi-build/multibuild/issues/24#issue-221951587
447
448
    command -v pkg-config > /dev/null 2>&1 || brew install pkg-config
}
xoviat's avatar
xoviat committed
449
450

function activate_ccache {
xoviat's avatar
xoviat committed
451

xoviat's avatar
xoviat committed
452
453
454
455
456
    brew install ccache
    export PATH=/usr/local/opt/ccache/libexec:$PATH
    export CCACHE_CPP2=1

    # Prove to the developer that ccache is activated
xoviat's avatar
xoviat committed
457
    echo "Using C compiler: $(which clang)"
xoviat's avatar
xoviat committed
458
}
459

460
function macos_intel_native_build_setup {
Isuru Fernando's avatar
Isuru Fernando committed
461
    # Setup native build for single arch x86_64 wheels
462
    export PLAT="x86_64"
463
    export _PYTHON_HOST_PLATFORM="macosx-${MB_PYTHON_OSX_VER}-x86_64"
464
465
466
467
468
469
470
    export CFLAGS+=" -arch x86_64"
    export CXXFLAGS+=" -arch x86_64"
    export ARCHFLAGS+=" -arch x86_64"
    export CPPFLAGS+=" -arch x86_64"
    export LDFLAGS+=" -arch x86_64"
}

471
function macos_intel_cross_build_setup {
Isuru Fernando's avatar
Isuru Fernando committed
472
    echo "universal2 builds on arm64 are not supported yet."
473
474
475
476
477
    exit 1
}

function macos_arm64_cross_build_setup {
    # Setup cross build for single arch arm_64 wheels
478
479
    export PLAT="arm64"
    export BUILD_PREFIX=/opt/arm64-builds
480
    sudo mkdir -p $BUILD_PREFIX/lib $BUILD_PREFIX/include
481
482
483
484
485
486
487
488
489
    sudo chown -R $USER $BUILD_PREFIX
    update_env_for_build_prefix
    export _PYTHON_HOST_PLATFORM="macosx-11.0-arm64"
    export CFLAGS+=" -arch arm64"
    export CXXFLAGS+=" -arch arm64"
    export CPPFLAGS+=" -arch arm64"
    export ARCHFLAGS+=" -arch arm64"
    export FCFLAGS+=" -arch arm64"
    export FC=$FC_ARM64
490
491
    export F90=${F90_ARM64:-${FC}}
    export F77=${F77_ARM64:-${FC}}
492
    export MACOSX_DEPLOYMENT_TARGET="11.0"
493
    export CROSS_COMPILING=1
494
495
496
497
498
    export LDFLAGS+=" -arch arm64 -L$BUILD_PREFIX/lib -Wl,-rpath,$BUILD_PREFIX/lib ${FC_ARM64_LDFLAGS:-}"
    # This would automatically let autoconf know that we are cross compiling for arm64 darwin
    export host_alias="aarch64-apple-darwin20.0.0"
}

499
500
501
function macos_arm64_native_build_setup {
    # Setup native build for single arch arm_64 wheels
    export PLAT="arm64"
Isuru Fernando's avatar
Isuru Fernando committed
502
    # We don't want universal2 builds and only want an arm64 build
503
    export _PYTHON_HOST_PLATFORM="macosx-11.0-arm64"
504
    export ARCHFLAGS+=" -arch arm64"
505
506
507
    $@
}

508
509
function fuse_macos_intel_arm64 {
    local wheelhouse=$(abspath ${WHEEL_SDIR:-wheelhouse})
510
    local py_osx_ver=$(echo ${MACOSX_DEPLOYMENT_TARGET} | sed "s/\./_/g")
Isuru Fernando's avatar
Isuru Fernando committed
511
    mkdir -p tmp_fused_wheelhouse
512
    for whl in $wheelhouse/*.whl; do
Isuru Fernando's avatar
Isuru Fernando committed
513
       if [[ "$whl" == *macosx_${py_osx_ver}_x86_64.whl ]]; then
514
           whl_base=$(sed "s/macosx_${py_osx_ver}_x86_64.whl//" <<< $whl)
515
           if [[ -f "${whl_base}macosx_11_0_arm64.whl" ]]; then
Isuru Fernando's avatar
Isuru Fernando committed
516
               delocate-fuse $whl "${whl_base}macosx_11_0_arm64.whl" -w tmp_fused_wheelhouse
Isuru Fernando's avatar
Isuru Fernando committed
517
               mv tmp_fused_wheelhouse/$(basename $whl) $wheelhouse/$(basename ${whl_base})macosx_${py_osx_ver}_universal2.whl
518
               # Since we want one wheel that's installable for testing we are deleting the *_x86_64 wheel.
519
520
521
522
523
524
525
526
527
528
               # We are not deleting arm64 wheel because the size is lower and homebrew/conda-forge python
               # will use them by default
               rm $whl
           fi
       fi
    done
}

function wrap_wheel_builder {
    if [[ "${PLAT:-}" == "universal2" ]]; then
529
530
531
532
533
534
535
536
537
        if [[ "$(uname -m)" == "arm64" ]]; then
            (macos_intel_cross_build_setup && $@)
            rm -rf *-stamp
            (macos_arm64_native_build_setup && $@)
        else
            (macos_intel_native_build_setup && $@)
            rm -rf *-stamp
            (macos_arm64_cross_build_setup && $@)
        fi
538
        fuse_macos_intel_arm64
539
540
541
542
543
544
    elif [[ "${PLAT:-}" == "arm64" ]]; then
        if [[ "$(uname -m)" == "arm64" ]]; then
            (macos_arm64_native_build_setup && $@)
        else
            (macos_arm64_cross_build_setup && $@)
        fi
Isuru Fernando's avatar
Isuru Fernando committed
545
    elif [[ "${PLAT:-}" == "x86_64" ]]; then
546
547
548
549
550
        if [[ "$(uname -m)" == "x86_64" ]]; then
            (macos_intel_native_build_setup && $@)
        else
            (macos_intel_cross_build_setup && $@)
        fi
Isuru Fernando's avatar
Isuru Fernando committed
551
552
    else
        $@
553
554
    fi
}