alecjacobson / gptoolbox

Matlab toolbox for Geometry Processing.
MIT License
629 stars 169 forks source link

Compiling mex on M1 Mac #130

Open alecjacobson opened 2 years ago

alecjacobson commented 2 years ago

Matlab currently runs on M1 Macs as using Rosetta https://www.mathworks.com/matlabcentral/answers/641925-is-matlab-supported-on-apple-silicon-macs

Unfortunately, this means that building the mex files as a native arm64 binaries will not work. :-(

Fortunately, it's not too hard to build the mex functions using rosetta. This requires a (still in PR) version of libigl which builds gmp and mpfr from source (rather than relying a system install from, say, homebrew which would just include arm64 binaries).

The upshot is that as of 36df062a7ce1b6fab3692c3f0c764f77feed0cc1 you can build mex on m1 macs using rosetta with:

cd mex/
mkdir build
cd build
cmake ../ -DLIBIGL_RESTRICTED_MATLAB=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_OSX_ARCHITECTURES=x86_64 -DMatlab_MEX_EXTENSION="mexmaci64"
arch -x86_64 make -j8

This hasn't yet been documented in the mex/README.md

paul0noah commented 1 year ago

Compiling gptoolbox for M1 Mac with Matlab R2022b beta (native apple silicon support):

I tried compiling gptoolbox/mex for Matlab beta (as it is faster than rosetta version). Unfortunately, this is not working out of the box. Here the steps i followed

1) Apply the following changes to file gptoobox/mex/cmake/findMATLAB.cmake:

if(APPLE)
  set(_matlab_bin_prefix "mac") # i should be for intel
  set(_matlab_bin_suffix_32bits "a") # previously was: set(_matlab_bin_suffix_32bits "i")
  set(_matlab_bin_suffix_64bits "a64")  # previously was: set(_matlab_bin_suffix_64bits "i64")

2) call cmake as follows:

cmake .. -DMatlab_ROOT_DIR="/path/to/MATLAB_R2022b_Beta.app/" -DMatlab_MEX_EXTENSION="mexmaca64" -DMATLAB_ADDITIONAL_VERSIONS="R2022b=9.13"
alecjacobson commented 1 year ago

cool. Thanks for trying this. I haven't tried the beta matlab silicon version yet.

what errors are you running into?

paul0noah commented 1 year ago

the main errors basically are that findMATLAB does not support the matlab silicon version, this includes:

additionally i had an issue with the cmake build system: my clang compiler did not support -march=native (for fix i upgraded clang)

alecjacobson commented 1 year ago

Oh I see. So it's working with your changes?

On Thu, Jan 5, 2023, 3:57 PM paul0noah @.***> wrote:

the main errors basically are that findMATLAB does not support, this includes:

  • matlab bin prefix can not be identified (fixed with step 1)
  • matlab mex extension not supported (fixed with -DMatlab_MEX_EXTENSION="mexmaca64")
  • matlab R2022b version not supported (fixed with -DMATLAB_ADDITIONAL_VERSIONS="R2022b=9.13")

additionally i had an issue with the cmake build system: my clang compiler did not support -march=native (for fix i upgraded clang) https://stackoverflow.com/questions/65966969/why-does-march-native-not-work-on-apple-m1

— Reply to this email directly, view it on GitHub https://github.com/alecjacobson/gptoolbox/issues/130#issuecomment-1372737530, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARDJGOAI6FELNFUVZMSN63WQ4YUFANCNFSM5PI4ST6A . You are receiving this because you authored the thread.Message ID: @.***>

paul0noah commented 1 year ago

Yes, sorry that my post did not make this clear enough.

alecjacobson commented 1 year ago

Great! Want to submit a PR ? Then I can just make sure it's still working elsewhere.

scarfelab commented 1 year ago

Hi. I am attempting to compile the gptoolbox Mex with Matlab 2023b (Arm Version) on an Apple M3 MacBook Pro. I have added "R2023b=23.2" to the MATLAB_VERSIONS_MAPPING in FindMATLAB.cmake.

If I run

