Commit f1208c1b

f1208c1bd3d3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

Emit FFI link/cflags markers from spinelc, matching legacy

spinelc parsed ffi_lib into its registry but the codegen never emitted the /* SPINEL_LINK: */ markers the driver scrapes, and ffi_cflags was not parsed at all. As a result ffi_lib-dependent programs (e.g. examples/ffi/sqlite, which needs -lsqlite3) failed to link through the C spinel driver; only the always-linked libc/libm worked.

Bring the C compiler to parity with the legacy Ruby backend:

  • Parse ffi_cflags "..." in register_ffi_decls, accumulating semicolon-separated cflags per module (mirrors the ffi_lib path).
  • Emit one /* SPINEL_LINK: -l<lib> */ per declared library and one /* SPINEL_CFLAGS: <flag> */ per cflag at the top of the generated C, in the same format the legacy emitter and the driver's sed-scrape expect.

examples/ffi/sqlite/blog.rb now compiles, links against -lsqlite3, and runs end-to-end via ./spinel. make test stays green (845 pass).

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
f1208c1bd3d3
Parent
28cb212936b1

src/analyze_scope.cmodifiedView file

@@ -899,6 +899,35 @@
899899 continue;
900900 }
901901
902+ if (!strcmp(dname, "ffi_cflags")) {
903+ if (an < 1) continue;
904+ const char *cflag = ffi_arg_str(nt, args[0]);
905+ if (!cflag) continue;
906+ /* find or create cflag entry, semicolon-merged per module */
907+ int mi = -1;
908+ for (int ci = 0; ci < c->n_ffi_cflags; ci++)
909+ if (!strcmp(c->ffi_cflag_mods[ci], mname)) { mi = ci; break; }
910+ if (mi < 0) {
911+ if (c->n_ffi_cflags >= c->c_ffi_cflags) {
912+ c->c_ffi_cflags = c->c_ffi_cflags ? c->c_ffi_cflags * 2 : 8;
913+ c->ffi_cflag_mods = realloc(c->ffi_cflag_mods, sizeof(char*) * (size_t)c->c_ffi_cflags);
914+ c->ffi_cflag_vals = realloc(c->ffi_cflag_vals, sizeof(char*) * (size_t)c->c_ffi_cflags);
915+ }
916+ c->ffi_cflag_mods[c->n_ffi_cflags] = strdup(mname);
917+ c->ffi_cflag_vals[c->n_ffi_cflags] = strdup(cflag);
918+ mi = c->n_ffi_cflags++;
919+ }
920+ else {
921+ size_t old_len = strlen(c->ffi_cflag_vals[mi]);
922+ size_t new_len = old_len + 1 + strlen(cflag) + 1;
923+ char *merged = malloc(new_len);
924+ snprintf(merged, new_len, "%s;%s", c->ffi_cflag_vals[mi], cflag);
925+ free(c->ffi_cflag_vals[mi]);
926+ c->ffi_cflag_vals[mi] = merged;
927+ }
928+ continue;
929+ }
930+
902931 if (!strcmp(dname, "ffi_func")) {
903932 if (an < 3) continue;
904933 const char *fname = ffi_arg_str(nt, args[0]);