stargazing/cosmo-kuroko/Dockerfile

137 lines
5.2 KiB
Docker

# MAIN: https://kuroko-lang.github.io/
# REPO: https://github.com/kuroko-lang/kuroko
# TODO: add /zip/ support by default and include dist modules.
FROM localhost/notwa-util AS downloader
ARG KUROKO_COMMIT=7d57acd49a430d697998b90f1a112b2c1fd31e59
ARG KUROKO_SHA256=29ab4ca692f5adf1507d9730888c80c312965d4daa33c7a8cae9d0d480cbb71a
RUN --mount=type=cache,id=common,target=/media/common,sharing=locked \
--mount=type=tmpfs,target=/tmp : \
&& cd /media/common \
&& name=kuroko \
&& export remote_fn="$KUROKO_COMMIT.tar.gz" \
&& export local_fn="$name-$remote_fn" \
&& export remote_url="https://github.com/kuroko-lang/$name/archive/$remote_fn" \
&& export dest=/$name \
&& export sha256="$KUROKO_SHA256" \
&& acquire \
;
FROM localhost/cosmo AS builder
COPY --from=downloader /kuroko /kuroko
WORKDIR /kuroko
RUN : \
&& cp -r . ../kuroko.orig \
\
&& sed -i '/bind(gamma)/{N;N;d;}' src/modules/module_math.c \
&& sed -i '/MATH_ONE(gamma)/d' src/modules/module_math.c \
\
&& sed -i 's@.*char \*\* environ.*@#ifndef COSMOPOLITAN_LIBC_RUNTIME_RUNTIME_H_\n&\n#endif@' src/os.c \
\
&& sed -i '/FUNC_SIG(list,append)/d' src/vm.c \
&& sed -i '/void _callSetName/i\\extern FUNC_SIG(list,append);' src/vm.c \
\
&& sed -i '/static.*cache/c\\static KrkClassCacheEntry * cache = 0;' src/vm.c \
&& sed -i $'/define CACHE_SIZE/,/nextCount/{N;w /temp\nd}' src/vm.c \
&& sed -i $'/void krk_initVM/{r /temp\nN}' src/vm.c \
&& sed -i $'/Reset current thread/i\\\tcache = calloc(CACHE_SIZE, sizeof(KrkClassCacheEntry));' src/vm.c \
&& sed -i $'/krk_freeMemoryDebugger();/a\\\tcache = (free(cache), NULL);' src/vm.c \
\
&& sed -i '/define gettid/d' src/threads.c \
&& sed -i '/"initial-exec"/d' src/vm.c \
\
&& sed -i $'/Ensure we don\'t have a macro/{\ni\\\
\#ifdef KRK_DISABLE_DYNAMIC\\\n\
KrkValue krk_module_onload_math(void);\\\n\
KrkValue krk_module_onload_random(void);\\\n\
KrkValue krk_module_onload_socket(void);\\\n\
KrkValue krk_module_onload_timeit(void);\\\n\
KrkValue krk_module_onload_wcwidth(void);\\\n\
\#endif\\\n\
\n\
}' src/vm.c \
&& sed -i $'/Ensure we don\'t have a macro/{\ni\\\
\#define BUILTIN_MODULE(name, init) { \\\\\\\n\
\tKrkInstance * module = (KrkInstance*)AS_OBJECT(init()); \\\\\\\n\
\tkrk_attachNamedObject(&vm.modules, name, (KrkObj*)module); \\\\\\\n\
\tkrk_attachNamedObject(&module->fields, "__name__", (KrkObj*)S(name)); \\\\\\\n\
\tkrk_attachNamedValue(&module->fields, "__file__", NONE_VAL()); \\\\\\\n\
} \\\n\
\n\
}' src/vm.c \
&& sed -i $'/krk_module_init_dis/{\na\\\
\#endif\\\n\
\#ifdef KRK_DISABLE_DYNAMIC\\\n\
\t\tBUILTIN_MODULE("math", krk_module_onload_math)\\\n\
\t\tBUILTIN_MODULE("random", krk_module_onload_random)\\\n\
\t\tBUILTIN_MODULE("socket", krk_module_onload_socket)\\\n\
\t\tBUILTIN_MODULE("timeit", krk_module_onload_timeit)\\\n\
\t\tBUILTIN_MODULE("wcwidth", krk_module_onload_wcwidth)\n\
}' src/vm.c \
\
&& sed -i '/netdb\.h/a\#include <netinet/in.h>' src/modules/module_socket.c \
&& sed -i $'/switch (afval)/{\ni\\\
\tif (afval == AF_INET) return "AF_INET";\\\n\
\tif (afval == AF_INET6) return "AF_INET6";\\\n\
\tif (afval == AF_UNIX) return "AF_UNIX";\\\n\
\tsnprintf(tmp,30,"%d",afval);\\\n\
\treturn tmp;\n\
}\n\
/switch (afval)/,/}/d' src/modules/module_socket.c \
\
&& sed -i $'/strlen(buf)-1/{\n\
s/if .*/if (buf[buflen-1] == \'\\\\n\') {/\n\
i\\\
\t\t\t\tsize_t buflen = strlen(buf);\n\
a\\\
\t\t\t\t\t\\if (buflen >= 2 && buf[buflen-2] == \'\\\\r\') buf[buflen-2] = \'\\\\n\', buf[buflen-1] = \'\\\\0\';\\\n\
\t\t\t\t} else {\n\
}' src/kuroko.c \
\
&& sed -i $'/ifdef KRK_DISABLE_THREADS/i\\\
ifdef KRK_DISABLE_DYNAMIC\\\n\
BIN_OBJS += src/modules/module_math.o src/modules/module_random.o src/modules/module_socket.o src/modules/module_timeit.o src/modules/module_wcwidth.o\\\n\
CFLAGS += -DKRK_DISABLE_DYNAMIC -DSTATIC_ONLY\\\n\
endif\\\n\
' Makefile \
&& sed -i $'/CFLAGS += -DKRK_DISABLE_THREADS/a\\\
else\\\n\
CFLAGS += -DKRK_MEDIOCRE_TLS\
' Makefile \
\
&& { diff -NrU3 /kuroko.orig/ /kuroko/ > /kuroko.patch; [ $? -le 1 ]; }
# diff exit status is normally 0 or 1, anything greater is an error.
RUN : \
# this isn't included in the patch because it's super specific to cosmopolitan. \
&& sed -i 's/int main/dontinline int _main/' src/kuroko.c \
&& printf %s >>src/kuroko.c $'\n\
STATIC_YOINK("__die");\n\
void ShowCrashReports(void);\n\
int LoadZipArgs(int *, char ***);\n\
int main(int argc, char * argv[]) {\n\
\tShowCrashReports();\n\
\tLoadZipArgs(&argc, &argv);\n\
\treturn _main(argc, argv);\n\
}\n\
'
RUN tar zxf /cosmopolitan/dist/headers.tar.gz -C /cosmopolitan
COPY --chmod=0755 --from=localhost/notwa-util /usr/local/bin/cosmocc /usr/bin/
RUN : \
&& CC=cosmocc CFLAGS="-Os -g" AR=ar.com ARFLAGS=rcsD \
make.com kuroko modules/codecs/sbencs.krk modules/codecs/dbdata.krk \
-j2 KRK_DISABLE_RLINE=1 KRK_DISABLE_DYNAMIC=1 \
&& cp kuroko kuroko.com.dbg \
&& objcopy -S -O binary kuroko.com.dbg kuroko.com \
;
RUN make.com test
FROM scratch AS runner
COPY --chmod=0755 --from=localhost/notwa-util /usr/local/bin/busybox /bin/busybox
COPY --chmod=0755 --from=builder /cosmopolitan/bin/ape /bin/ape
COPY --chmod=0755 --from=builder /kuroko/kuroko.com /kuroko/kuroko.com.dbg /bin/
COPY --from=builder /kuroko.patch /var/local/kuroko.patch
ENTRYPOINT ["/bin/ape", "/bin/kuroko.com"]