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 or UEFI with the best possible performance and the tiniest footprint imaginable.
Changes
- Productionize redbean web server bf03b2e af59806 01e6b3a e56a9d0 690be54 472b95f c029e83
- Make it possible for APE binaries to modify their own ZIP contents bf03b2e daa32d2 b918706 690be54
- Introduce
OpenExecutable
for restoring APE bf03b2e or alternativelyape-no-modify-self.o
da8a08f - Add Fabrice Bellard's JavaScript engine to third party 8f52c0d and get it to pass test262 8a91518
- System calls now return EFAULT in ASAN mode if memory is invalid 690be54
- Add SQLite to third party (thanks @ahgamut) #162 221817e
- Add sendmsg and recvmsg (thanks @fabriziobertocci) #148
- Added LaunchBrowser API to redbean (thanks @Rotario) #153
- Wide character classification now supports UNICODE 1b5a571
- Add syslog (thanks @fabriziobertocci) #136
- Add strxfrm (thanks @ahgamut) #160
- Add struct ip_mreq (thanks @fabriziobertocci) #143
- Add MacOS toolchain instructions (thanks @d4tocchini) #163
- Add 14 missing system call prototypes (thanks @dosisod) #142
- Improve consts.sh magnums bf03b2e b107d27 1b5a571 690be54
- Fix stdio in cases where it overflows kernel tty buffer b107d27
- Improve backwards compatibility with GNU Make 1f2288b
- Fix GDB attach on crash w/ OpenBSD msyscall() bf03b2e
- Remove undefined behaviors b383817
- Add TUI paneling example 26ac687
- Add netcat clone b107d27 4864565
- Improve rusage accounting apis bf03b2e
Getting Started
If you're doing your development work on Linux or BSD then you need just five files to get started. Here's what you do on Linux:
wget https://justine.lol/cosmopolitan/cosmopolitan-amalgamation-1.0.zip
unzip cosmopolitan-amalgamation-1.0.zip
printf 'main() { printf("hello world\\n"); }\n' >hello.c
gcc -g -Os -static -nostdlib -nostdinc -fno-pie -no-pie -mno-red-zone \
-fno-omit-frame-pointer -pg -mnop-mcount \
-o hello.com.dbg hello.c -fuse-ld=bfd -Wl,-T,ape.lds \
-include cosmopolitan.h crt.o ape.o cosmopolitan.a
objcopy -S -O binary hello.com.dbg hello.com
You now have a portable program. 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.
./hello.com
bash -c './hello.com' # zsh/fish workaround (we upstreamed patches)
So if you intend to copy the binary to Windows or Mac then please do that before you run it, not after.
If you're developing on MacOS you can install the GNU compiler collection for x86_64-elf via homebrew:
brew install x86_64-elf-gcc
Then in the above scripts just replace gcc
and objcopy
with x86_64-elf-gcc
and x86_64-elf-objcopy
to compile your APE binary.
If you're developing on Windows then you need to download an x86_64-pc-linux-gnu toolchain beforehand. See the Compiling on Windows tutorial. It's needed because the ELF object format is what makes universal binaries possible.
Cosmopolitan can also be compiled from source on any Linux distro.
wget https://justine.lol/cosmopolitan/cosmopolitan-1.0.tar.gz
tar xf cosmopolitan-1.0.tar.gz # see releases page
cd cosmopolitan
make -j8 # should take ~30 seconds
o//examples/hello.com
o/third_party/lua/lua.com -e 'print(2 + 2)'
o/third_party/quickjs/qjs.com -e 'console.log(2+2)'
echo select 2 + 2 | o/third_party/sqlite3/sqlite3.com
printf 'GET /404.html\n\n' | o//examples/nc.com redbean.justine.lol 80
o//tool/viz/life.com
Contributing
We have automated Travis CI testing. Any pull request you send is going to get feedback in 2 minutes and 16 seconds. That's how long Travis CI takes to test this whole repository, which builds 14,507 objects, 68 libraries, and 461 static executables, 289 of which are test executables which are run too. On a real PC, building and testing everything from scratch takes 34 seconds instead of two minutes. If the hostnames freebsd, openbsd, netbsd, rhel7, rhel5, xnu, win7, and win10 can be ssh'd into, then make test -j8
takes 15 additional seconds to deploy and run all 300 test executables on those operating systems too. See CONTRIBUTING.md for further details.
Support Vector
Platform | Min Version | Circa |
---|---|---|
AMD | K8 Venus | 2005 |
Intel | Core | 2006 |
New Technology | Vista | 2006 |
GNU/Systemd | 2.6.18 | 2007 |
XNU's Not UNIX! | 15.6 | 2018 |
FreeBSD | 12 | 2018 |
OpenBSD | 6.4 | 2018 |
NetBSD | 9.1 | 2020 |
GNU Make | 3.80 | 2010 |