iOS applications can run on multiple flavours of the ARM platform (armv7, armv7s, arm64). This is because they contain a fat binary, a file containing multiple binaries, one for each platform. There are plenty of tutorials explaining how to make (static) fat libraries, containing code for both the simulator (x86) and iOS (ARM).
Now, to what extend can we have different codes (as in: very different implementations) in a fat binary? Can we make the code aware of its target platform? And ultimately, would it be possible to make a fat binary executable (not a library), that runs on both iOS and on OSX?
Awareness of the target platform is simply done by exposing the CURRENT_ARCH Xcode environment variable as a macro definition.
To make the binary cross-platform, like fat libraries, the platform-specific binaries are merged together with lipo.
The resulting app contains a binary for both ARM and x86 code:
$ lipo -info ./Debug-iphoneos/Janus-iOS.app/Janus-iOS Architectures in the fat file: ./Debug-iphoneos/Janus-iOS.app/Janus-iOS are: x86_64 armv7 armv7s arm64
The app runs on iOS:
And its binary runs on OSX, from the console:
$ ./Debug-iphoneos/Janus-iOS.app/Janus-iOS x86_64 2014-03-27 15:29:31.160 Janus-iOS[73947:507] hello world, I'm an OSX binary running on x86_64
See Janus for all the dirty details.
The next step is to investigate if it would be possible to merge not only the binaries, but the apps.