Sign inSign up

marcelmaatkamp/cosmopolitan

By marcelmaatkamp

Updated over 5 years ago

Build environment for Cosmopolitan Libc, see https://github.com/jart/cosmopolitan

Image
0

547

marcelmaatkamp/cosmopolitan repository overview

Cosmopolitan in docker

This is the already compiled build environment for Cosmopolitan Libc in a docker container marcelmaatkamp/cosmopolitan which can be used as a base image to compile your own cosmoplitan software packages, for example this lua package.

TL;DR

This repository and its resulting docker container compiles the cosmopolitan libraries and all example programs from Cosmopolitan Libc in a docker container marcelmaatkamp/cosmopolitan where each individual example binary can be executed on multiple platforms like linux, mac, windows, FreeBSD, OpenBSD and you can even boot them in qemu or run on bare metal.

Examples

These are examples on how to run the executables. In the section files you can find all the possible example files. I've described a few examples (hello-world and nes emulator) on how to run on multiple platforms, the rest is more of the same. All of these examples are build with and contained in this docker container.

nes emulator example

An exampleL how to run a nes emulator in a terminal in linux/mac:

$ curl -sq \
   https://raw.githubusercontent.com/marcelmaatkamp/cosmopolitan/main/binaries/dist/examples/nesemu1.com \
   -o nesemu1.com &&\
  chmod +x nesemu1.com &&\
  sh ./nesemu1.com
  
COSMOPOLITAN NESEMU1

  [0] zip:usr/share/rom/mario.nes
  [1] zip:usr/share/rom/tetris.nes
  [2] zip:usr/share/rom/zelda.nes

https://user-images.githubusercontent.com/6911/119561709-ceb2a600-bda5-11eb-964d-d8981d7ea7a6.mp4

'hello world'-example

Let's showcase how to use this container with the simpelest example: hello world but it could also be your own creation:. We verify that the binary is succesfully compiled and does what it should do: 'print hello world' and use that binary in a seperate container which can be used by other processes.

docker
$ docker run --rm -ti marcelmaatkamp/cosmopolitan sh -c o/examples/hello.com

hello world
Dockerfile

Since these binaries are statically linked and thus contain everything in itself they need to function, you can include these binaries into an empty container called called 'scratch'

FROM alpine as alpine
ADD \
 https://raw.githubusercontent.com/marcelmaatkamp/cosmopolitan/main/binaries/dist/examples/hello.com.dbg \
 /hello.com
RUN \
 chmod +x /hello.com

FROM scratch
COPY --from=alpine /hello.com /hello.com
ENTRYPOINT ["/hello.com"]
build container
% docker build -t cosmopolitan-hello-world . 
run container
% docker run cosmopolitan-hello-world
  
hello world
size of container
% docker images
REPOSITORY                  TAG       IMAGE ID       CREATED          SIZE
cosmopolitan-hello-world   latest    b1fb2bc42c1e   6 seconds ago    260kB

(TBD: only the *.dbg seem to work in docker, which is a pity since the tiny binary executable is at least 10 times smaller)

multi-arch polyglot binaries

To showcase that each of these binaries can actually be used to execute on different environments I've uploaded them here on github in binaries/dist. To use them in each indiviual environment use the following shell scripts and note that it is always the same binary that is being used:

windows
C:\> curl -sq https://raw.githubusercontent.com/marcelmaatkamp/cosmopolitan/main/binaries/dist/examples/hello.com -o hello.com && hello.com

hello world
mac
$ curl -sq https://raw.githubusercontent.com/marcelmaatkamp/cosmopolitan/main/binaries/dist/examples/hello.com -o hello.com && sh ./hello.com

hello world
linux
$ curl -sq https://raw.githubusercontent.com/marcelmaatkamp/cosmopolitan/main/binaries/dist/examples/hello.com -o hello.com && sh ./hello.com

hello world
qemu

Use the binary to boot on bare metal or in a virtual machine via qemu

$ curl -sq https://raw.githubusercontent.com/marcelmaatkamp/cosmopolitan/main/binaries/dist/examples/hello.com -o hello.com &&\
  qemu-system-x86_64 -m 16 -no-reboot -nographic -fda ./hello.com

SeaBIOS (version rel-1.14.0-0-g155821a1990b-prebuilt.qemu.org)

