iliaal / php_excel

PHP Extension interface to the Excel writing/reading library

Home Page:http://ilia.ws

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Compiling for Apple silicon M1

xperseguers opened this issue · comments

Hi there,

Does anyone succeeds into compiling excel.so for Apple silicon M1?

[edit: I finally did, please read what I tried and finally succeeded to do]

I tried with PHP 7.4.21 to compile libXL v3.8.5.0 (which worked on Intel), I tried libXL v3.8.8.0 and libXL v4.0.0.0. For this latter, I had to patch the source of excel.c like this so that it compiles:

diff --git a/excel.c b/excel.c
index fd6f34d..0fd8a23 100644
--- a/excel.c
+++ b/excel.c
@@ -2679,7 +2679,9 @@ EXCEL_METHOD(Sheet, isDate)
 	Inserts rows from rowFirst to rowLast */
 EXCEL_METHOD(Sheet, insertRow)
 {
-#if LIBXL_VERSION >= 0x03080301
+#if LIBXL_VERSION >= 0x04000000
+	PHP_EXCEL_SHEET_GET_BOOL_STATE(InsertRow)
+#elif LIBXL_VERSION >= 0x03080301
 	PHP_EXCEL_SHEET_GET_BOOL_STATE_3831(InsertRow)
 #else
 	PHP_EXCEL_SHEET_GET_BOOL_STATE(InsertRow)
@@ -2691,7 +2693,9 @@ EXCEL_METHOD(Sheet, insertRow)
 	Inserts columns from colFirst to colLast */
 EXCEL_METHOD(Sheet, insertCol)
 {
-#if LIBXL_VERSION >= 0x03080301
+#if LIBXL_VERSION >= 0x04000000
+	PHP_EXCEL_SHEET_GET_BOOL_STATE(InsertCol)
+#elif LIBXL_VERSION >= 0x03080301
 	PHP_EXCEL_SHEET_GET_BOOL_STATE_3831(InsertCol)
 #else
 	PHP_EXCEL_SHEET_GET_BOOL_STATE(InsertCol)
@@ -2703,7 +2707,9 @@ EXCEL_METHOD(Sheet, insertCol)
 	Removes rows from rowFirst to rowLast */
 EXCEL_METHOD(Sheet, removeRow)
 {
-#if LIBXL_VERSION >= 0x03080301
+#if LIBXL_VERSION >= 0x04000000
+	PHP_EXCEL_SHEET_GET_BOOL_STATE(RemoveRow)
+#elif LIBXL_VERSION >= 0x03080301
 	PHP_EXCEL_SHEET_GET_BOOL_STATE_3831(RemoveRow)
 #else
 	PHP_EXCEL_SHEET_GET_BOOL_STATE(RemoveRow)
@@ -2715,7 +2721,9 @@ EXCEL_METHOD(Sheet, removeRow)
 	Removes columns from colFirst to colLast */
 EXCEL_METHOD(Sheet, removeCol)
 {
-#if LIBXL_VERSION >= 0x03080301
+#if LIBXL_VERSION >= 0x04000000
+	PHP_EXCEL_SHEET_GET_BOOL_STATE(RemoveCol)
+#elif LIBXL_VERSION >= 0x03080301
 	PHP_EXCEL_SHEET_GET_BOOL_STATE_3831(RemoveCol)
 #else
 	PHP_EXCEL_SHEET_GET_BOOL_STATE(RemoveCol)

When trying to load the excel.so extension, PHP crashes right away (<process id> Killed: 9).

After double checking I found this:

$ file libxl-mac-3.8.5.0/lib/libxl.dylib
libxl-mac-3.8.5.0/lib/libxl.dylib: Mach-O 64-bit dynamically linked shared library x86_64

$ file libxl-mac-3.8.8.0/lib/libxl.dylib 
libxl-mac-3.8.8.0/lib/libxl.dylib: Mach-O 64-bit dynamically linked shared library x86_64

$ file libxl-mac-4.0.0.0/lib/libxl.dylib 
libxl-mac-4.0.0.0/lib/libxl.dylib: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit dynamically linked shared library x86_64] [arm64]
libxl-mac-4.0.0.0/lib/libxl.dylib (for architecture x86_64):	Mach-O 64-bit dynamically linked shared library x86_64
libxl-mac-4.0.0.0/lib/libxl.dylib (for architecture arm64):	Mach-O 64-bit dynamically linked shared library arm64

So obviously v3.8.5.0 and v3.8.8.0 cannot be used anyway, I succeeded downloading version 3.9.3.0, which results into:

 $ file libxl-mac-3.9.3.0/lib/libxl.dylib         
libxl-mac-3.9.3.0/lib/libxl.dylib: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit dynamically linked shared library x86_64] [arm64]
libxl-mac-3.9.3.0/lib/libxl.dylib (for architecture x86_64):	Mach-O 64-bit dynamically linked shared library x86_64
libxl-mac-3.9.3.0/lib/libxl.dylib (for architecture arm64):	Mach-O 64-bit dynamically linked shared library arm64

So this should "work" but PHP crashes anyway after rebuilding excel.so against this 3.9.3.0 version of libXL.

Something's strange:

$ otool -L libxl-mac-3.9.3.0/lib/libxl.dylib
libxl-mac-3.9.3.0/lib/libxl.dylib (architecture x86_64):
	libxl.dylib (compatibility version 0.0.0, current version 0.0.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1746.0.0)
	/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 1109.1.1)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 904.4.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.0.0)
libxl-mac-3.9.3.0/lib/libxl.dylib (architecture arm64):
	libxl.dylib (compatibility version 0.0.0, current version 0.0.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1746.0.0)
	/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 1109.1.1)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 904.4.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.0.0)

$ sudo cp -p libxl-mac-3.9.3.0/lib/libxl.dylib /usr/local/lib/
$ otool -L /usr/local/lib/libxl.dylib
fatal error: /Library/Developer/CommandLineTools/usr/bin/otool: fatal error in /Library/Developer/CommandLineTools/usr/bin/otool-classic

Digging this problem, I found this: https://developer.apple.com/forums/thread/688830, which solved this issue by first deleting any /usr/local/lib/libxl.dylib prior to copying the version I wanted.

Unfortunately it doesn't help to fix the PHP crash. Recompiling again, I now had the same 4 errors in excel.c when using v3.9.3.0 as I had with v4.0.0.0, so I updated my patch to read 4 times

+#if LIBXL_VERSION >= 0x03090300

instead of

+#if LIBXL_VERSION >= 0x04000000

But nothing helps, either with v3.9.3.0 or v4.0.0.0.

I finally remembered that my build script was doing that (thanks to #146):

echo "Fixing 'unsafe use of relative rpath libxl.dylib'"
sudo install_name_tool -change libxl.dylib /usr/local/lib/libxl.dylib excel.so

So I tried NOT doing it, and IT WORKS!!!!

$ php -m

does not crash and shows excel!

Note: Possibly it would work with libXL v4.0.0.0 (now that there's no install_name_tool command to run) but I let you try on your own...

The latest binaries from libxl for v4.0.x are working with Apple M1