Skip to content

Commit 8a57e97

Browse files
committed
Add macOS arm64 support
1 parent a03d649 commit 8a57e97

File tree

3 files changed

+76
-38
lines changed

3 files changed

+76
-38
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ if(ITKPythonPackage_SUPERBUILD)
7272
#-----------------------------------------------------------------------------
7373
include(ExternalProject)
7474

75-
set(ITK_REPOSITORY "https://github.com/InsightSoftwareConsortium/ITK.git")
75+
set(ITK_REPOSITORY "https://github.com/thewtex/ITK.git")
7676

7777
# ITK nightly-master 2021-04-02
78-
set(ITK_GIT_TAG "3f8b14be54c78d9ecc0b8e8655b778c44c1d6c51")
78+
set(ITK_GIT_TAG "fde7fa6ce846fcc53f4378290484abdccbe318bf")
7979

8080
#-----------------------------------------------------------------------------
8181
# A separate project is used to download ITK, so that it can reused

scripts/macpython-build-wheels.sh

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ source "${script_dir}/macpython-build-common.sh"
2020
# Ensure that requirements are met
2121
brew update
2222
brew info doxygen | grep --quiet 'Not installed' && brew install doxygen
23+
brew info ninja | grep --quiet 'Not installed' && brew install ninja
24+
NINJA_EXECUTABLE=$(which ninja)
25+
brew info cmake | grep --quiet 'Not installed' && brew install cmake
26+
CMAKE_EXECUTABLE=$(which cmake)
2327
# -----------------------------------------------------------------------
2428
# Remove previous virtualenv's
2529
rm -rf ${SCRIPT_DIR}/../venvs
@@ -38,11 +42,7 @@ for PYBIN in "${PYBINARIES[@]}"; do
3842
done
3943

4044
VENV="${VENVS[0]}"
41-
Python3_EXECUTABLE=${VENV}/bin/python
42-
${Python3_EXECUTABLE} -m pip install --no-cache cmake
43-
CMAKE_EXECUTABLE=${VENV}/bin/cmake
44-
${Python3_EXECUTABLE} -m pip install --no-cache ninja
45-
NINJA_EXECUTABLE=${VENV}/bin/ninja
45+
Python3_EXECUTABLE=${VENV}/bin/python3
4646
${Python3_EXECUTABLE} -m pip install --no-cache delocate
4747
DELOCATE_LISTDEPS=${VENV}/bin/delocate-listdeps
4848
DELOCATE_WHEEL=${VENV}/bin/delocate-wheel
@@ -69,14 +69,19 @@ for VENV in "${VENVS[@]}"; do
6969
echo "Python3_EXECUTABLE:${Python3_EXECUTABLE}"
7070
echo "Python3_INCLUDE_DIR:${Python3_INCLUDE_DIR}"
7171

72-
# Install dependencies
73-
${Python3_EXECUTABLE} -m pip install --upgrade -r ${SCRIPT_DIR}/../requirements-dev.txt
74-
7572
build_type="Release"
76-
plat_name="macosx-10.9-x86_64"
77-
osx_target="10.9"
73+
if [[ $(arch) == "arm64" ]]; then
74+
${Python3_EXECUTABLE} -m pip install --upgrade scikit-build
75+
plat_name="macosx-10.9-x86_64"
76+
osx_target="10.9"
77+
build_path="${SCRIPT_DIR}/../ITK-${py_mm}-macosx_x86_64"
78+
else
79+
${Python3_EXECUTABLE} -m pip install --upgrade -r ${SCRIPT_DIR}/../requirements-dev.txt
80+
plat_name="macosx-11.0-arm64"
81+
osx_target="11.0"
82+
build_path="${SCRIPT_DIR}/../ITK-${py_mm}-macosx_arm64"
83+
fi
7884
source_path=${SCRIPT_DIR}/../ITK-source/ITK
79-
build_path="${SCRIPT_DIR}/../ITK-${py_mm}-macosx_x86_64"
8085
SETUP_PY_CONFIGURE="${script_dir}/setup_py_configure.py"
8186

