The holy grail of computing is having a way to distribute a program to any computer. It’s 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, however, with Redbean 2.0, which uses a single executable file to run a web server – or possibly other things – on six different operating systems. If all six operating systems were versions of Linux or Windows, that wouldn’t be very interesting. But thanks to the APE format – the truly portable executable format – you can run on Windows, Linux, MacOS, OpenBSD, NetBSD and FreeBSD.
It’s quite an achievement when you realize that most of them take very different file formats. There’s a small problem: you can’t use much on the host OS. However, if you look at Redbean, you’ll see that you can do a lot.
The APE loader takes advantage of some quirks. On Windows, the executable is just that – an executable. Alternatively, there is a small shell script that the APE loader can use to map the executable to memory and perform the ELF loading. You might say, “Wait! I don’t have the APE loader. Its good. Each APE program integrates the 4K loader that it will use if necessary.
Of course it works because at the bottom the x86 instructions are the same on all platforms. Since it’s hard to call 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 build working programs. The only place you might have trouble is Microsoft’s WSL which is a kind of Linux on Windows. It “knows” how to load Windows executables, even on Linux, so you need to disable it in some cases, and that’s explained on the APE page. You can also find some details about the executable format.
Of course, you need the right type of processor. This won’t be sent to your Raspberry Pi unless you’re running an emulator. But there are a lot of things we can think it would be perfect for. A web server on a USB key, for example. Or a way to put an application 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 can always bring your own hardware.