stargazing/cosmo-kuroko/Dockerfile
2022-09-23 13:19:43 -07:00

133 lines
4.7 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
# 1 commit after version 1.3.0:
ARG KUROKO_COMMIT=5f4d3df5d09a1f50e99c1e4d9673af1ad56fb110
ARG KUROKO_SHA256=ddf09ad47b61d1b3602bf59214f5c596cd6a51f5bea07542a01340e86494db1a
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 : \
&& 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.*_/* & */_' 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\\cache = calloc(CACHE_SIZE, sizeof(KrkClassCacheEntry));' src/vm.c \
&& sed -i '/krk_freeMemoryDebugger/a\\cache = (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 $'/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 \
\
&& { 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 $'\n\
STATIC_YOINK("__die");\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\
' >> src/kuroko.c
RUN : \
&& cd /cosmopolitan \
&& . ./env \
&& cd dist/"$flavor" \
&& mkdir -p /kuroko/libcosmo \
&& ln ape-copy-self.o ape.lds cosmopolitan.a crt.o ape-no-modify-self.o ape.o cosmopolitan.h \
/kuroko/libcosmo/
COPY super.mk .
RUN : \
&& CC=x86_64-linux-musl-gcc \
make.com -f super.mk -j2 \
KRK_DISABLE_RLINE=1 \
KRK_DISABLE_DYNAMIC=1 \
# KRK_DISABLE_THREADS=1 \
# KRK_DISABLE_DEBUG=1 KRK_NO_DISASSEMBLY=1 KRK_NO_TRACING=1 \
;
RUN : \
&& make.com -f super.mk 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"]