8287
# Clean up previous invocations
@@ -178,11 +183,15 @@ ${DELOCATE_LISTDEPS} ${SCRIPT_DIR}/../dist/*.whl # lists library dependencies
178183
${DELOCATE_WHEEL} ${SCRIPT_DIR}/../dist/*.whl # copies library dependencies into wheel
179184

180185
# Install packages and test
181-
for VENV in "${VENVS[@]}"; do
182-
${VENV}/bin/pip install numpy
183-
${VENV}/bin/pip install itk --no-cache-dir --no-index -f ${SCRIPT_DIR}/../dist
184-
(cd $HOME && ${VENV}/bin/python -c 'import itk;')
185-
(cd $HOME && ${VENV}/bin/python -c 'import itk; image = itk.Image[itk.UC, 2].New()')
186-
(cd $HOME && ${VENV}/bin/python -c 'import itkConfig; itkConfig.LazyLoading = False; import itk;')
187-
(cd $HOME && ${VENV}/bin/python ${SCRIPT_DIR}/../docs/code/testDriver.py )
188-
done
186+
# numpy wheel not currently available for the M1
187+
# https://github.com/numpy/numpy/issues/17807
188+
if [[ $(arch) != "arm64" ]]; then
189+
for VENV in "${VENVS[@]}"; do
190+
${VENV}/bin/pip install numpy
191+
${VENV}/bin/pip install itk --no-cache-dir --no-index -f ${SCRIPT_DIR}/../dist
192+
(cd $HOME && ${VENV}/bin/python -c 'import itk;')
193+
(cd $HOME && ${VENV}/bin/python -c 'import itk; image = itk.Image[itk.UC, 2].New()')
194+
(cd $HOME && ${VENV}/bin/python -c 'import itkConfig; itkConfig.LazyLoading = False; import itk;')
195+
(cd $HOME && ${VENV}/bin/python ${SCRIPT_DIR}/../docs/code/testDriver.py )
196+
done
197+
fi

scripts/macpython-install-python.sh

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ GET_PIP_URL=https://bootstrap.pypa.io/get-pip.py
4343
DOWNLOADS_SDIR=downloads
4444
WORKING_SDIR=working
4545

46-
# As of 26 January 2021 - latest Python of each version with binary download
46+
# As of 5 April 2021 - latest Python of each version with binary download
4747
# available.
4848
# See: https://www.python.org/downloads/mac-osx/
4949
LATEST_2p7=2.7.18
5050
LATEST_3p5=3.5.4
5151
LATEST_3p6=3.6.8
5252
LATEST_3p7=3.7.9
53-
LATEST_3p8=3.8.7
54-
LATEST_3p9=3.9.1
53+
LATEST_3p8=3.8.9
54+
LATEST_3p9=3.9.4
5555

5656

5757
function check_python {
@@ -135,7 +135,9 @@ function macpython_sdk_list_for_version {
135135
local _major=${_ver%%.*}
136136
local _return
137137

138-
if [ "$_major" -eq "2" ]; then
138+
if [ "${PLAT}" = "arm64" ]; then
139+
_return="11.0"
140+
elif [ "$_major" -eq "2" ]; then
139141
[ $(lex_ver $_ver) -lt $(lex_ver 2.7.18) ] && _return="10.6"
140142
[ $(lex_ver $_ver) -ge $(lex_ver 2.7.15) ] && _return="$_return 10.9"
141143
elif [ "$_major" -eq "3" ]; then
@@ -187,7 +189,15 @@ function pyinst_fname_for_version {
187189
local py_version=$1
188190
local py_osx_ver=${2:-$(macpython_sdk_for_version $py_version)}
189191
local inst_ext=$(pyinst_ext_for_version $py_version)
190-
echo "python-${py_version}-macosx${py_osx_ver}.${inst_ext}"
192+
if [ "${PLAT:-}" == "arm64" ] || [ "${PLAT:-}" == "universal2" ]; then
193+
if [ "$py_version" == "3.9.1" ]; then
194+
echo "python-${py_version}-macos11.0.${inst_ext}"
195+
else
196+
echo "python-${py_version}-macos11.${inst_ext}"
197+
fi
198+
else
199+
echo "python-${py_version}-macosx${py_osx_ver}.${inst_ext}"
200+
fi
191201
}
192202

193203
function get_macpython_arch {
@@ -200,7 +210,7 @@ function get_macpython_arch {
200210
# Note: MUST only be called after the version of Python used to build the
201211
# target wheel has been installed and is on the path
202212
local distutils_plat=${1:-$(get_distutils_platform)}
203-
if [[ $distutils_plat =~ macosx-(10\.[0-9]+)-(.*) ]]; then
213+
if [[ $distutils_plat =~ macosx-(1[0-9]\.[0-9]+)-(.*) ]]; then
204214
echo ${BASH_REMATCH[2]}
205215
else
206216
echo "Error parsing macOS distutils platform '$distutils_plat'"
@@ -218,7 +228,7 @@ function get_macpython_osx_ver {
218228
# Note: MUST only be called after the version of Python used to build the
219229
# target wheel has been installed and is on the path
220230
local distutils_plat=${1:-$(get_distutils_platform)}
221-
if [[ $distutils_plat =~ macosx-(10\.[0-9]+)-(.*) ]]; then
231+
if [[ $distutils_plat =~ macosx-(1[0-9]\.[0-9]+)-(.*) ]]; then
222232
echo ${BASH_REMATCH[1]}
223233
else
224234
echo "Error parsing macOS distutils platform '$distutils_plat'"
@@ -319,10 +329,19 @@ function install_mac_cpython {
319329
# sets $PYTHON_EXE variable to Python executable
320330
local py_version=$(fill_pyver $1)
321331
local py_osx_ver=$2
332+
#local py_stripped=$(strip_ver_suffix $py_version)
333+
local py_stripped=$py_version
322334
local py_inst=$(pyinst_fname_for_version $py_version $py_osx_ver)
323335
local inst_path=$DOWNLOADS_SDIR/$py_inst
336+
local retval=""
324337
mkdir -p $DOWNLOADS_SDIR
325-
curl $MACPYTHON_URL/$py_version/${py_inst} > $inst_path
338+
# exit early on curl errors, but don't let it exit the shell
339+
curl -f $MACPYTHON_URL/$py_stripped/${py_inst} > $inst_path || retval=$?
340+
if [ ${retval:-0} -ne 0 ]; then
341+
echo "Python download failed! Check ${py_inst} exists on the server."
342+
exit $retval
343+
fi
344+
326345
if [ "${py_inst: -3}" == "dmg" ]; then
327346
hdiutil attach $inst_path -mountpoint /Volumes/Python
328347
inst_path=/Volumes/Python/Python.mpkg
@@ -372,12 +391,22 @@ function make_workon_venv {
372391
# Remove previous versions
373392
sudo rm -rf ${MACPYTHON_FRAMEWORK}
374393

375-
for pyversion in $LATEST_3p6; do
376-
install_macpython $pyversion 10.6
377-
install_virtualenv
378-
done
379-
380-
for pyversion in $LATEST_3p7 $LATEST_3p8 $LATEST_3p9; do
381-
install_macpython $pyversion 10.9
382-
install_virtualenv
383-
done
394+
if test "$(arch)" == "arm64"; then
395+
echo "we are arm"
396+
PLAT=arm64
397+
for pyversion in $LATEST_3p9; do
398+
install_macpython $pyversion 11
399+
install_virtualenv
400+
done
401+
else
402+
# intel
403+
for pyversion in $LATEST_3p6; do
404+
install_macpython $pyversion 10.6
405+
install_virtualenv
406+
done
407+
408+
for pyversion in $LATEST_3p7 $LATEST_3p8 $LATEST_3p9; do
409+
install_macpython $pyversion 10.9
410+
install_virtualenv
411+
done
412+
fi

0 commit comments

Comments
 (0)