stargazing/cosmo-bootstrap/Dockerfile

114 lines
5 KiB
Text
Raw Normal View History

2022-09-23 13:06:23 -07:00
# MAIN: https://justine.lol/cosmopolitan/
# REPO: https://github.com/jart/cosmopolitan
ARG COSMO_FLAVOR=def
ARG COSMO_COMMIT=69bee64a5944a7e808be0dbf231a38e630af8588
ARG COSMO_SHA256=1c98aaccd0f9609753ed931b17fd5feb945d2c8bf4b13d78232ec19ff1f1fe6d
ARG COSMO_REBUILD=0
2022-09-23 13:06:23 -07:00
FROM localhost/notwa-util AS downloader
2022-10-05 09:13:38 -07:00
ARG COSMO_COMMIT COSMO_SHA256
2022-09-23 13:06:23 -07:00
RUN --mount=type=cache,id=common,target=/media/common,sharing=locked \
--mount=type=tmpfs,target=/tmp : \
2022-10-11 11:18:16 -07:00
&& acquire from=github repo=jart/cosmopolitan dest=/cosmopolitan env=COSMO \
2022-09-23 13:06:23 -07:00
;
FROM alpine:3.16 AS builder
2022-10-05 09:13:38 -07:00
ARG COSMO_FLAVOR COSMO_COMMIT
2022-09-23 13:06:23 -07:00
COPY --from=downloader /cosmopolitan /cosmopolitan
WORKDIR /cosmopolitan
# tell makefiles to be less noisy, also use /tmp:
ENV V=0 COLUMNS=80 TMPDIR=/tmp
COPY --chmod=0755 --from=localhost/notwa-util /nu/shed /usr/bin/
2022-09-23 13:06:23 -07:00
RUN : \
# allow optimized builds to be more portable, so the container is too. \
2022-10-01 10:36:12 -07:00
&& shed build/config.mk 's/-march=native/-march=haswell -mno-pclmul -mtune=generic/' \
2022-09-23 13:06:23 -07:00
# fix execution on alpine (although installing the loader mitigates this) \
&& sed -i 's/skip=" 76" count=" 128"/skip=76 count=128 /' build/bootstrap/*.com \
2022-10-15 00:30:32 -07:00
\
2022-09-23 13:06:23 -07:00
&& install -m0755 build/bootstrap/ape.elf /usr/bin/ape \
2022-10-04 03:52:16 -07:00
&& shed Makefile \
# TODO: apply changes to COSMOPOLITAN_HEADERS as to include them in cosmopolitan.h \
# NOTE: the order here is important! look at the generated o/depend file for hints. \
# include cosmopolitan linenoise (formerly known as bestline) \
'/\bNET_HTTP\b/i\\tTHIRD_PARTY_LINENOISE\t\t\\' \
2022-09-23 13:06:23 -07:00
# include LoadZipArgs in cosmopolitan.a \
2022-10-04 03:52:16 -07:00
'/\bNET_HTTP\b/i\\tTOOL_ARGS\t\t\\' \
2022-10-04 04:10:29 -07:00
# add missing includes \
&& shed libc/elf/elf.h '/"libc\/elf\/struct\/ehdr.h"/i\\#include "libc/elf/struct/dyn.h"' \
2022-09-26 19:43:44 -07:00
# awk's pledge isn't working for me for whatever reason \
2022-10-01 10:36:12 -07:00
&& shed third_party/awk/main.c '/pledge.*{/,/}/d' \
2022-09-23 13:06:23 -07:00
# create a mapping from flavor to mode (possibly an empty string) \
&& { [ "$COSMO_FLAVOR" != def ] && mode="$COSMO_FLAVOR" || mode= ;} \
2022-09-23 13:06:23 -07:00
&& printf '%s="%s"\n' >env \
commit "$COSMO_COMMIT" \
flavor "$COSMO_FLAVOR" \
mode "$mode" \
# make calling make a little shorter (don't put this in distrib!) \
&& printf '%s\n' >makeit '#!/usr/bin/env sh' \
"build/bootstrap/make.com -j2 MODE='$mode'"' "$@"' \
# make accessing some common paths a littler shorter (don't put this in distrib!) \
&& printf '%s\n' >expand '#!/usr/bin/env sh' \
"o='o/$mode' once=false" \
'for f; do g="${f#?//}"; case "$f" in' \
'(a//*) f="$o/ape/$g";;' \
'(b//*) f="$o/tool/build/$g";;' \
'(e//*) f="$o/examples/$g";;' \
'(o//*) f="$o/$g";;' \
'(t//*) f="$o/third_party/$g";;' \
'(T//*) f="third_party/$g";;' \
'esac; $once && set -- "$@" "$f" || set -- "$f"; once=true; done' \
'"$@"' \
2022-10-15 00:30:32 -07:00
# i always forget to do this \
&& chmod +x makeit expand \
2022-09-23 13:06:23 -07:00
;
# finally start building
RUN --mount=type=tmpfs,target=/tmp : \
&& ./expand ./makeit \
a//ape-copy-self.o a//ape-no-modify-self.o a//ape.elf \
a//ape.lds a//ape.macho a//ape.o \
a//public/ape.lds o//cosmopolitan.a o//libc/crt/crt.o \
o/cosmopolitan.h \
2022-09-23 13:06:23 -07:00
# build some extra tools for other containers to stem off of: \
&& rebuild() { ./expand ./makeit "$@" && ./expand cp -t build/bootstrap "$@" ;} \
2022-09-23 13:06:23 -07:00
&& rebuild \
t//make/make.com b//ar.com b//cocmd.com \
b//compile.com b//cp.com b//echo.com \
b//fixupobj.com b//gzip.com b//mkdeps.com \
b//mkdir.com b//package.com b//pwd.com \
b//rm.com b//touch.com b//unbundle.com \
b//zipobj.com \
# install the newly-built ape loader, just in case it has changed: \
&& ./expand install -m0755 a//ape.elf /usr/bin/ape \
;
# optionally use the newly-built bootstrapped binaries to build themselves again.
ARG COSMO_REBUILD
RUN --mount=type=tmpfs,target=/tmp : \
&& { [ "$COSMO_REBUILD" = 1 ] || exit 0 ;} \
&& rm -r o \
\
&& ./expand ./makeit \
a//ape-copy-self.o a//ape-no-modify-self.o a//ape.elf \
a//ape.lds a//ape.macho a//ape.o \
a//public/ape.lds o//cosmopolitan.a o//libc/crt/crt.o \
o/cosmopolitan.h \
# build some extra tools for other containers to stem off of: \
&& rebuild() { ./expand ./makeit "$@" && ./expand cp -t build/bootstrap "$@" ;} \
&& rebuild \
t//make/make.com b//ar.com b//cocmd.com \
b//compile.com b//cp.com b//echo.com \
b//fixupobj.com b//gzip.com b//mkdeps.com \
b//mkdir.com b//package.com b//pwd.com \
b//rm.com b//touch.com b//unbundle.com \
b//zipobj.com \
;
2022-09-23 13:06:23 -07:00
# these are essential too! \
RUN --mount=type=tmpfs,target=/tmp : \
&& ./expand ./makeit \
t//unzip/unzip.com t//zip/zip.com \
2022-09-23 13:06:23 -07:00
;