Anyone know of a C preprocessor #define introspection mechanism? (for FFI bindings)

  • From: demetri <demetri.spanos@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 10 Feb 2015 18:10:14 -0800

Sorry, I know this is an odd question, but having a positive answer
would greatly simplify some of our automated FFI code generation.

I would like some way to detect at compile time that a preprocessor
constant, say, AF_INET, is defined and automatically emit a line like

int lj_ffi_cpp_constant_AF_INET = AF_INET;

so that I could then, in FFI code, do something like:

whatever = ffi.C.lj_ffi_cpp_constant_AF_INET

I realize this is a bit of a stretch but if anyone knows of a way to
get at the #define state of the C preprocessor so that such a thing
could be done, I would greatly appreciate it :)

In moments of desperation I have considered an automated brute
force strategy enumerating all possible constant names of a given
size, emitting a test program to printf the value, testing for successful
compilation, and then adding that to a database of known values. Even
if this took a dat to run it might be worthwhile in terms of saving
developer  heartache, given our need to build on multiple platforms with
multiple bitness with multiple versions of dependencies.

Please don't hesitate to propose even awkward solutions! Nothing
could be worse than the brute-force plan. And even an awkward solution
is probably better than the manual #define constant curation that we
currently do.

Thanks,
Demetri

Other related posts: