# jart/cosmopolitan architecture

> Architecture diagram of the GitHub repository jart/cosmopolitan, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jart/cosmopolitan
- Repository: https://github.com/jart/cosmopolitan

## Overview

Cosmopolitan is a portable C/C++ runtime and executable format: users build programs with the external cosmocc toolchain, then run the resulting polyglot executable natively across supported operating systems. The repository tree shows the APE loader, libc facilities, DSP support, and several user-facing programs. README statements establish the portability and debugging workflow; source excerpts support the Plinko entry point and selected network utilities. Many subsystems are represented only by the tree, so their internal call paths are not asserted.

Main entry points: [`libc/runtime/runtime.h`](https://github.com/jart/cosmopolitan/blob/master/libc/runtime/runtime.h) (Process runtime), [`ape/ape-m1.c`](https://github.com/jart/cosmopolitan/blob/master/ape/ape-m1.c) (APE executable format), [`ape/loader.c`](https://github.com/jart/cosmopolitan/blob/master/ape/loader.c) (APE loader), [`tool/chat/client.c`](https://github.com/jart/cosmopolitan/blob/master/tool/chat/client.c) (Encrypted chat client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_exec["Executable portability"]
  node_ape["APE loader<br/>[loader.c]"]
  node_ape_format["APE executable format<br/>[ape-m1.c]"]
end

subgraph group_runtime["Libc runtime"]
  node_libc_calls["OS system calls<br/>[calls.h]"]
  node_libc_runtime["Process runtime<br/>[runtime.h]"]
  node_libc_nt["Windows compatibility<br/>[windowsesque.h]"]
  node_libc_str["String library<br/>[str.h]"]
  node_libc_threads["Thread contexts<br/>[makecontext.c]"]
end

subgraph group_media["DSP and terminal"]
  node_dsp_core["DSP primitives<br/>[core.h]"]
  node_audio["Audio I/O<br/>[audio.c]"]
  node_mpeg["MPEG decoder<br/>[pl_mpeg.c]"]
  node_scaling["Image scaling<br/>[scale.c]"]
  node_tty["Terminal graphics<br/>[tty.h]"]
end

subgraph group_programs["Programs and tools"]
  node_http_escape["HTTP escaping<br/>[escape.h]"]
  node_chat_server["Encrypted chat server<br/>[server.c]"]
  node_chat_client["Encrypted chat client<br/>[client.c]"]
  node_turfwar["HTTP server<br/>[turfwar.c]"]
  node_dns_tool["DNS query utility<br/>[libresolv_query.c]"]
  node_plinko["Plinko interpreter<br/>[main.c]"]
end

node_developer(("Program developer"))
node_cosmocc["cosmocc toolchain"]
node_external_os(("Supported operating systems"))

node_developer -->|"builds with"| node_cosmocc
node_cosmocc -->|"emits executable"| node_ape_format
node_developer -->|"runs program"| node_ape_format
node_ape_format -.->|"loads through"| node_ape
node_ape -.->|"starts runtime"| node_libc_runtime
node_libc_runtime -.->|"provides OS calls"| node_libc_calls
node_ape_format -->|"runs natively on"| node_external_os
node_libc_runtime -.->|"supports Windows API"| node_libc_nt
node_libc_runtime -.->|"provides contexts"| node_libc_threads
node_chat_server -->|"uses escaping"| node_http_escape
node_chat_client -->|"uses escaping"| node_http_escape
node_developer -.->|"runs chat client"| node_chat_client
node_developer -.->|"runs server"| node_turfwar
node_developer -.->|"runs query tool"| node_dns_tool
node_developer -->|"runs interpreter"| node_plinko

click node_ape "https://github.com/jart/cosmopolitan/blob/master/ape/loader.c"
click node_ape_format "https://github.com/jart/cosmopolitan/blob/master/ape/ape-m1.c"
click node_libc_calls "https://github.com/jart/cosmopolitan/blob/master/libc/calls/calls.h"
click node_libc_runtime "https://github.com/jart/cosmopolitan/blob/master/libc/runtime/runtime.h"
click node_libc_nt "https://github.com/jart/cosmopolitan/blob/master/libc/isystem/windowsesque.h"
click node_libc_str "https://github.com/jart/cosmopolitan/blob/master/libc/str/str.h"
click node_libc_threads "https://github.com/jart/cosmopolitan/blob/master/libc/thread/makecontext.c"
click node_http_escape "https://github.com/jart/cosmopolitan/blob/master/net/http/escape.h"
click node_chat_server "https://github.com/jart/cosmopolitan/blob/master/tool/chat/server.c"
click node_chat_client "https://github.com/jart/cosmopolitan/blob/master/tool/chat/client.c"
click node_turfwar "https://github.com/jart/cosmopolitan/blob/master/net/turfwar/turfwar.c"
click node_dns_tool "https://github.com/jart/cosmopolitan/blob/master/tool/net/libresolv_query.c"
click node_plinko "https://github.com/jart/cosmopolitan/blob/master/tool/plinko/main.c"
click node_dsp_core "https://github.com/jart/cosmopolitan/blob/master/dsp/core/core.h"
click node_audio "https://github.com/jart/cosmopolitan/blob/master/dsp/audio/audio.c"
click node_mpeg "https://github.com/jart/cosmopolitan/blob/master/dsp/mpeg/pl_mpeg.c"
click node_scaling "https://github.com/jart/cosmopolitan/blob/master/dsp/scale/scale.c"
click node_tty "https://github.com/jart/cosmopolitan/blob/master/dsp/tty/tty.h"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_ape,node_ape_format toneBlue
class node_libc_calls,node_libc_runtime,node_libc_nt,node_libc_str,node_libc_threads toneAmber
class node_dsp_core,node_audio,node_mpeg,node_scaling,node_tty toneMint
class node_http_escape,node_chat_server,node_chat_client,node_turfwar,node_dns_tool,node_plinko toneRose
class node_developer,node_cosmocc,node_external_os toneIndigo
```

## Components

### Executable portability

- [APE loader](https://github.com/jart/cosmopolitan/blob/master/ape/loader.c): `ape/loader.c`
- [APE executable format](https://github.com/jart/cosmopolitan/blob/master/ape/ape-m1.c): `ape/ape-m1.c`

### Libc runtime

- [OS system calls](https://github.com/jart/cosmopolitan/blob/master/libc/calls/calls.h): `libc/calls/calls.h`
- [Process runtime](https://github.com/jart/cosmopolitan/blob/master/libc/runtime/runtime.h): `libc/runtime/runtime.h`
- [Windows compatibility](https://github.com/jart/cosmopolitan/blob/master/libc/isystem/windowsesque.h): `libc/isystem/windowsesque.h`
- [String library](https://github.com/jart/cosmopolitan/blob/master/libc/str/str.h): `libc/str/str.h`
- [Thread contexts](https://github.com/jart/cosmopolitan/blob/master/libc/thread/makecontext.c): `libc/thread/makecontext.c`

### DSP and terminal

- [DSP primitives](https://github.com/jart/cosmopolitan/blob/master/dsp/core/core.h): `dsp/core/core.h`
- [Audio I/O](https://github.com/jart/cosmopolitan/blob/master/dsp/audio/audio.c): `dsp/audio/audio.c`
- [MPEG decoder](https://github.com/jart/cosmopolitan/blob/master/dsp/mpeg/pl_mpeg.c): `dsp/mpeg/pl_mpeg.c`
- [Image scaling](https://github.com/jart/cosmopolitan/blob/master/dsp/scale/scale.c): `dsp/scale/scale.c`
- [Terminal graphics](https://github.com/jart/cosmopolitan/blob/master/dsp/tty/tty.h): `dsp/tty/tty.h`

### Programs and tools

- [HTTP escaping](https://github.com/jart/cosmopolitan/blob/master/net/http/escape.h): `net/http/escape.h`
- [Encrypted chat server](https://github.com/jart/cosmopolitan/blob/master/tool/chat/server.c): `tool/chat/server.c`
- [Encrypted chat client](https://github.com/jart/cosmopolitan/blob/master/tool/chat/client.c): `tool/chat/client.c`
- [HTTP server](https://github.com/jart/cosmopolitan/blob/master/net/turfwar/turfwar.c): `net/turfwar/turfwar.c`
- [DNS query utility](https://github.com/jart/cosmopolitan/blob/master/tool/net/libresolv_query.c): `tool/net/libresolv_query.c`
- [Plinko interpreter](https://github.com/jart/cosmopolitan/blob/master/tool/plinko/main.c): `tool/plinko/main.c`

### Other components

- **Program developer**
- **cosmocc toolchain**
- **Supported operating systems**

## Connections

- Program developer → cosmocc toolchain: builds with
- cosmocc toolchain → APE executable format: emits executable
- Program developer → APE executable format: runs program
- APE executable format → APE loader: loads through
- APE loader → Process runtime: starts runtime
- Process runtime → OS system calls: provides OS calls
- APE executable format → Supported operating systems: runs natively on
- Process runtime → Windows compatibility: supports Windows API
- Process runtime → Thread contexts: provides contexts (evidence: `libc/thread/makecontext.c`)
- Encrypted chat server → HTTP escaping: uses escaping (evidence: `tool/chat/server.c`)
- Encrypted chat client → HTTP escaping: uses escaping (evidence: `tool/chat/client.c`)
- Program developer → Encrypted chat client: runs chat client
- Program developer → HTTP server: runs server
- Program developer → DNS query utility: runs query tool
- Program developer → Plinko interpreter: runs interpreter (evidence: `tool/plinko/main.c`)
