Patch for bsnes gitorious for iOS

Any chance this could be pushed to gitorious.org/bsnes/bsnes.git? It’s just a series of patches needed to get bsnes to build and run on iOS.

Notably: Calls to stat64 are replaced with calls to stat on iOS. Proper command line arguments are passed to clang during build. libco/armeabi.c is updated to link for iOS.


---
 libco/armeabi.c          | 11 ++++++++++-
 libco/libco.c            |  2 +-
 nall/file.hpp            | 19 ++++++++++++++++---
 nall/intrinsics.hpp      |  2 +-
 target-libretro/Makefile |  6 +++---
 5 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/libco/armeabi.c b/libco/armeabi.c
index ad9ed07..865b588 100644
--- a/libco/armeabi.c
+++ b/libco/armeabi.c
@@ -22,7 +22,9 @@ asm (
       ".arm
"
       ".align 4
"
       ".globl co_switch_arm
"
+      ".globl _co_switch_arm
"
       "co_switch_arm:
"
+      "_co_switch_arm:
"      
       "  stmia r1!, {r4, r5, r6, r7, r8, r9, r10, r11, sp, lr}
"
       "  ldmia r0!, {r4, r5, r6, r7, r8, r9, r10, r11, sp, pc}
"
     );
@@ -36,7 +38,14 @@ static void crash(void) {
 
 cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
    size = (size + 1023) & ~1023;
-   cothread_t handle = memalign(1024, size + 256);
+   cothread_t handle = 0;
+#if HAVE_POSIX_MEMALIGN >= 1
+   if (posix_memalign(&handle, 1024, size + 256) < 0)
+      return 0;
+#else
+   handle = memalign(1024, size + 256);
+#endif
+
    if (!handle)
       return handle;
 
diff --git a/libco/libco.c b/libco/libco.c
index 79fdd07..a997a24 100644
--- a/libco/libco.c
+++ b/libco/libco.c
@@ -10,7 +10,7 @@
   #include "amd64.c"
 #elif defined(__GNUC__) && defined(_ARCH_PPC)
   #include "ppc.c"
-#elif defined(__GNUC__) && defined(__ARM_EABI__)
+#elif defined(__GNUC__) && (defined(__ARM_EABI__) || defined(__arm__))
   #include "armeabi.c"
 #elif defined(__GNUC__)
   #include "sjlj.c"
diff --git a/nall/file.hpp b/nall/file.hpp
index 352682a..a529a16 100644
--- a/nall/file.hpp
+++ b/nall/file.hpp
@@ -8,6 +8,10 @@
 #include <nall/windows/utf8.hpp>
 #include <nall/stream/memory.hpp>
 
+#ifdef __APPLE__
+#include "TargetConditionals.h"
+#endif
+
 namespace nall {
 
 inline FILE* fopen_utf8(const string& filename, const string& mode) {
@@ -226,7 +230,10 @@ struct file {
   }
 
   static bool exists(const string& filename) {
-    #if !defined(_WIN32)
+    #if defined(__APPLE__) && TARGET_OS_IPHONE
+    struct stat data;
+    if(stat(filename, &data) != 0) return false;
+    #elif !defined(_WIN32)
     struct stat64 data;
     if(stat64(filename, &data) != 0) return false;
     #else
@@ -238,7 +245,10 @@ struct file {
   }
 
   static uintmax_t size(const string& filename) {
-    #if !defined(_WIN32)
+    #if defined(__APPLE__) && TARGET_OS_IPHONE
+    struct stat data;
+    if(stat(filename, &data) != 0) return false;
+    #elif !defined(_WIN32)
     struct stat64 data;
     stat64(filename, &data);
     #else
@@ -249,7 +259,10 @@ struct file {
   }
 
   static time_t timestamp(const string& filename, file::time mode = file::time::create) {
-    #if !defined(_WIN32)
+    #if defined(__APPLE__) && TARGET_OS_IPHONE
+    struct stat data;
+    if(stat(filename, &data) != 0) return false;
+    #elif !defined(_WIN32)
     struct stat64 data;
     stat64(filename, &data);
     #else
diff --git a/nall/intrinsics.hpp b/nall/intrinsics.hpp
index 8f1e3fe..e9d276b 100755
--- a/nall/intrinsics.hpp
+++ b/nall/intrinsics.hpp
@@ -45,7 +45,7 @@ struct Intrinsics {
 
 /* Endian detection */
 
-#if defined(__i386__) || defined(__amd64__) || defined(_M_IX86) || defined(_M_AMD64) || defined(__ARM_EABI__)
+#if defined(__i386__) || defined(__amd64__) || defined(_M_IX86) || defined(_M_AMD64) || defined(__ARM_EABI__) || defined(__arm__)
   #define ENDIAN_LSB
   #define ARCH_LSB
   Intrinsics::Endian Intrinsics::endian() { return Intrinsics::Endian::LSB; }
diff --git a/target-libretro/Makefile b/target-libretro/Makefile
index c088bf3..17a2275 100755
--- a/target-libretro/Makefile
+++ b/target-libretro/Makefile
@@ -10,7 +10,7 @@ ifeq ($(platform),unix)
 else ifeq ($(platform),osx)
   flags += -fPIC
 else ifeq ($(platform),ios)
-  flags += -fPIC
+  flags += -arch armv7 -marm -fPIC -isysroot $(IOSSDK) -DHAVE_POSIX_MEMALIGN=1 -w
 else ifeq ($(platform),win)
 endif
 
@@ -27,7 +27,7 @@ build: $(objects)
 ifeq ($(platform),unix)
 	$(compiler) -o out/bsnes_libretro.so -shared $(objects) -ldl -Wl,--no-undefined -Wl,--version-script=$(ui)/link.T
 else ifeq ($(platform),ios)
-	$(compiler) -o out/bsnes_libretro_ios.dylib -dynamiclib $(objects)
+	$(compiler) -o out/bsnes_libretro_ios.dylib -dynamiclib $(objects) -isysroot $(IOSSDK) -arch armv7 
 else ifeq ($(platform),osx)
 	$(compiler) -o out/bsnes_libretro.dylib -dynamiclib $(objects)
 else ifeq ($(platform),win)
@@ -40,7 +40,7 @@ ifeq ($(platform),unix)
 else ifeq ($(platform),osx)
 	cp out/bsnes_libretro.dylib $(DESTDIR)$(prefix)/lib/bsnes_libretro.dylib
 else ifeq ($(platform),ios)
-	cp out/bsnes_libretro_io.dylib $(DESTDIR)$(prefix)/lib/bsnes_libretro_ios.dylib
+	cp out/bsnes_libretro_ios.dylib $(DESTDIR)$(prefix)/lib/bsnes_libretro_ios.dylib
 endif
 
 uninstall:
-- 

Pushed. Patch didn’t go through cleanly (couldn’t see why though), so you should probably check if it’s correct.

Thank you. It’s all fine except that In target-libretro/Makefile the OSX and iOS flags got reversed.


else ifeq ($(platform),osx)
  flags += -arch armv7 -marm -fPIC -isysroot $(IOSSDK) -DHAVE_POSIX_MEMALIGN=1 -w
else ifeq ($(platform),ios)
  flags += -fPIC
else ifeq ($(platform),win)

should be


else ifeq ($(platform),osx)
  flags += -fPIC
else ifeq ($(platform),ios)
  flags += -arch armv7 -marm -fPIC -isysroot $(IOSSDK) -DHAVE_POSIX_MEMALIGN=1 -w
else ifeq ($(platform),win)

Still fails to compile for me with libretro-build-ios.sh even when manually reversing those two lines again that meancoot just pointed out - it fails on this -

./nall/directory.hpp:215:16: note: use ‘==’ to turn this assig./nall/file.hpp:238:8: error: call to unavailable function ‘stat64’: not available on iOS if(stat64(filename, &data) != 0) return false; ^~~~~~

Just have an ifdef that checks for ‘defined(IOS)’ or something, add that to ‘FLAGS’ for ‘platform = ios’ and that should take care of that. I don’t think relying on ‘if defined(APPLE) && TARGET_IPHONE_OS’ is working over here.

Did you update the Makefiles for perf and balanced too? The stat64 error is what you would get before you reversed the Makefile lines. Without the -isysroot argument passed to clang you end up with the OS X version of AppleIntrinsics.h which, obviously, doesn’t have TARGET_OS_IPHONE set.

Did you update the Makefiles for perf and balanced too? The stat64 error is what you would get before you reversed the Makefile lines. Without the -isysroot argument passed to clang you end up with the OS X version of AppleIntrinsics.h which, obviously, doesn’t have TARGET_OS_IPHONE set.[/quote]

I might have forgotten to patch those additional Makefiles too yeah. I’ll try it again tomorrow when I get up.

Sorry if I accidentally reversed those Makefile lines BTW. Must have been up for too long when I pushed that patch.

EDIT: Looks like I didn’t revert those Makefile lines after all.

https://gitorious.org/bsnes/bsnes/commi … 465f8f2f72