cmake .. -DMatlab_ROOT_DIR="/Applications/MATLAB_R2023b.app/" -DMatlab_MEX_EXTENSION="mexmaca64" -DMATLAB_ADDITIONAL_VERSIONS="R2023b=23.2"

I get

CMake Error at /Applications/CMake.app/Contents/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:230 (message): Could NOT find Matlab (missing: Matlab_MEX_LIBRARY MEX_COMPILER MX_LIBRARY ENG_LIBRARY) (found version "23.2") Call Stack (most recent call first): /Applications/CMake.app/Contents/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE) cmake/FindMatlab.cmake:1535 (find_package_handle_standard_args) CMakeLists.txt:80 (find_package)

If I manually specify the paths with

cmake .. -DMatlab_ROOT_DIR="/Applications/MATLAB_R2023b.app/" -DMatlab_MEX_EXTENSION="mexmaca64" -DMATLAB_ADDITIONAL_VERSIONS="R2023b=23.2.0" -DMatlab_MEX_LIBRARY="/Applications/MATLAB_R2023b.app/bin/maca64/libmex.dylib" -DMatlab_MEX_COMPILER="/Applications/MATLAB_R2023b.app/bin/maca64/mex" -DMatlab_MX_LIBRARY="/Applications/MATLAB_R2023b.app/bin/maca64/libmx.dylib" -DMatlab_ENG_LIBRARY="/Applications/MATLAB_R2023b.app/bin/maca64/libeng.dylib"

Things seems to work in that I get

-- Found Matlab: /Applications/MATLAB_R2023b.app/extern/include (found version "23.2") found components: MEX_COMPILER MX_LIBRARY ENG_LIBRARY

Boost is fetched etc. But a CMake error occurs

-- Creating target: igl_restricted::matlab (igl_restricted_matlab) CMake Error at /Applications/CMake.app/Contents/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:230 (message): Could NOT find Matlab (missing: Matlab_MEX_LIBRARY MAT_LIBRARY) (found version "23.2") Call Stack (most recent call first): /Applications/CMake.app/Contents/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE) cmake/FindMatlab.cmake:1535 (find_package_handle_standard_args) build/_deps/libigl-src/cmake/igl/modules/restricted/matlab.cmake:18 (find_package) build/_deps/libigl-src/cmake/igl/igl_include.cmake:32 (include) build/_deps/libigl-src/cmake/igl/igl_include.cmake:40 (_igl_include_full) build/_deps/libigl-src/cmake/libigl.cmake:32 (igl_include_optional) build/_deps/libigl-src/CMakeLists.txt:151 (include)

I am a little lost as to how to fix this issue.

paul0noah commented 1 year ago

Hi @scarfelab,

I did some debugging in the the findMATLAB.cmake file shipped with gptoolbox and found that CMAKE_OSX_ARCHITECTURES was unset which caused that the findMATLAB script was searching inside /Applications/MATLAB_R2023b.app/bin/maci64 instead of /Applications/MATLAB_R2023b.app/bin/maca64 folder.

For me this command worked, i.e. adding -DCMAKE_OSX_ARCHITECTURES="arm64" to your cmake call:

cmake .. -DMatlab_ROOT_DIR="/Applications/MATLAB_R2023b.app/" -DMatlab_MEX_EXTENSION="mexmaca64" -DMATLAB_ADDITIONAL_VERSIONS="R2023b=23.2" -DCMAKE_OSX_ARCHITECTURES="arm64"

I hope this helps

Paul

alecjacobson commented 1 year ago

Thanks. This is admittedly still a mess as cmake's findmatlab tries to catch up. Hopefully resolved soon and I don't need to maintain this findmatlab.cmake

On Mon, Nov 20, 2023 at 2:10 PM paul0noah @.***> wrote:

Hi @scarfelab https://github.com/scarfelab,

I did some debugging in the the findMATLAB.cmake file shiped with gptoolbox and found that CMAKE_OSX_ARCHITECTURES was unset which caused that the findMATLAB script was searching inside /Applications/MATLAB_R2023b.app/bin/maci64 instead of /Applications/MATLAB_R2023b.app/bin/maca64 folder.

For me this command worked, i.e. adding -DCMAKE_OSX_ARCHITECTURES="arm64" to your cmake call:

