Skip to content

Commit

Permalink
fix: adopt new embedding method and simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
jaromil committed Dec 16, 2024
1 parent 2f65976 commit d6401d5
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 296 deletions.
25 changes: 2 additions & 23 deletions build/deps.mk
Original file line number Diff line number Diff line change
@@ -1,34 +1,13 @@

.c.o:
$(cc) \
$(cflags) \
-c $< -o $@ \
-DVERSION=\"${VERSION}\" \
-DCURRENT_YEAR=\"${CURRENT_YEAR}\"

src/embed-dmon.c:
$(info Embedding dmon headers)
bash build/embed-dmon.sh

src/embed-musl-libc.c:
bash build/embed-musl-libc.sh
sed -i 's/unsigned char _lib_x86_64_linux_musl_libc_so/const unsigned char musl_libc/' src/embed-musl-libc.c
sed -i 's/unsigned int _lib_x86_64_linux_musl_libc_so_len/const unsigned int musl_libc_len/' src/embed-musl-libc.c

src/embed-libtcc1.c: lib/tinycc/libtcc1.a
$(info Embedding libtcc1: lib/tinycc/libtcc1.a)
bash build/embed-libtcc1.sh lib/tinycc/libtcc1.a
@sed -i 's/unsigned char lib_tinycc_libtcc1_a/const unsigned char libtcc1/' src/embed-libtcc1.c
@sed -i 's/unsigned int lib_tinycc_libtcc1_a_len/const unsigned int libtcc1_len/' src/embed-libtcc1.c

src/embed-headers.c:
$(info Embedding tinycc headers)
bash build/embed-headers.sh
sed -i 's/unsigned char/const char/' src/embed-headers.c
sed -i 's/unsigned int/const unsigned int/' src/embed-headers.c

lib/tinycc/libtcc.a lib/tinycc/libtcc1.a:
cd lib/tinycc && ./configure ${tinycc_config} ${extra_tinycc_config}
cd lib/tinycc \
&& ./configure ${tinycc_config} ${extra_tinycc_config}
${MAKE} -C lib/tinycc libtcc.a
${MAKE} -C lib/tinycc libtcc1.a

Expand Down
49 changes: 0 additions & 49 deletions build/embed-dmon.sh

This file was deleted.

20 changes: 0 additions & 20 deletions build/embed-libtcc1.sh

This file was deleted.

20 changes: 0 additions & 20 deletions build/embed-musl-libc.sh

This file was deleted.

74 changes: 74 additions & 0 deletions build/embed-path.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env bash

set -e

path="$1"
parent=`dirname ${1}`
name=${2:-`basename ${1}`}
pathname=`basename ${1}`
dst=src/embed_${name}.c

[ -r src/embedded.h ] || {
>&2 echo "Build must generate src/embedded.h first"
exit 1
}

[ -r src/embedded.c ] || {
>&2 echo "Build must generate src/embedded.c first"
exit 1
}

command -v xxd > /dev/null || {
>&2 echo "Error not found: xxd binary not installed"
exit 1
}

>&2 echo "parent: $parent"
>&2 echo "name: $name"
>&2 echo "pathname: $pathname"
>&2 echo "dest: $dst"

rm -f ${name}.tar.gz
prevpwd=`pwd`
cd ${parent}
[ "$pathname" != "$name" ] && cp -ra "$pathname" "$name"
tar --format ustar -czf ${prevpwd}/${name}.tar.gz "$name"
[ "$pathname" != "$name" ] && rm -rf "$name"
cd -

echo "// Embedded: $path" > $dst
echo "// source generated by cjit/build/embed-path.sh" >> $dst
echo "// `date`" >> $dst
echo "// ${name}" >> $dst
mv ${name}.tar.gz ${name}
xxd -i ${name} >> $dst
rm -f ${name}
# must be constant variables in stack
# sed inplace is not portable
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i'' -e 's/unsigned char/const char/' $dst
sed -i'' -e 's/unsigned int/const unsigned int/' $dst
else
sed -i -e 's/unsigned char/const char/' $dst
sed -i -e 's/unsigned int/const unsigned int/' $dst
fi

