2021-02-04 11:30:47 +00:00
|
|
|
|
![Cosmopolitan Honeybadger](usr/share/img/honeybadger.png)
|
|
|
|
|
|
2022-06-23 20:22:28 +00:00
|
|
|
|
[![build](https://github.com/jart/cosmopolitan/actions/workflows/build.yml/badge.svg)](https://github.com/jart/cosmopolitan/actions/workflows/build.yml)
|
2020-06-15 14:18:57 +00:00
|
|
|
|
# Cosmopolitan
|
|
|
|
|
|
2021-01-29 00:19:10 +00:00
|
|
|
|
[Cosmopolitan Libc](https://justine.lol/cosmopolitan/index.html) makes C
|
|
|
|
|
a build-once run-anywhere language, like Java, except it doesn't need an
|
2021-02-08 17:19:00 +00:00
|
|
|
|
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.
|
2021-01-29 00:19:10 +00:00
|
|
|
|
|
|
|
|
|
## Background
|
|
|
|
|
|
|
|
|
|
For an introduction to this project, please read the [αcτµαlly pδrταblε
|
|
|
|
|
εxεcµταblε](https://justine.lol/ape.html) blog post and [cosmopolitan
|
|
|
|
|
libc](https://justine.lol/cosmopolitan/index.html) website. We also have
|
|
|
|
|
[API documentation](https://justine.lol/cosmopolitan/documentation.html).
|
|
|
|
|
|
|
|
|
|
## Getting Started
|
|
|
|
|
|
2023-11-11 22:04:26 +00:00
|
|
|
|
You can start by obtaining a release of our `cosmocc` compiler from
|
|
|
|
|
<https://cosmo.zip/pub/cosmocc/>.
|
2021-01-29 00:19:10 +00:00
|
|
|
|
|
2023-07-25 03:15:18 +00:00
|
|
|
|
```sh
|
2023-11-11 22:04:26 +00:00
|
|
|
|
mkdir -p cosmocc
|
|
|
|
|
cd cosmocc
|
|
|
|
|
wget https://cosmo.zip/pub/cosmocc/cosmocc.zip
|
|
|
|
|
unzip cosmocc.zip
|
2021-03-04 15:54:41 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-11-11 22:04:26 +00:00
|
|
|
|
Here's an example program we can write:
|
2021-03-04 15:54:41 +00:00
|
|
|
|
|
2023-06-17 14:55:48 +00:00
|
|
|
|
```c
|
|
|
|
|
// hello.c
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
printf("hello world\n");
|
|
|
|
|
}
|
2021-02-08 17:19:00 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-11-11 22:04:26 +00:00
|
|
|
|
It can be compiled as follows:
|
2022-09-04 07:09:24 +00:00
|
|
|
|
|
2023-07-25 03:15:18 +00:00
|
|
|
|
```sh
|
2023-11-11 22:04:26 +00:00
|
|
|
|
cosmocc -o hello hello.c
|
|
|
|
|
./hello
|
2022-09-04 07:09:24 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-17 14:55:48 +00:00
|
|
|
|
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:
|
2022-12-22 07:00:51 +00:00
|
|
|
|
|
2023-07-25 03:15:18 +00:00
|
|
|
|
```sh
|
2023-11-11 22:04:26 +00:00
|
|
|
|
./hello --strace
|
2023-06-17 14:55:48 +00:00
|
|
|
|
```
|
2022-05-24 17:19:39 +00:00
|
|
|
|
|
2023-06-17 14:55:48 +00:00
|
|
|
|
Here's how you can get a much more verbose log of function calls:
|
2022-05-24 17:19:39 +00:00
|
|
|
|
|
2023-07-25 03:15:18 +00:00
|
|
|
|
```sh
|
2023-11-11 22:04:26 +00:00
|
|
|
|
./hello --ftrace
|
2023-09-11 20:51:37 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-11-11 22:04:26 +00:00
|
|
|
|
You can use the Cosmopolitan's toolchain to build conventional open
|
|
|
|
|
source projects which use autotools. This strategy normally works:
|
2023-09-11 20:51:37 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
2023-11-11 22:04:26 +00:00
|
|
|
|
export CC=x86_64-unknown-cosmo-cc
|
|
|
|
|
export CXX=x86_64-unknown-cosmo-c++
|
|
|
|
|
./configure --prefix=/opt/cosmos/x86_64
|
2023-09-11 20:51:37 +00:00
|
|
|
|
make -j
|
|
|
|
|
make install
|
|
|
|
|
```
|
|
|
|
|
|
2023-11-13 06:04:07 +00:00
|
|
|
|
## Cosmopolitan Source Builds
|
2021-05-07 14:52:32 +00:00
|
|
|
|
|
2023-11-13 06:04:07 +00:00
|
|
|
|
Cosmopolitan can be compiled from source on any of our supported
|
|
|
|
|
platforms. First, you need to download or clone the repository. If
|
|
|
|
|
you're not using x86-64 Linux then you'll need cosmocc too.
|
2021-02-08 17:19:00 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
2023-11-13 06:04:07 +00:00
|
|
|
|
git clone https://github.com/jart/cosmopolitan cosmo
|
|
|
|
|
cd cosmo
|
|
|
|
|
mkdir -p o/third_party/gcc
|
|
|
|
|
pushd o/third_party/gcc
|
|
|
|
|
wget https://cosmo.zip/pub/cosmocc/cosmocc.zip
|
|
|
|
|
unzip cosmocc.zip
|
|
|
|
|
popd
|
2022-05-25 18:31:08 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-11-13 06:04:07 +00:00
|
|
|
|
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.
|
2022-05-25 18:31:08 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
2023-11-13 06:04:07 +00:00
|
|
|
|
ape/apeinstall.sh
|
2021-01-29 00:19:10 +00:00
|
|
|
|
```
|
2021-03-01 00:40:36 +00:00
|
|
|
|
|
2023-11-13 06:04:07 +00:00
|
|
|
|
You can now build the mono repo with any modern version of GNU Make. To
|
|
|
|
|
make life easier, we've included one in the cosmocc toolchain, which is
|
|
|
|
|
guaranteed to be compatible and furthermore includes our extensions for
|
|
|
|
|
doing build system sandboxing.
|
2022-05-25 18:31:08 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
2023-11-13 06:04:07 +00:00
|
|
|
|
o//third_party/gcc/bin/make -j8
|
|
|
|
|
o//examples/hello.com
|
2022-05-25 18:31:08 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Since the Cosmopolitan repository is very large, you might only want to
|
2023-11-13 06:04:07 +00:00
|
|
|
|
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.
|
2022-05-25 18:31:08 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
2023-11-13 06:04:07 +00:00
|
|
|
|
rm -rf o//libc o//test
|
|
|
|
|
o//third_party/gcc/bin/make o//test/posix/signal_test.com
|
|
|
|
|
o//test/posix/signal_test.com
|
2022-05-25 18:31:08 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Sometimes it's desirable to build a subset of targets, without having to
|
2023-11-13 06:04:07 +00:00
|
|
|
|
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:
|
2022-05-25 18:31:08 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
2023-11-13 06:04:07 +00:00
|
|
|
|
o//third_party/gcc/bin/make o//test/posix
|
2022-05-25 18:31:08 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
```sh
|
2023-11-13 06:04:07 +00:00
|
|
|
|
o//third_party/gcc/bin/make m=tiny
|
2022-05-25 18:31:08 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-11-13 06:04:07 +00:00
|
|
|
|
You can furthermore cut out the bloat of other operating systems, and
|
|
|
|
|
have Cosmopolitan become much more similar to Musl Libc.
|
2022-05-25 18:31:08 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
2023-11-13 06:04:07 +00:00
|
|
|
|
o//third_party/gcc/bin/make m=tinylinux
|
2022-05-25 18:31:08 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
For further details, see [//build/config.mk](build/config.mk).
|
|
|
|
|
|
2023-11-13 06:04:07 +00:00
|
|
|
|
## Debugging
|
2023-06-17 14:55:48 +00:00
|
|
|
|
|
2023-11-13 06:04:07 +00:00
|
|
|
|
To print a log of system calls to stderr:
|
2023-06-17 14:55:48 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
2023-11-13 06:04:07 +00:00
|
|
|
|
cosmocc -o hello hello.c
|
|
|
|
|
./hello --strace
|
2023-06-17 14:55:48 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-11-13 06:04:07 +00:00
|
|
|
|
To print a log of function calls to stderr:
|
2023-06-17 14:55:48 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
2023-11-13 06:04:07 +00:00
|
|
|
|
cosmocc -o hello hello.c
|
|
|
|
|
./hello --ftrace
|
2023-06-17 14:55:48 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-11-13 06:04:07 +00:00
|
|
|
|
Both strace and ftrace use the unbreakable kprintf() facility, which is
|
|
|
|
|
able to be sent to a file by setting an environment variable.
|
2023-06-17 14:55:48 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
2023-11-13 06:04:07 +00:00
|
|
|
|
export KPRINTF_LOG=log
|
|
|
|
|
./hello --strace
|
2023-06-17 14:55:48 +00:00
|
|
|
|
```
|
|
|
|
|
|
2022-04-17 19:25:10 +00:00
|
|
|
|
## GDB
|
|
|
|
|
|
|
|
|
|
Here's the recommended `~/.gdbinit` config:
|
|
|
|
|
|
2022-09-04 05:19:55 +00:00
|
|
|
|
```gdb
|
2022-04-17 19:25:10 +00:00
|
|
|
|
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 `.com.dbg` file under gdb. If you need to debug the
|
|
|
|
|
`.com` file itself, then you can load the debug symbols independently as
|
|
|
|
|
|
2023-07-25 03:15:18 +00:00
|
|
|
|
```sh
|
2022-04-17 19:25:10 +00:00
|
|
|
|
gdb foo.com -ex 'add-symbol-file foo.com.dbg 0x401000'
|
|
|
|
|
```
|
|
|
|
|
|
2023-11-13 10:26:34 +00:00
|
|
|
|
## Platform Notes
|
|
|
|
|
|
2023-11-16 07:25:47 +00:00
|
|
|
|
### 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.
|
|
|
|
|
|
2023-11-13 10:26:34 +00:00
|
|
|
|
### Linux
|
|
|
|
|
|
2023-11-15 02:13:36 +00:00
|
|
|
|
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:
|
2023-11-13 10:26:34 +00:00
|
|
|
|
|
2023-11-15 02:13:36 +00:00
|
|
|
|
```sh
|
|
|
|
|
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`:
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
sudo wget -O /usr/bin/ape https://cosmo.zip/pub/cosmos/bin/ape-$(uname -m).elf
|
|
|
|
|
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"
|
2023-11-13 10:26:34 +00:00
|
|
|
|
```
|
2023-11-15 02:13:36 +00:00
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
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
|
2023-11-13 10:26:34 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### WSL
|
|
|
|
|
|
|
|
|
|
It's normally unsafe to use APE in a WSL environment, because it tries
|
2023-11-15 02:13:36 +00:00
|
|
|
|
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:
|
2023-11-13 10:26:34 +00:00
|
|
|
|
|
|
|
|
|
```sh
|
2023-11-15 02:13:36 +00:00
|
|
|
|
sudo sh -c "echo -1 > /proc/sys/fs/binfmt_misc/WSLInterop"
|
2023-11-13 10:26:34 +00:00
|
|
|
|
```
|
|
|
|
|
|
2023-03-30 17:49:04 +00:00
|
|
|
|
## Discord Chatroom
|
|
|
|
|
|
|
|
|
|
The Cosmopolitan development team collaborates on the Redbean Discord
|
2023-03-30 22:42:58 +00:00
|
|
|
|
server. You're welcome to join us! <https://discord.gg/FwAVVu7eJ4>
|
2023-03-30 17:49:04 +00:00
|
|
|
|
|
2021-03-01 00:40:36 +00:00
|
|
|
|
## Support Vector
|
|
|
|
|
|
2021-03-04 14:13:32 +00:00
|
|
|
|
| Platform | Min Version | Circa |
|
|
|
|
|
| :--- | ---: | ---: |
|
|
|
|
|
| AMD | K8 Venus | 2005 |
|
|
|
|
|
| Intel | Core | 2006 |
|
2022-07-19 23:05:19 +00:00
|
|
|
|
| Linux | 2.6.18 | 2007 |
|
2022-11-02 13:49:42 +00:00
|
|
|
|
| Windows | 8 [1] | 2012 |
|
2022-07-19 23:05:19 +00:00
|
|
|
|
| Mac OS X | 15.6 | 2018 |
|
2023-07-01 12:10:12 +00:00
|
|
|
|
| OpenBSD | 7 | 2021 |
|
2022-09-15 10:49:34 +00:00
|
|
|
|
| FreeBSD | 13 | 2020 |
|
2022-06-11 16:27:14 +00:00
|
|
|
|
| NetBSD | 9.2 | 2021 |
|
2022-06-22 11:35:43 +00:00
|
|
|
|
|
2022-11-02 13:49:42 +00:00
|
|
|
|
[1] See our [vista branch](https://github.com/jart/cosmopolitan/tree/vista)
|
|
|
|
|
for a community supported version of Cosmopolitan that works on Windows
|
|
|
|
|
Vista and Windows 7.
|
|
|
|
|
|
2022-06-22 11:35:43 +00:00
|
|
|
|
## Special Thanks
|
|
|
|
|
|
|
|
|
|
Funding for this project is crowdsourced using
|
|
|
|
|
[GitHub Sponsors](https://github.com/sponsors/jart) and
|
|
|
|
|
[Patreon](https://www.patreon.com/jart). Your support is what makes this
|
|
|
|
|
project possible. Thank you! We'd also like to give special thanks to
|
2022-09-28 02:00:11 +00:00
|
|
|
|
the following groups and individuals:
|
2022-06-22 11:35:43 +00:00
|
|
|
|
|
|
|
|
|
- [Joe Drumgoole](https://github.com/jdrumgoole)
|
2022-09-28 01:17:22 +00:00
|
|
|
|
- [Rob Figueiredo](https://github.com/robfig)
|
2022-09-28 02:00:11 +00:00
|
|
|
|
- [Wasmer](https://wasmer.io/)
|
2022-06-22 11:35:43 +00:00
|
|
|
|
|
|
|
|
|
For publicly sponsoring our work at the highest tier.
|