cmake .. -DMatlab_ROOT_DIR="/Applications/MATLAB_R2023b.app/" -DMatlab_MEX_EXTENSION="mexmaca64" -DMATLAB_ADDITIONAL_VERSIONS="R2023b=23.2" -DCMAKE_OSX_ARCHITECTURES="arm64"

I hope this helps

Paul

— Reply to this email directly, view it on GitHub https://github.com/alecjacobson/gptoolbox/issues/130#issuecomment-1819648177, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARDJGPTIG3H42GI2W62S33YFOTJBAVCNFSM5PI4ST6KU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBRHE3DIOBRG43Q . You are receiving this because you authored the thread.Message ID: @.***>

scarfelab commented 1 year ago

@paul0noah Many thanks for your help. Really appreciated. I am not very familiar with cmake, hence my difficulties. Following your instructions has worked much better, but I still get an error.

[ 45%] Performing configure step for 'gmp' configure: WARNING: unrecognized options: --disable-debug, --disable-dependency-tracking configure: error: cannot run /bin/sh /Users/peterscarfe/Documents/GitHub Local/gptoolbox/mex/build/_deps/gmp/src/gmp/config.sub make[2]: [_deps/gmp/src/gmp-stamp/gmp-configure] Error 1 make[1]: [_deps/libigl-build/CMakeFiles/gmp.dir/all] Error 2 make: *** [all] Error 2

I have checked in the "_deps" folder created by cmake and I can see a gmp folder with various files in, suggesting that it has downloaded. However, I notice that unlike most of the others, there is not "-build" "-src" and "-subbuild" variants. See attached screenshot. The same is also true of mpfr.

_deps

I notice that the instructions suggest I might need to install boost, mpfr and gmp. However, boost seems to have been dowloaded ok as has a "-build" "cmake-build" "-src" and "-subbuild" pattern similar to the other downloads.

Many thanks again for your help. It is really appreciated.

Peter

paul0noah commented 12 months ago

Hi Peter,

the folder strucutre looks similar on my machine and I also encounter compile errors with gmp stuff and I think @alecjacobson himself has to have a look at this.

Workaround to get parts of mex files built: Since i never needed these parts of gptoolbox my "workaround" was to compile with -i option, i.e. call make -i which has the effect to ignore errors and continue. Since gptoolbox consists of a lot of targets (e.g. decimate_libgl, exact_geodesic, etc. etc.) all of these targets will build successfully which covers most of gptoolboxes features.

Best Paul

scarfelab commented 12 months ago

@paul0noah Awesome. Thanks again Paul. This has indeed worked for me i.e. goes through everything building when it can. I really will have to get more familiar with cmake. Glad I was not doing anything wrong r.e. the issue I had with gmp mpfr.

Visually it looks like a fair number of the Mex files cannot be built. However, for me at the moment the key things were related to Embree, which seems to have been built ok. Though I still need to have a more extensive play.

@alecjacobson happy to test any modified version if it would be of any help.

Thank-you both again for your help. Really appreciated it.

scarfelab commented 11 months ago

Hi both @alecjacobson @paul0noah

It turned out some of the crucial mex files I needed were not built. Sigh... However, I have made a little more progress, all be it of dubious nature. I installed gmp and mpfr in the default /usr/local/ location. Installation and check went fine. I then re-ran

cmake .. -DMatlab_ROOT_DIR="/Applications/MATLAB_R2023b.app/" -DMatlab_MEX_EXTENSION="mexmaca64" -DMATLAB_ADDITIONAL_VERSIONS="R2023b=23.2" -DCMAKE_OSX_ARCHITECTURES="arm64"

Turns out this does not seem to locate either gmp or mpfr in /usr/local/.

However, the cmake command now progresses itself and gets further in grabbing and installing gmp and mpfr itself. Now many more of the mex files get built with make -i. Including some of the critical ones I need.

Thus, something appears to have changed in terms of cmake being able to find online what it needs in terms of gmp and mpfr. Indeed, I can completely remove my installed versions in /usr/local/ and things work just as well.

So, progress, but no thanks to my own efforts.