# xxd already converts dots in underscores
name=`echo $name | sed 's/\./_/g'`

# generate embeddings in source for extract_embeddings(char *tmpdir)
echo "extern char *${name};" >> src/embedded.h
echo "extern unsigned int ${name}_len;" >> src/embedded.h

cat <<EOF >> src/embedded.c
if(muntargz_to_path(tmpdir,(char*)&${name},${name}_len))
return(false);
{
if(!CJIT) return(false);
if(!CJIT->tmpdir) return(false);
char incpath[512];
snprintf(incpath,511,"%s/%s",CJIT->tmpdir,"${name}");
tcc_add_include_path(CJIT->TCC, incpath);
}
EOF
exit 0
33 changes: 33 additions & 0 deletions build/init-embeddings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

head=src/embedded.h
code=src/embedded.c
rm -f ${head} ${code}
cat <<EOF > ${head}
// Generated by cjit/build/init-embeddings.sh
// `date`
#ifndef __EMBEDDED_H__
#define __EMBEDDED_H__
EOF

cat <<EOF > ${code}
// Generated by cjit/build/init-embeddings.sh
// `date`
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <embedded.h>
#include <cjit.h>
// from file.c
extern int muntar_to_path(const char *path, const uint8_t *buf, const unsigned int len);
extern int muntargz_to_path(const char *path, const uint8_t *buf, const unsigned int len);
// main function
bool extract_embeddings(CJITState *CJIT, char *tmpdir) {
EOF

exit 0
5 changes: 3 additions & 2 deletions build/init.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ CFLAGS ?= -O2 -fomit-frame-pointer ${cflags_stack_protect}
cflags := ${CFLAGS} ${cflags_includes}

SOURCES := src/io.o src/file.o src/cflag.o src/cjit.o \
src/embed-libtcc1.o src/embed-headers.o \
src/exec-headers.o src/repl.o src/embed-dmon.o
src/embedded.o \
src/repl.o \
src/muntar.o src/tinflate.o src/tinfgzip.o

ldadd := lib/tinycc/libtcc.a

Expand Down
34 changes: 24 additions & 10 deletions build/linux.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,31 @@ include build/init.mk

cc := gcc

cflags += -DLIBC_GNU -D_GNU_SOURCE -DKILO_SUPPORTED -DCJIT_BUILD_LINUX
cflags += -DLIBC_GNU -D_GNU_SOURCE
cflags += -DKILO_SUPPORTED
cflags += -DCJIT_BUILD_LINUX

SOURCES += \
src/kilo.o \
src/embed_libtcc1.a.o \
src/embed_tinycc_include.o \
src/embed_contrib_headers.o

all: embed cjit

embed: lib/tinycc/libtcc1.a
$(info Generating embeddings)
bash build/init-embeddings.sh
bash build/embed-path.sh lib/tinycc/libtcc1.a
bash build/embed-path.sh lib/tinycc/include tinycc_include
bash build/embed-path.sh lib/contrib_headers
@echo "\nreturn(true);\n}\n" >> src/embedded.c
@echo "\n#endif\n" >> src/embedded.h

SOURCES += src/kilo.o
tinycc_config += --with-libgcc
ifeq ($(shell sestatus | awk -F': *' '/SELinux status:/ {print $2}'), enabled)
tinycc_config += --with-selinux
endif

ifdef ASAN
cflags := -Og -ggdb -DDEBUG=1 -fno-omit-frame-pointer -fsanitize=address
Expand All @@ -21,14 +43,6 @@ ifdef GDB
# tinycc_config += --extra-ldflags="${ldflags}"
endif

tinycc_config += --with-libgcc

ifeq ($(shell sestatus | awk -F': *' '/SELinux status:/ {print $2}'), enabled)
tinycc_config += --with-selinux
endif

all: lib/tinycc/libtcc.a cjit

cjit: ${SOURCES}
$(cc) $(cflags) -o $@ $(SOURCES) ${ldflags} ${ldadd}

Expand Down
26 changes: 23 additions & 3 deletions build/musl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,34 @@ cflags := -Wall -static -O2 ${cflags_stack_protect}
cflags += -Isrc -Ilib/tinycc -DLIBC_MUSL -nostdlib
cflags += -DKILO_SUPPORTED -DCJIT_BUILD_MUSL

ldadd := lib/tinycc/libtcc.a /usr/lib/x86_64-linux-musl/crt1.o /usr/lib/x86_64-linux-musl/libc.a
ldadd := lib/tinycc/libtcc.a \
/usr/lib/x86_64-linux-musl/crt1.o \
/usr/lib/x86_64-linux-musl/libc.a

SOURCES += src/embed-musl-libc.o src/musl-symbols.o src/kilo.o
SOURCES += \
src/kilo.o \
src/embed_libtcc1.a.o \
src/embed_tinycc_include.o \
src/embed_contrib_headers.o \
src/embed_libc.so.o \
src/musl-symbols.o

# SOURCES += src/embed-musl-libc.o src/musl-symbols.o src/kilo.o

tinycc_config += --config-musl --enable-static
tynycc_config += --extra-cflags=-static --extra-ldflags=-static

all: lib/tinycc/libtcc.a cjit
all: lib/tinycc/libtcc.a embed cjit

embed: lib/tinycc/libtcc1.a
$(info Generating embeddings)
bash build/init-embeddings.sh
bash build/embed-path.sh lib/tinycc/libtcc1.a
bash build/embed-path.sh lib/tinycc/include tinycc_include
bash build/embed-path.sh lib/contrib_headers
bash build/embed-path.sh /lib/x86_64-linux-musl/libc.so
@echo "\nreturn(true);\n}\n" >> src/embedded.c
@echo "\n#endif\n" >> src/embedded.h

cjit: ${SOURCES}
$(cc) $(cflags) -o $@ $(SOURCES) ${ldadd}
Expand Down
50 changes: 17 additions & 33 deletions build/osx.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,24 @@ include build/init.mk
cc := clang
cflags += -DCJIT_BUILD_OSX

all: deps cjit.command
SOURCES += \
src/kilo.o \
src/embed_libtcc1.a.o \
src/embed_tinycc_include.o \
src/embed_contrib_headers.o

all: embed cjit.command

embed: lib/tinycc/libtcc1.a
$(info Generating embeddings)
bash build/init-embeddings.sh
bash build/embed-path.sh lib/tinycc/libtcc1.a
bash build/embed-path.sh lib/tinycc/include tinycc_include
bash build/embed-path.sh lib/contrib_headers
@echo "\nreturn(true);\n}\n" >> src/embedded.c
@echo "\n#endif\n" >> src/embedded.h

cjit.command: ${SOURCES}
$(cc) $(cflags) -o $@ $(SOURCES) ${ldflags} ${ldadd}

.c.o:
$(cc) \
$(cflags) \
-c $< -o $@ \
-DVERSION=\"${VERSION}\" \
-DCURRENT_YEAR=\"${CURRENT_YEAR}\"

deps: lib/tinycc/libtcc.a src/embed-libtcc1.c src/embed-headers.c src/embed-dmon.c

## Custom deps targets for osx due to different sed

lib/tinycc/libtcc.a:
cd lib/tinycc && ./configure ${tinycc_config}
${MAKE} -C lib/tinycc libtcc.a
${MAKE} -C lib/tinycc libtcc1.a

src/embed-libtcc1.c:
$(info Embedding libtcc1)
sh build/embed-libtcc1.sh lib/tinycc/libtcc1.a$
sed -i'' -e 's/unsigned char lib_tinycc_libtcc1_a/const unsigned char libtcc1/' src/embed-libtcc1.c
sed -i'' -e 's/unsigned int lib_tinycc_libtcc1_a_len/const unsigned int libtcc1_len/' src/embed-libtcc1.c

src/embed-headers.c:
$(info Embedding tinycc headers)
bash build/embed-headers.sh win

src/embed-dmon.c:
$(info Embedding dmon headers)
bash build/embed-dmon.sh

# sed -i'' -e 's/unsigned char/const char/' src/embed-headers.c
# sed -i'' -e 's/unsigned int/const unsigned int/' src/embed-headers.c
include build/deps.mk
Loading

0 comments on commit d6401d5

Please sign in to comment.