stargazing/cosmo-kuroko/Dockerfile
2022-10-14 16:35:09 -07:00

131 lines
4.6 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=3f35d89fff2343ddb9f0de5c87bbdefa86ee34f5
ARG KUROKO_SHA256=b1d38c48afdc06633d848dc27cb5d37bebbb7c99c59d3649c155350f06cd93a7
RUN --mount=type=cache,id=common,target=/media/common,sharing=locked \
--mount=type=tmpfs,target=/tmp : \
&& acquire from=github repo=kuroko-lang/kuroko dest=/kuroko env=KUROKO \
;
FROM localhost/cosmo-base AS builder
COPY --from=downloader /kuroko /kuroko
WORKDIR /kuroko
COPY --chmod=0755 --from=localhost/notwa-util /nu/shed /usr/bin/
RUN : \
&& cp -r . ../kuroko.orig \
\
&& shed src/os.c \
's@.*char \*\* environ.*@#ifndef COSMOPOLITAN_LIBC_RUNTIME_RUNTIME_H_\n&\n#endif@' \
\
&& shed src/vm.c \
'/FUNC_SIG(list,append)/d' \
'/void _callSetName/i\\extern FUNC_SIG(list,append);' \
\
&& shed src/vm.c \
'/static.*cache/c\\static KrkClassCacheEntry * cache = 0;' \
$'/define CACHE_SIZE/,/nextCount/{N;w /temp\nd}' \
$'/void krk_initVM/{r /temp\nN}' \
$'/Reset current thread/i\\\tcache = calloc(CACHE_SIZE, sizeof(KrkClassCacheEntry));' \
$'/krk_freeMemoryDebugger();/a\\\tcache = (free(cache), NULL);' \
\
&& shed src/threads.c '/define gettid/d' \
&& shed src/vm.c '/"initial-exec"/d' \
\
&& shed src/vm.c \
$'/Ensure we don\'t have a macro/i\\\
\#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\
\#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\
} \\' \
$'/krk_module_init_dis/a\\\
\#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\
' \
\
&& shed src/modules/module_socket.c \
'/netdb\.h/a\#include <netinet/in.h>' \
$'/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' \
\
&& shed src/kuroko.c \
$'/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\
}' \
\
&& shed Makefile \
$'/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\
' \
$'/CFLAGS += -DKRK_DISABLE_THREADS/a\\\
else\\\n\
CFLAGS += -DKRK_MEDIOCRE_TLS\
' \
\
&& { 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. \
&& shed src/kuroko.c 's/int main/dontinline int _main/' \
&& 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 /nu/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 /nu/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"]