From e92dce58fb74f89fa81dd01e916ef374d18dfe0a Mon Sep 17 00:00:00 2001 From: dzaima Date: Fri, 3 Jun 2022 22:04:48 +0300 Subject: [PATCH] shared library build --- .gitignore | 3 +++ makefile | 12 +++++++++++- src/main.c | 2 +- test/ffi/makefile | 17 +++++++++++++++-- test/ffi/shared.expected | 1 + test/ffi/sharedTest.c | 9 +++++++++ 6 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 test/ffi/shared.expected create mode 100644 test/ffi/sharedTest.c diff --git a/.gitignore b/.gitignore index 16843744..dbac7953 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,7 @@ perf.* CBQNHeapDump /test/ffi/ffiTest.o /test/ffi/test.got +/test/ffi/shared.got +/test/ffi/sharedTest /test/ffi/lib.so +libcbqn.so diff --git a/makefile b/makefile index 93cbaef5..09ec95cf 100644 --- a/makefile +++ b/makefile @@ -33,6 +33,10 @@ wasi-o3: @${MAKE} i_singeli=0 i_t=wasi_o3 OUTPUT=BQN.wasm i_f="-DWASM -DWASI -DNO_MMAP -O3 -DCATCH_ERRORS=0 -D_WASI_EMULATED_MMAN --target=wasm32-wasi" i_lf="-lwasi-emulated-mman --target=wasm32-wasi -Wl,-z,stack-size=8388608 -Wl,--initial-memory=67108864" i_LD_LIBS= i_PIE= i_FFI=0 run_incremental_0 emcc-o3: @${MAKE} i_singeli=0 i_t=emcc_o3 OUTPUT=BQN.js CC=emcc i_f='-DWASM -DEMCC -O3' i_lf='-s EXPORTED_FUNCTIONS=_main,_cbqn_runLine,_cbqn_evalSrc -s EXPORTED_RUNTIME_METHODS=ccall,cwrap -s ALLOW_MEMORY_GROWTH=1' i_FFI=0 run_incremental_0 +shared-o3: + @${MAKE} OUTPUT=libcbqn.so i_SHARED=1 i_t=shared_o3 i_f="-O3" run_incremental_0 +shared-c: + @${MAKE} OUTPUT=libcbqn.so i_SHARED=1 custom=1 run_incremental_0 c: @${MAKE} custom=1 run_incremental_0 @@ -44,6 +48,12 @@ i_FFI := 2 i_singeli := 0 OUTPUT := BQN +ifeq ($(i_SHARED),1) + i_PIE := -shared + SHARED_CCFLAGS := -fPIC -DCBQN_SHARED +else + SHARED_CCFLAGS := +endif ifeq ($(origin CC),command line) i_CC := $(CC) custom = 1 @@ -94,7 +104,7 @@ else NOWARN = -Wno-parentheses endif -ALL_CC_FLAGS = -std=gnu11 -Wall -Wno-unused-function -fms-extensions $(CCFLAGS) $(f) $(i_f) $(NOWARN) -DSINGELI=$(i_singeli) -DFFI=$(i_FFI) +ALL_CC_FLAGS = -std=gnu11 -Wall -Wno-unused-function -fms-extensions $(CCFLAGS) $(f) $(i_f) $(NOWARN) -DSINGELI=$(i_singeli) -DFFI=$(i_FFI) $(SHARED_CCFLAGS) ALL_LD_FLAGS = $(LDFLAGS) $(lf) $(i_lf) $(i_PIE) $(i_LD_LIBS) ifneq (${manualJobs},1) diff --git a/src/main.c b/src/main.c index 30336bcb..7d9c6d68 100644 --- a/src/main.c +++ b/src/main.c @@ -269,7 +269,7 @@ void cbqn_evalSrc(char* src, i64 len) { int main() { repl_init(); } -#else +#elif !CBQN_SHARED int main(int argc, char* argv[]) { repl_init(); bool startREPL = argc==1; diff --git a/test/ffi/makefile b/test/ffi/makefile index 37676f95..3fbc6373 100644 --- a/test/ffi/makefile +++ b/test/ffi/makefile @@ -1,7 +1,20 @@ -test: build +test: testLib testShared + + + +testShared: buildShared + @LD_LIBRARY_PATH=../../ ./sharedTest > shared.got + @diff --color -su shared.expected shared.got + +buildShared: + $(CC) sharedTest.c -o sharedTest -L../.. -lcbqn + + + +testLib: buildLib @../../BQN test.bqn > test.got @diff --color -su test.expected test.got -build: +buildLib: $(CC) -O3 -g -c -fpic ffiTest.c -o ffiTest.o $(CC) -shared -olib.so ffiTest.o \ No newline at end of file diff --git a/test/ffi/shared.expected b/test/ffi/shared.expected new file mode 100644 index 00000000..b8626c4c --- /dev/null +++ b/test/ffi/shared.expected @@ -0,0 +1 @@ +4 diff --git a/test/ffi/sharedTest.c b/test/ffi/sharedTest.c new file mode 100644 index 00000000..443cd1ad --- /dev/null +++ b/test/ffi/sharedTest.c @@ -0,0 +1,9 @@ +#include +#include +#include "../../include/bqnffi.h" + +int main() { + bqn_init(); + double res = bqn_toF64(bqn_evalCStr("2+2")); + printf("%g\n", res); +}