diff --git a/Makefile b/Makefile index cdb1b548..6ac8f072 100644 --- a/Makefile +++ b/Makefile @@ -49,43 +49,16 @@ else endif # Source headers -JANET_GENERATED_HEADERS= \ - src/include/generated/core.h \ - src/include/generated/init.h JANET_HEADERS=$(sort $(wildcard src/include/janet/*.h)) JANET_LOCAL_HEADERS=$(sort $(wildcard src/*/*.h)) # Source files -JANET_CORE_SOURCES=$(sort $(wildcard src/core/*.c)) -JANET_MAINCLIENT_SOURCES=$(sort $(wildcard src/mainclient/*.c)) -JANET_WEBCLIENT_SOURCES=$(sort $(wildcard src/webclient/*.c)) +JANET_CORE_SOURCES=$(sort $(wildcard src/core/*.c)) src/core/core.gen.c +JANET_MAINCLIENT_SOURCES=$(sort $(wildcard src/mainclient/*.c)) src/mainclient/init.gen.c +JANET_WEBCLIENT_SOURCES=$(sort $(wildcard src/webclient/*.c)) src/webclient/webinit.gen.c all: $(JANET_TARGET) $(JANET_LIBRARY) -################################### -##### The code generator tool ##### -################################### - -xxd: src/tools/xxd.c - $(CC) $< -o $@ - -############################# -##### Generated Headers ##### -############################# - -src/include/generated/init.h: src/mainclient/init.janet xxd - ./xxd $< $@ janet_gen_init - -src/include/generated/webinit.h: src/webclient/webinit.janet xxd - ./xxd $< $@ janet_gen_webinit - -src/include/generated/core.h: src/core/core.janet xxd - ./xxd $< $@ janet_gen_core - -# Only a few files depend on the generated headers -src/core/corelib.o: src/include/generated/core.h -src/mainclient/main.o: src/include/generated/init.h - ########################################################## ##### The main interpreter program and shared object ##### ########################################################## @@ -119,16 +92,22 @@ JANET_EMTARGET=janet.js JANET_WEB_SOURCES=$(JANET_CORE_SOURCES) $(JANET_WEBCLIENT_SOURCES) JANET_EMOBJECTS=$(patsubst %.c,%.bc,$(JANET_WEB_SOURCES)) -# Only a few files depend on generated headers -src/core/corelib.bc: src/include/generated/core.h -src/webclient/main.bc: src/include/generated/webinit.h - %.bc: %.c $(JANET_HEADERS) $(JANET_LOCAL_HEADERS) $(EMCC) $(EMCCFLAGS) -o $@ -c $< $(JANET_EMTARGET): $(JANET_EMOBJECTS) $(EMCC) $(EMCCFLAGS) -shared -o $@ $^ +############################# +##### Generated C files ##### +############################# + +xxd: src/tools/xxd.c + $(CC) $< -o $@ + +%.gen.c: %.janet xxd + ./xxd $< $@ janet_gen_$(*F) + ################### ##### Testing ##### ################### @@ -184,7 +163,7 @@ clean: -rm $(JANET_LIBRARY) -rm ctest/*.o ctest/*.out -rm src/**/*.o src/**/*.bc vgcore.* *.js *.wasm *.html - -rm $(JANET_GENERATED_HEADERS) + -rm src/**/*.gen.c install: $(JANET_TARGET) mkdir -p $(BINDIR) diff --git a/build_win.bat b/build_win.bat index f49ed8f3..a3cc3897 100644 --- a/build_win.bat +++ b/build_win.bat @@ -28,10 +28,16 @@ mkdir build\mainclient @link /nologo /out:build\xxd.exe build\xxd.obj @if errorlevel 1 goto :BUILDFAIL -@rem Generate the headers -@build\xxd.exe src\core\core.janet src\include\generated\core.h janet_gen_core +@rem Generate the embedded sources +@build\xxd.exe src\core\core.janet build\core\core.gen.c janet_gen_core @if errorlevel 1 goto :BUILDFAIL -@build\xxd.exe src\mainclient\init.janet src\include\generated\init.h janet_gen_init +@build\xxd.exe src\mainclient\init.janet build\mainclient\init.gen.c janet_gen_init +@if errorlevel 1 goto :BUILDFAIL + +@rem Build the generated sources +@%JANET_COMPILE% /Fobuild\core\core.gen.obj build\core\core.gen.c +@if errorlevel 1 goto :BUILDFAIL +@%JANET_COMPILE% /Fobuild\mainclient\init.gen.obj build\mainclient\init.gen.c @if errorlevel 1 goto :BUILDFAIL @rem Build the sources diff --git a/src/core/compile.c b/src/core/compile.c index 730348da..e67da663 100644 --- a/src/core/compile.c +++ b/src/core/compile.c @@ -728,7 +728,7 @@ static int cfun(JanetArgs args) { static const JanetReg cfuns[] = { {"compile", cfun, - "(compile ast)\n\n" + "(compile ast env [, source])\n\n" "Compiles an Abstract Sytnax Tree (ast) into a janet function. " "Pair the compile function with parsing functionality to implement " "eval. Returns a janet function and does not modify ast. Throws an " diff --git a/src/core/corelib.c b/src/core/corelib.c index eb3c57c3..cd8bc75d 100644 --- a/src/core/corelib.c +++ b/src/core/corelib.c @@ -25,8 +25,9 @@ #include "state.h" #include "util.h" -/* Generated header */ -#include +/* Generated bytes */ +extern const unsigned char *janet_gen_core; +extern size_t janet_gen_core_size; /* Use LoadLibrary on windows or dlopen on posix to load dynamic libaries * with native code. */ @@ -809,7 +810,7 @@ JanetTable *janet_core_env(void) { janet_def(env, "_env", ret, "The environment table for the current scope."); /* Run bootstrap source */ - janet_dobytes(env, janet_gen_core, sizeof(janet_gen_core), "core.janet", NULL); + janet_dobytes(env, janet_gen_core, janet_gen_core_size, "core.janet", NULL); return env; } diff --git a/src/include/generated/.gitkeep b/src/include/generated/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/mainclient/main.c b/src/mainclient/main.c index a335e43f..6fbbdd5d 100644 --- a/src/mainclient/main.c +++ b/src/mainclient/main.c @@ -21,10 +21,11 @@ */ #include - -#include #include "line.h" +extern const unsigned char *janet_gen_init; +extern size_t janet_gen_init_size; + int main(int argc, char **argv) { int i, status; JanetArray *args; @@ -46,7 +47,7 @@ int main(int argc, char **argv) { janet_line_init(); /* Run startup script */ - status = janet_dobytes(env, janet_gen_init, sizeof(janet_gen_init), "init.janet", NULL); + status = janet_dobytes(env, janet_gen_init, janet_gen_init_size, "init.janet", NULL); /* Deinitialize vm */ janet_deinit(); diff --git a/src/tools/xxd.c b/src/tools/xxd.c index 760ee4e2..4529912c 100644 --- a/src/tools/xxd.c +++ b/src/tools/xxd.c @@ -36,6 +36,7 @@ int main(int argc, const char **argv) { static const char hex[] = "0123456789ABCDEF"; char buf[BUFSIZE]; size_t bytesRead = 0; + size_t totalRead = 0; int lineIndex = 0; int line = 0; @@ -58,12 +59,13 @@ int main(int argc, const char **argv) { } /* Write the header */ - fprintf(out, "/* Auto generated - DO NOT EDIT */\n\n"); - fprintf(out, "static const unsigned char %s[] = {", argv[3]); + fprintf(out, "/* Auto generated - DO NOT EDIT */\n\n#include \n\n"); + fprintf(out, "static const unsigned char bytes[] = {"); /* Read in chunks from buffer */ while ((bytesRead = fread(buf, 1, sizeof(buf), in)) > 0) { size_t i; + totalRead += bytesRead; for (i = 0; i < bytesRead; ++i) { int byte = ((uint8_t *)buf) [i]; @@ -89,6 +91,11 @@ int main(int argc, const char **argv) { /* Write the tail */ fputs("\n};\n\n", out); + fprintf(out, "const unsigned char *%s = bytes;\n\n", argv[3]); + + /* Write chunk size */ + fprintf(out, "size_t %s_size = %ld;\n", argv[3], totalRead); + /* Close the file handles */ fclose(in); fclose(out); diff --git a/src/webclient/main.c b/src/webclient/main.c index c912bcb0..fefc5089 100644 --- a/src/webclient/main.c +++ b/src/webclient/main.c @@ -21,9 +21,11 @@ */ #include -#include #include +extern const unsigned char *janet_gen_webinit; +extern size_t janet_gen_webinit_size; + static JanetFiber *repl_fiber = NULL; static JanetBuffer *line_buffer = NULL; static const uint8_t *line_prompt = NULL; @@ -78,7 +80,7 @@ void repl_init(void) { /* Run startup script */ Janet ret; - status = janet_dobytes(env, janet_gen_webinit, sizeof(janet_gen_webinit), "webinit.janet", &ret); + status = janet_dobytes(env, janet_gen_webinit, janet_gen_webinit_size, "webinit.janet", &ret); if (status == JANET_SIGNAL_ERROR) { printf("start up error.\n"); janet_deinit();