initial commit

This commit is contained in:
xenia 2020-10-13 04:37:53 -04:00
commit ca7466f388
7 changed files with 146 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
/run
/.cache
*.o
*.zo
*.dep

69
Makefile Normal file
View File

@ -0,0 +1,69 @@
.PHONY: all setup clean mrproper
APP_NAME=run
RKT_NAME=$(APP_NAME).rkt
####################################################################################################
# download urls for stuff
RKT_DIST=racket-minimal-7.8-src.tgz
# what's in the racket tgz
RKT_DIR=racket-7.8
RKT_URL="https://mirror.racket-lang.org/installers/7.8/$(RKT_DIST)"
ALPINE_DIST=alpine-minirootfs-3.12.0-x86_64.tar.gz
ALPINE_URL="http://dl-cdn.alpinelinux.org/alpine/v3.12/releases/x86_64/$(ALPINE_DIST)"
# container is built and stored in here
CACHE_DIR=.cache
all: setup exec-in-container
# rerun inside container
exec-in-container: setup
rsync -av $(RKT_NAME) main.c Makefile $(CACHE_DIR)/alpine/build/
sudo systemd-nspawn -UD $(CACHE_DIR)/alpine -- sh -c "cd build; make $(APP_NAME)"
cp $(CACHE_DIR)/alpine/build/$(APP_NAME) .
# main build stuff
$(APP_NAME): main.c app.o
$(CC) -o $@ -pipe -O3 -DAPP_NAME='"$(APP_NAME)"' -static $^ -lracket3m -lrktio -lucontext -lffi
# this is faster than --c-mods by a lot
# it's less portable but like, we're containerized already so it'll work
app.o: app.zo
$(LD) -r -b binary $< -o $@
app.zo: $(RKT_NAME)
raco ctool --mods $@ $<
clean:
$(RM) $(APP_NAME) *.zo *.o
mrproper: clean
sudo $(RM) -rf $(CACHE_DIR)/alpine
$(RM) -rf $(CACHE_DIR)/alpine
# setup stuff
setup: $(CACHE_DIR)/.setup
$(CACHE_DIR)/.setup: $(CACHE_DIR)/$(ALPINE_DIST) $(CACHE_DIR)/$(RKT_DIST)
-mkdir -p $(CACHE_DIR)/alpine
sudo rm -rf $(CACHE_DIR)/alpine
mkdir $(CACHE_DIR)/alpine
tar xf "$(CACHE_DIR)/$(ALPINE_DIST)" -C $(CACHE_DIR)/alpine
tar xf "$(CACHE_DIR)/$(RKT_DIST)" -C $(CACHE_DIR)/alpine
cp scripts/setup.sh $(CACHE_DIR)/alpine/setup.sh
cp scripts/repositories.conf $(CACHE_DIR)/alpine/etc/apk/repositories
mkdir $(CACHE_DIR)/alpine/build
chmod 777 $(CACHE_DIR)/alpine/build
sudo systemd-nspawn -UD $(CACHE_DIR)/alpine -- sh /setup.sh
touch $@
$(CACHE_DIR)/$(ALPINE_DIST):
-mkdir -p $(CACHE_DIR)/alpine
wget -O $@ $(ALPINE_URL)
$(CACHE_DIR)/$(RKT_DIST):
-mkdir -p $(CACHE_DIR)/alpine
wget -O $@ $(RKT_URL)

26
README.md Normal file
View File

@ -0,0 +1,26 @@
# racket-static
a base set of scripts that let you build fully self-contained static binaries for racket
applications. this is better than raco exe/raco dist because that tooling can generate distribution
folders that still depend on dynamic libraries the target system might not have, and it's also a
whole folder instead of one easily distributable file
## dependencies
- systemd-nspawn (containers!)
- sudo (for running the above)
- rsync (copying files into container)
- wget (downloading stuff)
## hello world demo
first time you run make it'll take a while. be patient. having more cores helps
```
make
./run
```
## references
<https://docs.racket-lang.org/inside/embedding.html>

25
main.c Normal file
View File

@ -0,0 +1,25 @@
#include <racket/scheme.h>
#include <stdio.h>
extern const char _binary_app_zo_start;
extern const char _binary_app_zo_end;
static int run(Scheme_Env *e, int argc, char *argv[]) {
Scheme_Object *a[2];
size_t load_size = ((uintptr_t) &_binary_app_zo_end) - ((uintptr_t) &_binary_app_zo_start);
scheme_register_embedded_load(load_size, &_binary_app_zo_start);
scheme_embedded_load(load_size, &_binary_app_zo_start, 1);
a[0] = scheme_make_pair(scheme_intern_symbol("quote"),
scheme_make_pair(scheme_intern_symbol(APP_NAME), scheme_make_null()));
a[1] = scheme_false;
scheme_dynamic_require(2, a);
return 0;
}
int main(int argc, char *argv[]) {
return scheme_main_setup(1, run, argc, argv);
}

3
run.rkt Normal file
View File

@ -0,0 +1,3 @@
#lang racket/base
(displayln "hello world")

View File

@ -0,0 +1,3 @@
http://dl-cdn.alpinelinux.org/alpine/edge/main
http://dl-cdn.alpinelinux.org/alpine/edge/community
http://dl-cdn.alpinelinux.org/alpine/edge/testing

15
scripts/setup.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
set -e
apk upgrade --update-cache --available
apk add alpine-sdk ca-certificates libcrypto1.1 libssl1.1 chrpath libffi-dev libucontext-dev
cd /racket-*/src
export CFLAGS="$CFLAGS -D_GNU_SOURCE"
export LDFLAGS="$LDFLAGS -lucontext"
rm -Rf src/foreign/libffi
./configure --prefix=/usr --sysconfdir=/etc --disable-docs --enable-strip --enable-libs --disable-gracket
make -j $(nproc) CPUS=$(nproc)
make install
raco pkg install --deps search-auto cext-lib