DiamondLightSource/cothread

Build failure w/ python 3.10 and osx/arm

Closed this issue · 4 comments

fyi. my P4P CI build for OSX/py3.10 appears to now include -arch arm64, which triggers a build failure.

      gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall
 -arch arm64 -arch x86_64 
 -isysroot /Applications/Xcode_12.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk 
 -I/usr/local/opt/sqlite/include -I/usr/local/opt/sqlite/include -I/Users/runner/hostedtoolcache/Python/3.10.5/x64/include/python3.10 
 -c context/switch.c -o build/temp.macosx-10.15-universal2-3.10/context/switch.o
 -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wundef -Wcast-align -Wwrite-strings
 -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes 
      context/switch.c:54:6: error: "Don't know how to support this platform"
          #error "Don't know how to support this platform"

A quick search suggests that apple mostly adheres to the aapcs64 convention.

I don't have access to any apple hardware, and so won't be able to test.

The fix may be as simple as:

diff --git a/context/switch.c b/context/switch.c
index 2e034e7..49f1c85 100644
--- a/context/switch.c
+++ b/context/switch.c
@@ -44,7 +44,7 @@
     #include "switch-x86.c"
 #elif defined(__x86_64__)
     #include "switch-x86_64.c"
-#elif defined(__aarch64__)  &&  defined(__unix__)
+#elif defined(__aarch64__)  &&  (defined(__unix__)  ||  defined(__APPLE__))
     #include "switch-arm64.c"
 #elif defined(__arm__)  &&  defined(__unix__)
     #include "switch-arm.c"

Can you test this?

I happen to come across this ticket today and I've run into the same problem before on darwin-aarch64 and can't remember if or how I worked around it... Anyway, I tried applying the proposed patch from @Araneidae but it didn't seem to do the trick:

EVAL="$(python cothread/load_ca.py)"  && \
        eval "$EVAL"  && \
        echo "libca_path = '$CATOOLS_LIBCA_PATH'" >cothread/libca_path.py
MODULEVER=0.0 python setup.py bdist_egg
running bdist_egg
running egg_info
creating cothread.egg-info
writing cothread.egg-info/PKG-INFO
writing dependency_links to cothread.egg-info/dependency_links.txt
writing entry points to cothread.egg-info/entry_points.txt
writing requirements to cothread.egg-info/requires.txt
writing top-level names to cothread.egg-info/top_level.txt
writing manifest file 'cothread.egg-info/SOURCES.txt'
reading manifest file 'cothread.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
adding license file 'COPYING'
writing manifest file 'cothread.egg-info/SOURCES.txt'
installing library code to build/bdist.macosx-12-arm64/egg
/Users/ulrik/Code/cothread/venv/lib/python3.9/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
running install_lib
running build_py
creating build
creating build/lib.macosx-12-arm64-cpython-39
creating build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/cadef.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/cothread.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/coserver.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/version.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/poll_win32.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/load_ca.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/input_hook.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/qt.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/py23.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/__init__.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/libca_path.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/pv.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/catools.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/cosocket.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/coselect.py -> build/lib.macosx-12-arm64-cpython-39/cothread
copying cothread/dbr.py -> build/lib.macosx-12-arm64-cpython-39/cothread
creating build/lib.macosx-12-arm64-cpython-39/cothread/tools
copying cothread/tools/pvtree.py -> build/lib.macosx-12-arm64-cpython-39/cothread/tools
copying cothread/tools/__init__.py -> build/lib.macosx-12-arm64-cpython-39/cothread/tools
running build_ext
building 'cothread._coroutine' extension
creating build/temp.macosx-12-arm64-cpython-39
creating build/temp.macosx-12-arm64-cpython-39/context
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -I/Users/ulrik/Code/cothread/venv/include -I/opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c context/_coroutine.c -o build/temp.macosx-12-arm64-cpython-39/context/_coroutine.o -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wundef -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -I/Users/ulrik/Code/cothread/venv/include -I/opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c context/cocore.c -o build/temp.macosx-12-arm64-cpython-39/context/cocore.o -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wundef -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -I/Users/ulrik/Code/cothread/venv/include -I/opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c context/switch.c -o build/temp.macosx-12-arm64-cpython-39/context/switch.o -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wundef -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes
<inline asm>:14:12: error: invalid operand for instruction
       mov ip0, sp
           ^
<inline asm>:15:12: error: invalid operand for instruction
       str ip0, [x0]
           ^
<inline asm>:32:12: error: invalid operand for instruction
       mov ip0, lr
           ^
<inline asm>:44:12: error: invalid operand for instruction
       br  ip0
           ^
4 errors generated.
error: command '/usr/bin/clang' failed with exit code 1
make: *** [dist] Error 1
(venv) ulrik@diamrm5103 cothread % 

Drat. Looks like the Apple asm doesn't understand ip0. Try the following patch instead:

diff --git a/context/switch-arm64.c b/context/switch-arm64.c
index 0949f58..9b1fbf4 100644
--- a/context/switch-arm64.c
+++ b/context/switch-arm64.c
@@ -79,8 +79,8 @@ FNAME(switch_frame)
 "       stp d10, d11, [sp, #-16]!\n"
 "       stp d12, d13, [sp, #-16]!\n"
 "       stp d14, d15, [sp, #-16]!\n"
-"       mov ip0, sp\n"
-"       str ip0, [x0]\n"
+"       mov x16, sp\n"
+"       str x16, [x0]\n"
 "       mov sp, x1\n"
 "       mov x0, x2\n"
 "       ldp d14, d15, [sp], #16\n"
@@ -104,7 +104,7 @@ FSIZE(switch_frame)
 //   x2      context argument to action
 FNAME(create_frame)
 "       stp x1, x2, [x0, #-16]!\n"
-"       mov ip0, lr\n"               // Save LR so can use same STP slot
+"       mov x16, lr\n"               // Save LR so can use same STP slot
 "       ldr lr, =action_entry\n"
 "       stp x19, x20, [x0, #-16]!\n"
 "       stp x21, x22, [x0, #-16]!\n"
@@ -116,7 +116,7 @@ FNAME(create_frame)
 "       stp d10, d11, [x0, #-16]!\n"
 "       stp d12, d13, [x0, #-16]!\n"
 "       stp d14, d15, [x0, #-16]!\n"
-"       br  ip0\n"
+"       br  x16\n"
 
 "action_entry:\n"
         // Receive control after first switch to new frame.  Top of stack has
diff --git a/context/switch.c b/context/switch.c
index 2e034e7..49f1c85 100644
--- a/context/switch.c
+++ b/context/switch.c
@@ -44,7 +44,7 @@
     #include "switch-x86.c"
 #elif defined(__x86_64__)
     #include "switch-x86_64.c"
-#elif defined(__aarch64__)  &&  defined(__unix__)
+#elif defined(__aarch64__)  &&  (defined(__unix__)  ||  defined(__APPLE__))
     #include "switch-arm64.c"
 #elif defined(__arm__)  &&  defined(__unix__)
     #include "switch-arm.c"

Drat. Looks like the Apple asm doesn't understand ip0. Try the following patch instead

So that didn't quite do the trick. We're past the build stage but crashing on import cothread. Continuing investigation :-)