From e64d1d11cb97b1380c2b29affe2f8e3560d69597 Mon Sep 17 00:00:00 2001
From: Florian Uhlig <f.uhlig@gsi.de>
Date: Mon, 22 Aug 2022 15:15:10 +0200
Subject: [PATCH] Fix externals on Apple silicon

Fix NicaFemto compilation on macosx arm64.
Remove hardcoded GSL search directory and pass the correct brew install
directory which is now taken from the brew command.

Fix KFParticle compilation on macosx arm64.
Add patch which adds an include file which defines _mm_malloc and _mm_free.
---
 external/InstallKFParticle.cmake  | 23 +++++++++++++++++------
 external/KFParticle_applem1.patch | 30 ++++++++++++++++++++++++++++++
 external/NicaFemto.patch          | 17 ++++++++++++-----
 3 files changed, 59 insertions(+), 11 deletions(-)
 create mode 100644 external/KFParticle_applem1.patch

diff --git a/external/InstallKFParticle.cmake b/external/InstallKFParticle.cmake
index c1441c8cec..30110cdd24 100644
--- a/external/InstallKFParticle.cmake
+++ b/external/InstallKFParticle.cmake
@@ -2,14 +2,25 @@ set(KFPARTICLE_LIBNAME "${CMAKE_SHARED_LIBRARY_PREFIX}KFParticle${CMAKE_SHARED_L
 
 set(KFPARTICLE_SRC_URL "https://github.com/cbmsw/KFParticle.git")
 set(KFPARTICLE_DESTDIR "${CMAKE_BINARY_DIR}/external/KFPARTICLE-prefix")
+set(KFPARTICLE_TAG     "1e1c5dee0e2bd98a3df677beb88e497fbef92504")
 
 # GIT_TAG is a hash for KFParticle tag cbm/v1.1-1 
-download_project_if_needed(PROJECT         kfparticle_source
-                           GIT_REPOSITORY  ${KFPARTICLE_SRC_URL}
-                           GIT_TAG         "1e1c5dee0e2bd98a3df677beb88e497fbef92504"
-                           SOURCE_DIR      ${CMAKE_CURRENT_SOURCE_DIR}/KFParticle
-                           TEST_FILE       CMakeLists.txt
-                          )
+if (CMAKE_SYSTEM_NAME MATCHES Darwin AND ${CMAKE_SYSTEM_PROCESSOR} MATCHES arm64)
+  download_project_if_needed(PROJECT         kfparticle_source
+                             GIT_REPOSITORY  ${KFPARTICLE_SRC_URL}
+                             GIT_TAG         ${KFPARTICLE_TAG}
+                             SOURCE_DIR      ${CMAKE_CURRENT_SOURCE_DIR}/KFParticle
+                             TEST_FILE       CMakeLists.txt
+                             PATCH_COMMAND   "patch -p1 < ${CMAKE_CURRENT_SOURCE_DIR}/KFParticle_applem1.patch"
+                            )
+else()
+  download_project_if_needed(PROJECT         kfparticle_source
+                             GIT_REPOSITORY  ${KFPARTICLE_SRC_URL}
+                             GIT_TAG         ${KFPARTICLE_TAG}
+                             SOURCE_DIR      ${CMAKE_CURRENT_SOURCE_DIR}/KFParticle
+                             TEST_FILE       CMakeLists.txt
+                            )
+endif()
 
 If(ProjectUpdated)
   File(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/external/KFPARTICLE-prefix)
diff --git a/external/KFParticle_applem1.patch b/external/KFParticle_applem1.patch
new file mode 100644
index 0000000000..c34458b717
--- /dev/null
+++ b/external/KFParticle_applem1.patch
@@ -0,0 +1,30 @@
+diff --git a/KFParticle/KFPInputData.h b/KFParticle/KFPInputData.h
+index 8f674f8..50f5143 100644
+--- a/KFParticle/KFPInputData.h
++++ b/KFParticle/KFPInputData.h
+@@ -29,6 +29,10 @@
+ #include <string>
+ #include <fstream>
+ 
++#ifdef __APPLE__
++#include <mm_malloc.h>
++#endif
++
+ /** @class KFPTrackIndex
+  ** @brief Helper structure to sort tracks in the KFPTrackVector object.
+  ** @author  M.Zyzak, I.Kisel
+diff --git a/KFParticle/KFPSimdAllocator.h b/KFParticle/KFPSimdAllocator.h
+index 20a13c4..42fd493 100644
+--- a/KFParticle/KFPSimdAllocator.h
++++ b/KFParticle/KFPSimdAllocator.h
+@@ -22,6 +22,10 @@
+ #ifndef KFPSimdAllocator_H
+ #define KFPSimdAllocator_H
+ 
++#ifdef __APPLE__
++#include <mm_malloc.h>
++#endif
++
+ #include <Vc/Vc>
+ 
+ /** @class KFPSimdAllocator
diff --git a/external/NicaFemto.patch b/external/NicaFemto.patch
index ff1f7650c5..939c2130c5 100644
--- a/external/NicaFemto.patch
+++ b/external/NicaFemto.patch
@@ -1,5 +1,5 @@
 diff --git a/CMakeLists.txt b/CMakeLists.txt
-index 60ab7e6..9cf7543 100644
+index 60ab7e6..4df2021 100644
 --- a/CMakeLists.txt
 +++ b/CMakeLists.txt
 @@ -27,11 +27,11 @@ if(NOT DEFINED NICAFEMTO_SUBDIR_BUILD)
@@ -77,17 +77,22 @@ index 60ab7e6..9cf7543 100644
  	Set(SIMPATH $ENV{SIMPATH})
  	Set(Boost_NO_SYSTEM_PATHS TRUE)
  	Set(Boost_NO_BOOST_CMAKE TRUE)
-@@ -131,7 +143,7 @@ if(NOT DEFINED NICAFEMTO_SUBDIR_BUILD)
+@@ -129,9 +141,13 @@ if(NOT DEFINED NICAFEMTO_SUBDIR_BUILD)
+ 	find_package(GSL)
+         if(NOT GSL_FOUND)
            if(APPLE)
-             set(GSL_DIR /usr/local)
+-            set(GSL_DIR /usr/local)
++            execute_process(COMMAND brew --prefix
++                            OUTPUT_VARIABLE _brew_install_dir
++                            OUTPUT_STRIP_TRAILING_WHITESPACE
++                           )
++            set(GSL_DIR "${_brew_install_dir}")
            else()
 -            unset(GSL_DIR)
 +            set(GSL_DIR /usr)
            endif()
            find_package(GSL REQUIRED)
          endif()
-diff --git a/dataformat/NicaDataFormatManager.h b/dataformat/NicaDataFormatManager.h
-index 4ecb407..a739199 100644
 diff --git a/cuts/CMakeLists.txt b/cuts/CMakeLists.txt
 index 1d1c4f0..e5676ba 100755
 --- a/cuts/CMakeLists.txt
@@ -114,6 +119,8 @@ index b8576ab..b3fc335 100644
  
  GENERATE_LIBRARY()
  
+diff --git a/dataformat/NicaDataFormatManager.h b/dataformat/NicaDataFormatManager.h
+index 4ecb407..a739199 100644
 --- a/dataformat/NicaDataFormatManager.h
 +++ b/dataformat/NicaDataFormatManager.h
 @@ -13,9 +13,9 @@
-- 
GitLab