From c6f79eca6d527b760d113428e2354c777ef6c959 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 14 May 2018 21:52:51 -0400 Subject: [PATCH] Make setting up stl easier. Add shared library output to Makefile. --- Makefile | 42 ++++++++++++++++++++++++++++++------------ src/compiler/stl.c | 5 +++-- src/include/dst/dst.h | 3 ++- src/mainclient/main.c | 3 +-- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 8799c822..6885a559 100644 --- a/Makefile +++ b/Makefile @@ -22,17 +22,21 @@ ##### Set global variables ##### ################################ -PREFIX?=/usr/local +PREFIX?=/usr + +INCLUDEDIR=$(PREFIX)/include/dst +LIBDIR=$(PREFIX)/lib BINDIR=$(PREFIX)/bin # CFLAGS=-std=c99 -Wall -Wextra -Isrc/include -Wl,--dynamic-list=src/exported.list -s -O3 # TODO - when api is finalized, only export public symbols instead of using rdynamic -# which exports all symbols. +# which exports all symbols. Saves a few KB in binary. -CFLAGS=-std=c99 -Wall -Wextra -Isrc/include -rdynamic -O2 +CFLAGS=-std=c99 -Wall -Wextra -Isrc/include -rdynamic -fpic -O2 CLIBS=-lm -ldl PREFIX=/usr/local DST_TARGET=dst +DST_LIBRARY=libdst.so DEBUGGER=gdb # Source headers @@ -51,7 +55,7 @@ DST_CORE_SOURCES=$(sort $(wildcard src/core/*.c)) DST_MAINCLIENT_SOURCES=$(sort $(wildcard src/mainclient/*.c)) DST_PARSER_SOURCES=$(sort $(wildcard src/parser/*.c)) -all: $(DST_TARGET) +all: $(DST_TARGET) $(DST_LIBRARY) ################################### ##### The code generator tool ##### @@ -70,24 +74,30 @@ src/mainclient/clientinit.gen.h: src/mainclient/init.dst xxd src/compiler/dststlbootstrap.gen.h: src/compiler/boot.dst xxd ./xxd $< $@ dst_stl_bootstrap_gen -######################################## -##### The main interpreter program ##### -######################################## +########################################################## +##### The main interpreter program and shared object ##### +########################################################## -DST_ALL_SOURCES=$(DST_ASM_SOURCES) \ +DST_LIB_SOURCES=$(DST_ASM_SOURCES) \ $(DST_COMPILER_SOURCES) \ $(DST_CONTEXT_SOURCES) \ $(DST_CORE_SOURCES) \ - $(DST_MAINCLIENT_SOURCES) \ $(DST_PARSER_SOURCES) +DST_ALL_SOURCES=$(DST_LIB_SOURCES) \ + $(DST_MAINCLIENT_SOURCES) + +DST_LIB_OBJECTS=$(patsubst %.c,%.o,$(DST_LIB_SOURCES)) DST_ALL_OBJECTS=$(patsubst %.c,%.o,$(DST_ALL_SOURCES)) %.o: %.c $(DST_ALL_HEADERS) $(CC) $(CFLAGS) -o $@ -c $< $(DST_TARGET): $(DST_ALL_OBJECTS) - $(CC) $(CFLAGS) -o $(DST_TARGET) $(DST_ALL_OBJECTS) $(CLIBS) + $(CC) $(CFLAGS) -o $(DST_TARGET) $^ $(CLIBS) + +$(DST_LIBRARY): $(DST_LIB_OBJECTS) + $(CC) $(CFLAGS) -shared -o $(DST_LIBRARY) $^ $(CLIBS) ################### ##### Testing ##### @@ -108,6 +118,7 @@ test: $(DST_TARGET) valtest: $(DST_TARGET) valgrind --leak-check=full -v ./$(DST_TARGET) test/suite0.dst + valgrind --leak-check=full -v ./$(DST_TARGET) test/suite1.dst ################# ##### Other ##### @@ -120,9 +131,16 @@ clean: rm $(DST_GENERATED_HEADERS) || true install: $(DST_TARGET) - cp $(DST_TARGET) $(BINDIR)/dst + cp $(DST_TARGET) $(BINDIR)/$(DST_TARGET) + mkdir -p $(INCLUDEDIR) + cp $(DST_HEADERS) $(INCLUDEDIR) + cp $(DST_LIBRARY) $(LIBDIR)/$(DST_LIBRARY) + ldconfig uninstall: - rm $(BINDIR)/dst + rm $(BINDIR)/$(DST_TARGET) + rm $(LIBDIR)/$(DST_LIBRARY) + rm -rf $(INCLUDEDIR) + ldconfig .PHONY: clean install repl debug valgrind test valtest install uninstall diff --git a/src/compiler/stl.c b/src/compiler/stl.c index ca0922de..c3534198 100644 --- a/src/compiler/stl.c +++ b/src/compiler/stl.c @@ -60,7 +60,7 @@ static const DstReg cfuns[] = { {NULL, NULL} }; -DstTable *dst_stl_env() { +DstTable *dst_stl_env(int flags) { static uint32_t error_asm[] = { DOP_ERROR }; @@ -112,7 +112,8 @@ DstTable *dst_stl_env() { /* Run bootstrap source */ dst_dobytes(env, dst_stl_bootstrap_gen, sizeof(dst_stl_bootstrap_gen)); - dst_gcunroot(dst_wrap_table(env)); + if (flags & DST_STL_NOGCROOT) + dst_gcunroot(dst_wrap_table(env)); return env; } diff --git a/src/include/dst/dst.h b/src/include/dst/dst.h index f8934966..95f7227e 100644 --- a/src/include/dst/dst.h +++ b/src/include/dst/dst.h @@ -195,7 +195,8 @@ Dst dst_env_resolve(DstTable *env, const char *name); DstTable *dst_env_arg(DstArgs args); /* STL */ -DstTable *dst_stl_env(void); +#define DST_STL_NOGCROOT 1 +DstTable *dst_stl_env(int flags); /* C Function helpers */ int dst_arity_err(DstArgs args, int32_t n, const char *prefix); diff --git a/src/mainclient/main.c b/src/mainclient/main.c index 47bcafeb..5923fc58 100644 --- a/src/mainclient/main.c +++ b/src/mainclient/main.c @@ -33,8 +33,7 @@ int main(int argc, char **argv) { /* Set up VM */ dst_init(); - env = dst_stl_env(); - dst_gcroot(dst_wrap_table(env)); + env = dst_stl_env(0); /* Create args tuple */ args = dst_array(argc);