iPXE (http://ipxe.org) 00:03.0 CA00 PCI2.10 PnP PMM+00F8F290+00EEF290 CA00

Booting from Hard Disk...
Boot failed: could not read the boot disk

hello world

build

If you want to compile the examples yourself just use the following command-line:

$ docker-compose up --build &&\
  sudo chown -R ${USER} ${PWD}/data/*

build options

$ docker run --rm -ti marcelmaatkamp/cosmopolitan

# build and run everything

% make -j8 -O
% make -j8 -O MODE=dbg
% make -j8 -O MODE=opt
% make -j8 -O MODE=rel
% make -j8 -O MODE=tiny

# build individual target
% make -j8 -O o//examples/hello.com
% o//examples/hello.com

  Hello world

# view source
% less examples/hello.c

# view binary
% o//tool/viz/bing.com o//examples/hello.com |
   o//tool/viz/fold.com
  
# view transitive closure of legalese
% o//tool/viz/bing.com -n o//examples/hello.com |
   o//tool/viz/fold.com

# basic debugging
% make -j8 -O MODE=dbg o/dbg/examples/crashreport.com
% o/dbg/examples/crashreport.com
% less examples/crashreport.c
  
# extremely tiny binaries
% make -j8 -O MODE=tiny \
   LDFLAGS+=-s \
   CPPFLAGS+=-DIM_FEELING_NAUGHTY	\
   CPPFLAGS+=-DSUPPORT_VECTOR=0b00000001 \
   o/tiny/examples/hello4.elf
% ls -hal o/tiny/examples/hello4.elf
% o/tiny/examples/hello4.elf
  
# TROUBLESHOOTING
% make -j8 -O V=1 o//examples/hello.com
% make o//examples/life.elf -pn |& less

For more build options see also build/config.mk

files

I have compiled and uploaded all example files in binaries/:

examplesdistreleasetinydebug
binaries/dist/examples//date.com💾💾💾💾
binaries/dist/examples//nesemu1.com💾💾💾💾
binaries/dist/examples//walk.com💾💾💾💾
binaries/dist/examples//hellolua.com💾💾💾💾
binaries/dist/examples//cplusplus.com💾💾💾💾
binaries/dist/examples//printargs.com💾💾💾💾
binaries/dist/examples//hangman.com💾💾💾💾
binaries/dist/examples//hello2.com💾💾💾💾
binaries/dist/examples//hello3.com💾💾💾💾
binaries/dist/examples//seq.com💾💾💾💾
binaries/dist/examples//ucontext-sigfpe-recovery.com💾💾💾💾
binaries/dist/examples//breakpoint.com💾💾💾💾
binaries/dist/examples//ctrlc.com💾💾💾💾
binaries/dist/examples//printprimes.com💾💾💾💾
binaries/dist/examples//generalized-automatic-datastructure-printing.com💾💾💾💾
binaries/dist/examples//lstime.com💾💾💾💾
binaries/dist/examples//hello.com💾💾💾💾
binaries/dist/examples//nc.com💾💾💾💾
binaries/dist/examples//x86split.com💾💾💾💾
binaries/dist/examples//cp.com💾💾💾💾
binaries/dist/examples//stat.com💾💾💾💾
binaries/dist/examples//hertz.com💾💾💾💾
binaries/dist/examples//forkrand.com💾💾💾💾
binaries/dist/examples//ispell.com💾💾💾💾
binaries/dist/examples//kilo.com💾💾💾💾
binaries/dist/examples//ls.com💾💾💾💾
binaries/dist/examples//hostname.com💾💾💾💾
binaries/dist/examples//crashreport.com💾💾💾💾
binaries/dist/examples//touch.com💾💾💾💾
binaries/dist/examples//sleep.com💾💾💾💾
binaries/dist/examples//life.com💾💾💾💾
binaries/dist/examples//panels.com💾💾💾💾
binaries/dist/examples//gui.com💾💾💾💾
binaries/dist/examples//unbourne.com💾💾💾💾
binaries/dist/examples//echo.com💾💾💾💾
binaries/dist/examples//rusage.com💾💾💾💾
binaries/dist/examples//system.com💾💾💾💾
binaries/dist/examples//curl.com💾💾💾💾
binaries/dist/examples//package/program.com💾💾💾💾
binaries/dist/examples//auto-launch-gdb-on-crash.com💾💾💾💾
binaries/dist/examples//ttyinfo.com💾💾💾💾
third_partydistreleasetinydebug
binaries/dist/third_party//chibicc/test/pragma-once_test.com💾[💾](https://github.com/marcelmaatkamp/cosmopolitan/raw/main/binaries/rel//third_party//chibicc/test/pragma-once_test.

Tag summary

Content type

Image

Digest

Size

1.8 GB

Last updated

over 5 years ago

docker pull marcelmaatkamp/cosmopolitan