From c818bf54b1b1c2135fc807c116ed4784244963ef Mon Sep 17 00:00:00 2001 From: dzaima Date: Mon, 26 Dec 2022 19:38:24 +0200 Subject: [PATCH] remove need of libffi from `make forbuild` --- makefile | 2 +- src/builtins/sysfn.c | 9 ++++- src/ffi.c | 90 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 96 insertions(+), 5 deletions(-) diff --git a/makefile b/makefile index 24b51f82..137b49f4 100644 --- a/makefile +++ b/makefile @@ -38,7 +38,7 @@ shared-o3: shared-c: @${MAKE} i_OUTPUT=libcbqn.so i_SHARED=1 custom=1 run_incremental_0 forbuild: - @${MAKE} i_singeli=0 i_t=forbuild i_f="-O2 -DFOR_BUILD" i_FFI=2 i_OUTPUT=build/obj2/for_build run_incremental_0 + @${MAKE} i_singeli=0 i_t=forbuild i_f="-O2 -DFOR_BUILD" i_FFI=0 i_OUTPUT=build/obj2/for_build run_incremental_0 c: @${MAKE} custom=1 run_incremental_0 diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index ad4bb842..2d2dec18 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -1380,9 +1380,14 @@ void getSysvals(B* res); static Body* file_nsGen; +#if FFI || FOR_BUILD +#define FFIOPT 1 +#else +#define FFIOPT 0 +#endif + #define OPTSYS_0(X) #define OPTSYS_1(X) X -#define OPTSYS_2(X) X #define OPTSYS_B(COND) OPTSYS_##COND #define OPTSYS(COND) OPTSYS_B(COND) @@ -1420,7 +1425,7 @@ static Body* file_nsGen; F("fbytes", U"•FBytes", tag(7,VAR_TAG)) \ F("flines", U"•FLines", tag(8,VAR_TAG)) \ F("import", U"•Import", tag(9,VAR_TAG)) \ - OPTSYS(FFI)(F("ffi", U"•FFI", tag(10,VAR_TAG))) \ + OPTSYS(FFIOPT)(F("ffi", U"•FFI", tag(10,VAR_TAG))) \ F("name", U"•name", tag(11,VAR_TAG)) \ F("path", U"•path", tag(12,VAR_TAG)) \ F("wdpath", U"•wdpath", tag(13,VAR_TAG)) \ diff --git a/src/ffi.c b/src/ffi.c index 08bf4c53..9964a7f3 100644 --- a/src/ffi.c +++ b/src/ffi.c @@ -924,6 +924,92 @@ void ffi_init() { } #else // i.e. FFI==0 -void ffi_init() { } -B ffiload_c2(B t, B w, B x) { thrM("CBQN was compiled without FFI"); } +#if !FOR_BUILD + void ffi_init() { } + B ffiload_c2(B t, B w, B x) { err("•FFI called"); } +#else // whatever build.bqn uses from •FFI + #include "nfns.h" + #include "utils/cstr.h" + #include + #include + typedef struct pollfd pollfd; + NFnDesc* forbuildDesc; + B forbuild_c1(B t, B x) { + i32 id = o2i(nfn_objU(t)); + switch (id) { default: err("bad id"); + case 0: { + char* s = toCStr(x); + chdir(s); + freeCStr(s); + return x; + } + case 1: { + dec(x); + return m_f64(fork()); + } + case 2: { + decG(x); + int vs[2]; + int r = pipe(vs); + return m_vec2(m_f64(r), m_vec2(m_f64(vs[0]), m_f64(vs[1]))); + } + case 3: { + SGetU(x) + int fd = o2i(GetU(x,0)); + I8Arr* buf = cpyI8Arr(GetU(x,1)); + usz maxlen = o2s(GetU(x,2)); + assert(PIA(buf)==maxlen); + int res = read(fd, buf->a, maxlen); + return m_vec2(m_f64(res), taga(buf)); + } + case 4: { + SGetU(x) + int fd = o2i(GetU(x,0)); + I8Arr* buf = cpyI8Arr(GetU(x,1)); + usz maxlen = o2s(GetU(x,2)); + int res = write(fd, buf->a, maxlen); + return m_f64(res); + } + case 5: { + return m_f64(close(o2i(x))); + } + case 6: { + SGetU(x) + I16Arr* buf = cpyI16Arr(GetU(x,0)); i16* a = (i16*)buf->a; + int nfds = o2i(GetU(x,1)); + int timeout = o2s(GetU(x,2)); + + TALLOC(pollfd, ps, nfds) + for (i32 i = 0; i < nfds; i++) ps[i] = (pollfd){.fd = a[i*4+0]|a[i*4+1]<<16, .events=a[i*4+2]}; + int res = poll(&ps[0], nfds, timeout); + for (i32 i = 0; i < nfds; i++) a[i*4+3] = ps[i].revents; + TFREE(ps); + + return m_vec2(m_f64(res), taga(buf)); + } + } + } + B names; + B ffiload_c2(B t, B w, B x) { + B name = IGetU(x, 1); + i32 id = 0; + while (id