git clone https://github.com/jart/cosmopolitan
cd cosmopolitan
# cross-compile on x86-64-linux for x86-64 linux+windows+macos+freebsd+openbsd+netbsd
make -j8 o//third_party/ggml/llama.com
o//third_party/ggml/llama.com --help
# cross-compile on x86-64-linux for aarch64-linux
make -j8 m=aarch64 o/aarch64/third_party/ggml/llama.com
# note: creates .elf file that runs on RasPi, etc.
# compile loader shim to run on arm64 macos
cc -o ape ape/ape-m1.c # use xcode
./ape ./llama.com --help # use elf aarch64 binary above
It goes the same speed as upstream for CPU inference. This is useful if you can't/won't recreate your weights files, or want to download old GGML weights off HuggingFace, since llama.com has support for every generation of the ggjt file format.I recommend obtaining Python from the Comsopolitan mono repo:
git clone https://github.com/jart/cosmopolitan
make -j8 o//third_party/python/python.com
That'll easily give you a static Python 3.6 binary that can be run under Blink.But it gets better. If you build Python in the Cosmopolitan Libc repository:
git clone https://github.com/jart/cosmopolitan
cd cosmopolitan
build/bootstrap/make.com -j8 o//third_party/python/python.com
Then you can use cosmo.pledge() directly from Python. $ o//third_party/python/python.com
Python 3.6.14+ (Actually Portable Python) [GCC 9.2.0] on cosmo
Type "help", "copyright", "credits" or "license" for more information.
>>: import cosmo, socket
>>: cosmo.pledge('stdio rpath wpath tty', None)
>>: print('hi')
hi
>>: socket.socket()
Traceback (most recent call last):
File "", line 1, in
File "/zip/.python/socket.py", line 144, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
PermissionError: [Errno 1] EPERM/1/Operation not permitted
Since we didn't put "inet" in the pledge, you can now be certain your PyPi deps aren't spying on you or uploading your bitcoin wallet to the cloud. You can even use os.fork() to rapidly put each dependency in its own process, then call cosmo.pledge() afterwards to grant each component of your app its own maximally restrictive policy.Cosmopolitan Python also ports OpenBSD's unveil() system call to Linux too. For example, to disallow all file system access, just call cosmo.unveil(None, None). You need a very recent version of Linux though. For instance, I use unveil() in production on GCE but I had to apt install linux-image-5.18.0-0.deb11.4-cloud-amd64 in order for Landlock LSM to be available to use unveil().
https://justine.lol/ape.html https://github.com/jart/cosmopolitan
Cosmopolitan is an incredibly cool project that does more than you think.
Its a lot more then that. Look at the code. https://github.com/jart/cosmopolitan You compile an executable with flags --nostdlib and --nostdinc because it includes cross platform standard libraries mapped to the OS specific magic numbers for system calls.
Think about a desktop computer with a graphics card. You can install either windows or linux on it. In both cases stuff gets rendered to the screen using the graphics cards. That is done through the driver, which maps higher level commands in libraries to instructions that graphics cards understands. While library format, is OS specific, the driver is essentially doing either ioctls or memory mapped io, both of which are the same x64 instructions.
So compiling everything with APE, means that library entry points are platform agnostic, and everything up the chain all the way to the game engine.
And, yes cosmopolitan only works on very basic things, as would be expected by the small number of people developing it, thats why I said IF it was adopted widely and developed further it would be possible.
git clone https://github.com/jart/cosmopolitan
cd cosmopolitan
make -j8 o//third_party/python/python.com
Then put Django in the zip under the .python directory. You can also use the `.args` file just like redbean to have `-m module` run by default. git clone https://github.com/jart/cosmopolitan.git
cd cosmopolitan
make -j16 o//third_party/python
That command will build Python and all its dependencies from scratch within the hermetic monorepo in addition to running its unit tests. On my $1,000 Core i9-9900 PC this takes 31.078 seconds. git clone https://github.com/jart/cosmopolitan
cd cosmopolitan
make -j8 o//third_party/quickjs/qjs.com
Build it on Linux but it runs anywhere. https://github.com/jart/cosmopolitan/tree/master/third_party... Here's a prebuilt binary https://justine.lol/qjs.com $ git clone https://github.com/jart/cosmopolitan && cd cosmopolitan
$ make -j8 o//examples/rusage.com o//examples/hello.com
$ o//examples/rusage.com o//examples/hello.com
hello world 123
RL: took 90µs wall time
RL: ballooned to 108kb in size
RL: needed 106µs cpu (0% kernel)
RL: caused 13 page faults (100% memcpy)
This one has a tiny bit more overhead than the practical minimum you saw earlier in the benchmark. Note that APE binaries start off as shell scripts so the first run is going to be slower. It's something I'm working towards improving in a variety of ways. Here's the executive summary of the upcoming changes: int ws, pid;
CHECK_NE(-1, (pid = fork()));
if (!pid) {
execve("ape.com", (char *const[]){"ape.com", 0}, environ);
perror("execve");
_Exit(127);
}
CHECK_EQ(pid, wait(&ws));
CHECK_TRUE(WIFEXITED(ws));
CHECK_EQ(0, WEXITSTATUS(ws));
Here's the latency of the various execution strategies: FORK+EXEC+EXIT+WAIT │ APE │ APE-LOADER │ BINFMT_MISC
───────────────────────────────────────────────────────────────
fork() + execve() │ 55µs │ 66µs │ 56µs
vfork() + execve() │ 25µs │ 446µs │ 35µs
/bin/sh -c ./ape (1st) │ 485µs │ 457µs │ 170µs
/bin/sh -c ./ape (avg) │ 148µs │ 466µs │ 159µs
> Cosmopolitan Libc makes C a build-once run-anywhere language, like Java, except it doesn't need an interpreter or virtual machine. Instead, it reconfigures stock GCC and Clang to output a POSIX-approved polyglot format that runs natively on Linux + Mac + Windows + FreeBSD + OpenBSD + NetBSD + BIOS with the best possible performance and the tiniest footprint imaginable.
> Please note that your APE binary will assimilate itself as a conventional resident of your platform after the first run, so it can be fast and efficient for subsequent executions.
Similar for Windows is https://github.com/sudachen/Molebox
Others:
- C/C++ has linker to link all to one binary
- CLI/webserver only https://github.com/jart/cosmopolitan like https://redbean.dev . Same exe works on many x64 OS like Windows/macOS/Linux/BSD, it embeds .zip file and can read/write to embedded .zip on the fly.
- AppImage https://appimage.org
- Tclkit https://wiki.tcl-lang.org/page/Tclkit , creator at http://kitcreator.rkeene.org/kitcreator
- Python Pop https://gitlab.com/saltstack/pop/pop
- Go has some code also to embed all to one binary, some is part of Buffalo web framework https://gobuffalo.io/en
-Godot Game engine https://godotengine.org can embed all to one executeable binary
Is there anything else similar, that embeds all to one executeable binary?
https://github.com/jart/cosmopolitan
(but, yeah, you probably want one version per target, even if hacks are cool)
the super example of this is justine's project
https://github.com/jart/cosmopolitan
it's pr fucking cool
> Cosmopolitan Libc makes C a build-once run-anywhere language, like Java, except it doesn't need an interpreter or virtual machine. Instead, it reconfigures stock GCC and Clang to output a POSIX-approved polyglot format that runs natively on Linux + Mac + Windows + FreeBSD + OpenBSD + NetBSD + BIOS with the best possible performance and the tiniest footprint imaginable.
[side note] I seem to recall there being something about the the way the musl header files are written that basically throws a wrench into any scientific computing code that's compiled with it. Might have something to do with security like those checks intended to prevent people from overlapping memcpy so someone should totally fix that.
Those concepts are implemented in the Cosmopolitan codebase here: https://github.com/jart/cosmopolitan/blob/master/ape/ape.S
That file runs a few hundred lines of old school assembly in order to bring us into the modern era. Towards the end of the file you'll notice it starts calling functions that are written in C which configure memory: https://github.com/jart/cosmopolitan/blob/master/libc/runtim...
After it configures memory, the ape.S file is able to call _start(). It also sets a bit in __hostos so that functions like write() know to use the serial port instead of issuing system calls: https://github.com/jart/cosmopolitan/blob/master/libc/calls/... That's the simplest possible example of a driver with spin locks which uses the x86 IN/OUT instructions.
If you want to take a dive off the deep end right now with Cosmopolitan on metal -- simulated -- then try the following build commands on your Linux terminal:
git clone https://github.com/jart/cosmopolitan
cd cosmopolitan
make -j8 o//examples/hello2.com
qemu-system-x86_64 -m 16 -nographic -fda o//examples/hello2.com
sed -i -e 's/USE_SYMBOL_HACK 0/USE_SYMBOL_HACK 1/' ape/ape.S
make -j8 o//examples/hello2.com o//tool/build/blinkenlights.com
o//tool/build/blinkenlights.com -rt o//examples/hello2.com
Since the overhead of each SYSCALL is ~1000 cycles and that this overhead just keeps getting worse thanks to things like Spectre, I'd say that's a real high performance operating system you've got there Google. History shows us that developers never choose microkernels. The only reason people use the things is because big corporations force us to. Like how Intel preinstalled MINIX on its chips without the consent of its customers. Where are the early authentic Google values that technology should be simple fast and stay out of the way? Your o/s is bad and you should feel bad for all the reasons Linus explained to Tannenbaum back in 1992.
If Google was smart they would support something people love which makes perfect technical sense, like Cosmopolitan, which enables normal POSIX programs to boot as unikernels that needn't incur any syscall overhead at all. The Actually Portable Executable format is also a valid disk image that can boot on bare metal in Google's Cloud. This is perfect for our brave new world of cloud services where instances only really need sockets to talk to network services like bigtable and therefore don't care about the overhead of a timesharing system. Google created this new world, yet it fails to understand its own creation if we consider how Fushcia is dragging us into the past by resurrecting designs that were too slow even during the days where multitenancy on a single machine mattered. See https://github.com/jart/cosmopolitan and https://github.com/jart/cosmopolitan/issues/20#issuecomment-... to learn more.
git clone https://github.com/jart/cosmopolitan
cd cosmopolitan
make -j8
o/third_party/chibicc/chibicc.com -include libc/integral/normalize.inc -o hello.com.dbg examples/hello.c o/cosmopolitan.a
o/third_party/gcc/bin/x86_64-linux-musl-objcopy -SO binary hello.com.dbg hello.com
./hello.com
Trapping (SYSCALL/INT) is a loose binding. The kernel can evolve all it wants internally. It can introduce new ABIs. Processes are also a loose binding. I think communicating with other tools via pipes and sockets is a fantastic model of cooperation. Same goes for vendoring with static linking. Does that mean I'm going to voluntarily load Linux distro DSOs into my address space? Never again. Programs that do that, won't have a future outside Docker containers.
Also, my executables are VM images. They can boot on metal too. Read https://justine.lol/ape.html and https://github.com/jart/cosmopolitan/issues/20#issuecomment-... Except unlike a Docker distro container, my exes are more on the order of 16kb in size. That's how fat an executable needs to be, in order to run on six different operating systems and boot from bios too.
(Looking at https://github.com/jart/cosmopolitan it's an interesting hack. Might be useful in some cases, for example command-line tools like git.)
Have you made something that looks like a better make? If so, it would be nice to know more. Thanks.
Using a macro is more succinct:
const char *s = gc(xasprintf("%s/%s", dir, name));
Than what's being proposed: char *s = xasprintf("%s/%s", dir, name);
defer free(s);
See this x86 reference implementation of defer() and gc(). https://gist.github.com/jart/aed0fd7a7fa68385d19e76a63db687f... That should just work with GCC and Clang. That code is originally from the Cosmopolitan C Library (https://github.com/jart/cosmopolitan) so check it out if you like the Gist and want more!Please note the macro operates at function call boundaries rather than block scoped. I consider that a feature since it behaves sort of like a memory pool. Having side effects at the block scope level requires changing compilers, the language itself, and it would cause several important gcc optimization passes to be disabled in places where it's used.
If you don't feel comfortable engaging with the new toxic culture, you can use my underground liferaft. My liferaft isn't an operating system, but rather an attempt to help us not depend on them as much. I have no idea who's controlling GNU/Linux these days and Occupy was enough drama for one lifetime. My code has the same focus on clarity that Antirez put into his Redis codebase. My liferaft also empowers you to build and distribute tiny native portable programs (like Kilo!) using hermetically sealed tools. See https://github.com/jart/cosmopolitan
jart@debian:~/cosmo$ make -j12 CPPFLAGS+=-DIM_FEELING_NAUGHTY MODE=tiny o/tiny/examples/hello.com
jart@debian:~/cosmo$ ls -sh o/tiny/examples/hello.com
20K o/tiny/examples/hello.com
Note: Output binary runs on Windows, Mac, and BSD too.However Apple comes across as cruel when they make decisions like these, which break software distributability of things like native machine learning code. For example one build system that enables that use case is: https://github.com/jart/cosmopolitan
At best these new POWER or ARM architecture Apple PCs are going to be like a C-class Mercedes. So I'm honestly not that concerned. Having distributable open source executables support them shouldn't be that difficult, it's just that it'd add bloat and require trading away important parts of Von Neumman architecture, such as self-modifying code, in order to be done easily.
If Apple wasn't being goofballs, they would have taken into consideration that the x86_64 patents should expire around this year, so Apple could have just as easily adapted the POWER design of which IBM divested themselves, to support x86, by simply bolting on a code morphing layer like Xed. Since it'd be pretty great to be able to buy an Apple PC that doesn't have MINIX lurking inside the chip, without making tradeoffs that could be most accurately described as breaking open source software to save money.