The holy grail of computing is to have some way to distribute a program to any computer. This is one of those totally unachievable goals, but many have tried with varying degrees of success. People naturally think of Java, but even before that there was UCSD’s P-code and many other attempts to pull off the same trick. We were impressed, though, with Redbean 2.0 which uses a single executable file to run a webserver — or possibly other things — on six different operating systems. If the six operating systems were all flavors of Linux or Windows that wouldn’t be very interesting. But thanks to APE — the Actually Portable Executable — format, you can run under Windows, Linux, MacOS, OpenBSD, NetBSD, and FreeBSD.
This is quite a feat when you realize that most of these take wildly different file formats. There is one small problem: you can’t use much of anything on the host operating system. However, if you look at Redbean, you’ll see there is quite a lot you can do.
The APE loader takes advantage of some quirks. Under Windows, the executable is just that — an executable. Otherwise, there is a small shell script that the APE loader can use to map the executable into memory and do the ELF loading. You might say, “Wait! I don’t have the APE loader.” That’s OK. Each APE program has the 4K loader embedded in it that it will use if necessary.
Of course, it works because, at the bottom, the x86 instructions are the same across all the platforms. Since it is hard to call out to the underlying system, there is a libc, Cosmopolitan, that you can include. There are other modules, including a Unix module that provides things like file access, sockets, and other things you probably need to make working programs. The only place you might have trouble is Microsoft’s WSL which is sort of Linux under Windows. It “knows” how to load Windows executables, even under Linux so you have to disable that in some cases, and that’s explained on the APE page. You can also find some details about the executable format.
Of course, you do need the right kind of processor. This isn’t going to ship out to your Raspberry Pi unless you run an emulator. But there are a lot of things we can think of that this would be perfect for. A web server on a USB stick, for example. Or a way to put an app on different Linux single-board computers. The APE loader is probably a good trick to have up your sleeve, too.
We had to wonder if you could integrate Tiddlywiki and have a portable notepad on a USB drive. Of course, if you want a portable web server, you could always just bring your own hardware.