vmtk / vmtk

the Vascular Modeling Toolkit
vmtk.org
Other
352 stars 166 forks source link

Build fails on M2 Mac: Bad CPU type in executable #452

Open zeroby0 opened 1 year ago

zeroby0 commented 1 year ago

I'm trying to install vmtk on an M2 Mac running MacOS Ventura. I cloned the repository, created a build directory, cd'd into it, and ran cmake .. and then make. This is the error I get while dependency ITK is being compiled:

[ 65%] Building CXX object Modules/ThirdParty/GoogleTest/src/GTest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 65%] Linking CXX static library ../../../../../../lib/libitkgtest-5.2.a
[ 65%] Built target gtest
[ 65%] Building CXX object Modules/ThirdParty/GoogleTest/src/GTest-build/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[ 65%] Linking CXX static library ../../../../../../lib/libitkgtest_main-5.2.a
[ 65%] Built target gtest_main
[ 65%] Built target ITKGoogleTest-all
[ 65%] Building C object Modules/ThirdParty/HDF5/src/itkhdf5/src/CMakeFiles/H5make_libsettings.dir/H5make_libsettings.c.o
[ 65%] Linking C executable ../../../../../../bin/H5make_libsettings
[ 65%] Built target H5make_libsettings
[ 65%] Building C object Modules/ThirdParty/HDF5/src/itkhdf5/src/CMakeFiles/H5detect.dir/H5detect.c.o
[ 65%] Linking C executable ../../../../../../bin/H5detect
[ 65%] Built target H5detect
[ 65%] Generating ../H5Tinit.c, ../gen_SRCS.stamp1
/bin/sh: /Users/usernamet/libs/github/vmtk/vmtk/vmtk-build/ITK-Build/bin/H5detect: Bad CPU type in executable
make[5]: *** [Modules/ThirdParty/HDF5/src/itkhdf5/H5Tinit.c] Error 126
make[5]: *** Deleting file `Modules/ThirdParty/HDF5/src/itkhdf5/H5Tinit.c'
make[4]: *** [Modules/ThirdParty/HDF5/src/itkhdf5/src/CMakeFiles/gen_hdf5-shared.dir/all] Error 2
make[3]: *** [all] Error 2
make[2]: *** [Stamp/ITK/ITK-build] Error 2
make[1]: *** [CMakeFiles/ITK.dir/all] Error 2
make: *** [all] Error 2

I assume the binary built is for x86_64, or the linker is trying to link it in an x86_64 way? So I installed rosetta2 with softwareupdate --install-rosetta and re-ran the build. The build is still running, but ITK has been successfully built.

If anyone knows of a better way to build natively on M1/M2 macs, could you please tell me about it? :)

I'm posting here instead of on itkhdf5 becuase I'm assuming cmake on this repo is configured to build for x86, but please let me know if I should be asking somewhere else instead :)

Thank you

zeroby0 commented 1 year ago

Further down, when compiling vmtk, I see this warning:

ld: warning: ignoring file ...../conda/envs/vmtk/lib/libpython3.10.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64

Which leads me to further believe that the cmake config here is not detecting arm64 macs and building accordingly

lassoan commented 1 year ago

Currently, VMTK build on Arm is not supported. Contributions are welcome.

Most likely the work has to start at looking at Arm compatibility of dependencies (ITK and VTK) and how VMTK's superbuild builds them.

We'll plan to provide 3D Slicer builds for Arm within a year and then we'll have a look at building of extensions, too. Since VMTK is used in the VMTK extension of Slicer, we may provide fixes necessary to allow Arm builds of VMTK.

zeroby0 commented 1 year ago

Thanks for the help :)

The good news is that ITK and VTK are very likely Arm ready:

Both have binaries for Apple Silicon on homebrew.

Ah, I found the CMAKE_OSX_ARCHITECTURES "x86_64" in the CMakeLists.txt. I'll try setting it to arm64 tomorrow and will report back how the build goes.

The entire build was successful after installing rosetta stone, and after sourcing the env file in the build/install directory, I was able to import VMTK from python 3.10.

I wish I'm able to test further and report the compatibility, but I'm extremely new to VMTK. I'm setting it up for someone else on their new laptop.

zeroby0 commented 1 year ago

I was able to compile VMTK with "arm64", but I get a segmentation fault whenever I run anything related to vmtk, so I'm guessing it doesn't work after all. The rosetta2 build might work if you link it against an x86 python.

lassoan commented 1 year ago

Yes, currently we use Rosetta 2 with x86 Python, VTK, and VMTK.

zeroby0 commented 1 year ago

Thank you for the tip! I tried it and it worked!

For readers of the future, here's what I did:

  1. Install rosetta2 (softwareupdate --install-rosetta)
  2. Download the x86_64 version of Anaconda
  3. Create a new env for vmtk and conda activate it.
  4. Enable the Conda Forge channel
  5. conda install vmtk

I'll leave the Issue open so you can use it for tracking arm64 support, but please feel free to close it :)