cosmopolitan README

The project's README from github.com/jart/cosmopolitan. As one markdown file: README.md.

Cosmopolitan Honeybadger

build

Cosmopolitan

Cosmopolitan Libc makes C/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 7.3 + NetBSD + BIOS with the best possible performance and the tiniest footprint imaginable.

Background

For an introduction to this project, please read the actually portable executable blog post and cosmopolitan libc website. We also have API documentation.

Getting Started

You can start by obtaining a release of our cosmocc compiler from https://cosmo.zip/pub/cosmocc/.

mkdir -p cosmocc
cd cosmocc
wget https://cosmo.zip/pub/cosmocc/cosmocc.zip
unzip cosmocc.zip

Here's an example program we can write:

// hello.c
#include <stdio.h>

int main() {
  printf("hello world\n");
}

It can be compiled as follows:

cosmocc -o hello hello.c
./hello

The Cosmopolitan Libc runtime links some heavyweight troubleshooting features by default, which are very useful for developers and admins. Here's how you can log system calls:

./hello --strace

Here's how you can get a much more verbose log of function calls:

./hello --ftrace

You can use the Cosmopolitan's toolchain to build conventional open source projects which use autotools. This strategy normally works:

export CC=x86_64-unknown-cosmo-cc
export CXX=x86_64-unknown-cosmo-c++
./configure --prefix=/opt/cosmos/x86_64
make -j
make install

Cosmopolitan Source Builds

Cosmopolitan can be compiled from source on any of our supported platforms. The Makefile will download cosmocc automatically.

It's recommended that you install a systemwide APE Loader. This command requires sudo access to copy the ape command to a system folder and register with binfmt_misc on Linux, for even more performance.

ape/apeinstall.sh

You can now build the mono repo with any modern version of GNU Make. To bootstrap your build, you can install Cosmopolitan Make from this site:

https://cosmo.zip/pub/cosmos/bin/make

E.g.:

curl -LO https://cosmo.zip/pub/cosmos/bin/make
./make -j8
o//examples/hello

After you've built the repo once, you can also use the make from your cosmocc at .cosmocc/current/bin/make. You might even prefer to alias make to $COSMO/.cosmocc/current/bin/make.

Since the Cosmopolitan repository is very large, you might only want to build one particular thing. Here's an example of a target that can be compiled relatively quickly, which is a simple POSIX test that only depends on core LIBC packages.

rm -rf o//libc o//test
.cosmocc/current/bin/make o//test/posix/signal_test
o//test/posix/signal_test

Sometimes it's desirable to build a subset of targets, without having to list out each individual one. For example if you wanted to build and run all the unit tests in the TEST_POSIX package, you could say:

.cosmocc/current/bin/make o//test/posix

Cosmopolitan provides a variety of build modes. For example, if you want really tiny binaries (as small as 12kb in size) then you'd say:

.cosmocc/current/bin/make m=tiny

You can furthermore cut out the bloat of other operating systems, and have Cosmopolitan become much more similar to Musl Libc.

.cosmocc/current/bin/make m=tinylinux

For further details, see //build/config.mk.

Debugging

To print a log of system calls to stderr:

cosmocc -o hello hello.c
./hello --strace

To print a log of function calls to stderr:

cosmocc -o hello hello.c
./hello --ftrace

Both strace and ftrace use the unbreakable kprintf() facility, which is able to be sent to a file by setting an environment variable.

export KPRINTF_LOG=log
./hello --strace

GDB

Here's the recommended ~/.gdbinit config:

set host-charset UTF-8
set target-charset UTF-8
set target-wide-charset UTF-8
set osabi none
set complaints 0
set confirm off
set history save on
set history filename ~/.gdb_history
define asm
  layout asm
  layout reg
end
define src
  layout src
  layout reg
end
src

You normally run the .dbg file under gdb. If you need to debug the `` file itself, then you can load the debug symbols independently as

gdb foo -ex 'add-symbol-file foo.dbg 0x401000'

Platform Notes

Shells

If you use zsh and have trouble running APE programs try sh -c ./prog or simply upgrade to zsh 5.9+ (since we patched it two years ago). The same is the case for Python subprocess, old versions of fish, etc.

Linux

Some Linux systems are configured to launch MZ executables under WINE. Other distros configure their stock installs so that APE programs will print "run-detectors: unable to find an interpreter". For example:

jart@ubuntu:~$ wget https://cosmo.zip/pub/cosmos/bin/dash
jart@ubuntu:~$ chmod +x dash
jart@ubuntu:~$ ./dash
run-detectors: unable to find an interpreter for ./dash

You can fix that by registering APE with binfmt_misc:

sudo wget -O /usr/bin/ape https://cosmo.zip/pub/cosmos/bin/ape-$(uname -m).elf
sudo chmod +x /usr/bin/ape
sudo sh -c "echo ':APE:M::MZqFpD::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"
sudo sh -c "echo ':APE-jart:M::jartsr::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"

