• Stars
    star
    14,718
  • Rank 1,897 (Top 0.04 %)
  • Language
    C
  • License
    ISC License
  • Created almost 4 years ago
  • Updated 29 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

build-once run-anywhere c library

Cosmopolitan Honeybadger

build

Cosmopolitan

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.

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 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.

build/bootstrap/make -j8
o//examples/hello

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
build/bootstrap/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:

build/bootstrap/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:

build/bootstrap/make m=tiny

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

build/bootstrap/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 Venus 2005
Intel Core 2006
Linux 2.6.18 2007
Windows 8 [1] 2012
Darwin (macOS) 23.1.0+ 2023
OpenBSD 7 2021
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.

More Repositories

1

blink

tiniest x86-64-linux emulator
C
6,589
star
2

sectorlisp

Bootstrapping LISP in a Boot Sector
C
1,147
star
3

hiptext

Turn images into text better than caca/aalib
C++
749
star
4

gosip

Public Switched Telecommunications Network Unleashed
Go
429
star
5

bestline

ANSI Standard X3.64 Teletypewriter Command Session Library
C
349
star
6

fabulous

Print images, colors, and stylish text to the terminal with Python
Python
340
star
7

disaster

Disassemble C/C++ code under cursor in Emacs
Emacs Lisp
276
star
8

landlock-make

Sandboxing for GNU Make has never been easier
C++
178
star
9

redisbayes

Naïve Bayesian Text Classifier on Redis
Python
114
star
10

occupywallst

Stomping out capitalism, one line of code at a time
JavaScript
94
star
11

pledge

OpenBSD APIs ported to Linux userspace using SECCOMP BPF and Landlock LSM
C
80
star
12

jtckdint

C23 Checked Arithmetic
C++
61
star
13

asterisk-voicechanger

Asterisk module for adjusting pitch of voices
C
31
star
14

spandsp

C
28
star
15

tokenbucket

Atomic SWAR Token Buckets
C
27
star
16

poemy2

poemy (a poetry generator) rewritten in C++
C++
24
star
17

django-bone

Generates Django Project Skeletons Simply and Properly
JavaScript
23
star
18

swatpd

Stolen WiFi Aggregate Tunneling Protocol Dæmon (RAID your internet connections!)
C
18
star
19

zsh

Zsh patched to support Actually Portable Executables git://git.code.sf.net/p/zsh/code (upstream pending)
C
15
star
20

sofia-sip

Forked for features and packaging
C
14
star
21

blink-isystem

C++
11
star
22

includeme

Automatic C/C++ '#include' and 'using' in Emacs (WORK IN PROGRESS)
Emacs Lisp
10
star
23

justinemacs

My emacs25 text editor configuration
Emacs Lisp
9
star
24

timespan

Check if timestamp falls within specific boundaries
Python
7
star
25

js2-closure

Automatic goog.require for Closure
Emacs Lisp
5
star
26

sparkles

A decision making tool (defunct, incomplete)
JavaScript
5
star
27

web_library_example

Bootstrap Web Apps for ML Visualization
HTML
5
star
28

emacs-copilot

Large language model code completion for Emacs
Emacs Lisp
5
star
29

poemy

Poetry Generator (DEPRECATED: See poemy2)
JavaScript
4
star
30

asterisk-parrot

Annoying robot that repeats what you say on the phone
C
4
star
31

jart.github.io

4
star
32

tensorflow-makefile

C++
3
star
33

libc-test

C
3
star
34

chessy

A happy toy chess engine, just for fun :) If you want a proper chess engine, check out Stockfish.
C++
3
star
35

long-s.org

Vintage Engliſh Tranſlator
CoffeeScript
2
star
36

freeswitch

Open Source Telecommunications Platform (Unofficial Fork)
C
2
star
37

verobject

Version controlled object database on Redis
Python
1
star