You should be good now. APE will not only work, it'll launch executables 400µs faster now too. However if things still didn't work out, it's also possible to disable binfmt_misc as follows:

sudo sh -c 'echo -1 > /proc/sys/fs/binfmt_misc/cli'     # remove Ubuntu's MZ interpreter
sudo sh -c 'echo -1 > /proc/sys/fs/binfmt_misc/status'  # remove ALL binfmt_misc entries

WSL

It's normally unsafe to use APE in a WSL environment, because it tries to run MZ executables as WIN32 binaries within the WSL environment. In order to make it safe to use Cosmopolitan software on WSL, run this:

sudo sh -c "echo -1 > /proc/sys/fs/binfmt_misc/WSLInterop"

Discord Chatroom

The Cosmopolitan development team collaborates on the Redbean Discord server. You're welcome to join us! https://discord.gg/FwAVVu7eJ4

Support Vector

Platform Min Version Circa
AMD K8 2003
Intel Core 2006
Linux 2.6.18 2007
Windows 8 [1] 2012
Darwin (macOS) 23.1.0+ 2023
OpenBSD 7.3 or earlier 2023
FreeBSD 13 2020
NetBSD 9.2 2021

[1] See our vista branch for a community supported version of Cosmopolitan that works on Windows Vista and Windows 7.

Special Thanks

Funding for this project is crowdsourced using GitHub Sponsors and Patreon. Your support is what makes this project possible. Thank you! We'd also like to give special thanks to the following groups and individuals:

For publicly sponsoring our work at the highest tier.

cosmopolitan (jart/cosmopolitan)

Searching Database…

这是做什么的?Cosmopolitan Libc 让 C/C++ 变成一次编译、到处运行的语言:你只需编译一次,同一个原生二进制文件就能在 Linux、Mac、Windows、各种 BSD 甚至 BIOS 上运行——就像 Java 一样,但不需要解释器或虚拟机。
它是一个库还是一套编译器工具链?两者都是——它是一个 libc 运行时,外加 cosmocc,也就是重新配置过的原版 GCC/Clang,能输出符合 POSIX 的多合一可执行文件,每个受支持的操作系统都能原生运行它,不需要虚拟机或解释器。
我该怎么用它来开始写程序?下载一个 cosmocc 编译器发行版,然后用它编译你的程序——README 里演示了一个 C 语言示例程序和对应的编译命令。编译输出的就是你可以交付的跨平台二进制文件。
使用之前我需要安装什么?正常使用只需要 cosmocc 工具链。如果想从源码构建 Cosmopolitan 本身,需要较新版本的 GNU Make(推荐使用 Cosmopolitan Make),Makefile 会自动下载 cosmocc。
运行这些二进制文件有什么坑吗?有几个:低于 5.9 的 zsh 无法启动 APE 程序(可以用 sh -c ./prog 绕过),一些 Linux 发行版会报 'run-detectors' 错误,直到你用 binfmt_misc 注册 APE 才好,另外 WSL 需要先做一些处理才能安全运行 APE。
它支持哪些操作系统,多老的硬件也能跑吗?一个二进制文件就能覆盖 Linux 2.6.18+、Windows 8+、macOS 23.1.0+、OpenBSD 7.3、FreeBSD 13 和 NetBSD 9.2,硬件上支持 AMD K8(2003 年)或 Intel Core(2006 年)及更新的 CPU,另外还支持 BIOS。社区维护的 vista 分支额外增加了对 Windows Vista/7 的支持。
APE 是什么?APE(Actually Portable Executable,真正可移植的可执行文件)是 cosmocc 输出的多合一文件格式——一个单独的二进制文件,Linux、macOS、Windows、各种 BSD 和 BIOS 都能原生执行它。安装系统级的 APE Loader(需要 sudo 权限)会在 Linux 上注册 binfmt_misc,让启动速度快大约 400 微秒。
它自带哪些调试工具?程序默认会链接 strace 和 ftrace:strace 记录系统调用,ftrace 记录函数调用,两者都通过非常可靠的 kprintf() 输出到 stderr,kprintf() 还可以通过环境变量把日志写到文件里。配合推荐的 ~/.gdbinit 和 .dbg 文件,GDB 也能正常使用。
生成的二进制文件能小到什么程度?有一种专门的构建模式可以生成小到 12kb 的二进制文件,你还可以进一步裁掉对其他操作系统的支持,让运行时表现得更接近 Musl Libc。
它能构建现有的开源项目吗?可以——cosmocc 工具链能够构建使用 autotools 的常规项目;README 说这种方案通常行得通,所以你可以把现有基于 configure 的构建指向 cosmocc。
What are similar projects?vis, ReBarUEFI, libfyaml, tailcat, btop, TurboBench, spdlog, nnn
Creator
jart
Size
318.3 MB
Stars
21.4K
Age
6y
Commits · 90 Days